From 5b21344a65306c3686bcc6671f98ad56abe03af1 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Sat, 1 Feb 2025 01:40:59 +1300 Subject: [PATCH] Add room list debugger --- src/d2m/actions/create-room.js | 2 +- src/web/pug/guild.pug | 37 ++++++++++++++++++++++++++++++++++ src/web/routes/guild.js | 37 +++++++++++++++++++++++++++++----- src/web/routes/link.js | 6 ++++++ 4 files changed, 76 insertions(+), 6 deletions(-) diff --git a/src/d2m/actions/create-room.js b/src/d2m/actions/create-room.js index 690ca2a..066d3fa 100644 --- a/src/d2m/actions/create-room.js +++ b/src/d2m/actions/create-room.js @@ -436,7 +436,7 @@ async function unbridgeDeletedChannel(channel, guildID) { powerLevelContent.users ??= {} const bot = `@${reg.sender_localpart}:${reg.ooye.server_name}` for (const mxid of Object.keys(powerLevelContent.users)) { - if (mUtils.eventSenderIsFromDiscord(mxid) && mxid !== bot) { + if (powerLevelContent.users[mxid] >= 100 && mUtils.eventSenderIsFromDiscord(mxid) && mxid !== bot) { delete powerLevelContent.users[mxid] await api.sendState(roomID, "m.room.power_levels", "", powerLevelContent, mxid) } diff --git a/src/web/pug/guild.pug b/src/web/pug/guild.pug index 4fce9a2..1d564dc 100644 --- a/src/web/pug/guild.pug +++ b/src/web/pug/guild.pug @@ -154,3 +154,40 @@ block body button.s-btn.s-btn__icon.s-btn__filled#link-button != icons.Icons.IconMerge = ` Link` + + details.mt48 + summary Debug room list + .d-grid.grid__2.gx24 + div + h3.mt24 Channels + p Channels are read from the channel_room table and then merged with the discord.channels memory cache to make the merged list. Anything in memory cache that's not in channel_room is considered unlinked. + div + h3.mt24 Rooms + p Rooms use the same merged list as channels, based on augmented channel_room data. Then, rooms are read from the space. Anything in the space that's not merged is considered unlinked. + div + h3.mt24 Unavailable channels: Deleted from Discord + .s-card.p0 + ul.my8.ml24 + each row in removedUncachedChannels + li: a(href=`https://discord.com/channels/${guild_id}/${row.channel_id}`)= row.nick || row.name + h3.mt24 Unavailable channels: Wrong type + .s-card.p0 + ul.my8.ml24 + each row in removedWrongTypeChannels + li: a(href=`https://discord.com/channels/${guild_id}/${row.channel_id}`) (#{row.type}) #{row.name} + div- // Rooms + h3.mt24 Unavailable rooms: Already linked + .s-card.p0 + ul.my8.ml24 + each row in removedLinkedRooms + li: a(href=`https://matrix.to/#/${row.room_id}`)= row.name + h3.mt24 Unavailable rooms: Wrong type + .s-card.p0 + ul.my8.ml24 + each row in removedWrongTypeRooms + li: a(href=`https://matrix.to/#/${row.room_id}`) (#{row.room_type}) #{row.name} + h3.mt24 Unavailable rooms: Archived thread + .s-card.p0 + ul.my8.ml24 + each row in removedArchivedThreadRooms + li: a(href=`https://matrix.to/#/${row.room_id}`)= row.name diff --git a/src/web/routes/guild.js b/src/web/routes/guild.js index 5944131..8a1cbc2 100644 --- a/src/web/routes/guild.js +++ b/src/web/routes/guild.js @@ -42,6 +42,26 @@ function getAPI(event) { /** @type {LRUCache} nonce to guild id */ const validNonce = new LRUCache({max: 200}) +/** + * Modifies the input, removing items that don't pass the filter. Returns the items that didn't pass. + * @param {T[]} xs + * @param {(x: T, i?: number) => any} fn + * @template T + * @returns T[] + */ +function filterTo(xs, fn) { + /** @type {T[]} */ + const filtered = [] + for (let i = xs.length-1; i >= 0; i--) { + const x = xs[i] + if (!fn(x, i)) { + filtered.unshift(x) + xs.splice(i, 1) + } + } + return filtered +} + /** * @param {string} guildID * @param {Ty.R.Hierarchy[]} rooms @@ -62,21 +82,28 @@ function getChannelRoomsLinks(guildID, rooms) { assert(channelIDs) let linkedChannels = select("channel_room", ["channel_id", "room_id", "name", "nick"], {channel_id: channelIDs}).all() - let linkedChannelsWithDetails = linkedChannels.map(c => ({channel: discord.channels.get(c.channel_id), ...c})).filter(c => c.channel) + let linkedChannelsWithDetails = linkedChannels.map(c => ({channel: discord.channels.get(c.channel_id), ...c})) + let removedUncachedChannels = filterTo(linkedChannelsWithDetails, c => c.channel) let linkedChannelIDs = linkedChannelsWithDetails.map(c => c.channel_id) linkedChannelsWithDetails.sort((a, b) => getPosition(a.channel) - getPosition(b.channel)) let unlinkedChannelIDs = channelIDs.filter(c => !linkedChannelIDs.includes(c)) - let unlinkedChannels = unlinkedChannelIDs.map(c => discord.channels.get(c)).filter(c => c && [0, 5].includes(c.type)) + let unlinkedChannels = unlinkedChannelIDs.map(c => discord.channels.get(c)) + let removedWrongTypeChannels = filterTo(unlinkedChannels, c => c && [0, 5].includes(c.type)) unlinkedChannels.sort((a, b) => getPosition(a) - getPosition(b)) let linkedRoomIDs = linkedChannels.map(c => c.room_id) - let unlinkedRooms = rooms.filter(r => !linkedRoomIDs.includes(r.room_id) && !r.room_type) + let unlinkedRooms = [...rooms] + let removedLinkedRooms = filterTo(unlinkedRooms, r => !linkedRoomIDs.includes(r.room_id)) + let removedWrongTypeRooms = filterTo(unlinkedRooms, r => !r.room_type) // https://discord.com/developers/docs/topics/threads#active-archived-threads // need to filter out linked archived threads from unlinkedRooms, will just do that by comparing against the name - unlinkedRooms = unlinkedRooms.filter(r => r.name && !r.name.match(/^\[(🔒)?⛓️\]/)) + let removedArchivedThreadRooms = filterTo(unlinkedRooms, r => r.name && !r.name.match(/^\[(🔒)?⛓️\]/)) - return {linkedChannelsWithDetails, unlinkedChannels, unlinkedRooms} + return { + linkedChannelsWithDetails, unlinkedChannels, unlinkedRooms, + removedUncachedChannels, removedWrongTypeChannels, removedLinkedRooms, removedWrongTypeRooms, removedArchivedThreadRooms + } } as.router.get("/guild", defineEventHandler(async event => { diff --git a/src/web/routes/link.js b/src/web/routes/link.js index 193d7a3..efc474f 100644 --- a/src/web/routes/link.js +++ b/src/web/routes/link.js @@ -63,6 +63,12 @@ as.router.post("/api/link", defineEventHandler(async event => { // Sync room data and space child await createRoom.syncRoom(parsedBody.discord) + // Send a notification in the room + await api.sendEvent(parsedBody.matrix, "m.room.message", { + msgtype: "m.notice", + body: "👋 This room is now bridged with Discord. Say hi!" + }) + setResponseHeader(event, "HX-Refresh", "true") return null // 204 }))