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

Commit 7c55904

Browse files
committed
WIP: Effective membership sorting
1 parent 09cc554 commit 7c55904

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

src/stores/room-list/algorithms/ChaoticAlgorithm.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { IAlgorithm, ITagMap, ITagSortingMap, ListAlgorithm } from "./IAlgorithm
1818
import { Room } from "matrix-js-sdk/src/models/room";
1919
import { isNullOrUndefined } from "matrix-js-sdk/src/utils";
2020
import { DefaultTagID } from "../models";
21+
import { splitRoomsByMembership } from "../membership";
2122

2223
/**
2324
* A demonstration/temporary algorithm to verify the API surface works.
@@ -65,7 +66,8 @@ export class ChaoticAlgorithm implements IAlgorithm {
6566
}
6667

6768
// TODO: Remove logging
68-
console.log({alg: this.representativeAlgorithm});
69+
const memberships = splitRoomsByMembership(rooms);
70+
console.log({alg: this.representativeAlgorithm, memberships});
6971

7072
// Step through each room and determine which tags it should be in.
7173
// We don't care about ordering or sorting here - we're simply organizing things.

src/stores/room-list/membership.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
Copyright 2020 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+
import {Room} from "matrix-js-sdk/src/models/room";
18+
import {Event} from "matrix-js-sdk/src/models/event";
19+
20+
/**
21+
* Approximation of a membership status for a given room.
22+
*/
23+
export enum EffectiveMembership {
24+
/**
25+
* The user is effectively joined to the room. For example, actually joined
26+
* or knocking on the room (when that becomes possible).
27+
*/
28+
Join = "JOIN",
29+
30+
/**
31+
* The user is effectively invited to the room. Currently this is a direct map
32+
* to the invite membership as no other membership states are effectively
33+
* invites.
34+
*/
35+
Invite = "INVITE",
36+
37+
/**
38+
* The user is effectively no longer in the room. For example, kicked,
39+
* banned, or voluntarily left.
40+
*/
41+
Leave = "LEAVE",
42+
}
43+
44+
export interface MembershipSplit {
45+
// @ts-ignore - TS wants this to be a string key, but we know better.
46+
[state: EffectiveMembership]: Room[];
47+
}
48+
49+
export function splitRoomsByMembership(rooms: Room[]): MembershipSplit {
50+
const split: MembershipSplit = {
51+
[EffectiveMembership.Invite]: [],
52+
[EffectiveMembership.Join]: [],
53+
[EffectiveMembership.Leave]: [],
54+
};
55+
56+
for (const room of rooms) {
57+
split[getEffectiveMembership(room.getMyMembership())].push(room);
58+
}
59+
60+
return split;
61+
}
62+
63+
export function getEffectiveMembership(membership: string): EffectiveMembership {
64+
if (membership === 'invite') {
65+
return EffectiveMembership.Invite;
66+
} else if (membership === 'join') {
67+
// TODO: Do the same for knock? Update docs as needed in the enum.
68+
return EffectiveMembership.Join;
69+
} else {
70+
// Probably a leave, kick, or ban
71+
return EffectiveMembership.Leave;
72+
}
73+
}

0 commit comments

Comments
 (0)