mirror of
https://github.com/matrix-org/mjolnir.git
synced 2024-09-28 20:26:02 +00:00
make roomIds
param on replaceRoomIdsWithPills a Set<string> (#146)
This commit is contained in:
parent
f70d97e4d9
commit
58e228be7d
@ -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,
|
||||
|
16
src/utils.ts
16
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<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),
|
||||
|
31
test/integration/utilsTest.ts
Normal file
31
test/integration/utilsTest.ts
Normal 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>`
|
||||
);
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user