|
| 1 | +/* |
| 2 | +Copyright 2021 The Matrix.org Foundation C.I.C. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +/** |
| 18 | + * @module room-hierarchy |
| 19 | + */ |
| 20 | + |
| 21 | +import { Room } from "./models/room"; |
| 22 | +import { IHierarchyRoom, IHierarchyRelation } from "./@types/spaces"; |
| 23 | +import { MatrixClient } from "./client"; |
| 24 | +import { EventType } from "./@types/event"; |
| 25 | + |
| 26 | +export class RoomHierarchy { |
| 27 | + // Map from room id to list of servers which are listed as a via somewhere in the loaded hierarchy |
| 28 | + public readonly viaMap = new Map<string, Set<string>>(); |
| 29 | + // Map from room id to list of rooms which claim this room as their child |
| 30 | + public readonly backRefs = new Map<string, string[]>(); |
| 31 | + // Map from room id to object |
| 32 | + public readonly roomMap = new Map<string, IHierarchyRoom>(); |
| 33 | + private loadRequest: ReturnType<MatrixClient["getRoomHierarchy"]>; |
| 34 | + private nextBatch?: string; |
| 35 | + private _rooms?: IHierarchyRoom[]; |
| 36 | + private serverSupportError?: Error; |
| 37 | + |
| 38 | + /** |
| 39 | + * Construct a new RoomHierarchy |
| 40 | + * |
| 41 | + * A RoomHierarchy instance allows you to easily make use of the /hierarchy API and paginate it. |
| 42 | + * |
| 43 | + * @param {Room} root the root of this hierarchy |
| 44 | + * @param {number} pageSize the maximum number of rooms to return per page, can be overridden per load request. |
| 45 | + * @param {number} maxDepth the maximum depth to traverse the hierarchy to |
| 46 | + * @param {boolean} suggestedOnly whether to only return rooms with suggested=true. |
| 47 | + * @constructor |
| 48 | + */ |
| 49 | + constructor( |
| 50 | + private readonly root: Room, |
| 51 | + private readonly pageSize?: number, |
| 52 | + private readonly maxDepth?: number, |
| 53 | + private readonly suggestedOnly = false, |
| 54 | + ) {} |
| 55 | + |
| 56 | + public get noSupport(): boolean { |
| 57 | + return !!this.serverSupportError; |
| 58 | + } |
| 59 | + |
| 60 | + public get canLoadMore(): boolean { |
| 61 | + return !!this.serverSupportError || !!this.nextBatch || !this._rooms; |
| 62 | + } |
| 63 | + |
| 64 | + public get rooms(): IHierarchyRoom[] { |
| 65 | + return this._rooms; |
| 66 | + } |
| 67 | + |
| 68 | + public async load(pageSize = this.pageSize): Promise<IHierarchyRoom[]> { |
| 69 | + if (this.loadRequest) return this.loadRequest.then(r => r.rooms); |
| 70 | + |
| 71 | + this.loadRequest = this.root.client.getRoomHierarchy( |
| 72 | + this.root.roomId, |
| 73 | + pageSize, |
| 74 | + this.maxDepth, |
| 75 | + this.suggestedOnly, |
| 76 | + this.nextBatch, |
| 77 | + ); |
| 78 | + |
| 79 | + let rooms: IHierarchyRoom[]; |
| 80 | + try { |
| 81 | + ({ rooms, next_batch: this.nextBatch } = await this.loadRequest); |
| 82 | + } catch (e) { |
| 83 | + if (e.errcode === "M_UNRECOGNIZED") { |
| 84 | + this.serverSupportError = e; |
| 85 | + } else { |
| 86 | + throw e; |
| 87 | + } |
| 88 | + |
| 89 | + return []; |
| 90 | + } finally { |
| 91 | + this.loadRequest = null; |
| 92 | + } |
| 93 | + |
| 94 | + if (this._rooms) { |
| 95 | + this._rooms = this._rooms.concat(rooms); |
| 96 | + } else { |
| 97 | + this._rooms = rooms; |
| 98 | + } |
| 99 | + |
| 100 | + rooms.forEach(room => { |
| 101 | + this.roomMap.set(room.room_id, room); |
| 102 | + |
| 103 | + room.children_state.forEach(ev => { |
| 104 | + if (ev.type !== EventType.SpaceChild) return; |
| 105 | + const childRoomId = ev.state_key; |
| 106 | + |
| 107 | + // track backrefs for quicker hierarchy navigation |
| 108 | + if (!this.backRefs.has(childRoomId)) { |
| 109 | + this.backRefs.set(childRoomId, []); |
| 110 | + } |
| 111 | + this.backRefs.get(childRoomId).push(ev.room_id); |
| 112 | + |
| 113 | + // fill viaMap |
| 114 | + if (Array.isArray(ev.content.via)) { |
| 115 | + if (!this.viaMap.has(childRoomId)) { |
| 116 | + this.viaMap.set(childRoomId, new Set()); |
| 117 | + } |
| 118 | + const vias = this.viaMap.get(childRoomId); |
| 119 | + ev.content.via.forEach(via => vias.add(via)); |
| 120 | + } |
| 121 | + }); |
| 122 | + }); |
| 123 | + |
| 124 | + return rooms; |
| 125 | + } |
| 126 | + |
| 127 | + public getRelation(parentId: string, childId: string): IHierarchyRelation { |
| 128 | + return this.roomMap.get(parentId)?.children_state.find(e => e.state_key === childId); |
| 129 | + } |
| 130 | + |
| 131 | + public isSuggested(parentId: string, childId: string): boolean { |
| 132 | + return this.getRelation(parentId, childId)?.content.suggested; |
| 133 | + } |
| 134 | + |
| 135 | + // locally remove a relation as a form of local echo |
| 136 | + public removeRelation(parentId: string, childId: string): void { |
| 137 | + const backRefs = this.backRefs.get(childId); |
| 138 | + if (backRefs?.length === 1) { |
| 139 | + this.backRefs.delete(childId); |
| 140 | + } else if (backRefs?.length) { |
| 141 | + this.backRefs.set(childId, backRefs.filter(ref => ref !== parentId)); |
| 142 | + } |
| 143 | + |
| 144 | + const room = this.roomMap.get(parentId); |
| 145 | + if (room) { |
| 146 | + room.children_state = room.children_state.filter(ev => ev.state_key !== childId); |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments