1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-29 16:43:09 +03:00

Simplify encryptAndSendToDevices (#2566)

It went to quite a lot of effort to gather a bunch of information to
return, but the only thing using it already had all that info anyway.
This commit is contained in:
David Baker
2022-08-05 15:58:33 +01:00
committed by GitHub
parent 7b7f8c1592
commit 575b416856
3 changed files with 9 additions and 28 deletions

View File

@@ -40,7 +40,6 @@ import { sleep } from './utils';
import { Direction, EventTimeline } from "./models/event-timeline"; import { Direction, EventTimeline } from "./models/event-timeline";
import { IActionsObject, PushProcessor } from "./pushprocessor"; import { IActionsObject, PushProcessor } from "./pushprocessor";
import { AutoDiscovery, AutoDiscoveryAction } from "./autodiscovery"; import { AutoDiscovery, AutoDiscoveryAction } from "./autodiscovery";
import { IEncryptAndSendToDevicesResult } from "./crypto";
import * as olmlib from "./crypto/olmlib"; import * as olmlib from "./crypto/olmlib";
import { decodeBase64, encodeBase64 } from "./crypto/olmlib"; import { decodeBase64, encodeBase64 } from "./crypto/olmlib";
import { IExportedDevice as IExportedOlmDevice } from "./crypto/OlmDevice"; import { IExportedDevice as IExportedOlmDevice } from "./crypto/OlmDevice";
@@ -2583,7 +2582,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
public encryptAndSendToDevices( public encryptAndSendToDevices(
userDeviceInfoArr: IOlmDevice<DeviceInfo>[], userDeviceInfoArr: IOlmDevice<DeviceInfo>[],
payload: object, payload: object,
): Promise<IEncryptAndSendToDevicesResult> { ): Promise<void> {
if (!this.crypto) { if (!this.crypto) {
throw new Error("End-to-End encryption disabled"); throw new Error("End-to-End encryption disabled");
} }

View File

@@ -606,19 +606,19 @@ class MegolmEncryption extends EncryptionAlgorithm {
private encryptAndSendKeysToDevices( private encryptAndSendKeysToDevices(
session: OutboundSessionInfo, session: OutboundSessionInfo,
chainIndex: number, chainIndex: number,
userDeviceMap: IOlmDevice[], devices: IOlmDevice[],
payload: IPayload, payload: IPayload,
): Promise<void> { ): Promise<void> {
return this.crypto.encryptAndSendToDevices( return this.crypto.encryptAndSendToDevices(
userDeviceMap, devices,
payload, payload,
).then(({ toDeviceBatch, deviceInfoByUserIdAndDeviceId }) => { ).then(() => {
// store that we successfully uploaded the keys of the current slice // store that we successfully uploaded the keys of the current slice
for (const msg of toDeviceBatch.batch) { for (const device of devices) {
session.markSharedWithDevice( session.markSharedWithDevice(
msg.userId, device.userId,
msg.deviceId, device.deviceInfo.deviceId,
deviceInfoByUserIdAndDeviceId.get(msg.userId).get(msg.deviceId).getIdentityKey(), device.deviceInfo.getIdentityKey(),
chainIndex, chainIndex,
); );
} }

View File

@@ -212,11 +212,6 @@ export interface IEncryptedContent {
} }
/* eslint-enable camelcase */ /* eslint-enable camelcase */
export interface IEncryptAndSendToDevicesResult {
toDeviceBatch: ToDeviceBatch;
deviceInfoByUserIdAndDeviceId: Map<string, Map<string, DeviceInfo>>;
}
export enum CryptoEvent { export enum CryptoEvent {
DeviceVerificationChanged = "deviceVerificationChanged", DeviceVerificationChanged = "deviceVerificationChanged",
UserTrustStatusChanged = "userTrustStatusChanged", UserTrustStatusChanged = "userTrustStatusChanged",
@@ -3129,12 +3124,11 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
public async encryptAndSendToDevices( public async encryptAndSendToDevices(
userDeviceInfoArr: IOlmDevice<DeviceInfo>[], userDeviceInfoArr: IOlmDevice<DeviceInfo>[],
payload: object, payload: object,
): Promise<IEncryptAndSendToDevicesResult> { ): Promise<void> {
const toDeviceBatch: ToDeviceBatch = { const toDeviceBatch: ToDeviceBatch = {
eventType: EventType.RoomMessageEncrypted, eventType: EventType.RoomMessageEncrypted,
batch: [], batch: [],
}; };
const deviceInfoByUserIdAndDeviceId = new Map<string, Map<string, DeviceInfo>>();
try { try {
await Promise.all(userDeviceInfoArr.map(async ({ userId, deviceInfo }) => { await Promise.all(userDeviceInfoArr.map(async ({ userId, deviceInfo }) => {
@@ -3145,17 +3139,6 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
ciphertext: {}, ciphertext: {},
}; };
// Assign to temp value to make type-checking happy
let userIdDeviceInfo = deviceInfoByUserIdAndDeviceId.get(userId);
if (userIdDeviceInfo === undefined) {
userIdDeviceInfo = new Map<string, DeviceInfo>();
deviceInfoByUserIdAndDeviceId.set(userId, userIdDeviceInfo);
}
// We hold by reference, this updates deviceInfoByUserIdAndDeviceId[userId]
userIdDeviceInfo.set(deviceId, deviceInfo);
toDeviceBatch.batch.push({ toDeviceBatch.batch.push({
userId, userId,
deviceId, deviceId,
@@ -3193,7 +3176,6 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
try { try {
await this.baseApis.queueToDevice(toDeviceBatch); await this.baseApis.queueToDevice(toDeviceBatch);
return { toDeviceBatch, deviceInfoByUserIdAndDeviceId };
} catch (e) { } catch (e) {
logger.error("sendToDevice failed", e); logger.error("sendToDevice failed", e);
throw e; throw e;