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")) { } 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 = "" 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) { if ("url" in event.content) {
// Unencrypted // Unencrypted
const url = mxUtils.getPublicUrlForMxc(event.content.url) const url = mxUtils.getPublicUrlForMxc(event.content.url)
assert(url) assert(url)
attachments.push({id: "0", filename}) attachments.push({id: "0", description, filename})
pendingFiles.push({name: filename, url}) pendingFiles.push({name: filename, url})
} else { } else {
// Encrypted // Encrypted
const url = mxUtils.getPublicUrlForMxc(event.content.file.url) const url = mxUtils.getPublicUrlForMxc(event.content.file.url)
assert(url) assert(url)
assert.equal(event.content.file.key.alg, "A256CTR") 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}) pendingFiles.push({name: filename, url, key: event.content.file.key.k, iv: event.content.file.iv})
} }
} else if (event.type === "m.sticker") { } else if (event.type === "m.sticker") {

View file

@ -2143,7 +2143,7 @@ test("event2message: text attachments work", async t => {
username: "cadence [they]", username: "cadence [they]",
content: "", content: "",
avatar_url: "https://matrix.cadence.moe/_matrix/media/r0/download/cadence.moe/azCAhThKTojXSZJRoWwZmhvU", 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"}] 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", sender: "@cadence:cadence.moe",
content: { content: {
body: "cool cat.png", body: "cool cat.png",
filename: "cool cat.png",
info: { info: {
size: 43170, size: 43170,
mimetype: "image/png", mimetype: "image/png",
@ -2178,7 +2179,43 @@ test("event2message: image attachments work", async t => {
username: "cadence [they]", username: "cadence [they]",
content: "", content: "",
avatar_url: "https://matrix.cadence.moe/_matrix/media/r0/download/cadence.moe/azCAhThKTojXSZJRoWwZmhvU", 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"}] 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]", username: "cadence [they]",
content: "", content: "",
avatar_url: "https://matrix.cadence.moe/_matrix/media/r0/download/cadence.moe/azCAhThKTojXSZJRoWwZmhvU", 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: [{ pendingFiles: [{
name: "image.png", name: "image.png",
url: "https://matrix.cadence.moe/_matrix/media/r0/download/heyquark.com/LOGkUTlVFrqfiExlGZNgCJJX", 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 = { export type M_Room_Message_File = {
msgtype: "m.file" | "m.image" | "m.video" | "m.audio" msgtype: "m.file" | "m.image" | "m.video" | "m.audio"
body: string body: string
filename?: string
url: string url: string
info?: any info?: any
"m.relates_to"?: { "m.relates_to"?: {
@ -107,6 +108,7 @@ export namespace Event {
export type M_Room_Message_Encrypted_File = { export type M_Room_Message_Encrypted_File = {
msgtype: "m.file" | "m.image" | "m.video" | "m.audio" msgtype: "m.file" | "m.image" | "m.video" | "m.audio"
body: string body: string
filename?: string
file: { file: {
url: string url: string
iv: string iv: string