From f8ac369ee6a102a384c94613b6feecb4424501bb Mon Sep 17 00:00:00 2001
From: Will Hunt <will@half-shot.uk>
Date: Thu, 25 Oct 2018 20:13:17 +0100
Subject: [PATCH] Do not show reply text if we can't get the event

---
 src/util.ts                       |  2 +-
 test/test_matrixeventprocessor.ts | 35 ++++++++++++++++++-------------
 test/test_util.ts                 |  4 ++++
 3 files changed, 26 insertions(+), 15 deletions(-)

diff --git a/src/util.ts b/src/util.ts
index 548b612..82fceb6 100644
--- a/src/util.ts
+++ b/src/util.ts
@@ -240,7 +240,7 @@ export class Util {
 
   public static GetReplyFromReplyBody(body: string) {
       const lines = body.split("\n");
-      while(lines[0].startsWith("> ") || lines[0].trim().length > 0) {
+      while(lines[0].startsWith("> ") || lines[0].trim().length === 0) {
           lines.splice(0,1);
           if (lines.length === 0) {
               return "";
diff --git a/test/test_matrixeventprocessor.ts b/test/test_matrixeventprocessor.ts
index 63e62d5..6505924 100644
--- a/test/test_matrixeventprocessor.ts
+++ b/test/test_matrixeventprocessor.ts
@@ -102,14 +102,17 @@ This is the first reply`,
     config.bridge.disableDiscordMentions = disableMentions;
     config.bridge.disableEveryoneMention = disableEveryone;
     config.bridge.disableHereMention = disableHere;
+
+    const Util = Object.assign(require("../src/util").Util, {
+        DownloadFile: (name: string) => {
+            const size = parseInt(name.substring(name.lastIndexOf("/") + 1), undefined);
+            return Buffer.alloc(size);
+        },
+    })
+
     return new (Proxyquire("../src/matrixeventprocessor", {
         "./util": {
-            Util: {
-                DownloadFile: (name: string) => {
-                    const size = parseInt(name.substring(name.lastIndexOf("/") + 1), undefined);
-                    return Buffer.alloc(size);
-                },
-            },
+            Util
         },
     })).MatrixEventProcessor(
         new MatrixEventProcessorOpts(
@@ -672,7 +675,7 @@ describe("MatrixEventProcessor", () => {
             });
             expect(result).to.be.undefined;
         });
-        it("should handle replies with a wrongly formatted body", async () => {
+        it("should handle replies without a fallback", async () => {
             const processor = createMatrixEventProcessor();
             const result = await processor.GetEmbedForReply({
                 sender: "@test:localhost",
@@ -681,12 +684,16 @@ describe("MatrixEventProcessor", () => {
                     "body": "Test",
                     "m.relates_to": {
                         "m.in_reply_to": {
-                            event_id: "!event:thing",
+                            event_id: "$goodEvent:localhost",
                         },
                     },
                 },
             });
-            expect(result).to.be.undefined;
+            expect(result[0].description).to.be.equal("Hello!");
+            expect(result[0].author.name).to.be.equal("Doggo!");
+            expect(result[0].author.icon_url).to.be.equal("https://fakeurl.com");
+            expect(result[0].author.url).to.be.equal("https://matrix.to/#/@doggo:localhost");
+            expect(result[1]).to.be.equal("Test");
         });
         it("should handle replies with a missing event", async () => {
             const processor = createMatrixEventProcessor();
@@ -699,15 +706,15 @@ describe("MatrixEventProcessor", () => {
 This is where the reply goes`,
                     "m.relates_to": {
                         "m.in_reply_to": {
-                            event_id: "!event:thing",
+                            event_id: "$event:thing",
                         },
                     },
                 },
             });
-            expect(result[0].description).to.be.equal("This is the fake body");
-            expect(result[0].author.name).to.be.equal("Doggo!");
-            expect(result[0].author.icon_url).to.be.equal("https://fakeurl.com");
-            expect(result[0].author.url).to.be.equal("https://matrix.to/#/@doggo:localhost");
+            expect(result[0].description).to.be.equal("Reply with unknown content");
+            expect(result[0].author.name).to.be.equal("Unknown");
+            expect(result[0].author.icon_url).to.be.undefined;
+            expect(result[0].author.url).to.be.undefined;
             expect(result[1]).to.be.equal("This is where the reply goes");
         });
         it("should handle replies with a valid reply event", async () => {
diff --git a/test/test_util.ts b/test/test_util.ts
index 6dc150e..dc48346 100644
--- a/test/test_util.ts
+++ b/test/test_util.ts
@@ -135,5 +135,9 @@ there are even more lines here.`);
 `);
             return expect(reply).to.equal("");
         });
+        it("Should return body if no reply found", () => {
+            const reply = Util.GetReplyFromReplyBody("Test\nwith\nhalfy");
+            return expect(reply).to.equal("Test\nwith\nhalfy");
+        });
     });
 });
-- 
GitLab