You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-11-28 05:03:59 +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:
@@ -40,7 +40,6 @@ import { sleep } from './utils';
|
||||
import { Direction, EventTimeline } from "./models/event-timeline";
|
||||
import { IActionsObject, PushProcessor } from "./pushprocessor";
|
||||
import { AutoDiscovery, AutoDiscoveryAction } from "./autodiscovery";
|
||||
import { IEncryptAndSendToDevicesResult } from "./crypto";
|
||||
import * as olmlib from "./crypto/olmlib";
|
||||
import { decodeBase64, encodeBase64 } from "./crypto/olmlib";
|
||||
import { IExportedDevice as IExportedOlmDevice } from "./crypto/OlmDevice";
|
||||
@@ -2583,7 +2582,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
|
||||
public encryptAndSendToDevices(
|
||||
userDeviceInfoArr: IOlmDevice<DeviceInfo>[],
|
||||
payload: object,
|
||||
): Promise<IEncryptAndSendToDevicesResult> {
|
||||
): Promise<void> {
|
||||
if (!this.crypto) {
|
||||
throw new Error("End-to-End encryption disabled");
|
||||
}
|
||||
|
||||
@@ -606,19 +606,19 @@ class MegolmEncryption extends EncryptionAlgorithm {
|
||||
private encryptAndSendKeysToDevices(
|
||||
session: OutboundSessionInfo,
|
||||
chainIndex: number,
|
||||
userDeviceMap: IOlmDevice[],
|
||||
devices: IOlmDevice[],
|
||||
payload: IPayload,
|
||||
): Promise<void> {
|
||||
return this.crypto.encryptAndSendToDevices(
|
||||
userDeviceMap,
|
||||
devices,
|
||||
payload,
|
||||
).then(({ toDeviceBatch, deviceInfoByUserIdAndDeviceId }) => {
|
||||
).then(() => {
|
||||
// store that we successfully uploaded the keys of the current slice
|
||||
for (const msg of toDeviceBatch.batch) {
|
||||
for (const device of devices) {
|
||||
session.markSharedWithDevice(
|
||||
msg.userId,
|
||||
msg.deviceId,
|
||||
deviceInfoByUserIdAndDeviceId.get(msg.userId).get(msg.deviceId).getIdentityKey(),
|
||||
device.userId,
|
||||
device.deviceInfo.deviceId,
|
||||
device.deviceInfo.getIdentityKey(),
|
||||
chainIndex,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -212,11 +212,6 @@ export interface IEncryptedContent {
|
||||
}
|
||||
/* eslint-enable camelcase */
|
||||
|
||||
export interface IEncryptAndSendToDevicesResult {
|
||||
toDeviceBatch: ToDeviceBatch;
|
||||
deviceInfoByUserIdAndDeviceId: Map<string, Map<string, DeviceInfo>>;
|
||||
}
|
||||
|
||||
export enum CryptoEvent {
|
||||
DeviceVerificationChanged = "deviceVerificationChanged",
|
||||
UserTrustStatusChanged = "userTrustStatusChanged",
|
||||
@@ -3129,12 +3124,11 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
|
||||
public async encryptAndSendToDevices(
|
||||
userDeviceInfoArr: IOlmDevice<DeviceInfo>[],
|
||||
payload: object,
|
||||
): Promise<IEncryptAndSendToDevicesResult> {
|
||||
): Promise<void> {
|
||||
const toDeviceBatch: ToDeviceBatch = {
|
||||
eventType: EventType.RoomMessageEncrypted,
|
||||
batch: [],
|
||||
};
|
||||
const deviceInfoByUserIdAndDeviceId = new Map<string, Map<string, DeviceInfo>>();
|
||||
|
||||
try {
|
||||
await Promise.all(userDeviceInfoArr.map(async ({ userId, deviceInfo }) => {
|
||||
@@ -3145,17 +3139,6 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
|
||||
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({
|
||||
userId,
|
||||
deviceId,
|
||||
@@ -3193,7 +3176,6 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
|
||||
|
||||
try {
|
||||
await this.baseApis.queueToDevice(toDeviceBatch);
|
||||
return { toDeviceBatch, deviceInfoByUserIdAndDeviceId };
|
||||
} catch (e) {
|
||||
logger.error("sendToDevice failed", e);
|
||||
throw e;
|
||||
|
||||
Reference in New Issue
Block a user