From d9dc9e482de9dde9d6d087037cbb64f572bdb1a8 Mon Sep 17 00:00:00 2001 From: Will Hunt <will@half-shot.uk> Date: Thu, 25 Oct 2018 17:44:01 +0100 Subject: [PATCH] Use simpler method of reply extraction --- src/matrixeventprocessor.ts | 27 ++++++++------------------- src/util.ts | 13 ++++++++++++- test/test_util.ts | 20 ++++++++++++++++++++ 3 files changed, 40 insertions(+), 20 deletions(-) diff --git a/src/matrixeventprocessor.ts b/src/matrixeventprocessor.ts index 1384199..ea734ee 100644 --- a/src/matrixeventprocessor.ts +++ b/src/matrixeventprocessor.ts @@ -14,7 +14,6 @@ const MaxFileSize = 8000000; const MIN_NAME_LENGTH = 2; const MAX_NAME_LENGTH = 32; const DISCORD_EMOJI_REGEX = /:(\w+):/g; -const REPLY_REGEX = /> <(@.*:.*)> (.*)\n\n(.*)/; export class MatrixEventProcessorOpts { constructor( @@ -207,10 +206,6 @@ export class MatrixEventProcessor { } public async GetEmbedForReply(event: any): Promise<[Discord.RichEmbed, string]|undefined> { - const REPLY_MATCHES = 4; - const INDEX_REPLY = 3; - const INDEX_SOURCE = 2; - const INDEX_SENDER = 1; const relatesTo = event.content["m.relates_to"]; let eventId = null; if (relatesTo && relatesTo["m.in_reply_to"]) { @@ -218,10 +213,11 @@ export class MatrixEventProcessor { } else { return; } - const matches = REPLY_REGEX.exec(event.content.body); - if (!matches || matches.length !== REPLY_MATCHES) { - return; + let reponseText = Util.GetReplyFromReplyBody(event.content.body || ""); + if (reponseText === "") { + reponseText = "Reply with unknown content"; } + const intent = this.bridge.getIntent(); const embed = new Discord.RichEmbed(); // Try to get the event. @@ -231,10 +227,7 @@ export class MatrixEventProcessor { // Check if this is also a reply. if (sourceEvent.content && sourceEvent.content["m.relates_to"] && sourceEvent.content["m.relates_to"]["m.in_reply_to"]) { - const sourceMatch = REPLY_REGEX.exec(sourceEvent.content.body); - if (sourceMatch && sourceMatch.length === REPLY_MATCHES) { - replyText = sourceMatch[INDEX_REPLY]; - } + replyText = Util.GetReplyFromReplyBody(sourceEvent.content.body); } embed.setDescription(replyText); this.SetEmbedAuthor( @@ -244,14 +237,10 @@ export class MatrixEventProcessor { ); } catch (ex) { // For some reason we failed to get the event, so using fallback. - embed.setDescription(matches[INDEX_SOURCE]); - this.SetEmbedAuthor( - embed, - matches[INDEX_SENDER], - await intent.getProfileInfo(matches[INDEX_SENDER]), - ); + embed.setDescription("Reply with unknown content"); + embed.setAuthor("Unknown"); } - return [embed, matches[INDEX_REPLY]]; + return [embed, reponseText]; } private SetEmbedAuthor(embed: Discord.RichEmbed, sender: string, profile: any) { diff --git a/src/util.ts b/src/util.ts index 61edd63..548b612 100644 --- a/src/util.ts +++ b/src/util.ts @@ -218,7 +218,7 @@ export class Util { params[param] = await parameters[param].get(args[i]); i++; } - + const retStr = await action.run(params); return retStr; } @@ -237,6 +237,17 @@ export class Util { } return {command, args}; } + + public static GetReplyFromReplyBody(body: string) { + const lines = body.split("\n"); + while(lines[0].startsWith("> ") || lines[0].trim().length > 0) { + lines.splice(0,1); + if (lines.length === 0) { + return ""; + } + } + return lines.join("\n").trim(); + } } interface IUploadResult { diff --git a/test/test_util.ts b/test/test_util.ts index ccc810b..6dc150e 100644 --- a/test/test_util.ts +++ b/test/test_util.ts @@ -116,4 +116,24 @@ describe("Util", () => { return expect(Util.GetMxidFromName(intent, "badboy", ["abc"])).to.eventually.be.rejected; }); }); + describe("GetReplyFromReplyBody", () => { + it("Should get a reply from the body", () => { + const reply = Util.GetReplyFromReplyBody(`> <@alice:example.org> This is the original body + +This is where the reply goes`); + return expect(reply).to.equal("This is where the reply goes"); + }); + it("Should get a multi-line reply from the body", () => { + const reply = Util.GetReplyFromReplyBody(`> <@alice:example.org> This is the original body + +This is where the reply goes and +there are even more lines here.`); + return expect(reply).to.equal("This is where the reply goes and\nthere are even more lines here."); + }); + it("Should get empty string from an empty reply", () => { + const reply = Util.GetReplyFromReplyBody(`> <@alice:example.org> This is the original body +`); + return expect(reply).to.equal(""); + }); + }); }); -- GitLab