Replace uses of checkDeviceTrust with getDeviceVerificationStatus (#10663)

matrix-org/matrix-js-sdk#3287 and matrix-org/matrix-js-sdk#3303 added a new API called getDeviceVerificationStatus. Let's use it.
This commit is contained in:
Richard van der Hoff 2023-04-24 14:19:46 +01:00 committed by GitHub
parent aa8c0f5cc7
commit d7bb8043ea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
22 changed files with 286 additions and 161 deletions

View file

@ -18,6 +18,7 @@ import { MatrixClient } from "matrix-js-sdk/src/client";
import { Room } from "matrix-js-sdk/src/models/room";
import DMRoomMap from "./DMRoomMap";
import { asyncSome } from "./arrays";
export enum E2EStatus {
Warning = "warning",
@ -54,8 +55,9 @@ export async function shieldStatusForRoom(client: MatrixClient, room: Room): Pro
const targets = includeUser ? [...verified, client.getUserId()!] : verified;
for (const userId of targets) {
const devices = client.getStoredDevicesForUser(userId);
const anyDeviceNotVerified = devices.some(({ deviceId }) => {
return !client.checkDeviceTrust(userId, deviceId).isVerified();
const anyDeviceNotVerified = await asyncSome(devices, async ({ deviceId }) => {
const verificationStatus = await client.getCrypto()?.getDeviceVerificationStatus(userId, deviceId);
return !verificationStatus?.isVerified();
});
if (anyDeviceNotVerified) {
return E2EStatus.Warning;

View file

@ -324,6 +324,16 @@ export async function asyncEvery<T>(values: T[], predicate: (value: T) => Promis
return true;
}
/**
* Async version of Array.some.
*/
export async function asyncSome<T>(values: T[], predicate: (value: T) => Promise<boolean>): Promise<boolean> {
for (const value of values) {
if (await predicate(value)) return true;
}
return false;
}
export function filterBoolean<T>(values: Array<T | null | undefined>): T[] {
return values.filter(Boolean) as T[];
}

View file

@ -25,10 +25,10 @@ import { MatrixClient } from "matrix-js-sdk/src/matrix";
* @returns `true` if the device has been correctly cross-signed. `false` if the device is unknown or not correctly
* cross-signed. `null` if there was an error fetching the device info.
*/
export const isDeviceVerified = (client: MatrixClient, deviceId: string): boolean | null => {
export const isDeviceVerified = async (client: MatrixClient, deviceId: string): Promise<boolean | null> => {
try {
const trustLevel = client.checkDeviceTrust(client.getSafeUserId(), deviceId);
return trustLevel.isCrossSigningVerified();
const trustLevel = await client.getCrypto()?.getDeviceVerificationStatus(client.getSafeUserId(), deviceId);
return trustLevel?.crossSigningVerified ?? false;
} catch (e) {
console.error("Error getting device cross-signing info", e);
return null;