Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 68210b1

Browse files
authored
Merge pull request #5963 from matrix-org/t3chguy/fix/17119
Update space ordering behaviour to match updates in MSC
2 parents 202dfd4 + 2b703e8 commit 68210b1

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

src/components/structures/SpaceRoomDirectory.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import {mediaFromMxc} from "../../customisations/Media";
3939
import InfoTooltip from "../views/elements/InfoTooltip";
4040
import TextWithTooltip from "../views/elements/TextWithTooltip";
4141
import {useStateToggle} from "../../hooks/useStateToggle";
42+
import {getOrder} from "../../stores/SpaceStore";
4243

4344
interface IHierarchyProps {
4445
space: Room;
@@ -254,7 +255,11 @@ export const HierarchyLevel = ({
254255
const space = cli.getRoom(spaceId);
255256
const hasPermissions = space?.currentState.maySendStateEvent(EventType.SpaceChild, cli.getUserId());
256257

257-
const sortedChildren = sortBy([...(relations.get(spaceId)?.values() || [])], ev => ev.content.order || null);
258+
const children = Array.from(relations.get(spaceId)?.values() || []);
259+
const sortedChildren = sortBy(children, ev => {
260+
// XXX: Space Summary API doesn't give the child origin_server_ts but once it does we should use it for sorting
261+
return getOrder(ev.content.order, null, ev.state_key);
262+
});
258263
const [subspaces, childRooms] = sortedChildren.reduce((result, ev: ISpaceSummaryEvent) => {
259264
const roomId = ev.state_key;
260265
if (!rooms.has(roomId)) return result;

src/stores/SpaceStore.tsx

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
import {sortBy, throttle} from "lodash";
17+
import {ListIteratee, Many, sortBy, throttle} from "lodash";
1818
import {EventType, RoomType} from "matrix-js-sdk/src/@types/event";
1919
import {Room} from "matrix-js-sdk/src/models/room";
2020
import {MatrixEvent} from "matrix-js-sdk/src/models/event";
@@ -61,15 +61,18 @@ const partitionSpacesAndRooms = (arr: Room[]): [Room[], Room[]] => { // [spaces,
6161
}, [[], []]);
6262
};
6363

64-
const getOrder = (ev: MatrixEvent): string | null => {
65-
const content = ev.getContent();
66-
if (typeof content.order === "string" && Array.from(content.order).every((c: string) => {
64+
// For sorting space children using a validated `order`, `m.room.create`'s `origin_server_ts`, `room_id`
65+
export const getOrder = (order: string, creationTs: number, roomId: string): Array<Many<ListIteratee<any>>> => {
66+
let validatedOrder: string = null;
67+
68+
if (typeof order === "string" && Array.from(order).every((c: string) => {
6769
const charCode = c.charCodeAt(0);
6870
return charCode >= 0x20 && charCode <= 0x7F;
6971
})) {
70-
return content.order;
72+
validatedOrder = order;
7173
}
72-
return null;
74+
75+
return [validatedOrder, creationTs, roomId];
7376
}
7477

7578
const getRoomFn: FetchRoomFn = (room: Room) => {
@@ -200,9 +203,16 @@ export class SpaceStoreClass extends AsyncStoreWithClient<IState> {
200203
private getChildren(spaceId: string): Room[] {
201204
const room = this.matrixClient?.getRoom(spaceId);
202205
const childEvents = room?.currentState.getStateEvents(EventType.SpaceChild).filter(ev => ev.getContent()?.via);
203-
return sortBy(childEvents, getOrder)
204-
.map(ev => this.matrixClient.getRoom(ev.getStateKey()))
205-
.filter(room => room?.getMyMembership() === "join" || room?.getMyMembership() === "invite") || [];
206+
return sortBy(childEvents, ev => {
207+
const roomId = ev.getStateKey();
208+
const childRoom = this.matrixClient?.getRoom(roomId);
209+
const createTs = childRoom?.currentState.getStateEvents(EventType.RoomCreate, "")?.getTs();
210+
return getOrder(ev.getContent().order, createTs, roomId);
211+
}).map(ev => {
212+
return this.matrixClient.getRoom(ev.getStateKey());
213+
}).filter(room => {
214+
return room?.getMyMembership() === "join" || room?.getMyMembership() === "invite";
215+
}) || [];
206216
}
207217

208218
public getChildRooms(spaceId: string): Room[] {

0 commit comments

Comments
 (0)