Use getChildren instead of getSpaceSummary as MSC2946 has moved on

This commit is contained in:
Michael Telatynski 2021-07-29 12:16:59 +01:00
parent ef8d2e8257
commit 6f37e8b223
3 changed files with 42 additions and 35 deletions

View file

@ -18,7 +18,7 @@ import React, { ReactNode, useMemo, useState } from "react";
import { Room } from "matrix-js-sdk/src/models/room"; import { Room } from "matrix-js-sdk/src/models/room";
import { MatrixClient } from "matrix-js-sdk/src/client"; import { MatrixClient } from "matrix-js-sdk/src/client";
import { EventType, RoomType } from "matrix-js-sdk/src/@types/event"; 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 classNames from "classnames";
import { sortBy } from "lodash"; import { sortBy } from "lodash";
@ -50,11 +50,11 @@ interface IHierarchyProps {
initialText?: string; initialText?: string;
refreshToken?: any; refreshToken?: any;
additionalButtons?: ReactNode; additionalButtons?: ReactNode;
showRoom(room: ISpaceSummaryRoom, viaServers?: string[], autoJoin?: boolean): void; showRoom(room: IRoomChild, viaServers?: string[], autoJoin?: boolean): void;
} }
interface ITileProps { interface ITileProps {
room: ISpaceSummaryRoom; room: IRoomChild;
suggested?: boolean; suggested?: boolean;
selected?: boolean; selected?: boolean;
numChildRooms?: number; numChildRooms?: number;
@ -205,7 +205,7 @@ const Tile: React.FC<ITileProps> = ({
</>; </>;
}; };
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: // 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. // fail earlier so they don't have to click back to the directory.
if (MatrixClientPeg.get().isGuest()) { if (MatrixClientPeg.get().isGuest()) {
@ -234,8 +234,8 @@ export const showRoom = (room: ISpaceSummaryRoom, viaServers?: string[], autoJoi
interface IHierarchyLevelProps { interface IHierarchyLevelProps {
spaceId: string; spaceId: string;
rooms: Map<string, ISpaceSummaryRoom>; rooms: Map<string, IRoomChild>;
relations: Map<string, Map<string, ISpaceSummaryEvent>>; relations: Map<string, Map<string, IRoomChildState>>;
parents: Set<string>; parents: Set<string>;
selectedMap?: Map<string, Set<string>>; selectedMap?: Map<string, Set<string>>;
onViewRoomClick(roomId: string, autoJoin: boolean): void; 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 // 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); 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; const roomId = ev.state_key;
if (!rooms.has(roomId)) return result; if (!rooms.has(roomId)) return result;
result[rooms.get(roomId).room_type === RoomType.Space ? 0 : 1].push(roomId); 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 // mutate argument refreshToken to force a reload
export const useSpaceSummary = (cli: MatrixClient, space: Room, refreshToken?: any): [ export const useSpaceSummary = (cli: MatrixClient, space: Room, refreshToken?: any): [
null, null,
ISpaceSummaryRoom[], IRoomChild[],
Map<string, Map<string, ISpaceSummaryEvent>>?, Map<string, Map<string, IRoomChildState>>?,
Map<string, Set<string>>?, Map<string, Set<string>>?,
Map<string, Set<string>>?, Map<string, Set<string>>?,
] | [Error] => { ] | [Error] => {
// TODO pagination // TODO pagination
return useAsyncMemo(async () => { return useAsyncMemo(async () => {
try { try {
const data = await cli.getSpaceSummary(space.roomId); const { rooms } = await cli.getRoomChildren(space.roomId);
const parentChildRelations = new EnhancedMap<string, Map<string, ISpaceSummaryEvent>>(); const parentChildRelations = new EnhancedMap<string, Map<string, IRoomChildState>>();
const childParentRelations = new EnhancedMap<string, Set<string>>(); const childParentRelations = new EnhancedMap<string, Set<string>>();
const viaMap = new EnhancedMap<string, Set<string>>(); const viaMap = new EnhancedMap<string, Set<string>>();
data.events.map((ev: ISpaceSummaryEvent) => {
if (ev.type === EventType.SpaceChild) { rooms.forEach(room => {
parentChildRelations.getOrCreate(ev.room_id, new Map()).set(ev.state_key, ev); room.children_state.forEach((ev: IRoomChildState) => {
childParentRelations.getOrCreate(ev.state_key, new Set()).add(ev.room_id); if (ev.type === EventType.SpaceChild) {
} parentChildRelations.getOrCreate(ev.room_id, new Map()).set(ev.state_key, ev);
if (Array.isArray(ev.content.via)) { childParentRelations.getOrCreate(ev.state_key, new Set()).add(ev.room_id);
const set = viaMap.getOrCreate(ev.state_key, new Set()); }
ev.content.via.forEach(via => set.add(via)); 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) { } catch (e) {
console.error(e); // TODO console.error(e); // TODO
return [e]; return [e];
@ -370,7 +373,7 @@ export const SpaceHierarchy: React.FC<IHierarchyProps> = ({
if (!rooms) return null; if (!rooms) return null;
const lcQuery = query.toLowerCase().trim(); const lcQuery = query.toLowerCase().trim();
const roomsMap = new Map<string, ISpaceSummaryRoom>(rooms.map(r => [r.room_id, r])); const roomsMap = new Map<string, IRoomChild>(rooms.map(r => [r.room_id, r]));
if (!lcQuery) return roomsMap; if (!lcQuery) return roomsMap;
const directMatches = rooms.filter(r => { const directMatches = rooms.filter(r => {
@ -614,7 +617,7 @@ const SpaceRoomDirectory: React.FC<IProps> = ({ space, onFinished, initialText }
<SpaceHierarchy <SpaceHierarchy
space={space} space={space}
showRoom={(room: ISpaceSummaryRoom, viaServers?: string[], autoJoin = false) => { showRoom={(room: IRoomChild, viaServers?: string[], autoJoin = false) => {
showRoom(room, viaServers, autoJoin); showRoom(room, viaServers, autoJoin);
onFinished(); onFinished();
}} }}
@ -637,6 +640,6 @@ export default SpaceRoomDirectory;
// Similar to matrix-react-sdk's MatrixTools.getDisplayAliasForRoom // Similar to matrix-react-sdk's MatrixTools.getDisplayAliasForRoom
// but works with the objects we get from the public room list // 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); return getDisplayAliasForAliasSet(room.canonical_alias, room.aliases);
} }

View file

@ -19,7 +19,7 @@ import { ListIteratee, Many, sortBy, throttle } from "lodash";
import { EventType, RoomType } from "matrix-js-sdk/src/@types/event"; import { EventType, RoomType } from "matrix-js-sdk/src/@types/event";
import { Room } from "matrix-js-sdk/src/models/room"; import { Room } from "matrix-js-sdk/src/models/room";
import { MatrixEvent } from "matrix-js-sdk/src/models/event"; 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 { JoinRule } from "matrix-js-sdk/src/@types/partials";
import { IRoomCapability } from "matrix-js-sdk/src/client"; 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"); export const UPDATE_SELECTED_SPACE = Symbol("selected-space");
// Space Room ID/HOME_SPACE will be emitted when a Space's children change // 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[]; viaServers: string[];
} }
@ -297,18 +297,20 @@ export class SpaceStoreClass extends AsyncStoreWithClient<IState> {
public fetchSuggestedRooms = async (space: Room, limit = MAX_SUGGESTED_ROOMS): Promise<ISuggestedRoom[]> => { public fetchSuggestedRooms = async (space: Room, limit = MAX_SUGGESTED_ROOMS): Promise<ISuggestedRoom[]> => {
try { 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<string, Set<string>>(); const viaMap = new EnhancedMap<string, Set<string>>();
data.events.forEach(ev => { rooms.forEach(room => {
if (ev.type === EventType.SpaceChild && ev.content.via?.length) { room.children_state.forEach(ev => {
ev.content.via.forEach(via => { if (ev.type === EventType.SpaceChild && ev.content.via?.length) {
viaMap.getOrCreate(ev.state_key, new Set()).add(via); 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 return roomInfo.room_type !== RoomType.Space
&& this.matrixClient.getRoom(roomInfo.room_id)?.getMyMembership() !== "join"; && this.matrixClient.getRoom(roomInfo.room_id)?.getMyMembership() !== "join";
}).map(roomInfo => ({ }).map(roomInfo => ({

View file

@ -86,7 +86,9 @@ export function createTestClient() {
isCryptoEnabled: () => false, isCryptoEnabled: () => false,
getSpaceSummary: jest.fn().mockReturnValue({ getSpaceSummary: jest.fn().mockReturnValue({
rooms: [], rooms: [],
events: [], }),
getRoomChildren: jest.fn().mockReturnValue({
rooms: [],
}), }),
// Used by various internal bits we aren't concerned with (yet) // Used by various internal bits we aren't concerned with (yet)