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")
|
2023-08-26 08:30:22 +00:00
|
|
|
/** @type {import("../../matrix/api")}) */
|
|
|
|
const api = sync.require("../../matrix/api")
|
2023-07-03 05:20:24 +00:00
|
|
|
|
|
|
|
/** @param {import("../../types").Event.Outer<any>} event */
|
|
|
|
async function sendEvent(event) {
|
2023-08-21 09:04:41 +00:00
|
|
|
// TODO: we just assume the bridge has already been created
|
2023-08-19 06:39:23 +00:00
|
|
|
const row = db.prepare("SELECT channel_id, thread_parent FROM channel_room WHERE room_id = ?").get(event.room_id)
|
|
|
|
let channelID = row.channel_id
|
|
|
|
let threadID = undefined
|
|
|
|
if (row.thread_parent) {
|
|
|
|
threadID = channelID
|
|
|
|
channelID = row.thread_parent // it's the thread's parent... get with the times...
|
|
|
|
}
|
2023-08-26 08:30:22 +00:00
|
|
|
// @ts-ignore
|
|
|
|
const guildID = discord.channels.get(channelID).guild_id
|
|
|
|
const guild = discord.guilds.get(guildID)
|
|
|
|
assert(guild)
|
2023-07-03 12:39:42 +00:00
|
|
|
|
2023-08-21 09:04:41 +00:00
|
|
|
// 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
|
|
|
|
2023-08-27 13:30:07 +00:00
|
|
|
const {messagesToEdit, messagesToSend, messagesToDelete} = await eventToMessage.eventToMessage(event, guild, {api})
|
2023-07-03 05:20:24 +00:00
|
|
|
|
2023-08-30 01:14:23 +00:00
|
|
|
let eventPart = 0 // 0 is primary, 1 is supporting
|
|
|
|
|
2023-08-21 09:04:41 +00:00
|
|
|
/** @type {DiscordTypes.APIMessage[]} */
|
2023-07-03 05:20:24 +00:00
|
|
|
const messageResponses = []
|
2023-08-28 13:31:52 +00:00
|
|
|
for (const data of messagesToEdit) {
|
|
|
|
const messageResponse = await channelWebhook.editMessageWithWebhook(channelID, data.id, data.message, threadID)
|
|
|
|
eventPart = 1
|
|
|
|
messageResponses.push(messageResponse)
|
|
|
|
}
|
2023-08-30 01:14:23 +00:00
|
|
|
|
|
|
|
for (const id of messagesToDelete) {
|
|
|
|
await channelWebhook.deleteMessageWithWebhook(channelID, id, threadID)
|
|
|
|
}
|
|
|
|
|
2023-08-27 13:30:07 +00:00
|
|
|
for (const message of messagesToSend) {
|
2023-08-21 09:04:41 +00:00
|
|
|
const messageResponse = await channelWebhook.sendMessageWithWebhook(channelID, message, threadID)
|
2023-08-28 05:32:55 +00:00
|
|
|
db.prepare("REPLACE INTO message_channel (message_id, channel_id) VALUES (?, ?)").run(messageResponse.id, channelID)
|
2023-08-28 12:05:25 +00:00
|
|
|
db.prepare("INSERT INTO event_message (event_id, event_type, event_subtype, message_id, part, source) VALUES (?, ?, ?, ?, ?, 0)").run(event.event_id, event.type, event.content.msgtype || null, messageResponse.id, eventPart) // source 0 = matrix
|
2023-07-03 05:20:24 +00:00
|
|
|
|
2023-08-30 01:14:23 +00:00
|
|
|
eventPart = 1
|
2023-07-03 05:20:24 +00:00
|
|
|
messageResponses.push(messageResponse)
|
|
|
|
}
|
|
|
|
|
|
|
|
return messageResponses
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.sendEvent = sendEvent
|