out-of-your-element/d2m/actions/send-message.js

60 lines
2.6 KiB
JavaScript
Raw Normal View History

2023-04-30 12:57:30 +00:00
// @ts-check
const assert = require("assert")
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")
/** @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) {
const roomID = await createRoom.ensureRoom(message.channel_id)
let senderMxid = null
if (!message.webhook_id) {
if (message.member) { // available on a gateway message create event
senderMxid = await registerUser.syncUser(message.author, message.member, message.guild_id, roomID)
} else { // well, good enough...
senderMxid = await registerUser.ensureSimJoined(message.author, roomID)
}
}
2023-08-16 08:44:38 +00:00
const events = await messageToEvent.messageToEvent(message, guild, {}, {api})
const eventIDs = []
let eventPart = 0 // 0 is primary, 1 is supporting
if (events.length) {
db.prepare("REPLACE INTO message_channel (message_id, channel_id) VALUES (?, ?)").run(message.id, message.channel_id)
2023-09-17 11:07:33 +00:00
if (senderMxid) api.sendTyping(roomID, false, senderMxid)
}
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
2023-08-28 04:20:16 +00:00
const useTimestamp = message["backfill"] ? new Date(message.timestamp).getTime() : undefined
const eventID = await api.sendEvent(roomID, eventType, eventWithoutType, senderMxid, useTimestamp)
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 (?, ?, ?, ?, ?, 1)").run(eventID, eventType, event.msgtype || null, message.id, eventPart) // source 1 = discord
// The primary event is part = 0 and has the most important and distinct information. It is used to provide reply previews, be pinned, and possibly future uses.
// The first event is chosen to be the primary part because it is usually the message text content and is more likely to be distinct.
// For example, "Reply to 'this meme made me think of you'" is more useful than "Replied to image".
eventPart = 1
eventIDs.push(eventID)
}
return eventIDs
2023-04-30 12:57:30 +00:00
}
module.exports.sendMessage = sendMessage