2023-10-10 01:03:53 +00:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
const Ty = require("../../types")
|
|
|
|
const DiscordTypes = require("discord-api-types/v10")
|
|
|
|
|
|
|
|
const passthrough = require("../../passthrough")
|
|
|
|
const {discord, sync, select} = passthrough
|
|
|
|
/** @type {import("../../m2d/converters/utils")} */
|
|
|
|
const utils = sync.require("../../m2d/converters/utils")
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef ReactionRemoveRequest
|
|
|
|
* @prop {string} eventID
|
|
|
|
* @prop {string | null} mxid
|
2024-03-06 04:37:16 +00:00
|
|
|
* @prop {bigint} [hash]
|
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-10-10 01:03:53 +00:00
|
|
|
* @param {string} key
|
|
|
|
*/
|
2023-10-14 10:34:02 +00:00
|
|
|
function removeReaction(data, reactions, key) {
|
2023-10-10 01:03:53 +00:00
|
|
|
/** @type {ReactionRemoveRequest[]} */
|
|
|
|
const removals = []
|
|
|
|
|
|
|
|
const wantToRemoveMatrixReaction = data.user_id === discord.application.id
|
2023-10-14 10:34:02 +00:00
|
|
|
for (const event of reactions) {
|
2023-10-10 01:03:53 +00:00
|
|
|
const eventID = event.event_id
|
|
|
|
if (event.content["m.relates_to"].key === key) {
|
|
|
|
const lookingAtMatrixReaction = !utils.eventSenderIsFromDiscord(event.sender)
|
|
|
|
if (lookingAtMatrixReaction && wantToRemoveMatrixReaction) {
|
|
|
|
// We are removing a Matrix user's reaction, so we need to redact from the correct user ID (not @_ooye_matrix_bridge).
|
|
|
|
// Even though the bridge bot only reacted once on Discord-side, multiple Matrix users may have
|
|
|
|
// reacted on Matrix-side. Semantically, we want to remove the reaction from EVERY Matrix user.
|
|
|
|
// Also need to clean up the database.
|
|
|
|
const hash = utils.getEventIDHash(event.event_id)
|
|
|
|
removals.push({eventID, mxid: null, hash})
|
|
|
|
}
|
|
|
|
if (!lookingAtMatrixReaction && !wantToRemoveMatrixReaction) {
|
|
|
|
// We are removing a Discord user's reaction, so we just make the sim user remove it.
|
|
|
|
const mxid = select("sim", "mxid", {user_id: data.user_id}).pluck().get()
|
|
|
|
if (mxid === event.sender) {
|
|
|
|
removals.push({eventID, mxid})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return removals
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {DiscordTypes.GatewayMessageReactionRemoveEmojiDispatchData} data
|
2023-10-14 10:34:02 +00:00
|
|
|
* @param {Ty.Event.Outer<Ty.Event.M_Reaction>[]} relations
|
2023-10-10 01:03:53 +00:00
|
|
|
* @param {string} key
|
|
|
|
*/
|
|
|
|
function removeEmojiReaction(data, relations, key) {
|
|
|
|
/** @type {ReactionRemoveRequest[]} */
|
|
|
|
const removals = []
|
|
|
|
|
2023-10-14 10:34:02 +00:00
|
|
|
for (const event of relations) {
|
2023-10-10 01:03:53 +00:00
|
|
|
const eventID = event.event_id
|
|
|
|
if (event.content["m.relates_to"].key === key) {
|
|
|
|
const mxid = utils.eventSenderIsFromDiscord(event.sender) ? event.sender : null
|
|
|
|
removals.push({eventID, mxid})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return removals
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {DiscordTypes.GatewayMessageReactionRemoveAllDispatchData} data
|
2023-10-14 10:34:02 +00:00
|
|
|
* @param {Ty.Event.Outer<Ty.Event.M_Reaction>[]} relations
|
2023-10-10 01:03:53 +00:00
|
|
|
* @returns {ReactionRemoveRequest[]}
|
|
|
|
*/
|
|
|
|
function removeAllReactions(data, relations) {
|
2023-10-14 10:34:02 +00:00
|
|
|
return relations.map(event => {
|
2023-10-10 01:03:53 +00:00
|
|
|
const eventID = event.event_id
|
|
|
|
const mxid = utils.eventSenderIsFromDiscord(event.sender) ? event.sender : null
|
|
|
|
return {eventID, mxid}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.removeReaction = removeReaction
|
|
|
|
module.exports.removeEmojiReaction = removeEmojiReaction
|
|
|
|
module.exports.removeAllReactions = removeAllReactions
|