From 13d1f86d31f3af9657a83a51b171547fb53b2325 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Tue, 13 Feb 2024 14:38:41 +1300 Subject: [PATCH] d->m: Fix conversion of PK replies to Discord user --- d2m/converters/message-to-event.js | 8 +++-- d2m/converters/message-to-event.pk.test.js | 34 ++++++++++++++++-- test/data.js | 41 +++++++++++++++++++++- 3 files changed, 78 insertions(+), 5 deletions(-) diff --git a/d2m/converters/message-to-event.js b/d2m/converters/message-to-event.js index 88e498f..ba51abf 100644 --- a/d2m/converters/message-to-event.js +++ b/d2m/converters/message-to-event.js @@ -261,10 +261,14 @@ async function messageToEvent(message, guild, options = {}, di) { we generate a partial referenced_message based on what PK provided. we don't need everything, since this will only be used for further message-to-event converting. the following properties are necessary: - content: used for generating the reply fallback + - author: used for the top of the reply fallback (only used for discord authors. for matrix authors, repliedToEventSenderMxid is set.) */ - // @ts-ignore message.referenced_message = { - content: message.embeds[0].description.replace(/^.*?\)\*\*\s*/, "") + content: message.embeds[0].description.replace(/^.*?\)\*\*\s*/, ""), + // @ts-ignore + author: { + username: message.embeds[0].author.name.replace(/\s*↩️\s*$/, "") + } } message.embeds.shift() repliedToEventRow = row diff --git a/d2m/converters/message-to-event.pk.test.js b/d2m/converters/message-to-event.pk.test.js index 48984e4..4bf8eb2 100644 --- a/d2m/converters/message-to-event.pk.test.js +++ b/d2m/converters/message-to-event.pk.test.js @@ -29,8 +29,8 @@ function mockGetEvent(t, roomID_in, eventID_in, outer) { } } -test("message2event: pk reply is converted to native matrix reply", async t => { - const events = await messageToEvent(data.pk_message.pk_reply, {}, {}, { +test("message2event: pk reply to matrix is converted to native matrix reply", async t => { + const events = await messageToEvent(data.pk_message.pk_reply_to_matrix, {}, {}, { api: { getEvent: mockGetEvent(t, "!TqlyQmifxGUggEmdBN:cadence.moe", "$NB6nPgO2tfXyIwwDSF0Ga0BUrsgX1S-0Xl-jAvI8ucU", { type: "m.room.message", @@ -62,3 +62,33 @@ test("message2event: pk reply is converted to native matrix reply", async t => { } }]) }) + +test("message2event: pk reply to discord is converted to native matrix reply", async t => { + const events = await messageToEvent(data.pk_message.pk_reply_to_discord, {}, {}, { + api: { + getEvent: mockGetEvent(t, "!TqlyQmifxGUggEmdBN:cadence.moe", "$NB6nPgO2tfXyIwwDSF0Ga0BUrsgX1S-0Xl-jAvI8ucU", { + type: "m.room.message", + sender: "@_ooye_.wing.:cadence.moe", + content: { + msgtype: "m.text", + body: "some text" + } + }) + } + }) + t.deepEqual(events, [{ + $type: "m.room.message", + msgtype: "m.text", + "m.mentions": {}, + body: "> wing: some text\n\nthis is a reply", + format: "org.matrix.custom.html", + formatted_body: '
In reply to wing
' + + "some text
" + + "this is a reply", + "m.relates_to": { + "m.in_reply_to": { + event_id: "$mtR8cJqM4fKno1bVsm8F4wUVqSntt2sq6jav1lyavuA" + } + } + }]) +}) diff --git a/test/data.js b/test/data.js index 9d86944..0832eaa 100644 --- a/test/data.js +++ b/test/data.js @@ -1765,7 +1765,7 @@ module.exports = { } }, pk_message: { - pk_reply: { + pk_reply_to_matrix: { id: "1202543812644306965", type: 0, content: "this is a reply", @@ -1803,6 +1803,45 @@ module.exports = { components: [], application_id: "466378653216014359", webhook_id: "1195662438662680720" + }, + pk_reply_to_discord: { + id: "1202543812644306965", + type: 0, + content: "this is a reply", + channel_id: "1160894080998461480", + author: { + id: "1195662438662680720", + username: "special name", + avatar: "6b44a106659e78a2550474c61889194d", + discriminator: "0000", + public_flags: 0, + flags: 0, + bot: true, + global_name: null + }, + attachments: [], + embeds: [ + { + type: "rich", + description: "**[Reply to:](https://discord.com/channels/112760669178241024/112760669178241024/1141501302736695316)** some text", + author: { + name: "wing ↩️", + icon_url: "https://cdn.discordapp.com/avatars/112890272819507200/47db1be7ab77e1d812a4573177af0692.png", + proxy_icon_url: "https://images-ext-1.discordapp.net/external/wWslraV-s-bLDwphL64YxeDm30M7PIhQQy0EQa8jpDc/https/cdn.discordapp.com/avatars/112890272819507200/47db1be7ab77e1d812a4573177af0692.png" + } + } + ], + mentions: [], + mention_roles: [], + pinned: false, + mention_everyone: false, + tts: false, + timestamp: "2024-02-01T09:19:47.118000+00:00", + edited_timestamp: null, + flags: 0, + components: [], + application_id: "466378653216014359", + webhook_id: "1195662438662680720" } }, message_with_embeds: {