2023-09-25 04:36:27 +00:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
const Ty = require("../../types")
|
2023-10-10 01:03:53 +00:00
|
|
|
const DiscordTypes = require("discord-api-types/v10")
|
2023-09-25 04:36:27 +00:00
|
|
|
|
|
|
|
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 10:15:36 +00:00
|
|
|
/** @type {import("../../m2d/converters/emoji")} */
|
|
|
|
const emoji = sync.require("../../m2d/converters/emoji")
|
2023-10-10 01:03:53 +00:00
|
|
|
/** @type {import("../converters/remove-reaction")} */
|
|
|
|
const converter = sync.require("../converters/remove-reaction")
|
2023-09-25 04:36:27 +00:00
|
|
|
|
|
|
|
/**
|
2023-10-10 01:03:53 +00:00
|
|
|
* @param {DiscordTypes.GatewayMessageReactionRemoveDispatchData | DiscordTypes.GatewayMessageReactionRemoveEmojiDispatchData | DiscordTypes.GatewayMessageReactionRemoveAllDispatchData} data
|
2023-09-25 04:36:27 +00:00
|
|
|
*/
|
2023-10-10 01:03:53 +00:00
|
|
|
async function removeSomeReactions(data) {
|
2023-10-05 23:31:10 +00:00
|
|
|
const roomID = select("channel_room", "room_id", {channel_id: data.channel_id}).pluck().get()
|
2023-09-25 04:36:27 +00:00
|
|
|
if (!roomID) return
|
2023-10-14 09:08:10 +00:00
|
|
|
const eventIDForMessage = select("event_message", "event_id", {message_id: data.message_id, reaction_part: 0}).pluck().get()
|
2023-09-25 04:36:27 +00:00
|
|
|
if (!eventIDForMessage) return
|
|
|
|
|
2024-08-31 05:44:56 +00:00
|
|
|
const reactions = await api.getFullRelations(roomID, eventIDForMessage, "m.annotation")
|
2023-09-25 04:36:27 +00:00
|
|
|
|
2023-10-10 01:03:53 +00:00
|
|
|
// Run the proper strategy and any strategy-specific database changes
|
|
|
|
const removals = await
|
2023-10-14 10:34:02 +00:00
|
|
|
( "user_id" in data ? removeReaction(data, reactions)
|
|
|
|
: "emoji" in data ? removeEmojiReaction(data, reactions)
|
|
|
|
: removeAllReactions(data, reactions))
|
2023-10-10 01:03:53 +00:00
|
|
|
|
|
|
|
// Redact the events and delete individual stored events in the database
|
|
|
|
for (const removal of removals) {
|
|
|
|
await api.redactEvent(roomID, removal.eventID, removal.mxid)
|
|
|
|
if (removal.hash) db.prepare("DELETE FROM reaction WHERE hashed_event_id = ?").run(removal.hash)
|
2023-09-25 10:15:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-10-10 01:03:53 +00:00
|
|
|
* @param {DiscordTypes.GatewayMessageReactionRemoveDispatchData} data
|
2023-10-14 10:34:02 +00:00
|
|
|
* @param {Ty.Event.Outer<Ty.Event.M_Reaction>[]} reactions
|
2023-09-25 10:15:36 +00:00
|
|
|
*/
|
2023-10-14 10:34:02 +00:00
|
|
|
async function removeReaction(data, reactions) {
|
2023-09-25 10:15:36 +00:00
|
|
|
const key = await emojiToKey.emojiToKey(data.emoji)
|
2023-10-14 10:34:02 +00:00
|
|
|
return converter.removeReaction(data, reactions, key)
|
2023-10-10 01:03:53 +00:00
|
|
|
}
|
2023-09-25 10:15:36 +00:00
|
|
|
|
2023-10-10 01:03:53 +00:00
|
|
|
/**
|
|
|
|
* @param {DiscordTypes.GatewayMessageReactionRemoveEmojiDispatchData} data
|
2023-10-14 10:34:02 +00:00
|
|
|
* @param {Ty.Event.Outer<Ty.Event.M_Reaction>[]} reactions
|
2023-10-10 01:03:53 +00:00
|
|
|
*/
|
2023-10-14 10:34:02 +00:00
|
|
|
async function removeEmojiReaction(data, reactions) {
|
2023-10-10 01:03:53 +00:00
|
|
|
const key = await emojiToKey.emojiToKey(data.emoji)
|
2023-09-25 10:15:36 +00:00
|
|
|
const discordPreferredEncoding = emoji.encodeEmoji(key, undefined)
|
|
|
|
db.prepare("DELETE FROM reaction WHERE message_id = ? AND encoded_emoji = ?").run(data.message_id, discordPreferredEncoding)
|
2023-10-10 01:03:53 +00:00
|
|
|
|
2023-10-14 10:34:02 +00:00
|
|
|
return converter.removeEmojiReaction(data, reactions, key)
|
2023-09-25 10:15:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-10-10 01:03:53 +00:00
|
|
|
* @param {DiscordTypes.GatewayMessageReactionRemoveAllDispatchData} data
|
2023-10-14 10:34:02 +00:00
|
|
|
* @param {Ty.Event.Outer<Ty.Event.M_Reaction>[]} reactions
|
2023-09-25 10:15:36 +00:00
|
|
|
*/
|
2023-10-14 10:34:02 +00:00
|
|
|
async function removeAllReactions(data, reactions) {
|
2023-09-25 10:15:36 +00:00
|
|
|
db.prepare("DELETE FROM reaction WHERE message_id = ?").run(data.message_id)
|
2023-10-10 01:03:53 +00:00
|
|
|
|
2023-10-14 10:34:02 +00:00
|
|
|
return converter.removeAllReactions(data, reactions)
|
2023-09-25 04:36:27 +00:00
|
|
|
}
|
|
|
|
|
2023-10-10 01:03:53 +00:00
|
|
|
module.exports.removeSomeReactions = removeSomeReactions
|