m->d: Support attachment body data as alt text

This commit is contained in:
Cadence Ember 2024-01-06 19:42:13 +13:00
parent 60cf40680f
commit 84d791cd8a
3 changed files with 48 additions and 6 deletions

View File

@ -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") {

View File

@ -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",

2
types.d.ts vendored
View File

@ -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