Fix speedbump in threads

This commit is contained in:
Cadence Ember 2024-02-07 14:52:12 +13:00
parent 0f25e73d67
commit 0e701b2d54
4 changed files with 28 additions and 17 deletions

View File

@ -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)
}
/**

View File

@ -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

View File

@ -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.

View File

@ -2,9 +2,9 @@
<img src="docs/img/icon.png" height="128" width="128">
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)