2023-08-17 04:41:28 +00:00
// @ts-check
const passthrough = require ( "../../passthrough" )
const { sync , db } = passthrough
/** @type {import("../converters/edit-to-changes")} */
const editToChanges = sync . require ( "../converters/edit-to-changes" )
/** @type {import("../../matrix/api")} */
const api = sync . require ( "../../matrix/api" )
/ * *
* @ param { import ( "discord-api-types/v10" ) . GatewayMessageCreateDispatchData } message
* @ param { import ( "discord-api-types/v10" ) . APIGuild } guild
* /
async function editMessage ( message , guild ) {
console . log ( ` *** applying edit for message ${ message . id } in channel ${ message . channel _id } ` )
const { roomID , eventsToRedact , eventsToReplace , eventsToSend , senderMxid } = await editToChanges . editToChanges ( message , guild , api )
2023-08-17 13:23:53 +00:00
console . log ( "making these changes:" )
console . dir ( { eventsToRedact , eventsToReplace , eventsToSend } , { depth : null } )
2023-07-28 05:05:13 +00:00
// 1. Replace all the things.
2023-08-17 04:41:28 +00:00
for ( const { oldID , newContent } of eventsToReplace ) {
const eventType = newContent . $type
/** @type {Pick<typeof newContent, Exclude<keyof newContent, "$type">> & { $type?: string }} */
const newContentWithoutType = { ... newContent }
delete newContentWithoutType . $type
await api . sendEvent ( roomID , eventType , newContentWithoutType , senderMxid )
// Ensure the database is up to date.
// The columns are event_id, event_type, event_subtype, message_id, channel_id, part, source. Only event_subtype could potentially be changed by a replacement event.
const subtype = newContentWithoutType . msgtype ? ? null
db . prepare ( "UPDATE event_message SET event_subtype = ? WHERE event_id = ?" ) . run ( subtype , oldID )
}
2023-07-28 05:05:13 +00:00
// 2. Redact all the things.
2023-08-17 04:41:28 +00:00
// Not redacting as the last action because the last action is likely to be shown in the room preview in clients, and we don't want it to look like somebody actually deleted a message.
for ( const eventID of eventsToRedact ) {
await api . redactEvent ( roomID , eventID , senderMxid )
2023-08-17 13:23:53 +00:00
db . prepare ( "DELETE from event_message WHERE event_id = ?" ) . run ( eventID )
2023-08-17 04:41:28 +00:00
// TODO: If I just redacted part = 0, I should update one of the other events to make it the new part = 0, right?
}
2023-07-28 05:05:13 +00:00
// 3. Send all the things.
2023-08-17 04:41:28 +00:00
for ( const content of eventsToSend ) {
const eventType = content . $type
/** @type {Pick<typeof content, Exclude<keyof content, "$type">> & { $type?: string }} */
const contentWithoutType = { ... content }
delete contentWithoutType . $type
const eventID = await api . sendEvent ( roomID , eventType , contentWithoutType , senderMxid )
db . prepare ( "INSERT INTO event_message (event_id, event_type, event_subtype, message_id, channel_id, part, source) VALUES (?, ?, ?, ?, ?, 1, 1)" ) . run ( eventID , eventType , content . msgtype || null , message . id , message . channel _id ) // part 1 = supporting; source 1 = discord
2023-07-28 05:05:13 +00:00
}
2023-08-17 04:41:28 +00:00
}
2023-07-28 05:05:13 +00:00
2023-08-17 04:41:28 +00:00
module . exports . editMessage = editMessage