2023-09-25 04:36:27 +00:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
const Ty = require("../../types")
|
|
|
|
const assert = require("assert").strict
|
|
|
|
|
|
|
|
const passthrough = require("../../passthrough")
|
|
|
|
const {discord, sync, db, select} = passthrough
|
|
|
|
/** @type {import("../../matrix/api")} */
|
|
|
|
const api = sync.require("../../matrix/api")
|
2023-09-25 09:20:23 +00:00
|
|
|
/** @type {import("../converters/emoji-to-key")} */
|
|
|
|
const emojiToKey = sync.require("../converters/emoji-to-key")
|
2023-09-25 04:36:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {import("discord-api-types/v10").GatewayMessageReactionRemoveDispatchData} data
|
|
|
|
*/
|
|
|
|
async function removeReaction(data) {
|
|
|
|
const roomID = select("channel_room", "room_id", "WHERE channel_id = ?").pluck().get(data.channel_id)
|
|
|
|
if (!roomID) return
|
|
|
|
const eventIDForMessage = select("event_message", "event_id", "WHERE message_id = ? AND part = 0").pluck().get(data.message_id)
|
|
|
|
if (!eventIDForMessage) return
|
|
|
|
const mxid = select("sim", "mxid", "WHERE discord_id = ?").pluck().get(data.user_id)
|
|
|
|
if (!mxid) return
|
|
|
|
|
|
|
|
/** @type {Ty.Pagination<Ty.Event.Outer<Ty.Event.M_Reaction>>} */
|
|
|
|
const relations = await api.getRelations(roomID, eventIDForMessage, "m.annotation")
|
2023-09-25 09:20:23 +00:00
|
|
|
const key = await emojiToKey.emojiToKey(data.emoji)
|
|
|
|
const eventIDForReaction = relations.chunk.find(e => e.sender === mxid && e.content["m.relates_to"].key === key)
|
2023-09-25 04:36:27 +00:00
|
|
|
if (!eventIDForReaction) return
|
|
|
|
|
2023-09-25 09:20:23 +00:00
|
|
|
await api.redactEvent(roomID, eventIDForReaction.event_id, mxid)
|
2023-09-25 04:36:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.removeReaction = removeReaction
|