Skip to content

Commit

Permalink
Overload MatrixClient. encryptAndSendToDevices instead of deprecating
Browse files Browse the repository at this point in the history
  • Loading branch information
hughns committed Sep 2, 2024
1 parent ca07b18 commit 6a0d8e2
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 16 deletions.
41 changes: 31 additions & 10 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3209,22 +3209,43 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
* Encrypts and sends a given object via Olm to-device messages to a given
* set of devices.
*
* @param userDeviceInfoArr - list of deviceInfo objects representing the devices to send to
* @param userDeviceInfoArr - @deprecated list of deviceInfo objects representing the devices to send to
* @param devices - list of (userId, deviceId) pairs objects representing the devices to send to
*
* @param payload - fields to include in the encrypted payload
*
* @returns Promise which
* resolves once the message has been encrypted and sent to the given
* userDeviceMap, and returns the `{ contentMap, deviceInfoByDeviceId }`
* of the successfully sent messages.
*
* @deprecated Instead use {@link CryptoApi.encryptToDeviceMessages} followed by {@link queueToDevice}.
* @returns Promise which resolves once the payload has been encrypted and sent to the given devices
*/
public encryptAndSendToDevices(userDeviceInfoArr: IOlmDevice<DeviceInfo>[], payload: object): Promise<void> {
if (!this.crypto) {
public encryptAndSendToDevices(userDeviceInfoArr: IOlmDevice<DeviceInfo>[], payload: object): Promise<void>;
public encryptAndSendToDevices(devices: { userId: string; deviceId: string }[], payload: object): Promise<void>;
public async encryptAndSendToDevices(
devices: { userId: string; deviceId: string }[] | IOlmDevice<DeviceInfo>[],
payload: object,
): Promise<void> {
if (devices.length == 0) {
return;
}

if ("deviceinfo" in devices[0]) {
if (!this.crypto) {
throw new Error("End-to-End encryption disabled");
}
return this.crypto.encryptAndSendToDevices(devices as IOlmDevice<DeviceInfo>[], payload);
}

const crypto = this.getCrypto();
if (!crypto) {
throw new Error("End-to-End encryption disabled");
}
return this.crypto.encryptAndSendToDevices(userDeviceInfoArr, payload);

const { type } = payload as { type: string };

const batch = await crypto.encryptToDeviceMessages(
type,
devices as { userId: string; deviceId: string }[],
payload,
);
await this.queueToDevice(batch);
}

/**
Expand Down
28 changes: 22 additions & 6 deletions src/embedded.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,14 +401,30 @@ export class RoomWidgetClient extends MatrixClient {
await this.widgetApi.sendToDevice(eventType, false, recursiveMapToObject(contentMap));
}

public async encryptAndSendToDevices(userDeviceInfoArr: IOlmDevice<DeviceInfo>[], payload: object): Promise<void> {
public async encryptAndSendToDevices(
devices: { userId: string; deviceId: string }[] | IOlmDevice<DeviceInfo>[],
payload: object,
): Promise<void> {
// map: user Id → device Id → payload
const contentMap: MapWithDefault<string, Map<string, object>> = new MapWithDefault(() => new Map());
for (const {
userId,
deviceInfo: { deviceId },
} of userDeviceInfoArr) {
contentMap.getOrCreate(userId).set(deviceId, payload);

if (devices.length == 0) {
return;
}

if ("deviceinfo" in devices[0]) {
// pre-CryptoApi style:
for (const {
userId,
deviceInfo: { deviceId },
} of devices as IOlmDevice<DeviceInfo>[]) {
contentMap.getOrCreate(userId).set(deviceId, payload);
}
} else {
// new CryptoApi style:
for (const { userId, deviceId } of devices as { userId: string; deviceId: string }[]) {
contentMap.getOrCreate(userId).set(deviceId, payload);
}
}

await this.widgetApi.sendToDevice((payload as { type: string }).type, true, recursiveMapToObject(contentMap));
Expand Down

0 comments on commit 6a0d8e2

Please sign in to comment.