From 6f37e8b223cb99fa7a36fab883b5f411e184e2f4 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Thu, 29 Jul 2021 12:16:59 +0100 Subject: [PATCH] Use getChildren instead of getSpaceSummary as MSC2946 has moved on --- .../structures/SpaceRoomDirectory.tsx | 51 ++++++++++--------- src/stores/SpaceStore.tsx | 22 ++++---- test/test-utils.js | 4 +- 3 files changed, 42 insertions(+), 35 deletions(-) diff --git a/src/components/structures/SpaceRoomDirectory.tsx b/src/components/structures/SpaceRoomDirectory.tsx index 038c1df514..195c31e959 100644 --- a/src/components/structures/SpaceRoomDirectory.tsx +++ b/src/components/structures/SpaceRoomDirectory.tsx @@ -18,7 +18,7 @@ import React, { ReactNode, useMemo, useState } from "react"; import { Room } from "matrix-js-sdk/src/models/room"; import { MatrixClient } from "matrix-js-sdk/src/client"; import { EventType, RoomType } from "matrix-js-sdk/src/@types/event"; -import { ISpaceSummaryRoom, ISpaceSummaryEvent } from "matrix-js-sdk/src/@types/spaces"; +import { IRoomChild, IRoomChildState } from "matrix-js-sdk/src/@types/spaces"; import classNames from "classnames"; import { sortBy } from "lodash"; @@ -50,11 +50,11 @@ interface IHierarchyProps { initialText?: string; refreshToken?: any; additionalButtons?: ReactNode; - showRoom(room: ISpaceSummaryRoom, viaServers?: string[], autoJoin?: boolean): void; + showRoom(room: IRoomChild, viaServers?: string[], autoJoin?: boolean): void; } interface ITileProps { - room: ISpaceSummaryRoom; + room: IRoomChild; suggested?: boolean; selected?: boolean; numChildRooms?: number; @@ -205,7 +205,7 @@ const Tile: React.FC = ({ ; }; -export const showRoom = (room: ISpaceSummaryRoom, viaServers?: string[], autoJoin = false) => { +export const showRoom = (room: IRoomChild, viaServers?: string[], autoJoin = false) => { // Don't let the user view a room they won't be able to either peek or join: // fail earlier so they don't have to click back to the directory. if (MatrixClientPeg.get().isGuest()) { @@ -234,8 +234,8 @@ export const showRoom = (room: ISpaceSummaryRoom, viaServers?: string[], autoJoi interface IHierarchyLevelProps { spaceId: string; - rooms: Map; - relations: Map>; + rooms: Map; + relations: Map>; parents: Set; selectedMap?: Map>; onViewRoomClick(roomId: string, autoJoin: boolean): void; @@ -260,7 +260,7 @@ export const HierarchyLevel = ({ // XXX: Space Summary API doesn't give the child origin_server_ts but once it does we should use it for sorting return getChildOrder(ev.content.order, null, ev.state_key); }); - const [subspaces, childRooms] = sortedChildren.reduce((result, ev: ISpaceSummaryEvent) => { + const [subspaces, childRooms] = sortedChildren.reduce((result, ev: IRoomChildState) => { const roomId = ev.state_key; if (!rooms.has(roomId)) return result; result[rooms.get(roomId).room_type === RoomType.Space ? 0 : 1].push(roomId); @@ -318,31 +318,34 @@ export const HierarchyLevel = ({ // mutate argument refreshToken to force a reload export const useSpaceSummary = (cli: MatrixClient, space: Room, refreshToken?: any): [ null, - ISpaceSummaryRoom[], - Map>?, + IRoomChild[], + Map>?, Map>?, Map>?, ] | [Error] => { // TODO pagination return useAsyncMemo(async () => { try { - const data = await cli.getSpaceSummary(space.roomId); + const { rooms } = await cli.getRoomChildren(space.roomId); - const parentChildRelations = new EnhancedMap>(); + const parentChildRelations = new EnhancedMap>(); const childParentRelations = new EnhancedMap>(); const viaMap = new EnhancedMap>(); - data.events.map((ev: ISpaceSummaryEvent) => { - if (ev.type === EventType.SpaceChild) { - parentChildRelations.getOrCreate(ev.room_id, new Map()).set(ev.state_key, ev); - childParentRelations.getOrCreate(ev.state_key, new Set()).add(ev.room_id); - } - if (Array.isArray(ev.content.via)) { - const set = viaMap.getOrCreate(ev.state_key, new Set()); - ev.content.via.forEach(via => set.add(via)); - } + + rooms.forEach(room => { + room.children_state.forEach((ev: IRoomChildState) => { + if (ev.type === EventType.SpaceChild) { + parentChildRelations.getOrCreate(ev.room_id, new Map()).set(ev.state_key, ev); + childParentRelations.getOrCreate(ev.state_key, new Set()).add(ev.room_id); + } + if (Array.isArray(ev.content.via)) { + const set = viaMap.getOrCreate(ev.state_key, new Set()); + ev.content.via.forEach(via => set.add(via)); + } + }); }); - return [null, data.rooms as ISpaceSummaryRoom[], parentChildRelations, viaMap, childParentRelations]; + return [null, rooms, parentChildRelations, viaMap, childParentRelations]; } catch (e) { console.error(e); // TODO return [e]; @@ -370,7 +373,7 @@ export const SpaceHierarchy: React.FC = ({ if (!rooms) return null; const lcQuery = query.toLowerCase().trim(); - const roomsMap = new Map(rooms.map(r => [r.room_id, r])); + const roomsMap = new Map(rooms.map(r => [r.room_id, r])); if (!lcQuery) return roomsMap; const directMatches = rooms.filter(r => { @@ -614,7 +617,7 @@ const SpaceRoomDirectory: React.FC = ({ space, onFinished, initialText } { + showRoom={(room: IRoomChild, viaServers?: string[], autoJoin = false) => { showRoom(room, viaServers, autoJoin); onFinished(); }} @@ -637,6 +640,6 @@ export default SpaceRoomDirectory; // Similar to matrix-react-sdk's MatrixTools.getDisplayAliasForRoom // but works with the objects we get from the public room list -function getDisplayAliasForRoom(room: ISpaceSummaryRoom) { +function getDisplayAliasForRoom(room: IRoomChild) { return getDisplayAliasForAliasSet(room.canonical_alias, room.aliases); } diff --git a/src/stores/SpaceStore.tsx b/src/stores/SpaceStore.tsx index d064b01257..e25ca7e32d 100644 --- a/src/stores/SpaceStore.tsx +++ b/src/stores/SpaceStore.tsx @@ -19,7 +19,7 @@ import { ListIteratee, Many, sortBy, throttle } from "lodash"; import { EventType, RoomType } from "matrix-js-sdk/src/@types/event"; import { Room } from "matrix-js-sdk/src/models/room"; import { MatrixEvent } from "matrix-js-sdk/src/models/event"; -import { ISpaceSummaryRoom } from "matrix-js-sdk/src/@types/spaces"; +import { IRoomChild } from "matrix-js-sdk/src/@types/spaces"; import { JoinRule } from "matrix-js-sdk/src/@types/partials"; import { IRoomCapability } from "matrix-js-sdk/src/client"; @@ -63,7 +63,7 @@ export const UPDATE_INVITED_SPACES = Symbol("invited-spaces"); export const UPDATE_SELECTED_SPACE = Symbol("selected-space"); // Space Room ID/HOME_SPACE will be emitted when a Space's children change -export interface ISuggestedRoom extends ISpaceSummaryRoom { +export interface ISuggestedRoom extends IRoomChild { viaServers: string[]; } @@ -297,18 +297,20 @@ export class SpaceStoreClass extends AsyncStoreWithClient { public fetchSuggestedRooms = async (space: Room, limit = MAX_SUGGESTED_ROOMS): Promise => { try { - const data = await this.matrixClient.getSpaceSummary(space.roomId, 0, true, false, limit); + const { rooms } = await this.matrixClient.getRoomChildren(space.roomId, limit, 1, true); const viaMap = new EnhancedMap>(); - data.events.forEach(ev => { - if (ev.type === EventType.SpaceChild && ev.content.via?.length) { - ev.content.via.forEach(via => { - viaMap.getOrCreate(ev.state_key, new Set()).add(via); - }); - } + rooms.forEach(room => { + room.children_state.forEach(ev => { + if (ev.type === EventType.SpaceChild && ev.content.via?.length) { + ev.content.via.forEach(via => { + viaMap.getOrCreate(ev.state_key, new Set()).add(via); + }); + } + }); }); - return data.rooms.filter(roomInfo => { + return rooms.filter(roomInfo => { return roomInfo.room_type !== RoomType.Space && this.matrixClient.getRoom(roomInfo.room_id)?.getMyMembership() !== "join"; }).map(roomInfo => ({ diff --git a/test/test-utils.js b/test/test-utils.js index 5e29fd242e..8bc357c7b3 100644 --- a/test/test-utils.js +++ b/test/test-utils.js @@ -86,7 +86,9 @@ export function createTestClient() { isCryptoEnabled: () => false, getSpaceSummary: jest.fn().mockReturnValue({ rooms: [], - events: [], + }), + getRoomChildren: jest.fn().mockReturnValue({ + rooms: [], }), // Used by various internal bits we aren't concerned with (yet)