|
| 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 { IAlgorithm, ITagMap, ITagSortingMap } from "./IAlgorithm"; |
| 18 | +import { Room } from "matrix-js-sdk/src/models/room"; |
| 19 | +import { isNullOrUndefined } from "matrix-js-sdk/src/utils"; |
| 20 | +import { DefaultTagID, TagID } from "../models"; |
| 21 | +import { splitRoomsByMembership } from "../membership"; |
| 22 | + |
| 23 | +/** |
| 24 | + * The determined category of a room. |
| 25 | + */ |
| 26 | +export enum Category { |
| 27 | + /** |
| 28 | + * The room has unread mentions within. |
| 29 | + */ |
| 30 | + Red = "RED", |
| 31 | + /** |
| 32 | + * The room has unread notifications within. Note that these are not unread |
| 33 | + * mentions - they are simply messages which the user has asked to cause a |
| 34 | + * badge count update or push notification. |
| 35 | + */ |
| 36 | + Grey = "GREY", |
| 37 | + /** |
| 38 | + * The room has unread messages within (grey without the badge). |
| 39 | + */ |
| 40 | + Bold = "BOLD", |
| 41 | + /** |
| 42 | + * The room has no relevant unread messages within. |
| 43 | + */ |
| 44 | + Idle = "IDLE", |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * An implementation of the "importance" algorithm for room list sorting. Where |
| 49 | + * the tag sorting algorithm does not interfere, rooms will be ordered into |
| 50 | + * categories of varying importance to the user. Alphabetical sorting does not |
| 51 | + * interfere with this algorithm, however manual ordering does. |
| 52 | + * |
| 53 | + * The importance of a room is defined by the kind of notifications, if any, are |
| 54 | + * present on the room. These are classified internally as Red, Grey, Bold, and |
| 55 | + * Idle. Red rooms have mentions, grey have unread messages, bold is a less noisy |
| 56 | + * version of grey, and idle means all activity has been seen by the user. |
| 57 | + * |
| 58 | + * The algorithm works by monitoring all room changes, including new messages in |
| 59 | + * tracked rooms, to determine if it needs a new category or different placement |
| 60 | + * within the same category. For more information, see the comments contained |
| 61 | + * within the class. |
| 62 | + */ |
| 63 | +export class ImportanceAlgorithm implements IAlgorithm { |
| 64 | + |
| 65 | + // HOW THIS WORKS |
| 66 | + // -------------- |
| 67 | + // |
| 68 | + // This block of comments assumes you've read the README one level higher. |
| 69 | + // You should do that if you haven't already. |
| 70 | + // |
| 71 | + // Tags are fed into the algorithmic functions from the TagManager changes, |
| 72 | + // which cause subsequent updates to the room list itself. Categories within |
| 73 | + // those tags are tracked as index numbers within the array (zero = top), with |
| 74 | + // each sticky room being tracked separately. Internally, the category index |
| 75 | + // can be found from `this.indices[tag][category]` and the sticky room information |
| 76 | + // from `this.stickyRooms[tag]`. |
| 77 | + // |
| 78 | + // Room categories are constantly re-evaluated and tracked in the `this.categorized` |
| 79 | + // object. Note that this doesn't track rooms by category but instead by room ID. |
| 80 | + // The theory is that by knowing the previous position, new desired position, and |
| 81 | + // category indices we can avoid tracking multiple complicated maps in memory. |
| 82 | + // |
| 83 | + // The room list store is always provided with the `this.cached` results, which are |
| 84 | + // updated as needed and not recalculated often. For example, when a room needs to |
| 85 | + // move within a tag, the array in `this.cached` will be spliced instead of iterated. |
| 86 | + |
| 87 | + private cached: ITagMap = {}; |
| 88 | + private sortAlgorithms: ITagSortingMap; |
| 89 | + private rooms: Room[] = []; |
| 90 | + private indices: { |
| 91 | + // @ts-ignore - TS wants this to be a string but we know better than it |
| 92 | + [tag: TagID]: { |
| 93 | + // @ts-ignore - TS wants this to be a string but we know better than it |
| 94 | + [category: Category]: number; // integer |
| 95 | + }; |
| 96 | + } = {}; |
| 97 | + private stickyRooms: { |
| 98 | + // @ts-ignore - TS wants this to be a string but we know better than it |
| 99 | + [tag: TagID]: { |
| 100 | + room?: Room; |
| 101 | + nAbove: number; // integer |
| 102 | + }; |
| 103 | + } = {}; |
| 104 | + private categorized: { |
| 105 | + // @ts-ignore - TS wants this to be a string but we know better than it |
| 106 | + [tag: TagID]: { |
| 107 | + // TODO: Remove note |
| 108 | + // Note: Should in theory be able to only track this by room ID as we'll know |
| 109 | + // the indices of each category and can determine if a category needs changing |
| 110 | + // in the cached list. Could potentially save a bunch of time if we can figure |
| 111 | + // out where a room is supposed to be using offsets, some math, and leaving the |
| 112 | + // array generally alone. |
| 113 | + [roomId: string]: { |
| 114 | + room: Room; |
| 115 | + category: Category; |
| 116 | + }; |
| 117 | + }; |
| 118 | + } = {}; |
| 119 | + |
| 120 | + constructor() { |
| 121 | + console.log("Constructed an ImportanceAlgorithm"); |
| 122 | + } |
| 123 | + |
| 124 | + getOrderedRooms(): ITagMap { |
| 125 | + return this.cached; |
| 126 | + } |
| 127 | + |
| 128 | + async populateTags(tagSortingMap: ITagSortingMap): Promise<any> { |
| 129 | + if (!tagSortingMap) throw new Error(`Map cannot be null or empty`); |
| 130 | + this.sortAlgorithms = tagSortingMap; |
| 131 | + this.setKnownRooms(this.rooms); // regenerate the room lists |
| 132 | + } |
| 133 | + |
| 134 | + handleRoomUpdate(room): Promise<boolean> { |
| 135 | + return undefined; |
| 136 | + } |
| 137 | + |
| 138 | + setKnownRooms(rooms: Room[]): Promise<any> { |
| 139 | + if (isNullOrUndefined(rooms)) throw new Error(`Array of rooms cannot be null`); |
| 140 | + if (!this.sortAlgorithms) throw new Error(`Cannot set known rooms without a tag sorting map`); |
| 141 | + |
| 142 | + this.rooms = rooms; |
| 143 | + |
| 144 | + const newTags = {}; |
| 145 | + for (const tagId in this.sortAlgorithms) { |
| 146 | + // noinspection JSUnfilteredForInLoop |
| 147 | + newTags[tagId] = []; |
| 148 | + } |
| 149 | + |
| 150 | + // If we can avoid doing work, do so. |
| 151 | + if (!rooms.length) { |
| 152 | + this.cached = newTags; |
| 153 | + return; |
| 154 | + } |
| 155 | + |
| 156 | + // TODO: Remove logging |
| 157 | + const memberships = splitRoomsByMembership(rooms); |
| 158 | + console.log({memberships}); |
| 159 | + |
| 160 | + // Step through each room and determine which tags it should be in. |
| 161 | + // We don't care about ordering or sorting here - we're simply organizing things. |
| 162 | + for (const room of rooms) { |
| 163 | + const tags = room.tags; |
| 164 | + let inTag = false; |
| 165 | + for (const tagId in tags) { |
| 166 | + // noinspection JSUnfilteredForInLoop |
| 167 | + if (isNullOrUndefined(newTags[tagId])) { |
| 168 | + // skip the tag if we don't know about it |
| 169 | + continue; |
| 170 | + } |
| 171 | + |
| 172 | + inTag = true; |
| 173 | + |
| 174 | + // noinspection JSUnfilteredForInLoop |
| 175 | + newTags[tagId].push(room); |
| 176 | + } |
| 177 | + |
| 178 | + // If the room wasn't pushed to a tag, push it to the untagged tag. |
| 179 | + if (!inTag) { |
| 180 | + newTags[DefaultTagID.Untagged].push(room); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + // TODO: Do sorting |
| 185 | + |
| 186 | + // Finally, assign the tags to our cache |
| 187 | + this.cached = newTags; |
| 188 | + } |
| 189 | +} |
0 commit comments