large files are now linked instead of uploaded

This commit is contained in:
Cadence Ember 2023-07-13 17:36:20 +12:00
parent 3a59d66626
commit f16900553a
6 changed files with 57 additions and 7 deletions

View file

@ -2,6 +2,7 @@
const assert = require("assert").strict const assert = require("assert").strict
const markdown = require("discord-markdown") const markdown = require("discord-markdown")
const pb = require("prettier-bytes")
const DiscordTypes = require("discord-api-types/v10") const DiscordTypes = require("discord-api-types/v10")
const passthrough = require("../../passthrough") const passthrough = require("../../passthrough")
@ -184,8 +185,24 @@ async function messageToEvent(message, guild, api) {
// Then attachments // Then attachments
const attachmentEvents = await Promise.all(message.attachments.map(async attachment => { const attachmentEvents = await Promise.all(message.attachments.map(async attachment => {
// TODO: handle large files differently - link them instead of uploading const emoji =
if (attachment.content_type?.startsWith("image/") && attachment.width && attachment.height) { attachment.content_type?.startsWith("image/jp") ? "📸"
: attachment.content_type?.startsWith("image/") ? "🖼️"
: attachment.content_type?.startsWith("video/") ? "🎞️"
: attachment.content_type?.startsWith("text/") ? "📝"
: attachment.content_type?.startsWith("audio/") ? "🎶"
: "📄"
// for large files, always link them instead of uploading so I don't use up all the space in the content repo
if (attachment.size > reg.ooye.max_file_size) {
return {
$type: "m.room.message",
"m.mentions": mentions,
msgtype: "m.text",
body: `${emoji} Uploaded file: ${attachment.url} (${pb(attachment.size)})`,
format: "org.matrix.custom.html",
formatted_body: `${emoji} Uploaded file: <a href="${attachment.url}">${attachment.filename}</a> (${pb(attachment.size)})`
}
} else if (attachment.content_type?.startsWith("image/") && attachment.width && attachment.height) {
return { return {
$type: "m.room.message", $type: "m.room.message",
"m.mentions": mentions, "m.mentions": mentions,
@ -206,7 +223,7 @@ async function messageToEvent(message, guild, api) {
$type: "m.room.message", $type: "m.room.message",
"m.mentions": mentions, "m.mentions": mentions,
msgtype: "m.text", msgtype: "m.text",
body: "Unsupported attachment:\n" + JSON.stringify(attachment, null, 2) body: `Unsupported attachment:\n${JSON.stringify(attachment, null, 2)}\n${attachment.url}`
} }
} }
})) }))

View file

@ -258,4 +258,29 @@ test("message2event: simple written @mention for matrix user", async t => {
}]) }])
}) })
test("message2event: very large attachment is linked instead of being uploaded", async t => {
const events = await messageToEvent({
content: "hey",
attachments: [{
filename: "hey.jpg",
url: "https://discord.com/404/hey.jpg",
content_type: "application/i-made-it-up",
size: 100e6
}]
})
t.deepEqual(events, [{
$type: "m.room.message",
"m.mentions": {},
msgtype: "m.text",
body: "hey"
}, {
$type: "m.room.message",
"m.mentions": {},
msgtype: "m.text",
body: "📄 Uploaded file: https://discord.com/404/hey.jpg (100 MB)",
format: "org.matrix.custom.html",
formatted_body: '📄 Uploaded file: <a href="https://discord.com/404/hey.jpg">hey.jpg</a> (100 MB)'
}])
})
// TODO: read "edits of replies" in the spec // TODO: read "edits of replies" in the spec

View file

@ -110,7 +110,7 @@ async function sendState(roomID, type, stateKey, content, mxid) {
} }
async function sendEvent(roomID, type, content, mxid) { async function sendEvent(roomID, type, content, mxid) {
console.log(`[api] event to ${roomID} as ${mxid || "default sim"}`) console.log(`[api] event ${type} to ${roomID} as ${mxid || "default sim"}`)
/** @type {Ty.R.EventSent} */ /** @type {Ty.R.EventSent} */
const root = await mreq.mreq("PUT", path(`/client/v3/rooms/${roomID}/send/${type}/${makeTxnId.makeTxnId()}`, mxid), content) const root = await mreq.mreq("PUT", path(`/client/v3/rooms/${roomID}/send/${type}/${makeTxnId.makeTxnId()}`, mxid), content)
return root.event_id return root.event_id

12
package-lock.json generated
View file

@ -18,6 +18,7 @@
"matrix-js-sdk": "^24.1.0", "matrix-js-sdk": "^24.1.0",
"mixin-deep": "^2.0.1", "mixin-deep": "^2.0.1",
"node-fetch": "^2.6.7", "node-fetch": "^2.6.7",
"prettier-bytes": "^1.0.4",
"snowtransfer": "^0.8.0", "snowtransfer": "^0.8.0",
"try-to-catch": "^3.0.1" "try-to-catch": "^3.0.1"
}, },
@ -2099,6 +2100,11 @@
"node": ">=10" "node": ">=10"
} }
}, },
"node_modules/prettier-bytes": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/prettier-bytes/-/prettier-bytes-1.0.4.tgz",
"integrity": "sha512-dLbWOa4xBn+qeWeIF60qRoB6Pk2jX5P3DIVgOQyMyvBpu931Q+8dXz8X0snJiFkQdohDDLnZQECjzsAj75hgZQ=="
},
"node_modules/pretty-format": { "node_modules/pretty-format": {
"version": "29.5.0", "version": "29.5.0",
"resolved": "https://registry.npmjs.org/pretty-format/-/pretty-format-29.5.0.tgz", "resolved": "https://registry.npmjs.org/pretty-format/-/pretty-format-29.5.0.tgz",
@ -2318,9 +2324,9 @@
} }
}, },
"node_modules/semver": { "node_modules/semver": {
"version": "7.5.0", "version": "7.5.4",
"resolved": "https://registry.npmjs.org/semver/-/semver-7.5.0.tgz", "resolved": "https://registry.npmjs.org/semver/-/semver-7.5.4.tgz",
"integrity": "sha512-+XC0AD/R7Q2mPSRuy2Id0+CGTZ98+8f+KvwirxOKIEyid+XSx6HbC63p+O4IndTHuX5Z+JxQ0TghCkO5Cg/2HA==", "integrity": "sha512-1bCSESV6Pv+i21Hvpxp3Dx+pSD8lIPt8uVjRrxAUt/nbswYc+tK6Y2btiULjd4+fnq15PX+nqQDC7Oft7WkwcA==",
"dependencies": { "dependencies": {
"lru-cache": "^6.0.0" "lru-cache": "^6.0.0"
}, },

View file

@ -24,6 +24,7 @@
"matrix-js-sdk": "^24.1.0", "matrix-js-sdk": "^24.1.0",
"mixin-deep": "^2.0.1", "mixin-deep": "^2.0.1",
"node-fetch": "^2.6.7", "node-fetch": "^2.6.7",
"prettier-bytes": "^1.0.4",
"snowtransfer": "^0.8.0", "snowtransfer": "^0.8.0",
"try-to-catch": "^3.0.1" "try-to-catch": "^3.0.1"
}, },

1
types.d.ts vendored
View file

@ -18,6 +18,7 @@ export type AppServiceRegistrationConfig = {
rate_limited: boolean rate_limited: boolean
ooye: { ooye: {
namespace_prefix: string namespace_prefix: string
max_file_size: number
} }
} }