From 3a84658e8b935ac1634c0af49425b008b16ed4a4 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Tue, 27 Aug 2024 19:10:38 +1200 Subject: [PATCH] Make a helper for the room hierarchy --- d2m/actions/create-space.js | 15 +++------------ discord/interactions/bridge.js | 24 +++++++++--------------- matrix/api.js | 19 +++++++++++++++++++ 3 files changed, 31 insertions(+), 27 deletions(-) diff --git a/d2m/actions/create-space.js b/d2m/actions/create-space.js index 44d0206e..b15cba6a 100644 --- a/d2m/actions/create-space.js +++ b/d2m/actions/create-space.js @@ -185,19 +185,10 @@ async function syncSpaceFully(guildID) { const spaceDiff = ks.diffKState(spaceKState, guildKState) await createRoom.applyKStateDiffToRoom(spaceID, spaceDiff) - /** @type {string[]} room IDs */ - let childRooms = [] - /** @type {string | undefined} */ - let nextBatch = undefined - do { - /** @type {Ty.HierarchyPagination} */ - const res = await api.getHierarchy(spaceID, {from: nextBatch}) - childRooms.push(...res.rooms.map(room => room.room_id)) - nextBatch = res.next_batch - } while (nextBatch) + const childRooms = await api.getFullHierarchy(spaceID) - for (const roomID of childRooms) { - const channelID = select("channel_room", "channel_id", {room_id: roomID}).pluck().get() + for (const {room_id} of childRooms) { + const channelID = select("channel_room", "channel_id", {room_id}).pluck().get() if (!channelID) continue if (discord.channels.has(channelID)) { await createRoom.syncRoom(channelID) diff --git a/discord/interactions/bridge.js b/discord/interactions/bridge.js index 53eee7dc..ee33bfd9 100644 --- a/discord/interactions/bridge.js +++ b/discord/interactions/bridge.js @@ -32,24 +32,18 @@ sync.addTemporaryListener(as, "type:m.room.name", /** @param {Ty.Event.StateOute }) // Manage adding to the cache -async function getHierarchy(spaceID) { +async function getCachedHierarchy(spaceID) { return cache.get(spaceID) || (() => { const entry = (async () => { + const result = await api.getFullHierarchy(spaceID) /** @type {{name: string, value: string}[]} */ - let childRooms = [] - /** @type {string | undefined} */ - let nextBatch = undefined - do { - /** @type {Ty.HierarchyPagination} */ - const res = await api.getHierarchy(spaceID, {from: nextBatch}) - for (const room of res.rooms) { - if (room.name) { - childRooms.push({name: room.name, value: room.room_id}) - reverseCache.set(room.room_id, spaceID) - } + const childRooms = [] + for (const room of result) { + if (room.name) { + childRooms.push({name: room.name, value: room.room_id}) + reverseCache.set(room.room_id, spaceID) } - nextBatch = res.next_batch - } while (nextBatch) + } return childRooms })() cache.set(spaceID, entry) @@ -74,7 +68,7 @@ async function interactAutocomplete({id, token, data, guild_id}) { }) } - let rooms = await getHierarchy(spaceID) + let rooms = await getCachedHierarchy(spaceID) // @ts-ignore rooms = rooms.filter(r => r.name.startsWith(data.options[0].value)) diff --git a/matrix/api.js b/matrix/api.js index 65ec8910..7d8ea9fd 100644 --- a/matrix/api.js +++ b/matrix/api.js @@ -135,6 +135,24 @@ function getHierarchy(roomID, pagination) { return mreq.mreq("GET", path) } +/** + * Like `getHierarchy` but collects all pages for you. + * @param {string} roomID + */ +async function getFullHierarchy(roomID) { + /** @type {Ty.R.Hierarchy[]} */ + let rooms = [] + /** @type {string | undefined} */ + let nextBatch = undefined + do { + /** @type {Ty.HierarchyPagination} */ + const res = await getHierarchy(roomID, {from: nextBatch}) + rooms.push(...res.rooms) + nextBatch = res.next_batch + } while (nextBatch) + return rooms +} + /** * @param {string} roomID * @param {string} eventID @@ -254,6 +272,7 @@ module.exports.getAllState = getAllState module.exports.getStateEvent = getStateEvent module.exports.getJoinedMembers = getJoinedMembers module.exports.getHierarchy = getHierarchy +module.exports.getFullHierarchy = getFullHierarchy module.exports.getRelations = getRelations module.exports.sendState = sendState module.exports.sendEvent = sendEvent