m->d: fix image captions spec, fix upload issues

This commit is contained in:
Cadence Ember 2025-02-28 00:39:03 +13:00
commit a8670323a0
10 changed files with 203 additions and 97 deletions

View file

@ -315,7 +315,7 @@ function getUserOrProxyOwnerID(mxid) {
* At the time of this executing, we know what the end of message emojis are, and we know that at least one of them is unknown.
* This function will strip them from the content and generate the correct pending file of the sprite sheet.
* @param {string} content
* @param {{id: string, name: string}[]} attachments
* @param {{id: string, filename: string}[]} attachments
* @param {({name: string, mxc: string} | {name: string, mxc: string, key: string, iv: string} | {name: string, buffer: Buffer})[]} pendingFiles
* @param {(mxc: string) => Promise<Buffer | undefined>} mxcDownloader function that will download the mxc URLs and convert to uncompressed PNG data. use `getAndConvertEmoji` or a mock.
*/
@ -329,9 +329,9 @@ async function uploadEndOfMessageSpriteSheet(content, attachments, pendingFiles,
// Create a sprite sheet of known and unknown emojis from the end of the message
const buffer = await emojiSheet.compositeMatrixEmojis(endOfMessageEmojis, mxcDownloader)
// Attach it
const name = "emojis.png"
attachments.push({id: String(attachments.length), name})
pendingFiles.push({name, buffer})
const filename = "emojis.png"
attachments.push({id: String(attachments.length), filename})
pendingFiles.push({name: filename, buffer})
return content
}
@ -486,6 +486,7 @@ async function eventToMessage(event, guild, di) {
}
let content = event.content.body // ultimate fallback
/** @type {{id: string, filename: string}[]} */
const attachments = []
/** @type {({name: string, mxc: string} | {name: string, mxc: string, key: string, iv: string} | {name: string, buffer: Buffer})[]} */
const pendingFiles = []
@ -493,7 +494,45 @@ async function eventToMessage(event, guild, di) {
const ensureJoined = []
// Convert content depending on what the message is
if (event.type === "m.room.message" && (event.content.msgtype === "m.text" || event.content.msgtype === "m.emote")) {
// Handle images first - might need to handle their `body`/`formatted_body` as well, which will fall through to the text processor
let shouldProcessTextEvent = event.type === "m.room.message" && (event.content.msgtype === "m.text" || event.content.msgtype === "m.emote")
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.filename || event.content.body
if ("url" in event.content) {
// Unencrypted
attachments.push({id: "0", filename})
pendingFiles.push({name: filename, mxc: event.content.url})
} else {
// Encrypted
assert.equal(event.content.file.key.alg, "A256CTR")
attachments.push({id: "0", filename})
pendingFiles.push({name: filename, mxc: event.content.file.url, key: event.content.file.key.k, iv: event.content.file.iv})
}
// Check if we also need to process a text event for this image - if it has a caption that's different from its filename
if ((event.content.body && event.content.filename && event.content.body !== event.content.filename) || event.content.formatted_body) {
shouldProcessTextEvent = true
}
}
if (event.type === "m.sticker") {
content = ""
let filename = event.content.body
if (event.type === "m.sticker") {
let mimetype
if (event.content.info?.mimetype?.includes("/")) {
mimetype = event.content.info.mimetype
} else {
const res = await di.api.getMedia(event.content.url, {method: "HEAD"})
if (res.status === 200) {
mimetype = res.headers.get("content-type")
}
if (!mimetype) throw new Error(`Server error ${res.status} or missing content-type while detecting sticker mimetype`)
}
filename += "." + mimetype.split("/")[1]
}
attachments.push({id: "0", filename})
pendingFiles.push({name: filename, mxc: event.content.url})
} else if (shouldProcessTextEvent) {
// Handling edits. If the edit was an edit of a reply, edits do not include the reply reference, so we need to fetch up to 2 more events.
// this event ---is an edit of--> original event ---is a reply to--> past event
await (async () => {
@ -780,40 +819,6 @@ async function eventToMessage(event, guild, di) {
// @ts-ignore bad type from turndown
content = turndownService.escape(content)
}
} 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.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
attachments.push({id: "0", description, filename})
pendingFiles.push({name: filename, mxc: event.content.url})
} else {
// Encrypted
assert.equal(event.content.file.key.alg, "A256CTR")
attachments.push({id: "0", description, filename})
pendingFiles.push({name: filename, mxc: event.content.file.url, key: event.content.file.key.k, iv: event.content.file.iv})
}
} else if (event.type === "m.sticker") {
content = ""
let filename = event.content.body
if (event.type === "m.sticker") {
let mimetype
if (event.content.info?.mimetype?.includes("/")) {
mimetype = event.content.info.mimetype
} else {
const res = await di.api.getMedia(event.content.url, {method: "HEAD"})
if (res.status === 200) {
mimetype = res.headers.get("content-type")
}
if (!mimetype) throw new Error(`Server error ${res.status} or missing content-type while detecting sticker mimetype`)
}
filename += "." + mimetype.split("/")[1]
}
attachments.push({id: "0", filename})
pendingFiles.push({name: filename, mxc: event.content.url})
}
content = displayNameRunoff + replyLine + content

View file

@ -3770,7 +3770,7 @@ test("event2message: text attachments work", async t => {
username: "cadence [they]",
content: "",
avatar_url: "https://bridge.example.org/download/matrix/cadence.moe/azCAhThKTojXSZJRoWwZmhvU",
attachments: [{id: "0", description: undefined, filename: "chiki-powerups.txt"}],
attachments: [{id: "0", filename: "chiki-powerups.txt"}],
pendingFiles: [{name: "chiki-powerups.txt", mxc: "mxc://cadence.moe/zyThGlYQxvlvBVbVgKDDbiHH"}]
}]
}
@ -3806,14 +3806,14 @@ test("event2message: image attachments work", async t => {
username: "cadence [they]",
content: "",
avatar_url: "https://bridge.example.org/download/matrix/cadence.moe/azCAhThKTojXSZJRoWwZmhvU",
attachments: [{id: "0", description: undefined, filename: "cool cat.png"}],
attachments: [{id: "0", filename: "cool cat.png"}],
pendingFiles: [{name: "cool cat.png", mxc: "mxc://cadence.moe/IvxVJFLEuksCNnbojdSIeEvn"}]
}]
}
)
})
test("event2message: image attachments can have a custom description", async t => {
test("event2message: image attachments can have a plaintext caption", async t => {
t.deepEqual(
await eventToMessage({
type: "m.room.message",
@ -3840,10 +3840,62 @@ test("event2message: image attachments can have a custom description", async t =
messagesToEdit: [],
messagesToSend: [{
username: "cadence [they]",
content: "",
content: "Cat emoji surrounded by pink hearts",
avatar_url: "https://bridge.example.org/download/matrix/cadence.moe/azCAhThKTojXSZJRoWwZmhvU",
attachments: [{id: "0", description: "Cat emoji surrounded by pink hearts", filename: "cool cat.png"}],
pendingFiles: [{name: "cool cat.png", mxc: "mxc://cadence.moe/IvxVJFLEuksCNnbojdSIeEvn"}]
attachments: [{id: "0", filename: "cool cat.png"}],
pendingFiles: [{name: "cool cat.png", mxc: "mxc://cadence.moe/IvxVJFLEuksCNnbojdSIeEvn"}],
allowed_mentions: {
parse: ["users", "roles"]
}
}]
}
)
})
test("event2message: image attachments can have a formatted caption", async t => {
t.deepEqual(
await eventToMessage({
content: {
body: "this event has `formatting`",
filename: "5740.jpg",
format: "org.matrix.custom.html",
formatted_body: "this event has <code>formatting</code>",
info: {
h: 1340,
mimetype: "image/jpeg",
size: 226689,
thumbnail_info: {
h: 670,
mimetype: "image/jpeg",
size: 80157,
w: 540
},
thumbnail_url: "mxc://thomcat.rocks/XhLsOCDBYyearsLQgUUrbAvw",
w: 1080,
"xyz.amorgan.blurhash": "KHJQG*55ic-.}?0M58J.9v"
},
msgtype: "m.image",
url: "mxc://thomcat.rocks/RTHsXmcMPXmuHqVNsnbKtRbh"
},
origin_server_ts: 1740607766895,
sender: "@cadence:cadence.moe",
type: "m.room.message",
event_id: "$NqNqVgukiQm1nynm9vIr9FIq31hZpQ3udOd7cBIW46U",
room_id: "!BnKuBPCvyfOkhcUjEu:cadence.moe"
}),
{
ensureJoined: [],
messagesToDelete: [],
messagesToEdit: [],
messagesToSend: [{
username: "cadence [they]",
content: "this event has `formatting`",
avatar_url: "https://bridge.example.org/download/matrix/cadence.moe/azCAhThKTojXSZJRoWwZmhvU",
attachments: [{id: "0", filename: "5740.jpg"}],
pendingFiles: [{name: "5740.jpg", mxc: "mxc://thomcat.rocks/RTHsXmcMPXmuHqVNsnbKtRbh"}],
allowed_mentions: {
parse: ["users", "roles"]
}
}]
}
)
@ -3892,7 +3944,7 @@ test("event2message: encrypted image attachments work", async t => {
username: "cadence [they]",
content: "",
avatar_url: "https://bridge.example.org/download/matrix/cadence.moe/azCAhThKTojXSZJRoWwZmhvU",
attachments: [{id: "0", description: undefined, filename: "image.png"}],
attachments: [{id: "0", filename: "image.png"}],
pendingFiles: [{
name: "image.png",
mxc: "mxc://heyquark.com/LOGkUTlVFrqfiExlGZNgCJJX",

View file

@ -1,9 +1,9 @@
// @ts-check
const mixin = require("@cloudrac3r/mixin-deep")
const stream = require("stream")
const streamWeb = require("stream/web")
const getStream = require("get-stream")
const {buffer} = require("stream/consumers")
const mixin = require("@cloudrac3r/mixin-deep")
const {reg, writeRegistration} = require("./read-registration.js")
@ -19,20 +19,33 @@ class MatrixServerError extends Error {
}
}
/**
* @param {undefined | string | object | streamWeb.ReadableStream | stream.Readable} body
* @returns {Promise<string | streamWeb.ReadableStream | stream.Readable | Buffer>}
*/
async function _convertBody(body) {
if (body == undefined || Object.is(body.constructor, Object)) {
return JSON.stringify(body) // almost every POST request is going to follow this one
} else if (body instanceof stream.Readable && reg.ooye.content_length_workaround) {
return await buffer(body) // content length workaround is set, so convert to buffer. the buffer consumer accepts node streams.
} else if (body instanceof stream.Readable) {
return stream.Readable.toWeb(body) // native fetch can only consume web streams
} else if (body instanceof streamWeb.ReadableStream && reg.ooye.content_length_workaround) {
return await buffer(body) // content lenght workaround is set, so convert to buffer. the buffer consumer accepts async iterables, which web streams are.
}
return body
}
/* c8 ignore start */
/**
* @param {string} method
* @param {string} url
* @param {string | object | streamWeb.ReadableStream | stream.Readable} [body]
* @param {string | object | streamWeb.ReadableStream | stream.Readable} [bodyIn]
* @param {any} [extra]
*/
async function mreq(method, url, body, extra = {}) {
if (body == undefined || Object.is(body.constructor, Object)) {
body = JSON.stringify(body)
} else if (body instanceof stream.Readable && reg.ooye.content_length_workaround) {
body = await getStream.buffer(body)
} else if (body instanceof streamWeb.ReadableStream && reg.ooye.content_length_workaround) {
body = await stream.consumers.buffer(stream.Readable.fromWeb(body))
}
async function mreq(method, url, bodyIn, extra = {}) {
const body = await _convertBody(bodyIn)
/** @type {RequestInit} */
const opts = mixin({
@ -86,3 +99,4 @@ module.exports.MatrixServerError = MatrixServerError
module.exports.baseUrl = baseUrl
module.exports.mreq = mreq
module.exports.withAccessToken = withAccessToken
module.exports._convertBody = _convertBody

47
src/matrix/mreq.test.js Normal file
View file

@ -0,0 +1,47 @@
// @ts-check
const assert = require("assert")
const stream = require("stream")
const streamWeb = require("stream/web")
const {buffer} = require("stream/consumers")
const {test} = require("supertape")
const {_convertBody} = require("./mreq")
const {reg} = require("./read-registration")
async function *generator() {
yield "a"
yield "b"
}
reg.ooye.content_length_workaround = false
test("convert body: converts object to string", async t => {
t.equal(await _convertBody({a: "1"}), `{"a":"1"}`)
})
test("convert body: leaves undefined as undefined", async t => {
t.equal(await _convertBody(undefined), undefined)
})
test("convert body: leaves web readable as web readable", async t => {
const webReadable = stream.Readable.toWeb(stream.Readable.from(generator()))
t.equal(await _convertBody(webReadable), webReadable)
})
test("convert body: converts node readable to web readable (for native fetch upload)", async t => {
const readable = stream.Readable.from(generator())
const webReadable = await _convertBody(readable)
assert(webReadable instanceof streamWeb.ReadableStream)
t.deepEqual(await buffer(webReadable), Buffer.from("ab"))
})
test("convert body: converts node readable to buffer", async t => {
reg.ooye.content_length_workaround = true
const readable = stream.Readable.from(generator())
t.deepEqual(await _convertBody(readable), Buffer.from("ab"))
})
test("convert body: converts web readable to buffer", async t => {
const webReadable = stream.Readable.toWeb(stream.Readable.from(generator()))
t.deepEqual(await _convertBody(webReadable), Buffer.from("ab"))
})

4
src/types.d.ts vendored
View file

@ -167,6 +167,8 @@ export namespace Event {
export type M_Room_Message_File = {
msgtype: "m.file" | "m.image" | "m.video" | "m.audio"
body: string
format?: "org.matrix.custom.html"
formatted_body?: string
filename?: string
url: string
info?: any
@ -184,6 +186,8 @@ export namespace Event {
export type M_Room_Message_Encrypted_File = {
msgtype: "m.file" | "m.image" | "m.video" | "m.audio"
body: string
format?: "org.matrix.custom.html"
formatted_body?: string
filename?: string
file: {
url: string

View file

@ -1,14 +1,14 @@
// @ts-check
const streamWeb = require("stream/web")
const {test} = require("supertape")
const {test} = require("../../test/web")
const {router} = require("../../test/web")
const assert = require("assert").strict
require("./server")
test("web server: can get home", async t => {
t.match(await router.test("get", "/", {}), /Add the bot to your Discord server./)
t.has(await router.test("get", "/", {}), /a bridge between the Discord and Matrix chat apps/)
})
test("web server: can get htmx", async t => {