Make a helper for the room hierarchy
This commit is contained in:
parent
89696fd161
commit
3a84658e8b
3 changed files with 31 additions and 27 deletions
|
@ -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<Ty.R.Hierarchy>} */
|
||||
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)
|
||||
|
|
|
@ -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<Ty.R.Hierarchy>} */
|
||||
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))
|
||||
|
||||
|
|
|
@ -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<Ty.R.Hierarchy>} */
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue