|
| 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