From 58e228be7d3f69f4361fac308019e7d1fc9a0b21 Mon Sep 17 00:00:00 2001 From: Jess Porter Date: Wed, 2 Feb 2022 17:35:02 +0000 Subject: [PATCH] make `roomIds` param on replaceRoomIdsWithPills a Set (#146) --- src/LogProxy.ts | 2 +- src/utils.ts | 16 +++++++++++++--- test/integration/utilsTest.ts | 31 +++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 test/integration/utilsTest.ts diff --git a/src/LogProxy.ts b/src/LogProxy.ts index ab2d807..d4d4fb5 100644 --- a/src/LogProxy.ts +++ b/src/LogProxy.ts @@ -36,7 +36,7 @@ export async function logMessage(level: LogLevel, module: string, message: strin const client = config.RUNTIME.client; const managementRoomId = await client.resolveRoom(config.managementRoom); - const roomIds = [managementRoomId, ...additionalRoomIds]; + const roomIds = new Set([managementRoomId, ...additionalRoomIds]); let evContent: TextualMessageEventContent = { body: message, diff --git a/src/utils.ts b/src/utils.ts index 2998894..29eba22 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -179,9 +179,19 @@ export async function getMessagesByUserIn(client: MatrixClient, sender: string, } } -export async function replaceRoomIdsWithPills(client: MatrixClient, text: string, roomIds: string[] | string, msgtype: MessageType = "m.text"): Promise { - if (!Array.isArray(roomIds)) roomIds = [roomIds]; - +/* + * Take an arbitrary string and a set of room IDs, and return a + * TextualMessageEventContent whose plaintext component replaces those room + * IDs with their canonical aliases, and whose html component replaces those + * room IDs with their matrix.to room pills. + * + * @param client The matrix client on which to query for room aliases + * @param text An arbitrary string to rewrite with room aliases and pills + * @param roomIds A set of room IDs to find and replace in `text` + * @param msgtype The desired message type of the returned TextualMessageEventContent + * @returns A TextualMessageEventContent with replaced room IDs + */ +export async function replaceRoomIdsWithPills(client: MatrixClient, text: string, roomIds: Set, msgtype: MessageType = "m.text"): Promise { const content: TextualMessageEventContent = { body: text, formatted_body: htmlEscape(text), diff --git a/test/integration/utilsTest.ts b/test/integration/utilsTest.ts new file mode 100644 index 0000000..22f853b --- /dev/null +++ b/test/integration/utilsTest.ts @@ -0,0 +1,31 @@ +import { strict as assert } from "assert"; + +import { UserID } from "matrix-bot-sdk"; +import config from "../../src/config"; +import { replaceRoomIdsWithPills } from "../../src/utils"; + +describe("Test: utils", function() { + it("replaceRoomIdsWithPills correctly turns a room ID in to a pill", async function() { + this.timeout(20000); + + await this.mjolnir.client.sendStateEvent( + this.mjolnir.managementRoomId, + "m.room.canonical_alias", + "", + { alias: config.managementRoom } + ); + + const out = await replaceRoomIdsWithPills( + this.mjolnir.client, + `it's fun here in ${this.mjolnir.managementRoomId}`, + new Set([this.mjolnir.managementRoomId]) + ); + + const ourHomeserver = new UserID(await this.mjolnir.client.getUserId()).domain; + assert.equal( + out.formatted_body, + `it's fun here in ${config.managementRoom}` + ); + }); +}); +