2023-05-09 05:13:59 +00:00
|
|
|
// @ts-check
|
|
|
|
|
2023-07-04 05:19:17 +00:00
|
|
|
const assert = require("assert").strict
|
2023-05-09 05:13:59 +00:00
|
|
|
|
|
|
|
const passthrough = require("../../passthrough")
|
|
|
|
const { discord, sync, db } = 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")
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {import("discord-api-types/v10").GatewayMessageReactionAddDispatchData} data
|
|
|
|
*/
|
|
|
|
async function addReaction(data) {
|
|
|
|
const user = data.member?.user
|
|
|
|
assert.ok(user && user.username)
|
|
|
|
const parentID = db.prepare("SELECT event_id FROM event_message WHERE message_id = ? AND part = 0").pluck().get(data.message_id) // 0 = primary
|
2023-07-04 05:19:17 +00:00
|
|
|
if (!parentID) return // Nothing can be done if the parent message was never bridged.
|
2023-05-09 05:13:59 +00:00
|
|
|
assert.equal(typeof parentID, "string")
|
|
|
|
const roomID = await createRoom.ensureRoom(data.channel_id)
|
|
|
|
const senderMxid = await registerUser.ensureSimJoined(user, roomID)
|
2023-08-19 06:40:01 +00:00
|
|
|
const eventID = await api.sendEvent(roomID, "m.reaction", {
|
2023-05-09 05:13:59 +00:00
|
|
|
"m.relates_to": {
|
|
|
|
rel_type: "m.annotation",
|
|
|
|
event_id: parentID,
|
|
|
|
key: data.emoji.name
|
|
|
|
}
|
|
|
|
}, senderMxid)
|
|
|
|
return eventID
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.addReaction = addReaction
|