make roomIds param on replaceRoomIdsWithPills a Set<string> (#146)

This commit is contained in:
Jess Porter 2022-02-02 17:35:02 +00:00 committed by GitHub
parent f70d97e4d9
commit 58e228be7d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 4 deletions

View File

@ -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,

View File

@ -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<TextualMessageEventContent> {
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<string>, msgtype: MessageType = "m.text"): Promise<TextualMessageEventContent> {
const content: TextualMessageEventContent = {
body: text,
formatted_body: htmlEscape(text),

View File

@ -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 <a href="https://matrix.to/#/${config.managementRoom}?via=${ourHomeserver}">${config.managementRoom}</a>`
);
});
});