From a0027b4e3fb2b472bb0d015dad22016500945b86 Mon Sep 17 00:00:00 2001
From: Sorunome <mail@sorunome.de>
Date: Fri, 21 Dec 2018 19:33:49 +0100
Subject: [PATCH] D->M fix unknown channel handling

---
 src/discordmessageprocessor.ts       | 3 +++
 test/test_discordmessageprocessor.ts | 5 +++--
 2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/src/discordmessageprocessor.ts b/src/discordmessageprocessor.ts
index bddd2cf..8df1273 100644
--- a/src/discordmessageprocessor.ts
+++ b/src/discordmessageprocessor.ts
@@ -150,6 +150,9 @@ export class DiscordMessageProcessor {
     public InsertChannel(node: IDiscordNode, msg: Discord.Message, html: boolean = false): string {
         const id = node.id;
         const channel = msg.guild.channels.get(id);
+        if (!channel) {
+            return html ? `&lt;#${escapeHtml(id)}&gt;` : `<#${id}>`;
+        }
         const channelStr = escapeHtml(channel ? "#" + channel.name : "#" + id);
         if (!html) {
             return channelStr;
diff --git a/test/test_discordmessageprocessor.ts b/test/test_discordmessageprocessor.ts
index 78f75ae..96d71c4 100644
--- a/test/test_discordmessageprocessor.ts
+++ b/test/test_discordmessageprocessor.ts
@@ -6,6 +6,7 @@ import { MockGuild } from "./mocks/guild";
 import { MockMember } from "./mocks/member";
 import { MockMessage } from "./mocks/message";
 import { MockRole } from "./mocks/role";
+import { MockChannel } from "./mocks/channel";
 
 // we are a test file and thus need those
 /* tslint:disable:no-unused-expression max-file-line-count no-any */
@@ -262,11 +263,11 @@ describe("DiscordMessageProcessor", () => {
             const msg = new MockMessage(channel) as any;
             const content = { id: "123456789" };
             let reply = processor.InsertChannel(content, msg);
-            Chai.assert.equal(reply, "#123456789");
+            Chai.assert.equal(reply, "<#123456789>");
 
             reply = processor.InsertChannel(content, msg, true);
             Chai.assert.equal(reply,
-                "<a href=\"https://matrix.to/#/#_discord_123_123456789:localhost\">#123456789</a>");
+                "&lt;#123456789&gt;");
         });
         it("processes channels correctly", () => {
             const processor = new DiscordMessageProcessor(
-- 
GitLab