Fix and test broken room bridge check #104

Merged
cadence merged 2 commits from l1s5/out-of-your-element:fix-room-bridged-sql-query into main 2026-11-04 22:26:37 +00:00
3 changed files with 26 additions and 4 deletions
Showing only changes of commit 94a96dd51a - Show all commits

Fix and test broken room bridge check

A `m.room.member` handler check intended to determine whether a room was
bridged or not ended up checking the truthiness of the db query itself,
resulting in a tautology.

This commit fixes the check and moves it over to matrix/utils so it may
be reused and tested in isolation.
l1s5 2026-11-02 18:13:52 +09:00
Signed by untrusted user: l1s5
GPG key ID: D0A90CC905B58D9B

View file

@ -422,8 +422,7 @@ async event => {
if (utils.eventSenderIsFromDiscord(event.state_key)) return if (utils.eventSenderIsFromDiscord(event.state_key)) return
const exists = select("channel_room", "room_id", {room_id: event.room_id}) ?? select("guild_space", "space_id", {space_id: event.room_id}) if (!utils.isBridgedRoom(event.room_id)) return // don't cache members in unbridged rooms
if (!exists) return // don't cache members in unbridged rooms
// Member is here // Member is here
let {powers: {[event.state_key]: memberPower}, tombstone} = await utils.getEffectivePower(event.room_id, [event.state_key], api) let {powers: {[event.state_key]: memberPower}, tombstone} = await utils.getEffectivePower(event.room_id, [event.state_key], api)

View file

@ -4,7 +4,7 @@ const assert = require("assert").strict
const Ty = require("../types") const Ty = require("../types")
const {tag} = require("@cloudrac3r/html-template-tag") const {tag} = require("@cloudrac3r/html-template-tag")
const passthrough = require("../passthrough") const passthrough = require("../passthrough")
const {db} = passthrough const {db, select} = passthrough
const {reg} = require("./read-registration") const {reg} = require("./read-registration")
const userRegex = reg.namespaces.users.map(u => new RegExp(u.regex)) const userRegex = reg.namespaces.users.map(u => new RegExp(u.regex))
@ -41,6 +41,16 @@ function eventSenderIsFromDiscord(sender) {
return false return false
} }
/**
* @param {string} roomID
* @returns {boolean} whether the room is a bridged channel or space
*/
function isBridgedRoom(roomID) {
if (select("channel_room", "room_id", {room_id: roomID}).get()) return true
if (select("guild_space", "space_id", {space_id: roomID}).get()) return true
return false
}
/** /**
* Event IDs are really big and have more entropy than we need. * Event IDs are really big and have more entropy than we need.
* If we want to store the event ID in the database, we can store a more compact version by hashing it with this. * If we want to store the event ID in the database, we can store a more compact version by hashing it with this.
@ -388,6 +398,7 @@ async function setUserPowerCascade(spaceID, mxid, power, api) {
module.exports.bot = bot module.exports.bot = bot
module.exports.BLOCK_ELEMENTS = BLOCK_ELEMENTS module.exports.BLOCK_ELEMENTS = BLOCK_ELEMENTS
module.exports.eventSenderIsFromDiscord = eventSenderIsFromDiscord module.exports.eventSenderIsFromDiscord = eventSenderIsFromDiscord
module.exports.isBridgedRoom = isBridgedRoom
module.exports.makeMxcPublic = makeMxcPublic module.exports.makeMxcPublic = makeMxcPublic
module.exports.getPublicUrlForMxc = getPublicUrlForMxc module.exports.getPublicUrlForMxc = getPublicUrlForMxc
module.exports.getEventIDHash = getEventIDHash module.exports.getEventIDHash = getEventIDHash

View file

@ -2,7 +2,7 @@
const {select} = require("../passthrough") const {select} = require("../passthrough")
const {test} = require("supertape") const {test} = require("supertape")
const {eventSenderIsFromDiscord, getEventIDHash, MatrixStringBuilder, getViaServers, roomHasAtLeastVersion, removeCreatorsFromPowerLevels, setUserPower} = require("./utils") const {eventSenderIsFromDiscord, isBridgedRoom, getEventIDHash, MatrixStringBuilder, getViaServers, roomHasAtLeastVersion, removeCreatorsFromPowerLevels, setUserPower} = require("./utils")
const util = require("util") const util = require("util")
/** @param {string[]} mxids */ /** @param {string[]} mxids */
@ -30,6 +30,18 @@ test("sender type: ooye puppet", t => {
t.ok(eventSenderIsFromDiscord("@_ooye_sheep:cadence.moe")) t.ok(eventSenderIsFromDiscord("@_ooye_sheep:cadence.moe"))
}) })
test("is bridged room: channel room", t => {
t.equal(isBridgedRoom("!kLRqKKUQXcibIMtOpl:cadence.moe"), true)
})
test("is bridged room: space", t => {
t.equal(isBridgedRoom("!jjmvBegULiLucuWEHU:cadence.moe"), true)
})
test("is bridged room: unbridged room", t => {
t.equal(isBridgedRoom("!unbridged:cadence.moe"), false)
})
test("event hash: hash is the same each time", t => { test("event hash: hash is the same each time", t => {
const eventID = "$example" const eventID = "$example"
t.equal(getEventIDHash(eventID), getEventIDHash(eventID)) t.equal(getEventIDHash(eventID), getEventIDHash(eventID))