diff --git a/d2m/actions/delete-message.js b/d2m/actions/delete-message.js index 4386ae5..440e123 100644 --- a/d2m/actions/delete-message.js +++ b/d2m/actions/delete-message.js @@ -11,7 +11,7 @@ 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"], {channel_id: data.channel_id}).get() + const row = select("channel_room", ["room_id", "speedbump_checked", "thread_parent"], {channel_id: data.channel_id}).get() if (!row) return const eventsToRedact = select("event_message", "event_id", {message_id: data.id}).pluck().all() @@ -22,7 +22,7 @@ async function deleteMessage(data) { await api.redactEvent(row.room_id, eventID) } - speedbump.updateCache(data.channel_id, row.speedbump_checked) + await speedbump.updateCache(row.thread_parent || data.channel_id, row.speedbump_checked) } /** diff --git a/d2m/actions/speedbump.js b/d2m/actions/speedbump.js index f49a378..e782ae0 100644 --- a/d2m/actions/speedbump.js +++ b/d2m/actions/speedbump.js @@ -2,7 +2,7 @@ const DiscordTypes = require("discord-api-types/v10") const passthrough = require("../../passthrough") -const {discord, db} = passthrough +const {discord, select, db} = passthrough const SPEEDBUMP_SPEED = 4000 // 4 seconds delay const SPEEDBUMP_UPDATE_FREQUENCY = 2 * 60 * 60 // 2 hours @@ -33,6 +33,7 @@ const bumping = new Set() /** * Slow down a message. After it passes the speedbump, return whether it's okay or if it's been deleted. * @param {string} messageID + * @returns whether it was deleted */ async function doSpeedbump(messageID) { bumping.add(messageID) @@ -40,6 +41,21 @@ async function doSpeedbump(messageID) { return !bumping.delete(messageID) } +/** + * Check whether to slow down a message, and do it. After it passes the speedbump, return whether it's okay or if it's been deleted. + * @param {string} channelID + * @param {string} messageID + * @returns whether it was deleted, and data about the channel's (not thread's) speedbump + */ +async function maybeDoSpeedbump(channelID, messageID) { + let row = select("channel_room", ["thread_parent", "speedbump_id", "speedbump_webhook_id"], {channel_id: channelID}).get() + if (row?.thread_parent) row = select("channel_room", ["thread_parent", "speedbump_id", "speedbump_webhook_id"], {channel_id: row.thread_parent}).get() // webhooks belong to the channel, not the thread + if (!row) return {affected: false, row: null}// not affected, no speedbump + // Edits need to go through the speedbump as well. If the message is delayed but the edit isn't, we don't have anything to edit from. + const affected = await doSpeedbump(messageID) + return {affected, row} // maybe affected, and there is a speedbump +} + /** * @param {string} messageID */ @@ -49,4 +65,5 @@ function onMessageDelete(messageID) { module.exports.updateCache = updateCache module.exports.doSpeedbump = doSpeedbump +module.exports.maybeDoSpeedbump = maybeDoSpeedbump module.exports.onMessageDelete = onMessageDelete diff --git a/d2m/event-dispatcher.js b/d2m/event-dispatcher.js index c630bfb..52bed72 100644 --- a/d2m/event-dispatcher.js +++ b/d2m/event-dispatcher.js @@ -246,11 +246,8 @@ module.exports = { if (row) return // The message was sent by the bridge's own webhook on discord. We don't want to reflect this back, so just drop it. } - const row = select("channel_room", ["speedbump_id", "speedbump_webhook_id"], {channel_id: message.channel_id}).get() - if (row && row.speedbump_id) { - const affected = await speedbump.doSpeedbump(message.id) - if (affected) return - } + const {affected, row} = await speedbump.maybeDoSpeedbump(message.channel_id, message.id) + if (affected) return // @ts-ignore await sendMessage.sendMessage(message, guild, row), @@ -267,12 +264,8 @@ module.exports = { if (row) return // The message was sent by the bridge's own webhook on discord. We don't want to reflect this back, so just drop it. } - const row = select("channel_room", ["speedbump_id", "speedbump_webhook_id"], {channel_id: data.channel_id}).get() - if (row) { - // Edits need to go through the speedbump as well. If the message is delayed but the edit isn't, we don't have anything to edit from. - const affected = await speedbump.doSpeedbump(data.id) - if (affected) return - } + const {affected, row} = await speedbump.maybeDoSpeedbump(data.channel_id, data.id) + if (affected) return // Based on looking at data they've sent me over the gateway, this is the best way to check for meaningful changes. // If the message content is a string then it includes all interesting fields and is meaningful. diff --git a/readme.md b/readme.md index 05d8f9c..49c2e66 100644 --- a/readme.md +++ b/readme.md @@ -2,9 +2,9 @@ -Modern Matrix-to-Discord appservice bridge. +Modern Matrix-to-Discord appservice bridge, created by [@cadence:cadence.moe](https://matrix.to/#/@cadence:cadence.moe) -Created by [@cadence:cadence.moe](https://matrix.to/#/@cadence:cadence.moe) // Discuss in [#out-of-your-element:cadence.moe](https://matrix.to/#/#out-of-your-element:cadence.moe) +[![Releases](https://img.shields.io/gitea/v/release/cadence/out-of-your-element?gitea_url=https%3A%2F%2Fgitdab.com&style=plastic&color=green)](https://gitdab.com/cadence/out-of-your-element/releases) [![Discuss on Matrix](https://img.shields.io/badge/discuss-%23out--of--your--element-white?style=plastic)](https://matrix.to/#/#out-of-your-element:cadence.moe) ## Docs @@ -76,7 +76,8 @@ Follow these steps: 1. [Get Node.js version 18 or later](https://nodejs.org/en/download/releases) (the version is required by the better-sqlite3 and matrix-appservice dependencies) -1. Clone this repo and checkout a specific tag. (Development happens on main. Stabler versions are tagged.) +1. Clone this repo and checkout a specific tag. (Development happens on main. Stable versions are tagged.) + * The latest release tag is ![](https://img.shields.io/gitea/v/release/cadence/out-of-your-element?gitea_url=https%3A%2F%2Fgitdab.com&style=flat-square&label=%20&color=black). 1. Install dependencies: `npm install --save-dev` (omit --save-dev if you will not run the automated tests)