From 6df728dcc73b48bd2153dca12d17f4861e942a29 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Tue, 9 May 2023 17:13:59 +1200 Subject: [PATCH] adding basic reactions to discord messages --- d2m/actions/add-reaction.js | 36 +++++++++++++++++++++++++++++++++++ d2m/actions/register-user.js | 2 ++ d2m/actions/send-message.js | 8 +++----- d2m/event-dispatcher.js | 5 ++++- db/ooye.db | Bin 90112 -> 94208 bytes 5 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 d2m/actions/add-reaction.js diff --git a/d2m/actions/add-reaction.js b/d2m/actions/add-reaction.js new file mode 100644 index 0000000..82449cd --- /dev/null +++ b/d2m/actions/add-reaction.js @@ -0,0 +1,36 @@ +// @ts-check + +const assert = require("assert") + +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) + // TODO: should add my own sent messages to event_message so they can be reacted to? + const parentID = db.prepare("SELECT event_id FROM event_message WHERE message_id = ? AND part = 0").pluck().get(data.message_id) // 0 = primary + if (!parentID) return // TODO: how to handle reactions for unbridged messages? is there anything I can do? + assert.equal(typeof parentID, "string") + const roomID = await createRoom.ensureRoom(data.channel_id) + const senderMxid = await registerUser.ensureSimJoined(user, roomID) + const eventID = api.sendEvent(roomID, "m.reaction", { + "m.relates_to": { + rel_type: "m.annotation", + event_id: parentID, + key: data.emoji.name + } + }, senderMxid) + return eventID +} + +module.exports.addReaction = addReaction diff --git a/d2m/actions/register-user.js b/d2m/actions/register-user.js index 508dd39..04b0998 100644 --- a/d2m/actions/register-user.js +++ b/d2m/actions/register-user.js @@ -42,6 +42,7 @@ async function createSim(user) { /** * Ensure a sim is registered for the user. * If there is already a sim, use that one. If there isn't one yet, register a new sim. + * @param {import("discord-api-types/v10").APIUser} user * @returns mxid */ async function ensureSim(user) { @@ -57,6 +58,7 @@ async function ensureSim(user) { /** * Ensure a sim is registered for the user and is joined to the room. + * @param {import("discord-api-types/v10").APIUser} user * @returns mxid */ async function ensureSimJoined(user, roomID) { diff --git a/d2m/actions/send-message.js b/d2m/actions/send-message.js index f828134..fb181d2 100644 --- a/d2m/actions/send-message.js +++ b/d2m/actions/send-message.js @@ -1,8 +1,5 @@ // @ts-check -const fetch = require("node-fetch").default -const reg = require("../../matrix/read-registration.js") - const passthrough = require("../../passthrough") const { discord, sync, db } = passthrough /** @type {import("../converters/message-to-event")} */ @@ -24,8 +21,9 @@ async function sendMessage(message) { if (!message.webhook_id) { senderMxid = await registerUser.ensureSimJoined(message.author, roomID) } - const eventID = api.sendEvent(roomID, "m.room.message", event, senderMxid) + const eventID = await api.sendEvent(roomID, "m.room.message", event, senderMxid) + db.prepare("INSERT INTO event_message (event_id, message_id, part) VALUES (?, ?, ?)").run(eventID, message.id, 0) // 0 is primary, 1 is supporting return eventID } -module.exports.sendMessage = sendMessage \ No newline at end of file +module.exports.sendMessage = sendMessage diff --git a/d2m/event-dispatcher.js b/d2m/event-dispatcher.js index 83a1a70..eeee451 100644 --- a/d2m/event-dispatcher.js +++ b/d2m/event-dispatcher.js @@ -4,6 +4,8 @@ const {sync} = require("../passthrough") /** @type {import("./actions/send-message")}) */ const sendMessage = sync.require("./actions/send-message") +/** @type {import("./actions/add-reaction")}) */ +const addReaction = sync.require("./actions/add-reaction") // Grab Discord events we care about for the bridge, check them, and pass them on @@ -22,7 +24,8 @@ module.exports = { * @param {import("discord-api-types/v10").GatewayMessageReactionAddDispatchData} data */ onReactionAdd(client, data) { + if (data.emoji.id !== null) return // TOOD: image emoji reactions console.log(data) - return {} + addReaction.addReaction(data) } } diff --git a/db/ooye.db b/db/ooye.db index 8ef07bdeb2c6c642965975defc772b7ffde0712a..4408064ae46001d0d87d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