2020-04-29 05:36:42 +03:00
|
|
|
# Room list sorting
|
|
|
|
|
|
|
|
It's so complicated it needs its own README.
|
|
|
|
|
2020-06-12 01:56:12 +03:00
|
|
|
![](img/RoomListStore2.png)
|
2020-06-12 01:52:47 +03:00
|
|
|
|
|
|
|
Legend:
|
2022-12-12 14:24:14 +03:00
|
|
|
|
2020-06-12 01:52:47 +03:00
|
|
|
- Orange = External event.
|
2021-04-01 08:36:36 +03:00
|
|
|
- Purple = Deterministic flow.
|
2020-06-12 01:52:47 +03:00
|
|
|
- Green = Algorithm definition.
|
|
|
|
- Red = Exit condition/point.
|
|
|
|
- Blue = Process definition.
|
|
|
|
|
2020-04-29 05:36:42 +03:00
|
|
|
## Algorithms involved
|
|
|
|
|
|
|
|
There's two main kinds of algorithms involved in the room list store: list ordering and tag sorting.
|
|
|
|
Throughout the code an intentional decision has been made to call them the List Algorithm and Sorting
|
2020-06-12 01:52:47 +03:00
|
|
|
Algorithm respectively. The list algorithm determines the primary ordering of a given tag whereas the
|
|
|
|
tag sorting defines how rooms within that tag get sorted, at the discretion of the list ordering.
|
|
|
|
|
|
|
|
Behaviour of the overall room list (sticky rooms, etc) are determined by the generically-named Algorithm
|
|
|
|
class. Here is where much of the coordination from the room list store is done to figure out which list
|
|
|
|
algorithm to call, instead of having all the logic in the room list store itself.
|
2020-04-29 05:36:42 +03:00
|
|
|
|
|
|
|
Tag sorting is effectively the comparator supplied to the list algorithm. This gives the list algorithm
|
2021-04-01 08:36:36 +03:00
|
|
|
the power to decide when and how to apply the tag sorting, if at all. For example, the importance algorithm,
|
|
|
|
later described in this document, heavily uses the list ordering behaviour to break the tag into categories.
|
2020-06-12 01:52:47 +03:00
|
|
|
Each category then gets sorted by the appropriate tag sorting algorithm.
|
2020-04-29 05:36:42 +03:00
|
|
|
|
|
|
|
### Tag sorting algorithm: Alphabetical
|
|
|
|
|
2020-05-14 22:26:17 +03:00
|
|
|
When used, rooms in a given tag will be sorted alphabetically, where the alphabet's order is a problem
|
|
|
|
for the browser. All we do is a simple string comparison and expect the browser to return something
|
|
|
|
useful.
|
2020-04-29 05:36:42 +03:00
|
|
|
|
|
|
|
### Tag sorting algorithm: Manual
|
|
|
|
|
2021-04-01 08:36:36 +03:00
|
|
|
Manual sorting makes use of the `order` property present on all tags for a room, per the
|
2020-04-29 05:36:42 +03:00
|
|
|
[Matrix specification](https://matrix.org/docs/spec/client_server/r0.6.0#room-tagging). Smaller values
|
|
|
|
of `order` cause rooms to appear closer to the top of the list.
|
|
|
|
|
|
|
|
### Tag sorting algorithm: Recent
|
|
|
|
|
2020-05-14 22:26:17 +03:00
|
|
|
Rooms get ordered by the timestamp of the most recent useful message. Usefulness is yet another algorithm
|
2020-04-29 05:36:42 +03:00
|
|
|
in the room list system which determines whether an event type is capable of bubbling up in the room list.
|
|
|
|
Normally events like room messages, stickers, and room security changes will be considered useful enough
|
|
|
|
to cause a shift in time.
|
|
|
|
|
|
|
|
Note that this is reliant on the event timestamps of the most recent message. Because Matrix is eventually
|
|
|
|
consistent this means that from time to time a room might plummet or skyrocket across the tag due to the
|
|
|
|
timestamp contained within the event (generated server-side by the sender's server).
|
|
|
|
|
|
|
|
### List ordering algorithm: Natural
|
|
|
|
|
|
|
|
This is the easiest of the algorithms to understand because it does essentially nothing. It imposes no
|
|
|
|
behavioural changes over the tag sorting algorithm and is by far the simplest way to order a room list.
|
2020-08-03 18:02:26 +03:00
|
|
|
Historically, it's been the only option in Element and extremely common in most chat applications due to
|
2020-04-29 05:36:42 +03:00
|
|
|
its relative deterministic behaviour.
|
|
|
|
|
|
|
|
### List ordering algorithm: Importance
|
|
|
|
|
|
|
|
On the other end of the spectrum, this is the most complicated algorithm which exists. There's major
|
2020-05-14 22:26:17 +03:00
|
|
|
behavioural changes, and the tag sorting algorithm gets selectively applied depending on circumstances.
|
2020-04-29 05:36:42 +03:00
|
|
|
|
|
|
|
Each tag which is not manually ordered gets split into 4 sections or "categories". Manually ordered tags
|
|
|
|
simply get the manual sorting algorithm applied to them with no further involvement from the importance
|
|
|
|
algorithm. There are 4 categories: Red, Grey, Bold, and Idle. Each has their own definition based off
|
|
|
|
relative (perceived) importance to the user:
|
|
|
|
|
|
|
|
- **Red**: The room has unread mentions waiting for the user.
|
|
|
|
- **Grey**: The room has unread notifications waiting for the user. Notifications are simply unread
|
2020-05-14 22:26:17 +03:00
|
|
|
messages which cause a push notification or badge count. Typically, this is the default as rooms get
|
2020-04-29 05:36:42 +03:00
|
|
|
set to 'All Messages'.
|
|
|
|
- **Bold**: The room has unread messages waiting for the user. Essentially this is a grey room without
|
|
|
|
a badge/notification count (or 'Mentions Only'/'Muted').
|
2021-04-01 08:36:36 +03:00
|
|
|
- **Idle**: No useful (see definition of useful above) activity has occurred in the room since the user
|
2020-05-14 22:26:17 +03:00
|
|
|
last read it.
|
2020-04-29 05:36:42 +03:00
|
|
|
|
2020-05-14 22:26:17 +03:00
|
|
|
Conveniently, each tag gets ordered by those categories as presented: red rooms appear above grey, grey
|
|
|
|
above bold, etc.
|
2020-04-29 05:36:42 +03:00
|
|
|
|
|
|
|
Once the algorithm has determined which rooms belong in which categories, the tag sorting algorithm
|
2020-06-12 01:52:47 +03:00
|
|
|
gets applied to each category in a sub-list fashion. This should result in the red rooms (for example)
|
2021-04-01 08:36:36 +03:00
|
|
|
being sorted alphabetically amongst each other as well as the grey rooms sorted amongst each other, but
|
2020-04-29 05:36:42 +03:00
|
|
|
collectively the tag will be sorted into categories with red being at the top.
|
|
|
|
|
2020-06-12 01:52:47 +03:00
|
|
|
## Sticky rooms
|
2020-06-06 03:44:05 +03:00
|
|
|
|
|
|
|
When the user visits a room, that room becomes 'sticky' in the list, regardless of ordering algorithm.
|
|
|
|
From a code perspective, the underlying algorithm is not aware of a sticky room and instead the base class
|
|
|
|
manages which room is sticky. This is to ensure that all algorithms handle it the same.
|
|
|
|
|
|
|
|
The sticky flag is simply to say it will not move higher or lower down the list while it is active. For
|
|
|
|
example, if using the importance algorithm, the room would naturally become idle once viewed and thus
|
|
|
|
would normally fly down the list out of sight. The sticky room concept instead holds it in place, never
|
|
|
|
letting it fly down until the user moves to another room.
|
|
|
|
|
|
|
|
Only one room can be sticky at a time. Room updates around the sticky room will still hold the sticky
|
|
|
|
room in place. The best example of this is the importance algorithm: if the user has 3 red rooms and
|
|
|
|
selects the middle room, they will see exactly one room above their selection at all times. If they
|
|
|
|
receive another notification which causes the room to move into the topmost position, the room that was
|
|
|
|
above the sticky room will move underneath to allow for the new room to take the top slot, maintaining
|
|
|
|
the sticky room's position.
|
|
|
|
|
2021-04-01 08:36:36 +03:00
|
|
|
Though only applicable to the importance algorithm, the sticky room is not aware of category boundaries
|
|
|
|
and thus the user can see a shift in what kinds of rooms move around their selection. An example would
|
|
|
|
be the user having 4 red rooms, the user selecting the third room (leaving 2 above it), and then having
|
|
|
|
the rooms above it read on another device. This would result in 1 red room and 1 other kind of room
|
2020-06-06 03:44:05 +03:00
|
|
|
above the sticky room as it will try to maintain 2 rooms above the sticky room.
|
2020-04-29 05:36:42 +03:00
|
|
|
|
|
|
|
An exception for the sticky room placement is when there's suddenly not enough rooms to maintain the placement
|
|
|
|
exactly. This typically happens if the user selects a room and leaves enough rooms where it cannot maintain
|
|
|
|
the N required rooms above the sticky room. In this case, the sticky room will simply decrease N as needed.
|
2021-04-01 08:36:36 +03:00
|
|
|
The N value will never increase while selection remains unchanged: adding a bunch of rooms after having
|
2020-04-29 05:36:42 +03:00
|
|
|
put the sticky room in a position where it's had to decrease N will not increase N.
|
|
|
|
|
|
|
|
## Responsibilities of the store
|
|
|
|
|
2021-04-01 08:36:36 +03:00
|
|
|
The store is responsible for the ordering, upkeep, and tracking of all rooms. The room list component simply gets
|
|
|
|
an object containing the tags it needs to worry about and the rooms within. The room list component will
|
|
|
|
decide which tags need rendering (as it commonly filters out empty tags in most cases), and will deal with
|
2020-04-29 05:36:42 +03:00
|
|
|
all kinds of filtering.
|
|
|
|
|
2020-05-29 16:59:06 +03:00
|
|
|
## Filtering
|
|
|
|
|
2021-04-01 08:36:36 +03:00
|
|
|
Filters are provided to the store as condition classes and have two major kinds: Prefilters and Runtime.
|
2020-05-29 16:59:06 +03:00
|
|
|
|
2021-04-01 08:36:36 +03:00
|
|
|
Prefilters flush out rooms which shouldn't appear to the algorithm implementations. Typically this is
|
|
|
|
due to some higher order room list filtering (such as spaces or tags) deliberately exposing a subset of
|
|
|
|
rooms to the user. The algorithm implementations will not see a room being prefiltered out.
|
|
|
|
|
|
|
|
Runtime filters are used for more dynamic filtering, such as the user filtering by room name. These
|
|
|
|
filters are passed along to the algorithm implementations where those implementations decide how and
|
|
|
|
when to apply the filter. In practice, the base `Algorithm` class ends up doing the heavy lifting for
|
|
|
|
optimization reasons.
|
|
|
|
|
|
|
|
The results of runtime filters get cached to avoid needlessly iterating over potentially thousands of
|
|
|
|
rooms, as the old room list store does. When a filter condition changes, it emits an update which (in this
|
|
|
|
case) the `Algorithm` class will pick up and act accordingly. Typically, this also means filtering a
|
2020-05-29 16:59:06 +03:00
|
|
|
minor subset where possible to avoid over-iterating rooms.
|
|
|
|
|
|
|
|
All filter conditions are considered "stable" by the consumers, meaning that the consumer does not
|
|
|
|
expect a change in the condition unless the condition says it has changed. This is intentional to
|
|
|
|
maintain the caching behaviour described above.
|
|
|
|
|
2021-04-01 08:36:36 +03:00
|
|
|
One might ask why we don't just use prefilter conditions for everything, and the answer is one of slight
|
2021-04-03 04:33:16 +03:00
|
|
|
subtlety: in the cases of prefilters we are knowingly exposing the user to a workspace-style UX where
|
2021-04-01 08:36:36 +03:00
|
|
|
room notifications are self-contained within that workspace. Runtime filters tend to not want to affect
|
|
|
|
visible notification counts (as it doesn't want the room header to suddenly be confusing to the user as
|
|
|
|
they type), and occasionally UX like "found 2/12 rooms" is desirable. If prefiltering were used instead,
|
|
|
|
the notification counts would vary while the user was typing and "found 2/12" UX would not be possible.
|
|
|
|
|
2020-04-29 05:36:42 +03:00
|
|
|
## Class breakdowns
|
|
|
|
|
2021-04-01 08:36:36 +03:00
|
|
|
The `RoomListStore` is the major coordinator of various algorithm implementations, which take care
|
|
|
|
of the various `ListAlgorithm` and `SortingAlgorithm` options. The `Algorithm` class is responsible
|
|
|
|
for figuring out which tags get which rooms, as Matrix specifies them as a reverse map: tags get
|
|
|
|
defined on rooms and are not defined as a collection of rooms (unlike how they are presented to the
|
|
|
|
user). Various list-specific utilities are also included, though they are expected to move somewhere
|
|
|
|
more general when needed. For example, the `membership` utilities could easily be moved elsewhere
|
2020-06-12 01:52:47 +03:00
|
|
|
as needed.
|
2020-05-14 22:26:17 +03:00
|
|
|
|
|
|
|
The various bits throughout the room list store should also have jsdoc of some kind to help describe
|
|
|
|
what they do and how they work.
|