From 019e0e9c5a9534801c8eb691c69ca74336e23151 Mon Sep 17 00:00:00 2001 From: derouet2018 <tristan.derouet@gmail.com> Date: Sat, 10 Jun 2023 10:07:06 +0200 Subject: [PATCH] Fix matrix reply on modified message for discord --- src/matrixeventprocessor.ts | 50 ++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/src/matrixeventprocessor.ts b/src/matrixeventprocessor.ts index fee2c30..af8e1df 100644 --- a/src/matrixeventprocessor.ts +++ b/src/matrixeventprocessor.ts @@ -324,6 +324,54 @@ export class MatrixEventProcessor { }; } + public async ReplyEventToEmbed( + event: IMatrixEvent, repliedEvent: IMatrixEvent, channel: Discord.TextChannel, getReply: boolean = true, + ): Promise<IMatrixEventProcessorResult> { + const mxClient = this.bridge.botIntent.underlyingClient; + const profile = await this.GetUserProfileForRoom(repliedEvent.room_id, repliedEvent.sender); + const params = { + mxClient, + roomId: repliedEvent.room_id, + userId: repliedEvent.sender, + } as IMatrixMessageProcessorParams; + if (profile) { + params.displayname = profile.displayname; + } + let formattedBodyRegex = new RegExp("<br \/>(.*)<\/blockquote>") + let bodyRegex = new RegExp("> <@[^\.>]*:[^\.>]*\.[^\.>]*> (.*)"); + let body: string = ""; + if (event.type !== "m.sticker") { + const replyContent = repliedEvent.content; + const baseContent = event.content; + if(replyContent && baseContent && replyContent["body"] && baseContent["body"]){ + replyContent["body"] = baseContent["body"].matchAll(bodyRegex)[0] + } + if(replyContent && baseContent && replyContent["formatted_body"] && baseContent["formatted_body"]){ + replyContent["formatted_body"] = baseContent["formatted_body"].matchAll(formattedBodyRegex)[0] + } + body = await this.matrixMsgProcessor.FormatMessage(replyContent as IMatrixMessage, channel.guild, params); + } + + const messageEmbed = new Discord.MessageEmbed(); + messageEmbed.setDescription(body); + await this.SetEmbedAuthor(messageEmbed, repliedEvent.sender, profile); + const replyEmbed = getReply ? (await this.GetEmbedForReply(repliedEvent, channel)) : undefined; + if (replyEmbed && replyEmbed.fields) { + for (let i = 0; i < replyEmbed.fields.length; i++) { + const f = replyEmbed.fields[i]; + if (f.name === "ping") { + messageEmbed.description += `\n(${f.value})`; + replyEmbed.fields.splice(i, 1); + break; + } + } + } + return { + messageEmbed, + replyEmbed, + }; + } + public async HandleAttachment( event: IMatrixEvent, mxClient: MatrixClient, @@ -391,7 +439,7 @@ export class MatrixEventProcessor { if (!sourceEvent || !sourceEvent.content || !sourceEvent.content.body) { throw Error("No content could be found"); } - const replyEmbed = (await this.EventToEmbed(sourceEvent, channel, true)).messageEmbed; + const replyEmbed = (await this.ReplyEventToEmbed(event, sourceEvent, channel, true)).messageEmbed; // if we reply to a discord member, ping them! if (this.bridge.isNamespacedUser(sourceEvent.sender)) { -- GitLab