2023-04-30 12:57:30 +00:00
// @ts-check
2024-03-06 20:13:25 +00:00
const assert = require ( "assert" ) . strict
const DiscordTypes = require ( "discord-api-types/v10" )
2023-05-10 10:15:20 +00:00
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" )
2024-01-20 11:54:18 +00:00
/** @type {import("./register-pk-user")} */
const registerPkUser = sync . require ( "./register-pk-user" )
2023-05-08 20:03:57 +00:00
/** @type {import("../actions/create-room")} */
const createRoom = sync . require ( "../actions/create-room" )
2023-10-09 22:23:51 +00:00
/** @type {import("../../discord/utils")} */
const dUtils = sync . require ( "../../discord/utils" )
2023-04-30 12:57:30 +00:00
/ * *
2024-03-06 20:13:25 +00:00
* @ param { DiscordTypes . GatewayMessageCreateDispatchData } message
* @ param { DiscordTypes . APIGuildChannel } channel
* @ param { DiscordTypes . APIGuild } guild
2024-01-20 11:54:18 +00:00
* @ param { { speedbump _id : string , speedbump _webhook _id : string } | null } row data about the webhook which is proxying messages in this channel
2023-04-30 12:57:30 +00:00
* /
2024-03-06 20:13:25 +00:00
async function sendMessage ( message , channel , guild , row ) {
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
2023-10-09 22:23:51 +00:00
if ( ! dUtils . isWebhookMessage ( message ) ) {
2024-03-19 09:15:44 +00:00
if ( message . author . id === discord . application . id ) {
// no need to sync the bot's own user
} else if ( message . member ) { // available on a gateway message create event
2024-03-06 20:13:25 +00:00
senderMxid = await registerUser . syncUser ( message . author , message . member , channel , guild , roomID )
2023-08-19 10:54:23 +00:00
} else { // well, good enough...
senderMxid = await registerUser . ensureSimJoined ( message . author , roomID )
}
2024-01-20 11:54:18 +00:00
} else if ( row && row . speedbump _webhook _id === message . webhook _id ) {
// Handle the PluralKit public instance
if ( row . speedbump _id === "466378653216014359" ) {
2024-07-16 10:01:20 +00:00
const pkMessage = await registerPkUser . fetchMessage ( message . id )
senderMxid = await registerPkUser . syncUser ( message . author , pkMessage , roomID )
2024-01-20 11:54:18 +00:00
}
2023-05-08 12:58:46 +00:00
}
2023-05-12 05:35:37 +00:00
2023-08-16 08:44:38 +00:00
const events = await messageToEvent . messageToEvent ( message , guild , { } , { api } )
2023-05-12 05:35:37 +00:00
const eventIDs = [ ]
2023-08-28 05:32:55 +00:00
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 )
2023-08-28 05:32:55 +00:00
}
2023-05-12 05:35:37 +00:00
for ( const event of events ) {
2023-10-14 09:08:10 +00:00
const part = event === events [ 0 ] ? 0 : 1
const reactionPart = event === events [ events . length - 1 ] ? 0 : 1
2023-05-12 05:35:37 +00:00
const eventType = event . $type
2023-10-14 04:23:55 +00:00
if ( "$sender" in event ) senderMxid = event . $sender
2023-10-12 07:30:41 +00:00
/** @type {Pick<typeof event, Exclude<keyof event, "$type" | "$sender">> & { $type?: string, $sender?: string }} */
2023-05-12 05:35:37 +00:00
const eventWithoutType = { ... event }
delete eventWithoutType . $type
2023-10-12 07:30:41 +00:00
delete eventWithoutType . $sender
2023-05-12 05:35:37 +00:00
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-10-14 09:08:10 +00:00
db . prepare ( "INSERT INTO event_message (event_id, event_type, event_subtype, message_id, part, reaction_part, source) VALUES (?, ?, ?, ?, ?, ?, 1)" ) . run ( eventID , eventType , event . msgtype || null , message . id , part , reactionPart ) // source 1 = discord
2023-05-12 05:35:37 +00:00
2023-10-06 03:58:18 +00:00
// 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".
2023-10-14 09:08:10 +00:00
// The last event gets reaction_part = 0. Reactions are managed there because reactions are supposed to appear at the bottom.
2023-05-12 05:35:37 +00:00
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