diff --git a/src/matrix/DeviceMessageHandler.js b/src/matrix/DeviceMessageHandler.js index bee5ae43..4da52e71 100644 --- a/src/matrix/DeviceMessageHandler.js +++ b/src/matrix/DeviceMessageHandler.js @@ -15,6 +15,7 @@ limitations under the License. */ import {OLM_ALGORITHM, MEGOLM_ALGORITHM} from "./e2ee/common.js"; +import {countBy} from "../utils/groupBy.js"; // key to store in session store const PENDING_ENCRYPTED_EVENTS = "pendingEncryptedDeviceEvents"; @@ -36,10 +37,7 @@ export class DeviceMessageHandler { */ async writeSync(toDeviceEvents, txn, log) { const encryptedEvents = toDeviceEvents.filter(e => e.type === "m.room.encrypted"); - log.set("encryptedCount", encryptedEvents.length); - const keyRequestCount = toDeviceEvents.reduce((sum, e) => sum + e.type === "m.room_key_request" ? 1 : 0, 0); - log.set("keyRequestCount", keyRequestCount); - log.set("otherCount", toDeviceEvents.length - encryptedEvents.length - keyRequestCount); + log.set("eventsCount", countBy(toDeviceEvents, e => e.type)); if (!encryptedEvents.length) { return false; } @@ -62,10 +60,12 @@ export class DeviceMessageHandler { return r.event?.type === "m.room_key" && r.event.content?.algorithm === MEGOLM_ALGORITHM; }); let roomKeys; - log.set("roomKeyCount", megOlmRoomKeysResults.length); + log.set("eventsCount", countBy(olmResults, r => r.event.type)); + log.set("roomKeys", megOlmRoomKeysResults.length); if (megOlmRoomKeysResults.length) { roomKeys = await this._megolmDecryption.addRoomKeys(megOlmRoomKeysResults, txn, log); } + log.set("newRoomKeys", roomKeys.length); return {roomKeys}; } diff --git a/src/utils/groupBy.js b/src/utils/groupBy.js index 5df2f36d..9bed5298 100644 --- a/src/utils/groupBy.js +++ b/src/utils/groupBy.js @@ -33,3 +33,26 @@ export function groupByWithCreator(array, groupFn, createCollectionFn, addCollec return map; }, new Map()); } + +export function countBy(events, mapper) { + return events.reduce((counts, event) => { + const mappedValue = mapper(event); + if (!counts[mappedValue]) { + counts[mappedValue] = 1; + } else { + counts[mappedValue] += 1; + } + return counts; + }, {}); +} + +export function tests() { + return { + countBy: assert => { + const counts = countBy([{type: "foo"}, {type: "bar"}, {type: "foo"}], o => o.type); + assert.equal(Object.keys(counts).length, 2); + assert.equal(counts.foo, 2); + assert.equal(counts.bar, 1); + } + } +}