From 77d10c93d67571092ed709208c7076aab199240f Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Thu, 21 Oct 2021 14:40:51 +0200 Subject: [PATCH] convert groupby and megolm decryption utils to typescript --- src/matrix/DeviceMessageHandler.js | 2 +- src/matrix/Session.js | 2 +- src/matrix/e2ee/RoomEncryption.js | 4 +-- .../megolm/decryption/{utils.js => utils.ts} | 30 ++++++++++--------- src/matrix/e2ee/olm/Decryption.js | 2 +- src/matrix/e2ee/olm/Encryption.js | 2 +- src/utils/{groupBy.js => groupBy.ts} | 10 +++---- 7 files changed, 27 insertions(+), 25 deletions(-) rename src/matrix/e2ee/megolm/decryption/{utils.js => utils.ts} (50%) rename src/utils/{groupBy.js => groupBy.ts} (79%) diff --git a/src/matrix/DeviceMessageHandler.js b/src/matrix/DeviceMessageHandler.js index 27f1d386..1f79d517 100644 --- a/src/matrix/DeviceMessageHandler.js +++ b/src/matrix/DeviceMessageHandler.js @@ -15,7 +15,7 @@ limitations under the License. */ import {OLM_ALGORITHM} from "./e2ee/common.js"; -import {countBy, groupBy} from "../utils/groupBy.js"; +import {countBy, groupBy} from "../utils/groupBy"; export class DeviceMessageHandler { constructor({storage}) { diff --git a/src/matrix/Session.js b/src/matrix/Session.js index 36c8e084..af1bf9a8 100644 --- a/src/matrix/Session.js +++ b/src/matrix/Session.js @@ -33,7 +33,7 @@ import {MEGOLM_ALGORITHM} from "./e2ee/common.js"; import {RoomEncryption} from "./e2ee/RoomEncryption.js"; import {DeviceTracker} from "./e2ee/DeviceTracker.js"; import {LockMap} from "../utils/LockMap.js"; -import {groupBy} from "../utils/groupBy.js"; +import {groupBy} from "../utils/groupBy"; import { keyFromCredential as ssssKeyFromCredential, readKey as ssssReadKey, diff --git a/src/matrix/e2ee/RoomEncryption.js b/src/matrix/e2ee/RoomEncryption.js index aba7d07d..cd2b5d59 100644 --- a/src/matrix/e2ee/RoomEncryption.js +++ b/src/matrix/e2ee/RoomEncryption.js @@ -15,9 +15,9 @@ limitations under the License. */ import {MEGOLM_ALGORITHM, DecryptionSource} from "./common.js"; -import {groupEventsBySession} from "./megolm/decryption/utils.js"; +import {groupEventsBySession} from "./megolm/decryption/utils"; import {mergeMap} from "../../utils/mergeMap.js"; -import {groupBy} from "../../utils/groupBy.js"; +import {groupBy} from "../../utils/groupBy"; import {makeTxnId} from "../common.js"; const ENCRYPTED_TYPE = "m.room.encrypted"; diff --git a/src/matrix/e2ee/megolm/decryption/utils.js b/src/matrix/e2ee/megolm/decryption/utils.ts similarity index 50% rename from src/matrix/e2ee/megolm/decryption/utils.js rename to src/matrix/e2ee/megolm/decryption/utils.ts index c38b1416..4207006b 100644 --- a/src/matrix/e2ee/megolm/decryption/utils.js +++ b/src/matrix/e2ee/megolm/decryption/utils.ts @@ -14,44 +14,46 @@ See the License for the specific language governing permissions and limitations under the License. */ -import {groupByWithCreator} from "../../../../utils/groupBy.js"; +import {groupByWithCreator} from "../../../../utils/groupBy"; +import type {TimelineEvent} from "../../../storage/types"; -function getSenderKey(event) { +function getSenderKey(event: TimelineEvent): string | undefined { return event.content?.["sender_key"]; } -function getSessionId(event) { +function getSessionId(event: TimelineEvent): string | undefined { return event.content?.["session_id"]; } -function getCiphertext(event) { +function getCiphertext(event: TimelineEvent): string | undefined { return event.content?.ciphertext; } -export function validateEvent(event) { +export function validateEvent(event: TimelineEvent) { return typeof getSenderKey(event) === "string" && typeof getSessionId(event) === "string" && typeof getCiphertext(event) === "string"; } -class SessionKeyGroup { +export class SessionKeyGroup { + public readonly events: TimelineEvent[]; constructor() { this.events = []; } - get senderKey() { - return getSenderKey(this.events[0]); + get senderKey(): string | undefined { + return getSenderKey(this.events[0]!); } - get sessionId() { - return getSessionId(this.events[0]); + get sessionId(): string | undefined { + return getSessionId(this.events[0]!); } } -export function groupEventsBySession(events) { - return groupByWithCreator(events, - event => `${getSenderKey(event)}|${getSessionId(event)}`, +export function groupEventsBySession(events: TimelineEvent[]): Map { + return groupByWithCreator(events, + (event: TimelineEvent) => `${getSenderKey(event)}|${getSessionId(event)}`, () => new SessionKeyGroup(), - (group, event) => group.events.push(event) + (group: SessionKeyGroup, event: TimelineEvent) => group.events.push(event) ); } diff --git a/src/matrix/e2ee/olm/Decryption.js b/src/matrix/e2ee/olm/Decryption.js index 7556c367..c3777bd3 100644 --- a/src/matrix/e2ee/olm/Decryption.js +++ b/src/matrix/e2ee/olm/Decryption.js @@ -15,7 +15,7 @@ limitations under the License. */ import {DecryptionError} from "../common.js"; -import {groupBy} from "../../../utils/groupBy.js"; +import {groupBy} from "../../../utils/groupBy"; import {MultiLock} from "../../../utils/Lock.js"; import {Session} from "./Session.js"; import {DecryptionResult} from "../DecryptionResult.js"; diff --git a/src/matrix/e2ee/olm/Encryption.js b/src/matrix/e2ee/olm/Encryption.js index 3bc66ec3..1b720ae7 100644 --- a/src/matrix/e2ee/olm/Encryption.js +++ b/src/matrix/e2ee/olm/Encryption.js @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -import {groupByWithCreator} from "../../../utils/groupBy.js"; +import {groupByWithCreator} from "../../../utils/groupBy"; import {verifyEd25519Signature, OLM_ALGORITHM} from "../common.js"; import {createSessionEntry} from "./Session.js"; diff --git a/src/utils/groupBy.js b/src/utils/groupBy.ts similarity index 79% rename from src/utils/groupBy.js rename to src/utils/groupBy.ts index 9bed5298..2d91b209 100644 --- a/src/utils/groupBy.js +++ b/src/utils/groupBy.ts @@ -14,14 +14,14 @@ See the License for the specific language governing permissions and limitations under the License. */ -export function groupBy(array, groupFn) { - return groupByWithCreator(array, groupFn, +export function groupBy(array: V[], groupFn: (V) => K): Map { + return groupByWithCreator(array, groupFn, () => {return [];}, (array, value) => array.push(value) ); } -export function groupByWithCreator(array, groupFn, createCollectionFn, addCollectionFn) { +export function groupByWithCreator(array: V[], groupFn: (V) => K, createCollectionFn: () => C, addCollectionFn: (C, V) => void): Map { return array.reduce((map, value) => { const key = groupFn(value); let collection = map.get(key); @@ -31,10 +31,10 @@ export function groupByWithCreator(array, groupFn, createCollectionFn, addCollec } addCollectionFn(collection, value); return map; - }, new Map()); + }, new Map()); } -export function countBy(events, mapper) { +export function countBy(events: V[], mapper: (V) => string | number): { [key: string]: number } { return events.reduce((counts, event) => { const mappedValue = mapper(event); if (!counts[mappedValue]) {