From ff7af39802e34ac3972396cda67d0442dabf00c3 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Mon, 25 Mar 2024 18:05:19 +1300 Subject: [PATCH] Exclude generated embeds for discord.com --- .../message-to-event.embeds.test.js | 34 ++++++++++++++++ d2m/converters/message-to-event.js | 4 ++ test/data.js | 40 +++++++++++++++++++ 3 files changed, 78 insertions(+) diff --git a/d2m/converters/message-to-event.embeds.test.js b/d2m/converters/message-to-event.embeds.test.js index fc02a3f..61a0822 100644 --- a/d2m/converters/message-to-event.embeds.test.js +++ b/d2m/converters/message-to-event.embeds.test.js @@ -282,3 +282,37 @@ test("message2event embeds: youtube video", async t => { "m.mentions": {} }]) }) + +test("message2event embeds: if discord creates an embed preview for a discord channel link, don't copy that embed", async t => { + const events = await messageToEvent(data.message_with_embeds.discord_server_included_punctuation_bad_discord, data.guild.general, {}, { + api: { + async getStateEvent(roomID, type, key) { + t.equal(roomID, "!TqlyQmifxGUggEmdBN:cadence.moe") + t.equal(type, "m.room.power_levels") + t.equal(key, "") + return { + users: { + "@_ooye_bot:cadence.moe": 100 + } + } + }, + async getJoinedMembers(roomID) { + t.equal(roomID, "!TqlyQmifxGUggEmdBN:cadence.moe") + return { + joined: { + "@_ooye_bot:cadence.moe": {display_name: null, avatar_url: null}, + "@user:matrix.org": {display_name: null, avatar_url: null} + } + } + } + } + }) + t.deepEqual(events, [{ + $type: "m.room.message", + msgtype: "m.text", + body: "(test https://matrix.to/#/!TqlyQmifxGUggEmdBN:cadence.moe/$NB6nPgO2tfXyIwwDSF0Ga0BUrsgX1S-0Xl-jAvI8ucU?via=cadence.moe&via=matrix.org)", + format: "org.matrix.custom.html", + formatted_body: `(test https://matrix.to/#/!TqlyQmifxGUggEmdBN:cadence.moe/$NB6nPgO2tfXyIwwDSF0Ga0BUrsgX1S-0Xl-jAvI8ucU?via=cadence.moe&via=matrix.org)`, + "m.mentions": {} + }]) +}) diff --git a/d2m/converters/message-to-event.js b/d2m/converters/message-to-event.js index 518d041..45e43dd 100644 --- a/d2m/converters/message-to-event.js +++ b/d2m/converters/message-to-event.js @@ -516,6 +516,10 @@ async function messageToEvent(message, guild, options = {}, di) { continue // Matrix's own URL previews are fine for images. } + if (embed.url?.startsWith("https://discord.com/")) { + continue // If discord creates an embed preview for a discord channel link, don't copy that embed + } + // Start building up a replica ("rep") of the embed in Discord-markdown format, which we will convert into both plaintext and formatted body at once const rep = new mxUtils.MatrixStringBuilder() diff --git a/test/data.js b/test/data.js index 17b92cf..77b2ada 100644 --- a/test/data.js +++ b/test/data.js @@ -2870,6 +2870,46 @@ module.exports = { } }, webhook_id: "1109360903096369153" + }, + discord_server_included_punctuation_bad_discord: { + id: "1221672425792606349", + type: 0, + content: "(test https://discord.com/channels/1160894080998461480/1160894080998461480/1202543413652881428)", + channel_id: "1160894080998461480", + author: { + id: "772659086046658620", + username: "cadence.worm", + avatar: "4b5c4b28051144e4c111f0113a0f1cf1", + discriminator: "0", + public_flags: 0, + premium_type: 0, + flags: 0, + banner: null, + accent_color: null, + global_name: "cadence", + avatar_decoration_data: null, + banner_color: null + }, + attachments: [], + embeds: [ + { + type: "article", + url: "https://discord.com/channels/1160894080998461480/1160894080998461480/1202543413652881428)", + title: "Discord - A New Way to Chat with Friends & Communities", + description: "Discord is the easiest way to communicate over voice, video, and text. Chat, hang out, and stay close with your friends and communities.", + provider: { name: "Discord" }, + content_scan_version: 0 + } + ], + mentions: [], + mention_roles: [], + pinned: false, + mention_everyone: false, + tts: false, + timestamp: "2024-03-25T04:10:03.885000+00:00", + edited_timestamp: null, + flags: 0, + components: [] } }, message_update: {