Fix m->d then d->m reactions not merging

This commit is contained in:
Cadence Ember
2025-06-16 22:50:34 +12:00
parent edf60bcd2d
commit 2c15468c22
7 changed files with 17 additions and 7 deletions

View File

@@ -25,7 +25,7 @@ async function addReaction(data) {
if (!parentID) return // Nothing can be done if the parent message was never bridged.
assert.equal(typeof parentID, "string")
const key = await emojiToKey.emojiToKey(data.emoji)
const key = await emojiToKey.emojiToKey(data.emoji, data.message_id)
const shortcode = key.startsWith("mxc://") ? `:${data.emoji.name}:` : undefined
const roomID = await createRoom.ensureRoom(data.channel_id)

View File

@@ -43,7 +43,7 @@ async function removeSomeReactions(data) {
* @param {Ty.Event.Outer<Ty.Event.M_Reaction>[]} reactions
*/
async function removeReaction(data, reactions) {
const key = await emojiToKey.emojiToKey(data.emoji)
const key = await emojiToKey.emojiToKey(data.emoji, data.message_id)
return converter.removeReaction(data, reactions, key)
}
@@ -52,7 +52,7 @@ async function removeReaction(data, reactions) {
* @param {Ty.Event.Outer<Ty.Event.M_Reaction>[]} reactions
*/
async function removeEmojiReaction(data, reactions) {
const key = await emojiToKey.emojiToKey(data.emoji)
const key = await emojiToKey.emojiToKey(data.emoji, data.message_id)
const discordPreferredEncoding = await emoji.encodeEmoji(key, undefined)
db.prepare("DELETE FROM reaction WHERE message_id = ? AND encoded_emoji = ?").run(data.message_id, discordPreferredEncoding)

View File

@@ -8,9 +8,10 @@ const file = sync.require("../../matrix/file")
/**
* @param {import("discord-api-types/v10").APIEmoji} emoji
* @param {string} message_id
* @returns {Promise<string>}
*/
async function emojiToKey(emoji) {
async function emojiToKey(emoji, message_id) {
let key
if (emoji.id) {
// Custom emoji
@@ -30,7 +31,10 @@ async function emojiToKey(emoji) {
// Default emoji
const name = emoji.name
assert(name)
key = name
// If the reaction was used on Matrix already, it might be using a different arrangement of Variation Selector 16 characters.
// We'll use the same arrangement that was originally used, otherwise a duplicate of the emoji will appear as a separate reaction.
const originalEncoding = select("reaction", "original_encoding", {message_id, encoded_emoji: encodeURIComponent(name)}).pluck().get()
key = originalEncoding || name
}
return key
}

View File

@@ -401,7 +401,7 @@ async function messageToEvent(message, guild, options = {}, di) {
const id = match[3]
const name = match[2]
const animated = !!match[1]
return emojiToKey.emojiToKey({id, name, animated}) // Register the custom emoji if needed
return emojiToKey.emojiToKey({id, name, animated}, message.id) // Register the custom emoji if needed
}))
async function transformParsedVia(parsed) {

View File

@@ -0,0 +1,5 @@
BEGIN TRANSACTION;
ALTER TABLE reaction ADD COLUMN original_encoding TEXT;
COMMIT;

View File

@@ -110,6 +110,7 @@ export type Models = {
hashed_event_id: number
message_id: string
encoded_emoji: string
original_encoding: string | null
}
auto_emoji: {

View File

@@ -38,7 +38,7 @@ async function addReaction(event) {
throw e
}
db.prepare("REPLACE INTO reaction (hashed_event_id, message_id, encoded_emoji) VALUES (?, ?, ?)").run(utils.getEventIDHash(event.event_id), messageID, discordPreferredEncoding)
db.prepare("REPLACE INTO reaction (hashed_event_id, message_id, encoded_emoji, original_encoding) VALUES (?, ?, ?, ?)").run(utils.getEventIDHash(event.event_id), messageID, discordPreferredEncoding, key)
}
module.exports.addReaction = addReaction