1
0
Fork 0
out-of-your-element-fork-th.../d2m/actions/add-reaction.js

45 lines
1.5 KiB
JavaScript
Raw Permalink Normal View History

// @ts-check
2023-07-04 05:19:17 +00:00
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")
/** @type {import("./register-user")} */
const registerUser = sync.require("./register-user")
/** @type {import("../actions/create-room")} */
const createRoom = sync.require("../actions/create-room")
/** @type {import("../converters/emoji-to-key")} */
const emojiToKey = sync.require("../converters/emoji-to-key")
/**
* @param {import("discord-api-types/v10").GatewayMessageReactionAddDispatchData} data
*/
async function addReaction(data) {
2023-08-21 09:04:41 +00:00
const user = data.member?.user
assert.ok(user && user.username)
2023-09-19 11:02:51 +00:00
const parentID = select("event_message", "event_id", {message_id: data.message_id, reaction_part: 0}).pluck().get()
2023-08-21 09:04:41 +00:00
if (!parentID) return // Nothing can be done if the parent message was never bridged.
assert.equal(typeof parentID, "string")
2023-09-19 11:02:51 +00:00
const key = await emojiToKey.emojiToKey(data.emoji)
const shortcode = key.startsWith("mxc://") ? `:${data.emoji.name}:` : undefined
2023-09-19 11:02:51 +00:00
const roomID = await createRoom.ensureRoom(data.channel_id)
const senderMxid = await registerUser.ensureSimJoined(user, roomID)
const eventID = await api.sendEvent(roomID, "m.reaction", {
2023-08-21 09:04:41 +00:00
"m.relates_to": {
rel_type: "m.annotation",
event_id: parentID,
2023-09-19 11:02:51 +00:00
key
},
shortcode
2023-08-21 09:04:41 +00:00
}, senderMxid)
return eventID
}
module.exports.addReaction = addReaction