diff --git a/src/matrixmessageprocessor.ts b/src/matrixmessageprocessor.ts index c455aac924af09f6cd5f94f1aecfab1df512202c..ff95b4588d3c221e75970a6fb70d7d05ec6eb90c 100644 --- a/src/matrixmessageprocessor.ts +++ b/src/matrixmessageprocessor.ts @@ -145,11 +145,25 @@ export class MatrixMessageProcessor { return `<@${match[1]}>`; } - private parseChannel(id: string): string { - const CHANNEL_REGEX = /^#_discord_[0-9]*_([0-9]*)/; + private async parseChannel(id: string): Promise<string> { + const CHANNEL_REGEX = /^#_discord_[0-9]*_([0-9]*):/; const match = id.match(CHANNEL_REGEX); if (!match || !this.guild.channels.get(match[1])) { - return MATRIX_TO_LINK + id; + /* + This isn't formatted in #_discord_, so let's fetch the internal room ID + and see if it is still a bridged room! + */ + if (this.params && this.params.mxClient) { + try { + const resp = await this.params.mxClient.getRoomIdForAlias(id); + if (resp && resp.room_id) { + const roomId = resp.room_id; + const channel = await this.bot.GetChannelFromRoomId(roomId); + return `<#${channel.id}>`; + } + } catch (err) { } // ignore, room ID wasn't found + } + return ""; } return `<#${match[1]}>`; } @@ -176,7 +190,7 @@ export class MatrixMessageProcessor { reply = this.parseUser(id); break; case "#": - reply = this.parseChannel(id); + reply = await this.parseChannel(id); break; } if (!reply) { diff --git a/test/test_matrixmessageprocessor.ts b/test/test_matrixmessageprocessor.ts index 99626917d695d263732a2c80b0f32adcb2edddf8..d154bfa98c55de25c00b4510d49ff08b487d3bca 100644 --- a/test/test_matrixmessageprocessor.ts +++ b/test/test_matrixmessageprocessor.ts @@ -1,5 +1,5 @@ /* -Copyright 2018 matrix-appservice-discord +Copyright 2018, 2019 matrix-appservice-discord Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -30,6 +30,12 @@ import { MatrixMessageProcessor } from "../src/matrixmessageprocessor"; const expect = Chai.expect; const bot = { + GetChannelFromRoomId: async (roomId: string): Promise<MockChannel> => { + if (roomId !== "!bridged:localhost") { + throw new Error("Not bridged"); + } + return new MockChannel("1234"); + }, GetEmojiByMxc: async (mxc: string): Promise<DbEmoji> => { if (mxc === "mxc://real_emote:localhost") { const emoji = new DbEmoji(); @@ -41,7 +47,7 @@ const bot = { } throw new Error("Couldn't fetch from store"); }, -} as DiscordBot; +} as any; function getPlainMessage(msg: string, msgtype: string = "m.text") { return { @@ -297,14 +303,30 @@ code guild.channels.set("12345", channel as any); const msg = getHtmlMessage("<a href=\"https://matrix.to/#/#_discord_1234_789:localhost\">#SomeChannel</a>"); const result = await mp.FormatMessage(msg, guild as any); - expect(result).is.equal("https://matrix.to/#/#_discord_1234_789:localhost"); + expect(result).is.equal("[#SomeChannel](https://matrix.to/#/#_discord_1234_789:localhost)"); }); it("Handles external channel pills", async () => { const mp = new MatrixMessageProcessor(bot); const guild = new MockGuild("1234"); const msg = getHtmlMessage("<a href=\"https://matrix.to/#/#matrix:matrix.org\">#SomeChannel</a>"); const result = await mp.FormatMessage(msg, guild as any); - expect(result).is.equal("https://matrix.to/#/#matrix:matrix.org"); + expect(result).is.equal("[#SomeChannel](https://matrix.to/#/#matrix:matrix.org)"); + }); + it("Handles external channel pills of rooms that are actually bridged", async () => { + const mp = new MatrixMessageProcessor(bot); + const guild = new MockGuild("1234"); + const msg = getHtmlMessage("<a href=\"https://matrix.to/#/#matrix:matrix.org\">#SomeChannel</a>"); + + const result = await mp.FormatMessage(msg, guild as any, { + mxClient: { + getRoomIdForAlias: async () => { + return { + room_id: "!bridged:localhost", + }; + }, + }, + }); + expect(result).is.equal("<#1234>"); }); it("Ignores links without href", async () => { const mp = new MatrixMessageProcessor(bot);