Make use of MatrixClient::getRoomUpgradeHistory

This commit is contained in:
Michael Telatynski 2021-10-07 13:19:51 +01:00
parent fa800796c7
commit e470d7d030
2 changed files with 17 additions and 34 deletions

View File

@ -49,7 +49,7 @@ import { mediaFromMxc } from "../../customisations/Media";
import InfoTooltip from "../views/elements/InfoTooltip";
import TextWithTooltip from "../views/elements/TextWithTooltip";
import { useStateToggle } from "../../hooks/useStateToggle";
import SpaceStore, { getChildOrder } from "../../stores/SpaceStore";
import { getChildOrder } from "../../stores/SpaceStore";
import AccessibleTooltipButton from "../views/elements/AccessibleTooltipButton";
import { linkifyElement } from "../../HtmlUtils";
import { useDispatcher } from "../../hooks/useDispatcher";
@ -334,8 +334,9 @@ interface IHierarchyLevelProps {
onToggleClick?(parentId: string, childId: string): void;
}
const toLocalRoom = (cli: MatrixClient, room: IHierarchyRoom, upgradedRoomMap: Map<string, string>): IHierarchyRoom => {
const cliRoom = cli.getRoom(SpaceStore.instance.findMostUpgradedVersion(room.room_id, upgradedRoomMap));
const toLocalRoom = (cli: MatrixClient, room: IHierarchyRoom): IHierarchyRoom => {
const history = cli.getRoomUpgradeHistory(room.room_id, true);
const cliRoom = history[history.length - 1];
if (cliRoom) {
return {
...room,
@ -374,11 +375,10 @@ export const HierarchyLevel = ({
return getChildOrder(ev.content.order, ev.origin_server_ts, ev.state_key);
});
const upgradedRoomMap = new Map<string, string>();
const [subspaces, childRooms] = sortedChildren.reduce((result, ev: IHierarchyRelation) => {
const room = hierarchy.roomMap.get(ev.state_key);
if (room && roomSet.has(room)) {
result[room.room_type === RoomType.Space ? 0 : 1].push(toLocalRoom(cli, room, upgradedRoomMap));
result[room.room_type === RoomType.Space ? 0 : 1].push(toLocalRoom(cli, room));
}
return result;
}, [[] as IHierarchyRoom[], [] as IHierarchyRoom[]]);

View File

@ -283,7 +283,8 @@ export class SpaceStoreClass extends AsyncStoreWithClient<IState> {
const createTs = childRoom?.currentState.getStateEvents(EventType.RoomCreate, "")?.getTs();
return getChildOrder(ev.getContent().order, createTs, roomId);
}).map(ev => {
return this.matrixClient.getRoom(this.findMostUpgradedVersion(ev.getStateKey()));
const history = this.matrixClient.getRoomUpgradeHistory(ev.getStateKey(), true);
return history[history.length - 1];
}).filter(room => {
return room?.getMyMembership() === "join" || room?.getMyMembership() === "invite";
}) || [];
@ -452,28 +453,6 @@ export class SpaceStoreClass extends AsyncStoreWithClient<IState> {
this.onRoomsUpdate();
};
// Utility to walk tombstones and find the most updated variant of the given room,
// takes a Map to enable caching of the responses given the recursive nature of the function.
public findMostUpgradedVersion(
roomId: string,
upgradedRoomMap?: Map<string, string>,
seen= new Set<string>(),
): string {
if (seen.has(roomId)) return roomId;
if (upgradedRoomMap?.has(roomId)) return upgradedRoomMap.get(roomId);
const room = this.matrixClient.getRoom(roomId);
const tombstone = room?.currentState.getStateEvents(EventType.RoomTombstone, "");
const replacementRoom = tombstone?.getContent().replacement_room;
if (replacementRoom && this.matrixClient.getRoom(replacementRoom)?.getMyMembership() === "join") {
seen.add(roomId);
const result = this.findMostUpgradedVersion(replacementRoom, upgradedRoomMap);
upgradedRoomMap?.set(roomId, result);
return result;
}
upgradedRoomMap?.set(roomId, roomId);
return roomId;
}
private onRoomsUpdate = throttle(() => {
// TODO resolve some updates as deltas
const visibleRooms = this.matrixClient.getVisibleRooms();
@ -501,7 +480,6 @@ export class SpaceStoreClass extends AsyncStoreWithClient<IState> {
});
});
const upgradedRoomMap = new Map<string, string>();
this.rootSpaces.forEach(s => {
// traverse each space tree in DFS to build up the supersets as you go up,
// reusing results from like subtrees.
@ -514,7 +492,7 @@ export class SpaceStoreClass extends AsyncStoreWithClient<IState> {
}
const [childSpaces, childRooms] = partitionSpacesAndRooms(this.getChildren(spaceId));
const roomIds = new Set(childRooms.map(r => this.findMostUpgradedVersion(r.roomId, upgradedRoomMap)));
const roomIds = new Set(childRooms.map(r => r.roomId));
const space = this.matrixClient?.getRoom(spaceId);
// Add relevant DMs
@ -528,14 +506,19 @@ export class SpaceStoreClass extends AsyncStoreWithClient<IState> {
const newPath = new Set(parentPath).add(spaceId);
childSpaces.forEach(childSpace => {
fn(childSpace.roomId, newPath)?.forEach(roomId => {
roomIds.add(this.findMostUpgradedVersion(roomId, upgradedRoomMap));
roomIds.add(roomId);
});
});
hiddenChildren.get(spaceId)?.forEach(roomId => {
roomIds.add(this.findMostUpgradedVersion(roomId, upgradedRoomMap));
roomIds.add(roomId);
});
this.spaceFilteredRooms.set(spaceId, roomIds);
return roomIds;
// Expand room IDs to all known versions of the given rooms
const expandedRoomIds = new Set(Array.from(roomIds).flatMap(roomId => {
return this.matrixClient.getRoomUpgradeHistory(roomId, true).map(r => r.roomId);
}));
this.spaceFilteredRooms.set(spaceId, expandedRoomIds);
return expandedRoomIds;
};
fn(s.roomId, new Set());