out-of-your-element/m2d/actions/redact.js

42 lines
1.4 KiB
JavaScript
Raw Normal View History

// @ts-check
const assert = require("assert").strict
const Ty = require("../../types")
const passthrough = require("../../passthrough")
const {discord, sync, db, select, from} = passthrough
/** @type {import("../converters/utils")} */
const utils = sync.require("../converters/utils")
/**
* @param {Ty.Event.Outer_M_Room_Redaction} event
*/
async function deleteMessage(event) {
2023-10-02 09:10:50 +00:00
const rows = from("event_message").join("message_channel", "message_id").select("channel_id", "message_id").and("WHERE event_id = ?").all(event.redacts)
for (const row of rows) {
discord.snow.channel.deleteMessage(row.channel_id, row.message_id, event.content.reason)
}
}
/**
* @param {Ty.Event.Outer_M_Room_Redaction} event
*/
async function removeReaction(event) {
const hash = utils.getEventIDHash(event.redacts)
const row = from("reaction").join("message_channel", "message_id").select("channel_id", "message_id", "encoded_emoji").and("WHERE hashed_event_id = ?").get(hash)
if (!row) return
await discord.snow.channel.deleteReactionSelf(row.channel_id, row.message_id, row.encoded_emoji)
db.prepare("DELETE FROM reaction WHERE hashed_event_id = ?").run(hash)
}
/**
* Try everything that could possibly be redacted.
* @param {Ty.Event.Outer_M_Room_Redaction} event
*/
async function handle(event) {
await deleteMessage(event)
await removeReaction(event)
}
module.exports.handle = handle