Emit event from DeviceMessageHandler

This commit is contained in:
RMidhunSuresh 2023-02-27 23:31:30 +05:30
parent 75688cf6f3
commit ed4eb9bde0
No known key found for this signature in database

View File

@ -17,9 +17,11 @@ limitations under the License.
import {OLM_ALGORITHM} from "./e2ee/common.js"; import {OLM_ALGORITHM} from "./e2ee/common.js";
import {countBy, groupBy} from "../utils/groupBy"; import {countBy, groupBy} from "../utils/groupBy";
import {LRUCache} from "../utils/LRUCache"; import {LRUCache} from "../utils/LRUCache";
import {EventEmitter} from "../utils/EventEmitter";
export class DeviceMessageHandler { export class DeviceMessageHandler extends EventEmitter{
constructor({storage, callHandler}) { constructor({storage, callHandler}) {
super();
this._storage = storage; this._storage = storage;
this._olmDecryption = null; this._olmDecryption = null;
this._megolmDecryption = null; this._megolmDecryption = null;
@ -39,6 +41,7 @@ export class DeviceMessageHandler {
async prepareSync(toDeviceEvents, lock, txn, log) { async prepareSync(toDeviceEvents, lock, txn, log) {
log.set("messageTypes", countBy(toDeviceEvents, e => e.type)); log.set("messageTypes", countBy(toDeviceEvents, e => e.type));
const encryptedEvents = toDeviceEvents.filter(e => e.type === "m.room.encrypted"); const encryptedEvents = toDeviceEvents.filter(e => e.type === "m.room.encrypted");
this._emitUnencryptedEvents(toDeviceEvents);
if (!this._olmDecryption) { if (!this._olmDecryption) {
log.log("can't decrypt, encryption not enabled", log.level.Warn); log.log("can't decrypt, encryption not enabled", log.level.Warn);
return; return;
@ -74,6 +77,7 @@ export class DeviceMessageHandler {
} }
async afterSyncCompleted(decryptionResults, deviceTracker, hsApi, log) { async afterSyncCompleted(decryptionResults, deviceTracker, hsApi, log) {
this._emitEncryptedEvents(decryptionResults);
if (this._callHandler) { if (this._callHandler) {
// if we don't have a device, we need to fetch the device keys the message claims // if we don't have a device, we need to fetch the device keys the message claims
// and check the keys, and we should only do network requests during // and check the keys, and we should only do network requests during
@ -101,6 +105,19 @@ export class DeviceMessageHandler {
} }
} }
} }
_emitUnencryptedEvents(toDeviceEvents) {
const unencryptedEvents = toDeviceEvents.filter(e => e.type !== "m.room.encrypted");
for (const event of unencryptedEvents) {
this.emit("message", { unencrypted: event });
}
}
_emitEncryptedEvents(decryptionResults) {
for (const result of decryptionResults) {
this.emit("message", { encrypted: result });
}
}
} }
class SyncPreparation { class SyncPreparation {