m->d fix for memorised emojis from other servers

This commit is contained in:
Cadence Ember 2023-09-27 23:25:46 +13:00
parent 56f16901ca
commit 60f3b67d2d
2 changed files with 47 additions and 24 deletions

View file

@ -120,32 +120,31 @@ turndownService.addRule("emoji", {
replacement: function (content, node) { replacement: function (content, node) {
const mxcUrl = node.getAttribute("src") const mxcUrl = node.getAttribute("src")
let row = select("emoji", ["id", "name", "animated"], "WHERE mxc_url = ?").get(mxcUrl) // Get the known emoji from the database. (We may not be able to actually use this if it was from another server.)
if (!row) { const row = select("emoji", ["id", "name", "animated"], "WHERE mxc_url = ?").get(mxcUrl)
// We don't know what this is... but maybe we can guess based on the name? // Also guess a suitable emoji based on the ID (if available) or name
const guessedName = node.getAttribute("title").replace(/^:|:$/g, "") let guess = null
for (const guild of discord?.guilds.values() || []) { const guessedName = node.getAttribute("title").replace(/^:|:$/g, "")
/** @type {{name: string, id: string, animated: number}[]} */ for (const guild of discord?.guilds.values() || []) {
// @ts-ignore /** @type {{name: string, id: string, animated: number}[]} */
const emojis = guild.emojis // @ts-ignore
const match = emojis.find(e => e.name === guessedName) || emojis.find(e => e.name?.toLowerCase() === guessedName.toLowerCase()) const emojis = guild.emojis
if (match) { const match = emojis.find(e => e.id === row?.id) || emojis.find(e => e.name === guessedName) || emojis.find(e => e.name?.toLowerCase() === guessedName.toLowerCase())
row = match if (match) {
break guess = match
} break
} }
} }
if (row) { if (guess) {
const animatedChar = row.animated ? "a" : "" // We know an emoji, and we can use it
return `<${animatedChar}:${row.name}:${row.id}>` const animatedChar = guess.animated ? "a" : ""
return `<${animatedChar}:${guess.name}:${guess.id}>`
} else if (endOfMessageEmojis.includes(mxcUrl)) {
// We can't locate or use a suitable emoji. After control returns, it will rewind over this, delete this section, and upload the emojis as a sprite sheet.
return `<::>`
} else { } else {
if (endOfMessageEmojis.includes(mxcUrl)) { // We prefer not to upload this as a sprite sheet because the emoji is not at the end of the message, it is in the middle.
// After control returns to the main converter, it will rewind over this, delete this section, and upload the emojis as a sprite sheet. return `[${node.getAttribute("title")}](${utils.getPublicUrlForMxc(mxcUrl)})`
return `<::>`
} else {
// This emoji is not at the end of the message, it is in the middle. We don't upload middle emojis as a sprite sheet.
return `[${node.getAttribute("title")}](${utils.getPublicUrlForMxc(mxcUrl)})`
}
} }
} }
}) })

View file

@ -1805,7 +1805,6 @@ slow()("event2message: unknown emoji in the end is reuploaded as a sprite sheet"
}) })
slow()("event2message: known and unknown emojis in the end are reuploaded as a sprite sheet", async t => { slow()("event2message: known and unknown emojis in the end are reuploaded as a sprite sheet", async t => {
t.comment("SKIPPED")
const messages = await eventToMessage({ const messages = await eventToMessage({
type: "m.room.message", type: "m.room.message",
sender: "@cadence:cadence.moe", sender: "@cadence:cadence.moe",
@ -1829,3 +1828,28 @@ slow()("event2message: known and unknown emojis in the end are reuploaded as a s
fileContentStart: "iVBORw0KGgoAAAANSUhEUgAAAGAAAAAwCAYAAADuFn/PAAAACXBIWXMAAAPoAAAD6AG1e1JrAAAT5UlEQVR4nOVbCXSVRZauR9gMsoYlvKwvARKSkPUlJOyL" fileContentStart: "iVBORw0KGgoAAAANSUhEUgAAAGAAAAAwCAYAAADuFn/PAAAACXBIWXMAAAPoAAAD6AG1e1JrAAAT5UlEQVR4nOVbCXSVRZauR9gMsoYlvKwvARKSkPUlJOyL"
}) })
}) })
slow()("event2message: all unknown chess emojis are reuploaded as a sprite sheet", async t => {
const messages = await eventToMessage({
type: "m.room.message",
sender: "@cadence:cadence.moe",
content: {
msgtype: "m.text",
body: "testing :chess_good_move::chess_incorrect::chess_blund::chess_brilliant_move::chess_blundest::chess_draw_black:",
format: "org.matrix.custom.html",
formatted_body: "testing <img data-mx-emoticon height=\"32\" src=\"mxc://cadence.moe/lHfmJpzgoNyNtYHdAmBHxXix\" title=\":chess_good_move:\" alt=\":chess_good_move:\"><img data-mx-emoticon height=\"32\" src=\"mxc://cadence.moe/MtRdXixoKjKKOyHJGWLsWLNU\" title=\":chess_incorrect:\" alt=\":chess_incorrect:\"><img data-mx-emoticon height=\"32\" src=\"mxc://cadence.moe/HXfFuougamkURPPMflTJRxGc\" title=\":chess_blund:\" alt=\":chess_blund:\"><img data-mx-emoticon height=\"32\" src=\"mxc://cadence.moe/ikYKbkhGhMERAuPPbsnQzZiX\" title=\":chess_brilliant_move:\" alt=\":chess_brilliant_move:\"><img data-mx-emoticon height=\"32\" src=\"mxc://cadence.moe/AYPpqXzVJvZdzMQJGjioIQBZ\" title=\":chess_blundest:\" alt=\":chess_blundest:\"><img data-mx-emoticon height=\"32\" src=\"mxc://cadence.moe/UVuzvpVUhqjiueMxYXJiFEAj\" title=\":chess_draw_black:\" alt=\":chess_draw_black:\">"
},
event_id: "$Me6iE8C8CZyrDEOYYrXKSYRuuh_25Jj9kZaNrf7LKr4",
room_id: "!maggESguZBqGBZtSnr:cadence.moe"
})
const testResult = {
content: messages.messagesToSend[0].content,
fileName: messages.messagesToSend[0].pendingFiles[0].name,
fileContentStart: messages.messagesToSend[0].pendingFiles[0].buffer.subarray(0, 90).toString("base64")
}
t.deepEqual(testResult, {
content: "testing",
fileName: "emojis.png",
fileContentStart: "iVBORw0KGgoAAAANSUhEUgAAASAAAAAwCAYAAACxIqevAAAACXBIWXMAAAPoAAAD6AG1e1JrAAAgAElEQVR4nOV9B1xUV9r3JMbEGBQLbRodhukDg2jWZP02"
})
})