2023-04-30 12:57:30 +00:00
|
|
|
// @ts-check
|
|
|
|
|
2023-05-10 10:15:20 +00:00
|
|
|
const assert = require("assert")
|
|
|
|
|
2023-05-08 12:58:46 +00:00
|
|
|
const passthrough = require("../../passthrough")
|
|
|
|
const { discord, sync, db } = passthrough
|
|
|
|
/** @type {import("../converters/message-to-event")} */
|
|
|
|
const messageToEvent = sync.require("../converters/message-to-event")
|
|
|
|
/** @type {import("../../matrix/api")} */
|
|
|
|
const api = sync.require("../../matrix/api")
|
|
|
|
/** @type {import("./register-user")} */
|
|
|
|
const registerUser = sync.require("./register-user")
|
2023-05-08 20:03:57 +00:00
|
|
|
/** @type {import("../actions/create-room")} */
|
|
|
|
const createRoom = sync.require("../actions/create-room")
|
2023-04-30 12:57:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {import("discord-api-types/v10").GatewayMessageCreateDispatchData} message
|
2023-06-28 11:38:58 +00:00
|
|
|
* @param {import("discord-api-types/v10").APIGuild} guild
|
2023-04-30 12:57:30 +00:00
|
|
|
*/
|
2023-06-28 11:38:58 +00:00
|
|
|
async function sendMessage(message, guild) {
|
2023-05-08 20:03:57 +00:00
|
|
|
const roomID = await createRoom.ensureRoom(message.channel_id)
|
2023-05-12 05:35:37 +00:00
|
|
|
|
2023-05-08 12:58:46 +00:00
|
|
|
let senderMxid = null
|
|
|
|
if (!message.webhook_id) {
|
2023-06-28 11:38:58 +00:00
|
|
|
assert(message.member)
|
2023-05-08 12:58:46 +00:00
|
|
|
senderMxid = await registerUser.ensureSimJoined(message.author, roomID)
|
2023-05-10 10:15:20 +00:00
|
|
|
await registerUser.syncUser(message.author, message.member, message.guild_id, roomID)
|
2023-05-08 12:58:46 +00:00
|
|
|
}
|
2023-05-12 05:35:37 +00:00
|
|
|
|
2023-06-28 12:06:56 +00:00
|
|
|
const events = await messageToEvent.messageToEvent(message, guild)
|
2023-05-12 05:35:37 +00:00
|
|
|
const eventIDs = []
|
|
|
|
let eventPart = 0 // 0 is primary, 1 is supporting
|
|
|
|
for (const event of events) {
|
|
|
|
const eventType = event.$type
|
|
|
|
/** @type {Pick<typeof event, Exclude<keyof event, "$type">> & { $type?: string }} */
|
|
|
|
const eventWithoutType = {...event}
|
|
|
|
delete eventWithoutType.$type
|
|
|
|
|
|
|
|
const eventID = await api.sendEvent(roomID, eventType, event, senderMxid)
|
2023-07-03 12:39:42 +00:00
|
|
|
db.prepare("INSERT INTO event_message (event_id, message_id, part, source) VALUES (?, ?, ?, 1)").run(eventID, message.id, eventPart) // source 1 = discord
|
2023-05-12 05:35:37 +00:00
|
|
|
|
|
|
|
eventPart = 1 // TODO: use more intelligent algorithm to determine whether primary or supporting
|
|
|
|
eventIDs.push(eventID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return eventIDs
|
2023-04-30 12:57:30 +00:00
|
|
|
}
|
|
|
|
|
2023-05-09 05:13:59 +00:00
|
|
|
module.exports.sendMessage = sendMessage
|