vector-im-hydrogen-web/src/matrix/DeviceMessageHandler.js

100 lines
4.2 KiB
JavaScript
Raw Normal View History

/*
Copyright 2020 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import {OLM_ALGORITHM} from "./e2ee/common.js";
import {countBy, groupBy} from "../utils/groupBy";
2022-02-14 17:14:21 +01:00
import {LRUCache} from "../../utils/LRUCache";
export class DeviceMessageHandler {
2022-02-14 17:14:21 +01:00
constructor({storage, callHandler}) {
this._storage = storage;
2020-09-02 14:24:38 +02:00
this._olmDecryption = null;
this._megolmDecryption = null;
2022-02-14 17:14:21 +01:00
this._callHandler = callHandler;
this._senderDeviceCache = new LRUCache(10, di => di.curve25519Key);
2020-09-02 14:24:38 +02:00
}
enableEncryption({olmDecryption, megolmDecryption}) {
this._olmDecryption = olmDecryption;
2020-09-02 14:24:38 +02:00
this._megolmDecryption = megolmDecryption;
}
obtainSyncLock(toDeviceEvents) {
return this._olmDecryption?.obtainDecryptionLock(toDeviceEvents);
}
async prepareSync(toDeviceEvents, lock, txn, log) {
log.set("messageTypes", countBy(toDeviceEvents, e => e.type));
const encryptedEvents = toDeviceEvents.filter(e => e.type === "m.room.encrypted");
2020-09-02 14:24:38 +02:00
if (!this._olmDecryption) {
log.log("can't decrypt, encryption not enabled", log.level.Warn);
2020-09-02 14:24:38 +02:00
return;
}
// only know olm for now
const olmEvents = encryptedEvents.filter(e => e.content?.algorithm === OLM_ALGORITHM);
if (olmEvents.length) {
const olmDecryptChanges = await this._olmDecryption.decryptAll(olmEvents, lock, txn);
log.set("decryptedTypes", countBy(olmDecryptChanges.results, r => r.event?.type));
for (const err of olmDecryptChanges.errors) {
log.child("decrypt_error").catch(err);
}
const newRoomKeys = this._megolmDecryption.roomKeysFromDeviceMessages(olmDecryptChanges.results, log);
2022-02-14 17:14:21 +01:00
const callMessages = olmDecryptChanges.results.filter(dr => this._callHandler.handlesDeviceMessageEventType(dr.event?.type));
2022-03-07 10:15:54 +01:00
// load devices by sender key
2022-02-14 17:14:21 +01:00
await Promise.all(callMessages.map(async dr => {
dr.setDevice(await this._getDevice(dr.senderCurve25519Key, txn));
}));
2022-03-07 10:15:54 +01:00
// TODO: pass this in the prep and run it in afterSync or afterSyncComplete (as callHandler can send events as well)?
for (const dr of callMessages) {
this._callHandler.handleDeviceMessage(dr.device.userId, dr.device.deviceId, dr.event.type, dr.event.content, log);
}
2022-02-14 17:14:21 +01:00
// TODO: somehow include rooms that received a call to_device message in the sync state?
// or have updates flow through event emitter?
// well, we don't really need to update the room other then when a call starts or stops
// any changes within the call will be emitted on the call object?
return new SyncPreparation(olmDecryptChanges, newRoomKeys);
}
}
/** check that prep is not undefined before calling this */
async writeSync(prep, txn) {
// write olm changes
prep.olmDecryptChanges.write(txn);
const didWriteValues = await Promise.all(prep.newRoomKeys.map(key => this._megolmDecryption.writeRoomKey(key, txn)));
return didWriteValues.some(didWrite => !!didWrite);
}
2022-02-14 17:14:21 +01:00
async _getDevice(senderKey, txn) {
let device = this._senderDeviceCache.get(senderKey);
if (!device) {
device = await txn.deviceIdentities.getByCurve25519Key(senderKey);
if (device) {
this._senderDeviceCache.set(device);
}
}
return device;
}
}
class SyncPreparation {
constructor(olmDecryptChanges, newRoomKeys) {
this.olmDecryptChanges = olmDecryptChanges;
this.newRoomKeys = newRoomKeys;
this.newKeysByRoom = groupBy(newRoomKeys, r => r.roomId);
}
}