summary refs log tree commit diff
path: root/src/d2m/actions/delete-message.js
blob: 39b9fc8479a70a135f92a2b90b4f0e76fd11be1d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// @ts-check

const passthrough = require("../../passthrough")
const {sync, db, select, from} = passthrough
/** @type {import("../../matrix/api")} */
const api = sync.require("../../matrix/api")
/** @type {import("./speedbump")} */
const speedbump = sync.require("./speedbump")

/**
 * @param {import("discord-api-types/v10").GatewayMessageDeleteDispatchData} data
 */
async function deleteMessage(data) {
	const row = select("channel_room", ["room_id", "speedbump_checked", "thread_parent"], {channel_id: data.channel_id}).get()
	if (!row) return

	// Assume we can redact from tombstoned rooms.
	const eventsToRedact = from("event_message").join("message_room", "message_id").join("historical_channel_room", "historical_room_index")
		.select("event_id", "room_id").where({message_id: data.id}).all()
	db.prepare("DELETE FROM message_room WHERE message_id = ?").run(data.id)
	for (const {event_id, room_id} of eventsToRedact) {
		// Unfortunately, we can't specify a sender to do the redaction as, unless we find out that info via the audit logs
		await api.redactEvent(room_id, event_id)
	}

	await speedbump.updateCache(row.thread_parent || data.channel_id, row.speedbump_checked)
}

/**
 * @param {import("discord-api-types/v10").GatewayMessageDeleteBulkDispatchData} data
 */
async function deleteMessageBulk(data) {
	const row = select("channel_room", "room_id", {channel_id: data.channel_id}).get()
	if (!row) return

	const sids = JSON.stringify(data.ids)
	// Assume we can redact from tombstoned rooms.
	const eventsToRedact = from("event_message").join("message_room", "message_id").join("historical_channel_room", "historical_room_index")
		.select("event_id", "room_id").and("WHERE message_id IN (SELECT value FROM json_each(?))").all(sids)
	db.prepare("DELETE FROM message_room WHERE message_id IN (SELECT value FROM json_each(?))").run(sids)
	for (const {event_id, room_id} 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(room_id, event_id)
	}
}

module.exports.deleteMessage = deleteMessage
module.exports.deleteMessageBulk = deleteMessageBulk