From f16900553a8135e7f6168f7c0be4ea5610512efc Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Thu, 13 Jul 2023 17:36:20 +1200 Subject: [PATCH] large files are now linked instead of uploaded --- d2m/converters/message-to-event.js | 23 ++++++++++++++++++++--- d2m/converters/message-to-event.test.js | 25 +++++++++++++++++++++++++ matrix/api.js | 2 +- package-lock.json | 12 +++++++++--- package.json | 1 + types.d.ts | 1 + 6 files changed, 57 insertions(+), 7 deletions(-) diff --git a/d2m/converters/message-to-event.js b/d2m/converters/message-to-event.js index 300d1543..49a387a3 100644 --- a/d2m/converters/message-to-event.js +++ b/d2m/converters/message-to-event.js @@ -2,6 +2,7 @@ const assert = require("assert").strict const markdown = require("discord-markdown") +const pb = require("prettier-bytes") const DiscordTypes = require("discord-api-types/v10") const passthrough = require("../../passthrough") @@ -184,8 +185,24 @@ async function messageToEvent(message, guild, api) { // Then attachments const attachmentEvents = await Promise.all(message.attachments.map(async attachment => { - // TODO: handle large files differently - link them instead of uploading - if (attachment.content_type?.startsWith("image/") && attachment.width && attachment.height) { + const emoji = + 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: ${attachment.filename} (${pb(attachment.size)})` + } + } else if (attachment.content_type?.startsWith("image/") && attachment.width && attachment.height) { return { $type: "m.room.message", "m.mentions": mentions, @@ -206,7 +223,7 @@ async function messageToEvent(message, guild, api) { $type: "m.room.message", "m.mentions": mentions, msgtype: "m.text", - body: "Unsupported attachment:\n" + JSON.stringify(attachment, null, 2) + body: `Unsupported attachment:\n${JSON.stringify(attachment, null, 2)}\n${attachment.url}` } } })) diff --git a/d2m/converters/message-to-event.test.js b/d2m/converters/message-to-event.test.js index 58093ca3..17079e54 100644 --- a/d2m/converters/message-to-event.test.js +++ b/d2m/converters/message-to-event.test.js @@ -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: hey.jpg (100 MB)' + }]) +}) + // TODO: read "edits of replies" in the spec diff --git a/matrix/api.js b/matrix/api.js index ed9980b2..7f9d74e8 100644 --- a/matrix/api.js +++ b/matrix/api.js @@ -110,7 +110,7 @@ async function sendState(roomID, type, stateKey, 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} */ const root = await mreq.mreq("PUT", path(`/client/v3/rooms/${roomID}/send/${type}/${makeTxnId.makeTxnId()}`, mxid), content) return root.event_id diff --git a/package-lock.json b/package-lock.json index 7dcde49e..13b6799b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18,6 +18,7 @@ "matrix-js-sdk": "^24.1.0", "mixin-deep": "^2.0.1", "node-fetch": "^2.6.7", + "prettier-bytes": "^1.0.4", "snowtransfer": "^0.8.0", "try-to-catch": "^3.0.1" }, @@ -2099,6 +2100,11 @@ "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": { "version": "29.5.0", "resolved": "https://registry.npmjs.org/pretty-format/-/pretty-format-29.5.0.tgz", @@ -2318,9 +2324,9 @@ } }, "node_modules/semver": { - "version": "7.5.0", - "resolved": "https://registry.npmjs.org/semver/-/semver-7.5.0.tgz", - "integrity": "sha512-+XC0AD/R7Q2mPSRuy2Id0+CGTZ98+8f+KvwirxOKIEyid+XSx6HbC63p+O4IndTHuX5Z+JxQ0TghCkO5Cg/2HA==", + "version": "7.5.4", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.5.4.tgz", + "integrity": "sha512-1bCSESV6Pv+i21Hvpxp3Dx+pSD8lIPt8uVjRrxAUt/nbswYc+tK6Y2btiULjd4+fnq15PX+nqQDC7Oft7WkwcA==", "dependencies": { "lru-cache": "^6.0.0" }, diff --git a/package.json b/package.json index 86043301..951089b8 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "matrix-js-sdk": "^24.1.0", "mixin-deep": "^2.0.1", "node-fetch": "^2.6.7", + "prettier-bytes": "^1.0.4", "snowtransfer": "^0.8.0", "try-to-catch": "^3.0.1" }, diff --git a/types.d.ts b/types.d.ts index 32aa21f3..eeb4b759 100644 --- a/types.d.ts +++ b/types.d.ts @@ -18,6 +18,7 @@ export type AppServiceRegistrationConfig = { rate_limited: boolean ooye: { namespace_prefix: string + max_file_size: number } }