@@ -9,6 +9,7 @@ import type { Room } from "matrix-js-sdk/src/matrix";
9
9
import { type Sorter , SortingAlgorithm } from "." ;
10
10
import { getLastTs } from "../../../room-list/algorithms/tag-sorting/RecentAlgorithm" ;
11
11
import { RoomNotificationStateStore } from "../../../notifications/RoomNotificationStateStore" ;
12
+ import { DefaultTagID } from "../../../room-list/models" ;
12
13
13
14
export class RecencySorter implements Sorter {
14
15
public constructor ( private myUserId : string ) { }
@@ -19,11 +20,9 @@ export class RecencySorter implements Sorter {
19
20
}
20
21
21
22
public comparator ( roomA : Room , roomB : Room , cache ?: any ) : number {
22
- // Check mute status first; muted rooms should be at the bottom
23
- const isRoomAMuted = RoomNotificationStateStore . instance . getRoomState ( roomA ) . muted ;
24
- const isRoomBMuted = RoomNotificationStateStore . instance . getRoomState ( roomB ) . muted ;
25
- if ( isRoomAMuted && ! isRoomBMuted ) return 1 ;
26
- if ( isRoomBMuted && ! isRoomAMuted ) return - 1 ;
23
+ // First check if the rooms are low priority or muted
24
+ const exceptionalOrdering = this . getScore ( roomA ) - this . getScore ( roomB ) ;
25
+ if ( exceptionalOrdering !== 0 ) return exceptionalOrdering ;
27
26
28
27
// Then check recency; recent rooms should be at the top
29
28
const roomALastTs = this . getTs ( roomA , cache ) ;
@@ -35,6 +34,28 @@ export class RecencySorter implements Sorter {
35
34
return SortingAlgorithm . Recency ;
36
35
}
37
36
37
+ /**
38
+ * This sorter mostly sorts rooms by recency but there are two exceptions:
39
+ * 1. Muted rooms are sorted to the bottom of the list.
40
+ * 2. Low priority rooms are sorted to the bottom of the list but before muted rooms.
41
+ *
42
+ * The following method provides a numerical value that takes care of this
43
+ * exceptional ordering. For two rooms A and B, it works as follows:
44
+ * - If getScore(A) - getScore(B) > 0, A should come after B
45
+ * - If getScore(A) - getScore(B) < 0, A should come before B
46
+ * - If getScore(A) - getScore(B) = 0, no special ordering needed, just use recency
47
+ */
48
+ private getScore ( room : Room ) : number {
49
+ const isLowPriority = ! ! room . tags [ DefaultTagID . LowPriority ] ;
50
+ const isMuted = RoomNotificationStateStore . instance . getRoomState ( room ) . muted ;
51
+ // These constants are chosen so that the following order is maintained:
52
+ // Low priority rooms -> Low priority and muted rooms -> Muted rooms
53
+ if ( isMuted && isLowPriority ) return 5 ;
54
+ else if ( isMuted ) return 10 ;
55
+ else if ( isLowPriority ) return 2 ;
56
+ else return 0 ;
57
+ }
58
+
38
59
private getTs ( room : Room , cache ?: { [ roomId : string ] : number } ) : number {
39
60
const ts = cache ?. [ room . roomId ] ?? getLastTs ( room , this . myUserId ) ;
40
61
if ( cache ) {
0 commit comments