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")
|
2023-09-18 10:51:59 +00:00
|
|
|
const {discord, sync, db, select} = passthrough
|
2023-05-09 05:13:59 +00:00
|
|
|
/** @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")
|
2023-09-19 12:37:15 +00:00
|
|
|
/** @type {import("../../matrix/file")} */
|
|
|
|
const file = sync.require("../../matrix/file")
|
2023-05-09 05:13:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
2023-09-19 07:10:02 +00:00
|
|
|
const parentID = select("event_message", "event_id", "WHERE message_id = ? AND part = 0").pluck().get(data.message_id) // 0 = primary
|
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
|
|
|
|
|
|
|
let key
|
|
|
|
if (data.emoji.id) {
|
|
|
|
// Custom emoji
|
2023-09-19 12:37:15 +00:00
|
|
|
const mxc = select("emoji", "mxc_url", "WHERE id = ?").pluck().get(data.emoji.id)
|
2023-09-19 11:02:51 +00:00
|
|
|
if (mxc) {
|
|
|
|
// The custom emoji is registered and we should send it
|
|
|
|
key = mxc
|
|
|
|
} else {
|
2023-09-19 12:37:15 +00:00
|
|
|
// The custom emoji is not registered. We will register it and then add it.
|
|
|
|
const mxc = await file.uploadDiscordFileToMxc(file.emoji(data.emoji.id, data.emoji.animated))
|
2023-09-19 19:49:36 +00:00
|
|
|
db.prepare("INSERT OR IGNORE INTO emoji (id, name, animated, mxc_url) VALUES (?, ?, ?, ?)").run(data.emoji.id, data.emoji.name, +!!data.emoji.animated, mxc)
|
2023-09-19 12:37:15 +00:00
|
|
|
key = mxc
|
|
|
|
// TODO: what happens if the matrix user also tries adding this reaction? the bridge bot isn't able to use that emoji...
|
2023-09-19 11:02:51 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Default emoji
|
|
|
|
key = data.emoji.name
|
|
|
|
}
|
|
|
|
|
2023-05-09 05:13:59 +00:00
|
|
|
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-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
|
2023-08-21 09:04:41 +00:00
|
|
|
}
|
|
|
|
}, senderMxid)
|
2023-05-09 05:13:59 +00:00
|
|
|
return eventID
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.addReaction = addReaction
|