diff --git a/src/matrixeventprocessor.ts b/src/matrixeventprocessor.ts index 13841998a2f689bc07441ac02479c4ec9e4d02f8..ea734eeb7ff556fd2adb7525f84fa3c4397b88e0 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 61edd636899ea5d785d5717a794313566e7f288e..548b61231bde464ba906cb29b46b6ace6a65c313 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 ccc810b872048f81b49b5092d0572b2632a38631..6dc150e572228d7e17d0aa674752cfd86168ea9f 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(""); + }); + }); });