1
0
Fork 0

Enumerate child rooms with hierarchy endpoint

This commit is contained in:
Cadence Ember 2024-03-26 01:10:29 +13:00
parent ff7af39802
commit 642be26313
3 changed files with 42 additions and 3 deletions

View file

@ -3,6 +3,7 @@
const assert = require("assert").strict
const {isDeepStrictEqual} = require("util")
const DiscordTypes = require("discord-api-types/v10")
const Ty = require("../../types")
const reg = require("../../matrix/read-registration")
const passthrough = require("../../passthrough")
@ -181,9 +182,16 @@ async function syncSpaceFully(guildID) {
const spaceDiff = ks.diffKState(spaceKState, guildKState)
await createRoom.applyKStateDiffToRoom(spaceID, spaceDiff)
const childRooms = ks.kstateToState(spaceKState).filter(({type, content}) => {
return type === "m.space.child" && "via" in content
}).map(({state_key}) => state_key)
/** @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)
for (const roomID of childRooms) {
const channelID = select("channel_room", "channel_id", {room_id: roomID}).pluck().get()

View file

@ -121,6 +121,19 @@ function getJoinedMembers(roomID) {
return mreq.mreq("GET", `/client/v3/rooms/${roomID}/joined_members`)
}
/**
* @param {string} roomID
* @param {{from?: string, limit?: any}} pagination
* @returns {Promise<Ty.HierarchyPagination<Ty.R.Hierarchy>>}
*/
function getHierarchy(roomID, pagination) {
let path = `/client/v1/rooms/${roomID}/hierarchy`
if (!pagination.from) delete pagination.from
if (!pagination.limit) pagination.limit = 50
path += `?${new URLSearchParams(pagination)}`
return mreq.mreq("GET", path)
}
/**
* @param {string} roomID
* @param {string} eventID
@ -239,6 +252,7 @@ module.exports.getEventForTimestamp = getEventForTimestamp
module.exports.getAllState = getAllState
module.exports.getStateEvent = getStateEvent
module.exports.getJoinedMembers = getJoinedMembers
module.exports.getHierarchy = getHierarchy
module.exports.getRelations = getRelations
module.exports.sendState = sendState
module.exports.sendEvent = sendEvent

17
types.d.ts vendored
View file

@ -257,6 +257,18 @@ export namespace R {
export type EventRedacted = {
event_id: string
}
export type Hierarchy = {
avatar_url?: string
canonical_alias?: string
children_state: {}
guest_can_join: boolean
join_rule?: string
name?: string
num_joined_members: number
room_id: string
room_type?: string
}
}
export type Pagination<T> = {
@ -264,3 +276,8 @@ export type Pagination<T> = {
next_batch?: string
prev_match?: string
}
export type HierarchyPagination<T> = {
rooms: T[]
next_batch?: string
}