From c015348a1e4dffa9ae2e6d876ad8b51e79a71079 Mon Sep 17 00:00:00 2001 From: mini-bomba <55105495+mini-bomba@users.noreply.github.com> Date: Sat, 14 Nov 2026 20:21:43 +0100 Subject: [PATCH 1/2] bridge discord's channel ordering to the matrix space --- src/d2m/actions/create-room.js | 88 +++++++++++++++++++++++++++++++++- 1 file changed, 86 insertions(+), 2 deletions(-) diff --git a/src/d2m/actions/create-room.js b/src/d2m/actions/create-room.js index 5f9707a..fad37cf 100644 --- a/src/d2m/actions/create-room.js +++ b/src/d2m/actions/create-room.js @@ -543,6 +543,53 @@ async function unbridgeChannel(channel, guildID, messageBeforeLeave = "This room await api.leaveRoom(roomID) } +const BASE26ALPHABET = "abcdefghijklmnopqrstuvwxyz" +const BASE26PAIRMAX = 26*26-1 + +/** + * Helper function for _encodeOrder + * + * @param {number | null | undefined} order + * @returns {string} + */ +function _toBase26Pair(order) { + if (order == null || !isFinite(order)) return " " + else if (order < 0) order = 0 + else if (order > BASE26PAIRMAX) order = BASE26PAIRMAX + + const lower = order % 26; + const upper = (order - lower) / 26 + + return `${BASE26ALPHABET.at(upper)}${BASE26ALPHABET.at(lower)}` +} + +/** + * Converts a set of discord numeric channel order parameters to a single matrix space ordering string + * + * We abuse the lexiographic ordering scheme used by matrix to cheaply bridge the channel ordering, + * even with the channel/category structure being flattened on the matrix side: + * - The order string is 4 characters for channels and 6 characters for threads. + * - Each pair of 2 characters is treated as a single base26 number (alphabet: a-z) + * - The first pair is the order number of the category the channel belongs to + * - If the channel does not have a category, this pair is set to " ", + * which orders these rooms before rooms under categories + * - The second pair is the order number of the channel + * - The third (optional) pair is the order of the thread + * - If not present, it is omitted - this orders threads right after the parent channel + * + * @param {number | null | undefined} categoryOrder category position + * @param {number} channelOrder channel position + * @param {number | null | undefined} threadOrder thread position + * @returns {string} + */ +function _encodeOrder(categoryOrder, channelOrder, threadOrder) { + const categoryPair = _toBase26Pair(categoryOrder) + const channelPair = _toBase26Pair(channelOrder) + const threadPair = threadOrder == null ? "" : _toBase26Pair(threadOrder) + + return `${categoryPair}${channelPair}${threadPair}` +} + /** * Async because it gets all space state from the homeserver, then if necessary sends one state event back. * @param {DiscordTypes.APIGuildTextChannel} channel @@ -555,11 +602,15 @@ async function _syncSpaceMember(channel, spaceID, roomID, guild_id) { // If space is self-service then only permit changes to space parenting for threads // (The person setting up self-service has already put it in their preferred space to be able to get this far.) const autocreate = select("guild_active", "autocreate", {guild_id}).pluck().get() - if (autocreate === 0 && ![DiscordTypes.ChannelType.PrivateThread, DiscordTypes.ChannelType.PublicThread, DiscordTypes.ChannelType.AnnouncementThread].includes(channel.type)) { + const isThread = [DiscordTypes.ChannelType.PrivateThread, DiscordTypes.ChannelType.PublicThread, DiscordTypes.ChannelType.AnnouncementThread].includes(channel.type) + if (autocreate === 0 && !isThread) { return [] } const spaceKState = await ks.roomToKState(spaceID) + const childStateKey = `m.space.child/${roomID}` + const currentChildEvent = spaceKState[childStateKey] + /** @type {{via?: any, suggested?: any, order?: any}} */ let spaceEventContent = {} if ( channel.type !== DiscordTypes.ChannelType.PrivateThread // private threads do not belong in the space (don't offer people something they can't join) @@ -571,9 +622,42 @@ async function _syncSpaceMember(channel, spaceID, roomID, guild_id) { spaceEventContent = { via: [reg.ooye.server_name] } + + // let admins mark rooms as suggested on the [m] side without us removing the flag + const currentSuggested = currentChildEvent?.suggested + if (currentSuggested != null) { + spaceEventContent.suggested = currentSuggested + } + + if (autocreate === 0) { + // don't mess with room ordering in self-service spaces + const currentOrder = currentChildEvent?.order + if (currentOrder != null) { + spaceEventContent.order = currentOrder + } + } else { + // bridge the discord channel order + /** @ts-ignore @type {DiscordTypes.APIThreadChannel | undefined} */ + const thread = isThread && channel.parent_id != null ? channel : undefined + /** @ts-ignore @type {DiscordTypes.APIGuildTextChannel | undefined} */ + const channelForReal = isThread && channel.parent_id != null ? discord.channels.get(channel.parent_id) : channel + /** @ts-ignore @type {DiscordTypes.APIGuildCategoryChannel | undefined} */ + const category = discord.channels.get(channelForReal?.parent_id ?? "") + + // we actually don't have the thread's position - put 0 here for now, matrix will sort it automatically by timestamp + const threadPosition = thread == null ? null : 0 + let channelPosition = channelForReal?.position ?? 0 + const categoryPosition = category?.position ?? null + + // since voice channels are always ordered after all text channels on discord, + // offset the order by half of the available range - this should be enough in most cases + if (channel.type === DiscordTypes.ChannelType.GuildVoice) channelPosition += 13*26 + + spaceEventContent.order = _encodeOrder(categoryPosition, channelPosition, threadPosition) + } } const spaceDiff = ks.diffKState(spaceKState, { - [`m.space.child/${roomID}`]: spaceEventContent + [childStateKey]: spaceEventContent }) return ks.applyKStateDiffToRoom(spaceID, spaceDiff) } -- 2.34.1 From 3f08ba7e61f2b027eb124feac8f3c64571463a71 Mon Sep 17 00:00:00 2001 From: mini-bomba <55105495+mini-bomba@users.noreply.github.com> Date: Sat, 14 Nov 2026 20:58:32 +0100 Subject: [PATCH 2/2] fix error on everyone role perms update --- src/d2m/event-dispatcher.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/d2m/event-dispatcher.js b/src/d2m/event-dispatcher.js index 5c6287b..1c434db 100644 --- a/src/d2m/event-dispatcher.js +++ b/src/d2m/event-dispatcher.js @@ -238,7 +238,7 @@ module.exports = { if (!spaceID) return if (data.role.id === data.guild_id) { // @everyone role changed - find a way to do this more efficiently in the future to handle many role updates - await createSpace.syncSpaceFully(guild) + await createSpace.syncSpaceFully(data.guild_id) } }, -- 2.34.1