Fix speedbump in threads
This commit is contained in:
		
							parent
							
								
									0f25e73d67
								
							
						
					
					
						commit
						0e701b2d54
					
				
					 4 changed files with 28 additions and 17 deletions
				
			
		| 
						 | 
					@ -11,7 +11,7 @@ const speedbump = sync.require("./speedbump")
 | 
				
			||||||
 * @param {import("discord-api-types/v10").GatewayMessageDeleteDispatchData} data
 | 
					 * @param {import("discord-api-types/v10").GatewayMessageDeleteDispatchData} data
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
async function deleteMessage(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
 | 
						if (!row) return
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	const eventsToRedact = select("event_message", "event_id", {message_id: data.id}).pluck().all()
 | 
						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)
 | 
							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)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -2,7 +2,7 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const DiscordTypes = require("discord-api-types/v10")
 | 
					const DiscordTypes = require("discord-api-types/v10")
 | 
				
			||||||
const passthrough = require("../../passthrough")
 | 
					const passthrough = require("../../passthrough")
 | 
				
			||||||
const {discord, db} = passthrough
 | 
					const {discord, select, db} = passthrough
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const SPEEDBUMP_SPEED = 4000 // 4 seconds delay
 | 
					const SPEEDBUMP_SPEED = 4000 // 4 seconds delay
 | 
				
			||||||
const SPEEDBUMP_UPDATE_FREQUENCY = 2 * 60 * 60 // 2 hours
 | 
					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.
 | 
					 * Slow down a message. After it passes the speedbump, return whether it's okay or if it's been deleted.
 | 
				
			||||||
 * @param {string} messageID
 | 
					 * @param {string} messageID
 | 
				
			||||||
 | 
					 * @returns whether it was deleted
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
async function doSpeedbump(messageID) {
 | 
					async function doSpeedbump(messageID) {
 | 
				
			||||||
	bumping.add(messageID)
 | 
						bumping.add(messageID)
 | 
				
			||||||
| 
						 | 
					@ -40,6 +41,21 @@ async function doSpeedbump(messageID) {
 | 
				
			||||||
	return !bumping.delete(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
 | 
					 * @param {string} messageID
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
| 
						 | 
					@ -49,4 +65,5 @@ function onMessageDelete(messageID) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
module.exports.updateCache = updateCache
 | 
					module.exports.updateCache = updateCache
 | 
				
			||||||
module.exports.doSpeedbump = doSpeedbump
 | 
					module.exports.doSpeedbump = doSpeedbump
 | 
				
			||||||
 | 
					module.exports.maybeDoSpeedbump = maybeDoSpeedbump
 | 
				
			||||||
module.exports.onMessageDelete = onMessageDelete
 | 
					module.exports.onMessageDelete = onMessageDelete
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -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.
 | 
								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()
 | 
							const {affected, row} = await speedbump.maybeDoSpeedbump(message.channel_id, message.id)
 | 
				
			||||||
		if (row && row.speedbump_id) {
 | 
					 | 
				
			||||||
			const affected = await speedbump.doSpeedbump(message.id)
 | 
					 | 
				
			||||||
		if (affected) return
 | 
							if (affected) return
 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// @ts-ignore
 | 
							// @ts-ignore
 | 
				
			||||||
		await sendMessage.sendMessage(message, guild, row),
 | 
							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.
 | 
								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()
 | 
							const {affected, row} = await speedbump.maybeDoSpeedbump(data.channel_id, data.id)
 | 
				
			||||||
		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
 | 
							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.
 | 
							// 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.
 | 
							// If the message content is a string then it includes all interesting fields and is meaningful.
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -2,9 +2,9 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
<img src="docs/img/icon.png" height="128" width="128">
 | 
					<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)
 | 
					[](https://gitdab.com/cadence/out-of-your-element/releases) [](https://matrix.to/#/#out-of-your-element:cadence.moe)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
## Docs
 | 
					## 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. [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 .
 | 
				
			||||||
 | 
					
 | 
				
			||||||
1. Install dependencies: `npm install --save-dev` (omit --save-dev if you will not run the automated tests)
 | 
					1. Install dependencies: `npm install --save-dev` (omit --save-dev if you will not run the automated tests)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue