refactor custom emoji schema; make reactions work

This commit is contained in:
Cadence Ember 2023-09-20 00:37:15 +12:00
parent c7ddf638db
commit 92dee012fc
9 changed files with 70 additions and 34 deletions

View file

@ -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

View file

@ -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

View file

@ -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 `<img data-mx-emoticon height="32" src="${mxc}" title=":${node.name}:" alt=":${node.name}:">`
} else {