2023-07-04 05:19:17 +00:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
const assert = require("assert").strict
|
|
|
|
const Ty = require("../../types")
|
|
|
|
|
|
|
|
const passthrough = require("../../passthrough")
|
2023-09-18 10:51:59 +00:00
|
|
|
const {discord, sync, db, select} = passthrough
|
2023-07-04 05:19:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Ty.Event.Outer<Ty.Event.M_Reaction>} event
|
|
|
|
*/
|
|
|
|
async function addReaction(event) {
|
2023-09-18 10:51:59 +00:00
|
|
|
const channelID = select("channel_room", "channel_id", "WHERE room_id = ?").pluck().get(event.room_id)
|
2023-07-04 05:19:17 +00:00
|
|
|
if (!channelID) return // We just assume the bridge has already been created
|
2023-09-18 10:51:59 +00:00
|
|
|
const messageID = select("event_message", "message_id", "WHERE event_id = ? AND part = 0").pluck().get(event.content["m.relates_to"].event_id) // 0 = primary
|
2023-07-04 05:19:17 +00:00
|
|
|
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
|
|
|
|
|
2023-07-04 20:41:15 +00:00
|
|
|
let emoji = event.content["m.relates_to"].key // TODO: handle custom text or emoji reactions
|
2023-08-19 06:40:01 +00:00
|
|
|
let encoded = encodeURIComponent(emoji)
|
|
|
|
let encodedTrimmed = encoded.replace(/%EF%B8%8F/g, "")
|
2023-07-04 05:19:17 +00:00
|
|
|
|
2023-08-26 10:51:42 +00:00
|
|
|
// https://github.com/discord/discord-api-docs/issues/2723#issuecomment-807022205 ????????????
|
2023-08-19 06:40:01 +00:00
|
|
|
|
2023-08-26 10:51:42 +00:00
|
|
|
const forceTrimmedList = [
|
2023-09-06 12:55:49 +00:00
|
|
|
"%F0%9F%91%8D", // 👍
|
2023-08-26 10:51:42 +00:00
|
|
|
"%E2%AD%90" // ⭐
|
|
|
|
]
|
|
|
|
|
|
|
|
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)
|
2023-07-04 05:19:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.addReaction = addReaction
|