diff --git a/src/d2m/converters/message-to-event.js b/src/d2m/converters/message-to-event.js index cf8d4c2d..195bdddb 100644 --- a/src/d2m/converters/message-to-event.js +++ b/src/d2m/converters/message-to-event.js @@ -428,6 +428,12 @@ async function messageToEvent(message, guild, options = {}, di) { return {body, html} } + /** + * After converting Discord content to Matrix plaintext and HTML content, post-process the bodies and push the resulting text event + * @param {string} body matrix event plaintext body + * @param {string} html matrix event HTML body + * @param {string} msgtype matrix event msgtype (maybe m.text or m.notice) + */ async function addTextEvent(body, html, msgtype) { // Star * prefix for fallback edits if (options.includeEditFallbackStar) { @@ -436,7 +442,7 @@ async function messageToEvent(message, guild, options = {}, di) { } const flags = message.flags || 0 - if (flags & 2) { + if (flags & DiscordTypes.MessageFlags.IsCrosspost) { body = `[๐Ÿ”€ ${message.author.username}]\n` + body html = `๐Ÿ”€ ${message.author.username}
` + html } @@ -508,7 +514,57 @@ async function messageToEvent(message, guild, options = {}, di) { message.content = "changed the channel name to **" + message.content + "**" } + // Forwarded content appears first + if (message.message_reference?.type === DiscordTypes.MessageReferenceType.Forward && message.message_snapshots?.length) { + // Forwarded notice + const eventID = select("event_message", "event_id", {message_id: message.message_reference.message_id}).pluck().get() + const room = select("channel_room", ["room_id", "name", "nick"], {channel_id: message.message_reference.channel_id}).get() + const forwardedNotice = new mxUtils.MatrixStringBuilder() + if (eventID && room) { + const via = await getViaServersMemo(room.room_id) + forwardedNotice.addLine( + `[๐Ÿ”€ Forwarded from #${room.nick || room.name}]`, + tag`๐Ÿ”€ Forwarded from ${room.nick || room.name}` + ) + } else if (room) { + const via = await getViaServersMemo(room.room_id) + forwardedNotice.addLine( + `[๐Ÿ”€ Forwarded from #${room.nick || room.name}]`, + tag`๐Ÿ”€ Forwarded from ${room.nick || room.name}` + ) + } else { + forwardedNotice.addLine( + `[๐Ÿ”€ Forwarded message]`, + tag`๐Ÿ”€ Forwarded message` + ) + } + // Forwarded content + // @ts-ignore + const forwardedEvents = await messageToEvent(message.message_snapshots[0].message, guild, {includeReplyFallback: false, includeEditFallbackStar: false}, di) + + // Indent + for (const event of forwardedEvents) { + if (["m.text", "m.notice"].includes(event.msgtype)) { + event.msgtype = "m.notice" + event.body = event.body.split("\n").map(l => "ยป " + l).join("\n") + event.formatted_body = `
${event.formatted_body}
` + } + } + + // Try to merge the forwarded content with the forwarded notice + let {body, formatted_body} = forwardedNotice.get() + if (forwardedEvents.length >= 1 && ["m.text", "m.notice"].includes(forwardedEvents[0].msgtype)) { // Try to merge the forwarded content and the forwarded notice + forwardedNotice.add("\n", "
") + forwardedEvents[0].body = body + forwardedEvents[0].body + forwardedEvents[0].formatted_body = formatted_body + forwardedEvents[0].formatted_body + } else { + await addTextEvent(body, formatted_body, "m.notice") + } + events.push(...forwardedEvents) + } + + // Then text content if (message.content) { // Mentions scenario 3: scan the message content for written @mentions of matrix users. Allows for up to one space between @ and mention. const matches = [...message.content.matchAll(/@ ?([a-z0-9._]+)\b/gi)] @@ -527,7 +583,6 @@ async function messageToEvent(message, guild, options = {}, di) { } } - // Text content appears first const {body, html} = await transformContent(message.content) await addTextEvent(body, html, msgtype) } diff --git a/src/d2m/converters/message-to-event.test.js b/src/d2m/converters/message-to-event.test.js index 20630d60..7213bb8e 100644 --- a/src/d2m/converters/message-to-event.test.js +++ b/src/d2m/converters/message-to-event.test.js @@ -1017,12 +1017,29 @@ test("message2event: @everyone within a link", async t => { test("message2event: forwarded image", async t => { const events = await messageToEvent(data.message.forwarded_image) - t.deepEqual(events, [{ - $type: "m.room.message", - msgtype: "m.text", - body: "https://github.com/@everyone", - format: "org.matrix.custom.html", - formatted_body: `https://github.com/@everyone`, - "m.mentions": {} - }]) + t.deepEqual(events, [ + { + $type: "m.room.message", + body: "[๐Ÿ”€ Forwarded message]", + format: "org.matrix.custom.html", + formatted_body: "๐Ÿ”€ Forwarded message", + "m.mentions": {}, + msgtype: "m.notice", + }, + { + $type: "m.room.message", + body: "100km.gif", + external_url: "https://bridge.example.org/download/discordcdn/112760669178241024/1296237494987133070/100km.gif", + filename: "100km.gif", + info: { + h: 300, + mimetype: "image/gif", + size: 2965649, + w: 300, + }, + "m.mentions": {}, + msgtype: "m.image", + url: "mxc://cadence.moe/qDAotmebTfEIfsAIVCEZptLh", + }, + ]) }) diff --git a/test/ooye-test-data.sql b/test/ooye-test-data.sql index b6f0951c..7e22e9f6 100644 --- a/test/ooye-test-data.sql +++ b/test/ooye-test-data.sql @@ -123,7 +123,8 @@ INSERT INTO file (discord_url, mxc_url) VALUES ('https://cdn.discordapp.com/emojis/288858540888686602.png', 'mxc://cadence.moe/mwZaCtRGAQQyOItagDeCocEO'), ('https://cdn.discordapp.com/attachments/112760669178241024/1197621094786531358/Ins_1960637570.mp4', 'mxc://cadence.moe/kMqLycqMURhVpwleWkmASpnU'), ('https://cdn.discordapp.com/attachments/1099031887500034088/1112476845502365786/voice-message.ogg', 'mxc://cadence.moe/MRRPDggXQMYkrUjTpxQbmcxB'), -('https://cdn.discordapp.com/attachments/122155380120748034/1174514575220158545/the.yml', 'mxc://cadence.moe/HnQIYQmmlIKwOQsbFsIGpzPP'); +('https://cdn.discordapp.com/attachments/122155380120748034/1174514575220158545/the.yml', 'mxc://cadence.moe/HnQIYQmmlIKwOQsbFsIGpzPP'), +('https://cdn.discordapp.com/attachments/112760669178241024/1296237494987133070/100km.gif', 'mxc://cadence.moe/qDAotmebTfEIfsAIVCEZptLh'); INSERT INTO emoji (emoji_id, name, animated, mxc_url) VALUES ('230201364309868544', 'hippo', 0, 'mxc://cadence.moe/qWmbXeRspZRLPcjseyLmeyXC'),