shieldStatusForRoom: avoid deprecated MatrixClient methods (#10944)

Update this method to use modern crypto methods
This commit is contained in:
Richard van der Hoff 2023-05-19 11:57:45 +01:00 committed by GitHub
parent 606eaa5274
commit cc842aac8a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 6 deletions

View file

@ -16,6 +16,7 @@ limitations under the License.
import { MatrixClient } from "matrix-js-sdk/src/client";
import { Room } from "matrix-js-sdk/src/models/room";
import { logger } from "matrix-js-sdk/src/logger";
import DMRoomMap from "./DMRoomMap";
import { asyncSome } from "./arrays";
@ -27,6 +28,11 @@ export enum E2EStatus {
}
export async function shieldStatusForRoom(client: MatrixClient, room: Room): Promise<E2EStatus> {
const crypto = client.getCrypto();
if (!crypto) {
return E2EStatus.Warning;
}
const members = (await room.getEncryptionTargetMembers()).map(({ userId }) => userId);
const inDMMap = !!DMRoomMap.shared().getUserIdForRoomId(room.roomId);
@ -53,10 +59,18 @@ export async function shieldStatusForRoom(client: MatrixClient, room: Room): Pro
members.length !== 2) || // Don't alarm for self in 1:1 chats with other users
members.length === 1; // Do alarm for self if we're alone in a room
const targets = includeUser ? [...verified, client.getUserId()!] : verified;
const devicesByUser = await crypto.getUserDeviceInfo(targets);
for (const userId of targets) {
const devices = client.getStoredDevicesForUser(userId);
const anyDeviceNotVerified = await asyncSome(devices, async ({ deviceId }) => {
const verificationStatus = await client.getCrypto()?.getDeviceVerificationStatus(userId, deviceId);
const devices = devicesByUser.get(userId);
if (!devices) {
// getUserDeviceInfo returned nothing about this user, which means we know nothing about their device list.
// That seems odd, so treat it as a warning.
logger.warn(`No device info for user ${userId}`);
return E2EStatus.Warning;
}
const anyDeviceNotVerified = await asyncSome(devices.keys(), async (deviceId) => {
const verificationStatus = await crypto.getDeviceVerificationStatus(userId, deviceId);
return !verificationStatus?.isVerified();
});
if (anyDeviceNotVerified) {

View file

@ -319,7 +319,7 @@ export const concat = (...arrays: Uint8Array[]): Uint8Array => {
/**
* Async version of Array.every.
*/
export async function asyncEvery<T>(values: T[], predicate: (value: T) => Promise<boolean>): Promise<boolean> {
export async function asyncEvery<T>(values: Iterable<T>, predicate: (value: T) => Promise<boolean>): Promise<boolean> {
for (const value of values) {
if (!(await predicate(value))) return false;
}
@ -329,7 +329,7 @@ export async function asyncEvery<T>(values: T[], predicate: (value: T) => Promis
/**
* Async version of Array.some.
*/
export async function asyncSome<T>(values: T[], predicate: (value: T) => Promise<boolean>): Promise<boolean> {
export async function asyncSome<T>(values: Iterable<T>, predicate: (value: T) => Promise<boolean>): Promise<boolean> {
for (const value of values) {
if (await predicate(value)) return true;
}

View file

@ -27,12 +27,14 @@ function mkClient(selfTrust = false) {
Promise.resolve({
isVerified: () => (userId === "@self:localhost" ? selfTrust : userId[2] == "T"),
}),
getUserDeviceInfo: async (userIds: string[]) => {
return new Map(userIds.map((u) => [u, new Map([["DEVICE", {}]])]));
},
}),
checkUserTrust: (userId: string) => ({
isCrossSigningVerified: () => userId[1] == "T",
wasCrossSigningVerified: () => userId[1] == "T" || userId[1] == "W",
}),
getStoredDevicesForUser: (userId: string) => ["DEVICE"],
} as unknown as MatrixClient;
}