2023-08-17 13:22:14 +00:00
// @ts-check
const passthrough = require ( "../../passthrough" )
2024-01-11 02:56:42 +00:00
const { sync , db , select , from } = passthrough
2023-08-17 13:22:14 +00:00
/** @type {import("../../matrix/api")} */
const api = sync . require ( "../../matrix/api" )
2024-01-19 12:01:34 +00:00
/** @type {import("./speedbump")} */
const speedbump = sync . require ( "./speedbump" )
2023-08-17 13:22:14 +00:00
/ * *
* @ param { import ( "discord-api-types/v10" ) . GatewayMessageDeleteDispatchData } data
* /
async function deleteMessage ( data ) {
2024-02-07 01:52:12 +00:00
const row = select ( "channel_room" , [ "room_id" , "speedbump_checked" , "thread_parent" ] , { channel _id : data . channel _id } ) . get ( )
2024-01-19 12:01:34 +00:00
if ( ! row ) return
2023-08-17 13:22:14 +00:00
2023-10-05 23:31:10 +00:00
const eventsToRedact = select ( "event_message" , "event_id" , { message _id : data . id } ) . pluck ( ) . all ( )
2024-01-10 02:48:13 +00:00
db . prepare ( "DELETE FROM message_channel WHERE message_id = ?" ) . run ( data . id )
db . prepare ( "DELETE FROM event_message WHERE message_id = ?" ) . run ( data . id )
2023-08-17 13:22:14 +00:00
for ( const eventID of eventsToRedact ) {
2023-09-06 00:31:38 +00:00
// Unfortunately, we can't specify a sender to do the redaction as, unless we find out that info via the audit logs
2024-01-19 12:01:34 +00:00
await api . redactEvent ( row . room _id , eventID )
2023-08-17 13:22:14 +00:00
}
2024-01-19 12:01:34 +00:00
2024-02-07 01:52:12 +00:00
await speedbump . updateCache ( row . thread _parent || data . channel _id , row . speedbump _checked )
2023-08-17 13:22:14 +00:00
}
2024-01-11 02:56:42 +00:00
/ * *
* @ param { import ( "discord-api-types/v10" ) . GatewayMessageDeleteBulkDispatchData } data
* /
async function deleteMessageBulk ( data ) {
const roomID = select ( "channel_room" , "room_id" , { channel _id : data . channel _id } ) . pluck ( ) . get ( )
if ( ! roomID ) return
const sids = JSON . stringify ( data . ids )
2024-06-20 01:50:38 +00:00
const eventsToRedact = from ( "event_message" ) . pluck ( "event_id" ) . and ( "WHERE message_id IN (SELECT value FROM json_each(?))" ) . all ( sids )
db . prepare ( "DELETE FROM message_channel WHERE message_id IN (SELECT value FROM json_each(?))" ) . run ( sids )
db . prepare ( "DELETE FROM event_message WHERE message_id IN (SELECT value FROM json_each(?))" ) . run ( sids )
2024-01-11 02:56:42 +00:00
for ( const eventID of eventsToRedact ) {
// Awaiting will make it go slower, but since this could be a long-running operation either way, we want to leave rate limit capacity for other operations
await api . redactEvent ( roomID , eventID )
}
}
2023-08-17 13:22:14 +00:00
module . exports . deleteMessage = deleteMessage
2024-01-11 02:56:42 +00:00
module . exports . deleteMessageBulk = deleteMessageBulk