Kill off references to deprecated getStoredDevice
and getStoredDevicesForUser
(#11152)
* Use new `CryptoEvent.VerificationRequestReceived` event https://github.com/matrix-org/matrix-js-sdk/pull/3514 deprecates `CryptoEvent.VerificationRequest` in favour of `CryptoEvent.VerificationRequestReceived`. Use the new event. * Factor out `getDeviceCryptoInfo` function I seem to be writing this logic several times, so let's factor it out. * Factor out `getUserDeviceIds` function Another utility function * VerificationRequestToast: `getStoredDevice` -> `getDeviceCryptoInfo` * SlashCommands: `getStoredDevice` -> `getDeviceCryptoInfo` * MemberTile: `getStoredDevicesForUser` -> `getUserDeviceIds` * Remove redundant mock of `getStoredDevicesForUser`
This commit is contained in:
parent
0a3a111327
commit
46eb34a55d
11 changed files with 189 additions and 34 deletions
|
@ -46,6 +46,7 @@ import { recordClientInformation, removeClientInformation } from "./utils/device
|
|||
import SettingsStore, { CallbackFn } from "./settings/SettingsStore";
|
||||
import { UIFeature } from "./settings/UIFeature";
|
||||
import { isBulkUnverifiedDeviceReminderSnoozed } from "./utils/device/snoozeBulkUnverifiedDeviceReminder";
|
||||
import { getUserDeviceIds } from "./utils/crypto/deviceInfo";
|
||||
|
||||
const KEY_BACKUP_POLL_INTERVAL = 5 * 60 * 1000;
|
||||
|
||||
|
@ -161,12 +162,8 @@ export default class DeviceListener {
|
|||
*/
|
||||
private async getDeviceIds(): Promise<Set<string>> {
|
||||
const cli = this.client;
|
||||
const crypto = cli?.getCrypto();
|
||||
if (crypto === undefined) return new Set();
|
||||
|
||||
const userId = cli!.getSafeUserId();
|
||||
const devices = await crypto.getUserDeviceInfo([userId]);
|
||||
return new Set(devices.get(userId)?.keys() ?? []);
|
||||
if (!cli) return new Set();
|
||||
return await getUserDeviceIds(cli, cli.getSafeUserId());
|
||||
}
|
||||
|
||||
private onWillUpdateDevices = async (users: string[], initialFetch?: boolean): Promise<void> => {
|
||||
|
|
|
@ -70,6 +70,7 @@ import { leaveRoomBehaviour } from "./utils/leave-behaviour";
|
|||
import { isLocalRoom } from "./utils/localRoom/isLocalRoom";
|
||||
import { SdkContextClass } from "./contexts/SDKContext";
|
||||
import { MatrixClientPeg } from "./MatrixClientPeg";
|
||||
import { getDeviceCryptoInfo } from "./utils/crypto/deviceInfo";
|
||||
|
||||
// XXX: workaround for https://github.com/microsoft/TypeScript/issues/31816
|
||||
interface HTMLInputEvent extends Event {
|
||||
|
@ -1031,7 +1032,7 @@ export const Commands = [
|
|||
|
||||
return success(
|
||||
(async (): Promise<void> => {
|
||||
const device = cli.getStoredDevice(userId, deviceId);
|
||||
const device = await getDeviceCryptoInfo(cli, userId, deviceId);
|
||||
if (!device) {
|
||||
throw new UserFriendlyError(
|
||||
"Unknown (user, session) pair: (%(userId)s, %(deviceId)s)",
|
||||
|
|
|
@ -36,6 +36,7 @@ import E2EIcon, { E2EState } from "../rooms/E2EIcon";
|
|||
import Spinner from "../elements/Spinner";
|
||||
import AccessibleButton from "../elements/AccessibleButton";
|
||||
import VerificationShowSas from "../verification/VerificationShowSas";
|
||||
import { getDeviceCryptoInfo } from "../../../utils/crypto/deviceInfo";
|
||||
|
||||
interface IProps {
|
||||
layout: string;
|
||||
|
@ -224,12 +225,7 @@ export default class VerificationPanel extends React.PureComponent<IProps, IStat
|
|||
return;
|
||||
}
|
||||
this.haveCheckedDevice = true;
|
||||
|
||||
const deviceMap = await client.getCrypto()?.getUserDeviceInfo([userId]);
|
||||
if (!deviceMap) return;
|
||||
const userDevices = deviceMap.get(userId);
|
||||
if (!userDevices) return;
|
||||
this.setState({ otherDeviceDetails: userDevices.get(deviceId) });
|
||||
this.setState({ otherDeviceDetails: await getDeviceCryptoInfo(client, userId, deviceId) });
|
||||
}
|
||||
|
||||
private renderQRReciprocatePhase(): JSX.Element {
|
||||
|
|
|
@ -34,6 +34,7 @@ import DisambiguatedProfile from "../messages/DisambiguatedProfile";
|
|||
import UserIdentifierCustomisations from "../../../customisations/UserIdentifier";
|
||||
import { E2EState } from "./E2EIcon";
|
||||
import { asyncSome } from "../../../utils/arrays";
|
||||
import { getUserDeviceIds } from "../../../utils/crypto/deviceInfo";
|
||||
|
||||
interface IProps {
|
||||
member: RoomMember;
|
||||
|
@ -127,9 +128,8 @@ export default class MemberTile extends React.Component<IProps, IState> {
|
|||
return;
|
||||
}
|
||||
|
||||
const devices = cli.getStoredDevicesForUser(userId);
|
||||
const anyDeviceUnverified = await asyncSome(devices, async (device) => {
|
||||
const { deviceId } = device;
|
||||
const deviceIDs = await getUserDeviceIds(cli, userId);
|
||||
const anyDeviceUnverified = await asyncSome(deviceIDs, async (deviceId) => {
|
||||
// For your own devices, we use the stricter check of cross-signing
|
||||
// verification to encourage everyone to trust their own devices via
|
||||
// cross-signing so that other users can then safely trust you.
|
||||
|
|
|
@ -20,8 +20,8 @@ import {
|
|||
VerificationRequest,
|
||||
VerificationRequestEvent,
|
||||
} from "matrix-js-sdk/src/crypto-api";
|
||||
import { DeviceInfo } from "matrix-js-sdk/src/crypto/deviceinfo";
|
||||
import { logger } from "matrix-js-sdk/src/logger";
|
||||
import { Device } from "matrix-js-sdk/src/matrix";
|
||||
|
||||
import { _t } from "../../../languageHandler";
|
||||
import { MatrixClientPeg } from "../../../MatrixClientPeg";
|
||||
|
@ -35,6 +35,7 @@ import { Action } from "../../../dispatcher/actions";
|
|||
import VerificationRequestDialog from "../dialogs/VerificationRequestDialog";
|
||||
import RightPanelStore from "../../../stores/right-panel/RightPanelStore";
|
||||
import { ViewRoomPayload } from "../../../dispatcher/payloads/ViewRoomPayload";
|
||||
import { getDeviceCryptoInfo } from "../../../utils/crypto/deviceInfo";
|
||||
|
||||
interface IProps {
|
||||
toastKey: string;
|
||||
|
@ -44,7 +45,7 @@ interface IProps {
|
|||
interface IState {
|
||||
/** number of seconds left in the timeout counter. Zero if there is no timeout. */
|
||||
counter: number;
|
||||
device?: DeviceInfo;
|
||||
device?: Device;
|
||||
ip?: string;
|
||||
}
|
||||
|
||||
|
@ -74,15 +75,13 @@ export default class VerificationRequestToast extends React.PureComponent<IProps
|
|||
// a toast hanging around after logging in if you did a verification as part of login).
|
||||
this.checkRequestIsPending();
|
||||
|
||||
if (request.isSelfVerification) {
|
||||
const otherDeviceId = request.otherDeviceId;
|
||||
if (request.isSelfVerification && !!otherDeviceId) {
|
||||
const cli = MatrixClientPeg.safeGet();
|
||||
const device = request.otherDeviceId ? await cli.getDevice(request.otherDeviceId) : null;
|
||||
const ip = device?.last_seen_ip;
|
||||
const device = await cli.getDevice(otherDeviceId);
|
||||
this.setState({
|
||||
device:
|
||||
(request.otherDeviceId && cli.getStoredDevice(cli.getSafeUserId(), request.otherDeviceId)) ||
|
||||
undefined,
|
||||
ip,
|
||||
ip: device.last_seen_ip,
|
||||
device: await getDeviceCryptoInfo(cli, cli.getSafeUserId(), otherDeviceId),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -158,7 +157,7 @@ export default class VerificationRequestToast extends React.PureComponent<IProps
|
|||
let detail;
|
||||
if (request.isSelfVerification) {
|
||||
if (this.state.device) {
|
||||
description = this.state.device.getDisplayName();
|
||||
description = this.state.device.displayName;
|
||||
detail = _t("%(deviceId)s from %(ip)s", {
|
||||
deviceId: this.state.device.deviceId,
|
||||
ip: this.state.ip,
|
||||
|
|
67
src/utils/crypto/deviceInfo.ts
Normal file
67
src/utils/crypto/deviceInfo.ts
Normal file
|
@ -0,0 +1,67 @@
|
|||
/*
|
||||
Copyright 2023 The Matrix.org Foundation C.I.C.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import { Device, MatrixClient } from "matrix-js-sdk/src/matrix";
|
||||
|
||||
/**
|
||||
* Get crypto information on a specific device.
|
||||
*
|
||||
* Only devices with Crypto support are returned. If the MatrixClient doesn't support cryptography, `undefined` is
|
||||
* returned.
|
||||
*
|
||||
* @param client - Matrix Client.
|
||||
* @param userId - ID of the user owning the device.
|
||||
* @param deviceId - ID of the device.
|
||||
* @param downloadUncached - If true, download the device list for users whose device list we are not
|
||||
* currently tracking. Defaults to false.
|
||||
*
|
||||
* @returns Information on the device if it is known.
|
||||
*/
|
||||
export async function getDeviceCryptoInfo(
|
||||
client: MatrixClient,
|
||||
userId: string,
|
||||
deviceId: string,
|
||||
downloadUncached?: boolean,
|
||||
): Promise<Device | undefined> {
|
||||
const crypto = client.getCrypto();
|
||||
if (!crypto) {
|
||||
// no crypto support, no device.
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const deviceMap = await crypto.getUserDeviceInfo([userId], downloadUncached);
|
||||
return deviceMap.get(userId)?.get(deviceId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the IDs of the given user's devices.
|
||||
*
|
||||
* Only devices with Crypto support are returned. If the MatrixClient doesn't support cryptography, an empty Set is
|
||||
* returned.
|
||||
*
|
||||
* @param client - Matrix Client.
|
||||
* @param userId - ID of the user to query.
|
||||
*/
|
||||
|
||||
export async function getUserDeviceIds(client: MatrixClient, userId: string): Promise<Set<string>> {
|
||||
const crypto = client.getCrypto();
|
||||
if (!crypto) {
|
||||
return new Set();
|
||||
}
|
||||
|
||||
const deviceMap = await crypto.getUserDeviceInfo([userId]);
|
||||
return new Set(deviceMap.get(userId)?.keys() ?? []);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue