From 159b2b49cad3f9e028851d3a2ebbd39510a9c6ae Mon Sep 17 00:00:00 2001 From: Will Hunt Date: Wed, 17 Nov 2021 16:59:44 +0000 Subject: [PATCH] Tweaks to code --- src/components/encrypted-intent.ts | 21 +++++++-------------- src/components/encryption.ts | 4 ++-- src/components/event-types.ts | 15 +++++---------- src/components/intent.ts | 17 +++++------------ src/components/membership-cache.ts | 2 +- 5 files changed, 20 insertions(+), 39 deletions(-) diff --git a/src/components/encrypted-intent.ts b/src/components/encrypted-intent.ts index aee93c8c..1337b9a5 100644 --- a/src/components/encrypted-intent.ts +++ b/src/components/encrypted-intent.ts @@ -4,6 +4,7 @@ import Logging from "./logging"; import { ReadStream } from "fs"; import BotSdk, { MatrixClient } from "matrix-bot-sdk"; import { FileUploadOpts, Intent, IntentOpts } from "./intent"; +import { WeakStateEvent } from "./event-types"; const log = Logging.get("EncryptedIntent"); @@ -19,7 +20,7 @@ export interface EncryptedIntentOpts { */ export class EncryptedIntent extends Intent { - private readonly encryptedRooms = new Map(); + private readonly encryptedRooms = new Map(); private encryptionReadyPromise?: Promise; // A client that talks directly to the homeserver, bypassing pantalaimon. private encryptionHsClient: MatrixClient; @@ -52,19 +53,11 @@ export class EncryptedIntent extends Intent { return super.uploadContent(content, opts); } - public onEvent(event: { - type: string, - // eslint-disable-next-line camelcase - content: {membership: UserMembership, displayname?: string, avatar_url?: string, algorithm?: string}, - // eslint-disable-next-line camelcase - state_key: unknown, - // eslint-disable-next-line camelcase - room_id: string - }): void { + public onEvent(event: WeakStateEvent): void { super.onEvent(event); if (event.type === "m.room.encryption" && typeof event.content.algorithm === "string") { log.info(`Room ${event.room_id} enabled encryption (${event.content.algorithm})`); - this.encryptedRooms.set(event.room_id, event.content.algorithm); + this.encryptedRooms.set(event.room_id, true); } } @@ -207,7 +200,7 @@ export class EncryptedIntent extends Intent { * @param roomId The room ID to be checked * @returns The encryption algorithm or false */ - public async isRoomEncrypted(roomId: string): Promise { + public async isRoomEncrypted(roomId: string): Promise { const existing = this.encryptedRooms.get(roomId); if (existing !== undefined) { return existing; @@ -220,8 +213,8 @@ export class EncryptedIntent extends Intent { } const algo = ev.algorithm as unknown; if (typeof algo === 'string' && algo) { - this.encryptedRooms.set(roomId, algo); - return algo; + this.encryptedRooms.set(roomId, true); + return true; } // Return false if missing, not a string or empty. return false; diff --git a/src/components/encryption.ts b/src/components/encryption.ts index 0ac7de93..7b9372fd 100644 --- a/src/components/encryption.ts +++ b/src/components/encryption.ts @@ -11,7 +11,7 @@ const log = Logging.get("EncryptedEventBroker"); export const APPSERVICE_LOGIN_TYPE = "uk.half-shot.msc2778.login.application_service"; const EVENT_CACHE_FOR_MS = 5 * 60000; // 5 minutes -interface PanWeakEvent extends WeakEvent { +interface PantalaimonWeakEvent extends WeakEvent { decrypted: true; } @@ -184,7 +184,7 @@ export class EncryptedEventBroker { return false; } - private onSyncEvent(roomId: string, event: PanWeakEvent): void { + private onSyncEvent(roomId: string, event: PantalaimonWeakEvent): void { if (!event.decrypted) { // We only care about encrypted events, and pantalaimon appends a decrypted key to each event. return; diff --git a/src/components/event-types.ts b/src/components/event-types.ts index e550d2ce..b39bf38a 100644 --- a/src/components/event-types.ts +++ b/src/components/event-types.ts @@ -1,16 +1,13 @@ +/* eslint-disable camelcase */ export interface WeakEvent extends Record { - // eslint-disable-next-line camelcase event_id: string; - // eslint-disable-next-line camelcase room_id: string; sender: string; content: Record; unsigned?: { age?: number; } - // eslint-disable-next-line camelcase origin_server_ts: number; - // eslint-disable-next-line camelcase state_key?: string; type: string; } @@ -21,7 +18,6 @@ export interface TypingEvent { // eslint-disable-next-line camelcase user_ids: string[]; } - // eslint-disable-next-line camelcase room_id: string; } @@ -36,24 +32,23 @@ export interface ReadReceiptEvent { } } type: "m.receipt"; - // eslint-disable-next-line camelcase room_id: string; } export interface PresenceEvent { content: { - // eslint-disable-next-line camelcase avatar_url?: string; - // eslint-disable-next-line camelcase currently_active?: boolean; - // eslint-disable-next-line camelcase last_active_ago?: number; presence: "online"|"offline"|"unavailable"; - // eslint-disable-next-line camelcase status_msg?: string; }, sender: string; type: "m.presence"; } +export interface WeakStateEvent extends WeakEvent { + state_key: string; +} + export type EphemeralEvent = TypingEvent|ReadReceiptEvent|PresenceEvent; diff --git a/src/components/intent.ts b/src/components/intent.ts index 9e0f247b..5bd6209d 100644 --- a/src/components/intent.ts +++ b/src/components/intent.ts @@ -22,6 +22,7 @@ import BridgeErrorReason = unstable.BridgeErrorReason; import Logging from "./logging"; import { ReadStream } from "fs"; import BotSdk, { MatrixClient, MatrixProfileInfo, PresenceState } from "matrix-bot-sdk"; +import { WeakStateEvent } from "./event-types"; const log = Logging.get("Intent"); export type IntentBackingStore = { @@ -853,15 +854,7 @@ export class Intent { * if a backing store was provided to the Intent. * @param event The incoming event JSON */ - public onEvent(event: { - type: string, - // eslint-disable-next-line camelcase - content: {membership: UserMembership, displayname?: string, avatar_url?: string, algorithm?: string}, - // eslint-disable-next-line camelcase - state_key: unknown, - // eslint-disable-next-line camelcase - room_id: string - }) { + public onEvent(event: WeakStateEvent): void { if (event.state_key === undefined) { // We MUST operate on state events exclusively return; @@ -876,13 +869,13 @@ export class Intent { event.state_key === this.userId && event.content.membership) { const profile: MatrixProfileInfo = {}; - if (event.content.displayname) { + if (typeof event.content.displayname === "string") { profile.displayname = event.content.displayname; } - if (event.content.avatar_url) { + if (typeof event.content.avatar_url === "string") { profile.avatar_url = event.content.avatar_url; } - this._membershipStates[event.room_id] = [event.content.membership, profile]; + this._membershipStates[event.room_id] = [event.content.membership as UserMembership, profile]; } else if (event.type === "m.room.power_levels") { this.opts.backingStore.setPowerLevelContent(event.room_id, event.content as unknown as PowerLevelContent); diff --git a/src/components/membership-cache.ts b/src/components/membership-cache.ts index 171eb6f8..e9b1e9e6 100644 --- a/src/components/membership-cache.ts +++ b/src/components/membership-cache.ts @@ -91,7 +91,7 @@ export class MembershipCache { * Is a user considered registered with the homeserver. * @param userId A Matrix userId */ - public isUserRegistered(userId: string): boolean { + public isUserRegistered(userId: string): boolean { return this.registeredUsers.has(userId); }