From 84d791cd8ab858eb99c40c292047ed2ac19cf711 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Sat, 6 Jan 2024 19:42:13 +1300 Subject: [PATCH] m->d: Support attachment body data as alt text --- m2d/converters/event-to-message.js | 9 ++++-- m2d/converters/event-to-message.test.js | 43 +++++++++++++++++++++++-- types.d.ts | 2 ++ 3 files changed, 48 insertions(+), 6 deletions(-) diff --git a/m2d/converters/event-to-message.js b/m2d/converters/event-to-message.js index 1afd882..37262ce 100644 --- a/m2d/converters/event-to-message.js +++ b/m2d/converters/event-to-message.js @@ -535,19 +535,22 @@ async function eventToMessage(event, guild, di) { } } else if (event.type === "m.room.message" && (event.content.msgtype === "m.file" || event.content.msgtype === "m.video" || event.content.msgtype === "m.audio" || event.content.msgtype === "m.image")) { content = "" - const filename = event.content.body + const filename = event.content.filename || event.content.body + // A written `event.content.body` will be bridged to Discord's image `description` which is like alt text. + // Bridging as description rather than message content in order to match Matrix clients (Element, Neochat) which treat this as alt text or title text. + const description = (event.content.body !== event.content.filename && event.content.filename && event.content.body) || undefined if ("url" in event.content) { // Unencrypted const url = mxUtils.getPublicUrlForMxc(event.content.url) assert(url) - attachments.push({id: "0", filename}) + attachments.push({id: "0", description, filename}) pendingFiles.push({name: filename, url}) } else { // Encrypted const url = mxUtils.getPublicUrlForMxc(event.content.file.url) assert(url) assert.equal(event.content.file.key.alg, "A256CTR") - attachments.push({id: "0", filename}) + attachments.push({id: "0", description, filename}) pendingFiles.push({name: filename, url, key: event.content.file.key.k, iv: event.content.file.iv}) } } else if (event.type === "m.sticker") { diff --git a/m2d/converters/event-to-message.test.js b/m2d/converters/event-to-message.test.js index 4b30292..8a57c41 100644 --- a/m2d/converters/event-to-message.test.js +++ b/m2d/converters/event-to-message.test.js @@ -2143,7 +2143,7 @@ test("event2message: text attachments work", async t => { username: "cadence [they]", content: "", avatar_url: "https://matrix.cadence.moe/_matrix/media/r0/download/cadence.moe/azCAhThKTojXSZJRoWwZmhvU", - attachments: [{id: "0", filename: "chiki-powerups.txt"}], + attachments: [{id: "0", description: undefined, filename: "chiki-powerups.txt"}], pendingFiles: [{name: "chiki-powerups.txt", url: "https://matrix.cadence.moe/_matrix/media/r0/download/cadence.moe/zyThGlYQxvlvBVbVgKDDbiHH"}] }] } @@ -2157,6 +2157,7 @@ test("event2message: image attachments work", async t => { sender: "@cadence:cadence.moe", content: { body: "cool cat.png", + filename: "cool cat.png", info: { size: 43170, mimetype: "image/png", @@ -2178,7 +2179,43 @@ test("event2message: image attachments work", async t => { username: "cadence [they]", content: "", avatar_url: "https://matrix.cadence.moe/_matrix/media/r0/download/cadence.moe/azCAhThKTojXSZJRoWwZmhvU", - attachments: [{id: "0", filename: "cool cat.png"}], + attachments: [{id: "0", description: undefined, filename: "cool cat.png"}], + pendingFiles: [{name: "cool cat.png", url: "https://matrix.cadence.moe/_matrix/media/r0/download/cadence.moe/IvxVJFLEuksCNnbojdSIeEvn"}] + }] + } + ) +}) + +test("event2message: image attachments can have a custom description", async t => { + t.deepEqual( + await eventToMessage({ + type: "m.room.message", + sender: "@cadence:cadence.moe", + content: { + body: "Cat emoji surrounded by pink hearts", + filename: "cool cat.png", + info: { + size: 43170, + mimetype: "image/png", + w: 480, + h: 480, + "xyz.amorgan.blurhash": "URTHsVaTpdj2eKZgkkkXp{pHl7feo@lSl9Z$" + }, + msgtype: "m.image", + url: "mxc://cadence.moe/IvxVJFLEuksCNnbojdSIeEvn" + }, + event_id: "$CXQy3Wmg1A-gL_xAesC1HQcQTEXwICLdSwwUx55FBTI", + room_id: "!BnKuBPCvyfOkhcUjEu:cadence.moe" + }), + { + ensureJoined: [], + messagesToDelete: [], + messagesToEdit: [], + messagesToSend: [{ + username: "cadence [they]", + content: "", + avatar_url: "https://matrix.cadence.moe/_matrix/media/r0/download/cadence.moe/azCAhThKTojXSZJRoWwZmhvU", + attachments: [{id: "0", description: "Cat emoji surrounded by pink hearts", filename: "cool cat.png"}], pendingFiles: [{name: "cool cat.png", url: "https://matrix.cadence.moe/_matrix/media/r0/download/cadence.moe/IvxVJFLEuksCNnbojdSIeEvn"}] }] } @@ -2228,7 +2265,7 @@ test("event2message: encrypted image attachments work", async t => { username: "cadence [they]", content: "", avatar_url: "https://matrix.cadence.moe/_matrix/media/r0/download/cadence.moe/azCAhThKTojXSZJRoWwZmhvU", - attachments: [{id: "0", filename: "image.png"}], + attachments: [{id: "0", description: undefined, filename: "image.png"}], pendingFiles: [{ name: "image.png", url: "https://matrix.cadence.moe/_matrix/media/r0/download/heyquark.com/LOGkUTlVFrqfiExlGZNgCJJX", diff --git a/types.d.ts b/types.d.ts index e93f379..9e9d72b 100644 --- a/types.d.ts +++ b/types.d.ts @@ -91,6 +91,7 @@ export namespace Event { export type M_Room_Message_File = { msgtype: "m.file" | "m.image" | "m.video" | "m.audio" body: string + filename?: string url: string info?: any "m.relates_to"?: { @@ -107,6 +108,7 @@ export namespace Event { export type M_Room_Message_Encrypted_File = { msgtype: "m.file" | "m.image" | "m.video" | "m.audio" body: string + filename?: string file: { url: string iv: string