1
0
Fork 0
out-of-your-element-fork-th.../m2d/actions/send-event.js

38 lines
1.6 KiB
JavaScript
Raw Normal View History

2023-07-03 05:20:24 +00:00
// @ts-check
const assert = require("assert").strict
const DiscordTypes = require("discord-api-types/v10")
const passthrough = require("../../passthrough")
const {sync, discord, db} = passthrough
/** @type {import("./channel-webhook")} */
const channelWebhook = sync.require("./channel-webhook")
/** @type {import("../converters/event-to-message")} */
const eventToMessage = sync.require("../converters/event-to-message")
/** @param {import("../../types").Event.Outer<any>} event */
async function sendEvent(event) {
// TODO: we just assume the bridge has already been created
const channelID = db.prepare("SELECT channel_id FROM channel_room WHERE room_id = ?").pluck().get(event.room_id)
// no need to sync the matrix member to the other side. but if I did need to, this is where I'd do it
2023-07-03 05:20:24 +00:00
const messages = eventToMessage.eventToMessage(event)
assert(Array.isArray(messages)) // sanity
2023-07-03 05:20:24 +00:00
/** @type {DiscordTypes.APIMessage[]} */
const messageResponses = []
let eventPart = 0 // 0 is primary, 1 is supporting
for (const message of messages) {
const messageResponse = await channelWebhook.sendMessageWithWebhook(channelID, message)
2023-07-28 05:05:13 +00:00
db.prepare("INSERT INTO event_message (event_id, event_type, event_subtype, message_id, channel_id, part, source) VALUES (?, ?, ?, ?, ?, ?, 0)").run(event.event_id, event.type, event.content.msgtype || null, messageResponse.id, channelID, eventPart) // source 0 = matrix
2023-07-03 05:20:24 +00:00
eventPart = 1 // TODO: use more intelligent algorithm to determine whether primary or supporting?
2023-07-03 05:20:24 +00:00
messageResponses.push(messageResponse)
}
return messageResponses
}
module.exports.sendEvent = sendEvent