From c7ddf638dbd658057ec6a7a018a23b0693a97890 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Tue, 19 Sep 2023 23:02:51 +1200 Subject: [PATCH 1/4] d->m custom emoji reactions --- d2m/actions/add-reaction.js | 20 +++++++++++++++++++- d2m/event-dispatcher.js | 1 - 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/d2m/actions/add-reaction.js b/d2m/actions/add-reaction.js index a08d584..c9e99a5 100644 --- a/d2m/actions/add-reaction.js +++ b/d2m/actions/add-reaction.js @@ -17,16 +17,34 @@ const createRoom = sync.require("../actions/create-room") async function addReaction(data) { const user = data.member?.user assert.ok(user && user.username) + const parentID = select("event_message", "event_id", "WHERE message_id = ? AND part = 0").pluck().get(data.message_id) // 0 = primary if (!parentID) return // Nothing can be done if the parent message was never bridged. assert.equal(typeof parentID, "string") + + let key + if (data.emoji.id) { + // Custom emoji + const mxc = select("emoji", "mxc_url", "WHERE emoji_id = ?").pluck().get(data.emoji.id) + if (mxc) { + // The custom emoji is registered and we should send it + key = mxc + } else { + // The custom emoji is not registered. We *could* register it right now and it would work, but for now I'm just going to send the name. It's whatever. TODO change this probably. + key = "<" + data.emoji.name + ">" + } + } else { + // Default emoji + key = data.emoji.name + } + const roomID = await createRoom.ensureRoom(data.channel_id) const senderMxid = await registerUser.ensureSimJoined(user, roomID) const eventID = await api.sendEvent(roomID, "m.reaction", { "m.relates_to": { rel_type: "m.annotation", event_id: parentID, - key: data.emoji.name + key } }, senderMxid) return eventID diff --git a/d2m/event-dispatcher.js b/d2m/event-dispatcher.js index 93f7468..82c6adb 100644 --- a/d2m/event-dispatcher.js +++ b/d2m/event-dispatcher.js @@ -207,7 +207,6 @@ module.exports = { async onReactionAdd(client, data) { if (data.user_id === client.user.id) return // m2d reactions are added by the discord bot user - do not reflect them back to matrix. discordCommandHandler.onReactionAdd(data) - if (data.emoji.id !== null) return // TODO: image emoji reactions await addReaction.addReaction(data) }, From 92dee012fcb4af8053e384545539d43cf2aefc88 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Wed, 20 Sep 2023 00:37:15 +1200 Subject: [PATCH 2/4] refactor custom emoji schema; make reactions work --- d2m/actions/add-reaction.js | 11 +++++-- d2m/converters/expression.js | 2 +- d2m/converters/message-to-event.js | 3 +- db/ooye-schema.sql | 5 ++-- db/ooye-test-data.sql | 6 ++-- db/orm-utils.d.ts | 3 +- docs/notes.md | 4 +-- m2d/actions/add-reaction.js | 47 +++++++++++++++++++----------- m2d/converters/event-to-message.js | 23 ++++++++++++--- 9 files changed, 70 insertions(+), 34 deletions(-) diff --git a/d2m/actions/add-reaction.js b/d2m/actions/add-reaction.js index c9e99a5..14a9eb7 100644 --- a/d2m/actions/add-reaction.js +++ b/d2m/actions/add-reaction.js @@ -10,6 +10,8 @@ const api = sync.require("../../matrix/api") const registerUser = sync.require("./register-user") /** @type {import("../actions/create-room")} */ const createRoom = sync.require("../actions/create-room") +/** @type {import("../../matrix/file")} */ +const file = sync.require("../../matrix/file") /** * @param {import("discord-api-types/v10").GatewayMessageReactionAddDispatchData} data @@ -25,13 +27,16 @@ async function addReaction(data) { let key if (data.emoji.id) { // Custom emoji - const mxc = select("emoji", "mxc_url", "WHERE emoji_id = ?").pluck().get(data.emoji.id) + const mxc = select("emoji", "mxc_url", "WHERE id = ?").pluck().get(data.emoji.id) if (mxc) { // The custom emoji is registered and we should send it key = mxc } else { - // The custom emoji is not registered. We *could* register it right now and it would work, but for now I'm just going to send the name. It's whatever. TODO change this probably. - key = "<" + data.emoji.name + ">" + // The custom emoji is not registered. We will register it and then add it. + const mxc = await file.uploadDiscordFileToMxc(file.emoji(data.emoji.id, data.emoji.animated)) + db.prepare("INSERT OR IGNORE INTO emoji (id, name, animated, mxc_url) VALUES (?, ?, ?, ?)").run(data.emoji.id, data.emoji.name, data.emoji.animated, mxc) + key = mxc + // TODO: what happens if the matrix user also tries adding this reaction? the bridge bot isn't able to use that emoji... } } else { // Default emoji diff --git a/d2m/converters/expression.js b/d2m/converters/expression.js index b10ff27..b2b2590 100644 --- a/d2m/converters/expression.js +++ b/d2m/converters/expression.js @@ -29,7 +29,7 @@ async function emojisToState(emojis) { }, url } - db.prepare("INSERT OR IGNORE INTO emoji (emoji_id, animated, mxc_url) VALUES (?, ?, ?)").run(emoji.id, +!!emoji.animated, url) + db.prepare("INSERT OR IGNORE INTO emoji (id, name, animated, mxc_url) VALUES (?, ?, ?, ?)").run(emoji.id, emoji.name, +!!emoji.animated, url) }).catch(e => { if (e.data.errcode === "M_TOO_LARGE") { // Very unlikely to happen. Only possible for 3x-series emojis uploaded shortly after animated emojis were introduced, when there was no 256 KB size limit. return diff --git a/d2m/converters/message-to-event.js b/d2m/converters/message-to-event.js index 37a6f6f..fd8078f 100644 --- a/d2m/converters/message-to-event.js +++ b/d2m/converters/message-to-event.js @@ -41,7 +41,8 @@ function getDiscordParseCallbacks(message, useHTML) { /** @param {{animated: boolean, name: string, id: string, type: "discordEmoji"}} node */ emoji: node => { if (useHTML) { - const mxc = select("emoji", "mxc_url", "WHERE emoji_id = ?").pluck().get(node.id) + const mxc = select("emoji", "mxc_url", "WHERE id = ?").pluck().get(node.id) + // TODO: upload and register the emoji so it can be added no matter what if (mxc) { return `:${node.name}:` } else { diff --git a/db/ooye-schema.sql b/db/ooye-schema.sql index 2e1cb03..43642ee 100644 --- a/db/ooye-schema.sql +++ b/db/ooye-schema.sql @@ -64,9 +64,10 @@ CREATE TABLE IF NOT EXISTS "lottie" ( PRIMARY KEY("id") ) WITHOUT ROWID; CREATE TABLE IF NOT EXISTS "emoji" ( - "emoji_id" TEXT NOT NULL, + "id" TEXT NOT NULL, + "name" TEXT NOT NULL, "animated" INTEGER NOT NULL, "mxc_url" TEXT NOT NULL, - PRIMARY KEY("emoji_id") + PRIMARY KEY("id") ) WITHOUT ROWID; COMMIT; diff --git a/db/ooye-test-data.sql b/db/ooye-test-data.sql index 1f5f4a6..9ea11de 100644 --- a/db/ooye-test-data.sql +++ b/db/ooye-test-data.sql @@ -70,9 +70,9 @@ INSERT INTO file (discord_url, mxc_url) VALUES ('https://cdn.discordapp.com/emojis/230201364309868544.png', 'mxc://cadence.moe/qWmbXeRspZRLPcjseyLmeyXC'), ('https://cdn.discordapp.com/emojis/393635038903926784.gif', 'mxc://cadence.moe/WbYqNlACRuicynBfdnPYtmvc'); -INSERT INTO emoji (emoji_id, animated, mxc_url) VALUES -('230201364309868544', 0, 'mxc://cadence.moe/qWmbXeRspZRLPcjseyLmeyXC'), -('393635038903926784', 1, 'mxc://cadence.moe/WbYqNlACRuicynBfdnPYtmvc'); +INSERT INTO emoji (id, name, animated, mxc_url) VALUES +('230201364309868544', 'hippo', 0, 'mxc://cadence.moe/qWmbXeRspZRLPcjseyLmeyXC'), +('393635038903926784', 'hipposcope', 1, 'mxc://cadence.moe/WbYqNlACRuicynBfdnPYtmvc'); INSERT INTO member_cache (room_id, mxid, displayname, avatar_url) VALUES ('!kLRqKKUQXcibIMtOpl:cadence.moe', '@cadence:cadence.moe', 'cadence [they]', NULL), diff --git a/db/orm-utils.d.ts b/db/orm-utils.d.ts index 43df0dd..0ae0844 100644 --- a/db/orm-utils.d.ts +++ b/db/orm-utils.d.ts @@ -64,7 +64,8 @@ export type Models = { } emoji: { - emoji_id: string + id: string + name: string animated: number mxc_url: string } diff --git a/docs/notes.md b/docs/notes.md index 1449c24..c908476 100644 --- a/docs/notes.md +++ b/docs/notes.md @@ -159,8 +159,8 @@ Can use custom transaction ID (?) to send the original timestamps to Matrix. See ``` pragma case_sensitive_like = 1; -insert into emoji select replace(substr(discord_url, 35), ".gif", "") as emoji_id, 1 as animated, mxc_url from file where discord_url like 'https://cdn.discordapp.com/emojis/%.gif'; -insert into emoji select replace(substr(discord_url, 35), ".png", "") as emoji_id, 0 as animated, mxc_url from file where discord_url like 'https://cdn.discordapp.com/emojis/%.png'; +insert into emoji select replace(substr(discord_url, 35), ".gif", "") as id, 1 as animated, mxc_url from file where discord_url like 'https://cdn.discordapp.com/emojis/%.gif'; +insert into emoji select replace(substr(discord_url, 35), ".png", "") as id, 0 as animated, mxc_url from file where discord_url like 'https://cdn.discordapp.com/emojis/%.png'; ``` # Various considerations diff --git a/m2d/actions/add-reaction.js b/m2d/actions/add-reaction.js index fbee20f..d5d5cc7 100644 --- a/m2d/actions/add-reaction.js +++ b/m2d/actions/add-reaction.js @@ -15,27 +15,40 @@ async function addReaction(event) { const messageID = select("event_message", "message_id", "WHERE event_id = ? AND part = 0").pluck().get(event.content["m.relates_to"].event_id) // 0 = primary if (!messageID) return // Nothing can be done if the parent message was never bridged. - // no need to sync the matrix member to the other side. but if I did need to, this is where I'd do it + const emoji = event.content["m.relates_to"].key // TODO: handle custom text or emoji reactions + let discordPreferredEncoding + if (emoji.startsWith("mxc://")) { + // Custom emoji + const row = select("emoji", ["id", "name"], "WHERE mxc_url = ?").get(emoji) + if (row) { + // Great, we know exactly what this emoji is! + discordPreferredEncoding = encodeURIComponent(`${row.name}:${row.id}`) + } else { + // We don't have this emoji and there's no realistic way to just-in-time upload a new emoji somewhere. + // We can't try using a known emoji with the same name because we don't even know what the name is. We only have the mxc url. + // Sucks! + return + } + } else { + // Default emoji + // https://github.com/discord/discord-api-docs/issues/2723#issuecomment-807022205 ???????????? + const encoded = encodeURIComponent(emoji) + const encodedTrimmed = encoded.replace(/%EF%B8%8F/g, "") - let emoji = event.content["m.relates_to"].key // TODO: handle custom text or emoji reactions - let encoded = encodeURIComponent(emoji) - let encodedTrimmed = encoded.replace(/%EF%B8%8F/g, "") + const forceTrimmedList = [ + "%F0%9F%91%8D", // 👍 + "%E2%AD%90" // ⭐ + ] - // https://github.com/discord/discord-api-docs/issues/2723#issuecomment-807022205 ???????????? + discordPreferredEncoding = + ( forceTrimmedList.includes(encodedTrimmed) ? encodedTrimmed + : encodedTrimmed !== encoded && [...emoji].length === 2 ? encoded + : encodedTrimmed) - const forceTrimmedList = [ - "%F0%9F%91%8D", // 👍 - "%E2%AD%90" // ⭐ - ] + console.log("add reaction from matrix:", emoji, encoded, encodedTrimmed, "chosen:", discordPreferredEncoding) + } - let discordPreferredEncoding = - ( forceTrimmedList.includes(encodedTrimmed) ? encodedTrimmed - : encodedTrimmed !== encoded && [...emoji].length === 2 ? encoded - : encodedTrimmed) - - console.log("add reaction from matrix:", emoji, encoded, encodedTrimmed, "chosen:", discordPreferredEncoding) - - return discord.snow.channel.createReaction(channelID, messageID, discordPreferredEncoding) + return discord.snow.channel.createReaction(channelID, messageID, discordPreferredEncoding) // acting as the discord bot itself } module.exports.addReaction = addReaction diff --git a/m2d/converters/event-to-message.js b/m2d/converters/event-to-message.js index accaee5..18dee17 100644 --- a/m2d/converters/event-to-message.js +++ b/m2d/converters/event-to-message.js @@ -120,9 +120,25 @@ turndownService.addRule("inlineLink", { turndownService.addRule("emoji", { filter: function (node, options) { if (node.nodeName !== "IMG" || !node.hasAttribute("data-mx-emoticon") || !node.getAttribute("src")) return false - const row = select("emoji", ["emoji_id", "animated"], "WHERE mxc_url = ?").get(node.getAttribute("src")) + let row = select("emoji", ["id", "name", "animated"], "WHERE mxc_url = ?").get(node.getAttribute("src")) + if (!row) { + // We don't know what this is... but maybe we can guess based on the name? + const guessedName = node.getAttribute("title")?.replace?.(/^:|:$/g, "") + if (!guessedName) return false + for (const guild of discord.guilds.values()) { + /** @type {{name: string, id: string, animated: number}[]} */ + // @ts-ignore + const emojis = guild.emojis + const match = emojis.find(e => e.name === guessedName) || emojis.find(e => e.name?.toLowerCase() === guessedName.toLowerCase()) + if (match) { + row = match + break + } + } + } if (!row) return false - node.setAttribute("data-emoji-id", row.emoji_id) + node.setAttribute("data-emoji-id", row.id) + node.setAttribute("data-emoji-name", row.name) node.setAttribute("data-emoji-animated-char", row.animated ? "a" : "") return true }, @@ -133,8 +149,7 @@ turndownService.addRule("emoji", { /** @type {string} */ const animatedChar = node.getAttribute("data-emoji-animated-char") /** @type {string} */ - const title = node.getAttribute("title") || "__" - const name = title.replace(/^:|:$/g, "") + const name = node.getAttribute("data-emoji-name") return `<${animatedChar}:${name}:${id}>` } }) From 90a32225e0b850c0a120fc04879e3b9d3baadfc3 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Wed, 20 Sep 2023 07:49:36 +1200 Subject: [PATCH 3/4] d->m reactions: fix sqlite parameter error --- d2m/actions/add-reaction.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/d2m/actions/add-reaction.js b/d2m/actions/add-reaction.js index 14a9eb7..bbce42d 100644 --- a/d2m/actions/add-reaction.js +++ b/d2m/actions/add-reaction.js @@ -34,7 +34,7 @@ async function addReaction(data) { } else { // The custom emoji is not registered. We will register it and then add it. const mxc = await file.uploadDiscordFileToMxc(file.emoji(data.emoji.id, data.emoji.animated)) - db.prepare("INSERT OR IGNORE INTO emoji (id, name, animated, mxc_url) VALUES (?, ?, ?, ?)").run(data.emoji.id, data.emoji.name, data.emoji.animated, mxc) + db.prepare("INSERT OR IGNORE INTO emoji (id, name, animated, mxc_url) VALUES (?, ?, ?, ?)").run(data.emoji.id, data.emoji.name, +!!data.emoji.animated, mxc) key = mxc // TODO: what happens if the matrix user also tries adding this reaction? the bridge bot isn't able to use that emoji... } From 044ccc08e06f11b48e16c7f7637d3c088008119a Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Wed, 20 Sep 2023 08:03:19 +1200 Subject: [PATCH 4/4] d->m handle emojis we don't know about --- d2m/converters/message-to-event.js | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/d2m/converters/message-to-event.js b/d2m/converters/message-to-event.js index fd8078f..2a50d62 100644 --- a/d2m/converters/message-to-event.js +++ b/d2m/converters/message-to-event.js @@ -42,10 +42,9 @@ function getDiscordParseCallbacks(message, useHTML) { emoji: node => { if (useHTML) { const mxc = select("emoji", "mxc_url", "WHERE id = ?").pluck().get(node.id) - // TODO: upload and register the emoji so it can be added no matter what if (mxc) { return `:${node.name}:` - } else { + } else { // We shouldn't get here since all emojis should have been added ahead of time in the messageToEvent function. return `:${node.name}:` } } else { @@ -185,6 +184,26 @@ async function messageToEvent(message, guild, options = {}, di) { } } + // Handling emojis that we don't know about. The emoji has to be present in the DB for it to be picked up in the emoji markdown converter. + // So we scan the message ahead of time for all its emojis and ensure they are in the DB. + const emojiMatches = [...content.matchAll(/<(a?):([^:>]{2,20}):([0-9]+)>/g)] + const emojiDownloads = [] + for (const match of emojiMatches) { + const id = match[3] + const name = match[2] + const animated = +!!match[1] + const row = select("emoji", "id", "WHERE id = ?").pluck().get(id) + if (!row) { + // The custom emoji is not registered. We will register it and then add it. + emojiDownloads.push( + file.uploadDiscordFileToMxc(file.emoji(id, animated)).then(mxc => { + db.prepare("INSERT OR IGNORE INTO emoji (id, name, animated, mxc_url) VALUES (?, ?, ?, ?)").run(id, name, animated, mxc) + }) + ) + } + } + await Promise.all(emojiDownloads) + // Star * prefix for fallback edits if (options.includeEditFallbackStar) { body = "* " + body