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)
|
const spaceDiff = ks.diffKState(spaceKState, guildKState)
|
||||||
await createRoom.applyKStateDiffToRoom(spaceID, spaceDiff)
|
await createRoom.applyKStateDiffToRoom(spaceID, spaceDiff)
|
||||||
|
|
||||||
/** @type {string[]} room IDs */
|
const childRooms = await api.getFullHierarchy(spaceID)
|
||||||
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)
|
|
||||||
|
|
||||||
for (const roomID of childRooms) {
|
for (const {room_id} of childRooms) {
|
||||||
const channelID = select("channel_room", "channel_id", {room_id: roomID}).pluck().get()
|
const channelID = select("channel_room", "channel_id", {room_id}).pluck().get()
|
||||||
if (!channelID) continue
|
if (!channelID) continue
|
||||||
if (discord.channels.has(channelID)) {
|
if (discord.channels.has(channelID)) {
|
||||||
await createRoom.syncRoom(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
|
// Manage adding to the cache
|
||||||
async function getHierarchy(spaceID) {
|
async function getCachedHierarchy(spaceID) {
|
||||||
return cache.get(spaceID) || (() => {
|
return cache.get(spaceID) || (() => {
|
||||||
const entry = (async () => {
|
const entry = (async () => {
|
||||||
|
const result = await api.getFullHierarchy(spaceID)
|
||||||
/** @type {{name: string, value: string}[]} */
|
/** @type {{name: string, value: string}[]} */
|
||||||
let childRooms = []
|
const childRooms = []
|
||||||
/** @type {string | undefined} */
|
for (const room of result) {
|
||||||
let nextBatch = undefined
|
if (room.name) {
|
||||||
do {
|
childRooms.push({name: room.name, value: room.room_id})
|
||||||
/** @type {Ty.HierarchyPagination<Ty.R.Hierarchy>} */
|
reverseCache.set(room.room_id, spaceID)
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
nextBatch = res.next_batch
|
}
|
||||||
} while (nextBatch)
|
|
||||||
return childRooms
|
return childRooms
|
||||||
})()
|
})()
|
||||||
cache.set(spaceID, entry)
|
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
|
// @ts-ignore
|
||||||
rooms = rooms.filter(r => r.name.startsWith(data.options[0].value))
|
rooms = rooms.filter(r => r.name.startsWith(data.options[0].value))
|
||||||
|
|
||||||
|
|
|
@ -135,6 +135,24 @@ function getHierarchy(roomID, pagination) {
|
||||||
return mreq.mreq("GET", path)
|
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} roomID
|
||||||
* @param {string} eventID
|
* @param {string} eventID
|
||||||
|
@ -254,6 +272,7 @@ module.exports.getAllState = getAllState
|
||||||
module.exports.getStateEvent = getStateEvent
|
module.exports.getStateEvent = getStateEvent
|
||||||
module.exports.getJoinedMembers = getJoinedMembers
|
module.exports.getJoinedMembers = getJoinedMembers
|
||||||
module.exports.getHierarchy = getHierarchy
|
module.exports.getHierarchy = getHierarchy
|
||||||
|
module.exports.getFullHierarchy = getFullHierarchy
|
||||||
module.exports.getRelations = getRelations
|
module.exports.getRelations = getRelations
|
||||||
module.exports.sendState = sendState
|
module.exports.sendState = sendState
|
||||||
module.exports.sendEvent = sendEvent
|
module.exports.sendEvent = sendEvent
|
||||||
|
|
Loading…
Reference in a new issue