@@ -18,7 +18,7 @@ limitations under the License.
18
18
import { MatrixClient } from "matrix-js-sdk/src/client" ;
19
19
import SettingsStore from "../../settings/SettingsStore" ;
20
20
import { DefaultTagID , OrderedDefaultTagIDs , RoomUpdateCause , TagID } from "./models" ;
21
- import { Algorithm } from "./algorithms/list-ordering/Algorithm" ;
21
+ import { Algorithm , LIST_UPDATED_EVENT } from "./algorithms/list-ordering/Algorithm" ;
22
22
import TagOrderStore from "../TagOrderStore" ;
23
23
import { AsyncStore } from "../AsyncStore" ;
24
24
import { Room } from "matrix-js-sdk/src/models/room" ;
@@ -27,6 +27,8 @@ import { getListAlgorithmInstance } from "./algorithms/list-ordering";
27
27
import { ActionPayload } from "../../dispatcher/payloads" ;
28
28
import defaultDispatcher from "../../dispatcher/dispatcher" ;
29
29
import { readReceiptChangeIsFor } from "../../utils/read-receipts" ;
30
+ import { IFilterCondition } from "./filters/IFilterCondition" ;
31
+ import { TagWatcher } from "./TagWatcher" ;
30
32
31
33
interface IState {
32
34
tagsEnabled ?: boolean ;
@@ -41,11 +43,13 @@ interface IState {
41
43
*/
42
44
export const LISTS_UPDATE_EVENT = "lists_update" ;
43
45
44
- class _RoomListStore extends AsyncStore < ActionPayload > {
45
- private matrixClient : MatrixClient ;
46
+ export class RoomListStore2 extends AsyncStore < ActionPayload > {
47
+ private _matrixClient : MatrixClient ;
46
48
private initialListsGenerated = false ;
47
49
private enabled = false ;
48
50
private algorithm : Algorithm ;
51
+ private filterConditions : IFilterCondition [ ] = [ ] ;
52
+ private tagWatcher = new TagWatcher ( this ) ;
49
53
50
54
private readonly watchedSettings = [
51
55
'RoomList.orderAlphabetically' ,
@@ -65,6 +69,10 @@ class _RoomListStore extends AsyncStore<ActionPayload> {
65
69
return this . algorithm . getOrderedRooms ( ) ;
66
70
}
67
71
72
+ public get matrixClient ( ) : MatrixClient {
73
+ return this . _matrixClient ;
74
+ }
75
+
68
76
// TODO: Remove enabled flag when the old RoomListStore goes away
69
77
private checkEnabled ( ) {
70
78
this . enabled = SettingsStore . isFeatureEnabled ( "feature_new_room_list" ) ;
@@ -96,7 +104,7 @@ class _RoomListStore extends AsyncStore<ActionPayload> {
96
104
this . checkEnabled ( ) ;
97
105
if ( ! this . enabled ) return ;
98
106
99
- this . matrixClient = payload . matrixClient ;
107
+ this . _matrixClient = payload . matrixClient ;
100
108
101
109
// Update any settings here, as some may have happened before we were logically ready.
102
110
console . log ( "Regenerating room lists: Startup" ) ;
@@ -111,7 +119,7 @@ class _RoomListStore extends AsyncStore<ActionPayload> {
111
119
// Reset state without causing updates as the client will have been destroyed
112
120
// and downstream code will throw NPE errors.
113
121
this . reset ( null , true ) ;
114
- this . matrixClient = null ;
122
+ this . _matrixClient = null ;
115
123
this . initialListsGenerated = false ; // we'll want to regenerate them
116
124
}
117
125
@@ -152,8 +160,21 @@ class _RoomListStore extends AsyncStore<ActionPayload> {
152
160
153
161
const roomId = eventPayload . event . getRoomId ( ) ;
154
162
const room = this . matrixClient . getRoom ( roomId ) ;
155
- console . log ( `[RoomListDebug] Live timeline event ${ eventPayload . event . getId ( ) } in ${ roomId } ` ) ;
156
- await this . handleRoomUpdate ( room , RoomUpdateCause . Timeline ) ;
163
+ const tryUpdate = async ( updatedRoom : Room ) => {
164
+ console . log ( `[RoomListDebug] Live timeline event ${ eventPayload . event . getId ( ) } in ${ updatedRoom . roomId } ` ) ;
165
+ await this . handleRoomUpdate ( updatedRoom , RoomUpdateCause . Timeline ) ;
166
+ } ;
167
+ if ( ! room ) {
168
+ console . warn ( `Live timeline event ${ eventPayload . event . getId ( ) } received without associated room` ) ;
169
+ console . warn ( `Queuing failed room update for retry as a result.` ) ;
170
+ setTimeout ( async ( ) => {
171
+ const updatedRoom = this . matrixClient . getRoom ( roomId ) ;
172
+ await tryUpdate ( updatedRoom ) ;
173
+ } , 100 ) ; // 100ms should be enough for the room to show up
174
+ return ;
175
+ } else {
176
+ await tryUpdate ( room ) ;
177
+ }
157
178
} else if ( payload . action === 'MatrixActions.Event.decrypted' ) {
158
179
const eventPayload = ( < any > payload ) ; // TODO: Type out the dispatcher types
159
180
const roomId = eventPayload . event . getRoomId ( ) ;
@@ -171,11 +192,20 @@ class _RoomListStore extends AsyncStore<ActionPayload> {
171
192
// TODO: Update DMs
172
193
console . log ( payload ) ;
173
194
} else if ( payload . action === 'MatrixActions.Room.myMembership' ) {
195
+ // TODO: Improve new room check
196
+ const membershipPayload = ( < any > payload ) ; // TODO: Type out the dispatcher types
197
+ if ( ! membershipPayload . oldMembership && membershipPayload . membership === "join" ) {
198
+ console . log ( `[RoomListDebug] Handling new room ${ membershipPayload . room . roomId } ` ) ;
199
+ await this . algorithm . handleRoomUpdate ( membershipPayload . room , RoomUpdateCause . NewRoom ) ;
200
+ }
201
+
174
202
// TODO: Update room from membership change
175
203
console . log ( payload ) ;
176
204
} else if ( payload . action === 'MatrixActions.Room' ) {
177
- // TODO: Update room from creation/join
178
- console . log ( payload ) ;
205
+ // TODO: Improve new room check
206
+ // const roomPayload = (<any>payload); // TODO: Type out the dispatcher types
207
+ // console.log(`[RoomListDebug] Handling new room ${roomPayload.room.roomId}`);
208
+ // await this.algorithm.handleRoomUpdate(roomPayload.room, RoomUpdateCause.NewRoom);
179
209
} else if ( payload . action === 'view_room' ) {
180
210
// TODO: Update sticky room
181
211
console . log ( payload ) ;
@@ -211,11 +241,22 @@ class _RoomListStore extends AsyncStore<ActionPayload> {
211
241
}
212
242
213
243
private setAlgorithmClass ( ) {
244
+ if ( this . algorithm ) {
245
+ this . algorithm . off ( LIST_UPDATED_EVENT , this . onAlgorithmListUpdated ) ;
246
+ }
214
247
this . algorithm = getListAlgorithmInstance ( this . state . preferredAlgorithm ) ;
248
+ this . algorithm . setFilterConditions ( this . filterConditions ) ;
249
+ this . algorithm . on ( LIST_UPDATED_EVENT , this . onAlgorithmListUpdated ) ;
215
250
}
216
251
252
+ private onAlgorithmListUpdated = ( ) => {
253
+ console . log ( "Underlying algorithm has triggered a list update - refiring" ) ;
254
+ this . emit ( LISTS_UPDATE_EVENT , this ) ;
255
+ } ;
256
+
217
257
private async regenerateAllLists ( ) {
218
258
console . warn ( "Regenerating all room lists" ) ;
259
+
219
260
const tags : ITagSortingMap = { } ;
220
261
for ( const tagId of OrderedDefaultTagIDs ) {
221
262
tags [ tagId ] = this . getSortAlgorithmFor ( tagId ) ;
@@ -234,16 +275,38 @@ class _RoomListStore extends AsyncStore<ActionPayload> {
234
275
235
276
this . emit ( LISTS_UPDATE_EVENT , this ) ;
236
277
}
278
+
279
+ public addFilter ( filter : IFilterCondition ) : void {
280
+ console . log ( "Adding filter condition:" , filter ) ;
281
+ this . filterConditions . push ( filter ) ;
282
+ if ( this . algorithm ) {
283
+ this . algorithm . addFilterCondition ( filter ) ;
284
+ }
285
+ }
286
+
287
+ public removeFilter ( filter : IFilterCondition ) : void {
288
+ console . log ( "Removing filter condition:" , filter ) ;
289
+ const idx = this . filterConditions . indexOf ( filter ) ;
290
+ if ( idx >= 0 ) {
291
+ this . filterConditions . splice ( idx , 1 ) ;
292
+
293
+ if ( this . algorithm ) {
294
+ this . algorithm . removeFilterCondition ( filter ) ;
295
+ }
296
+ }
297
+ }
237
298
}
238
299
239
300
export default class RoomListStore {
240
- private static internalInstance : _RoomListStore ;
301
+ private static internalInstance : RoomListStore2 ;
241
302
242
- public static get instance ( ) : _RoomListStore {
303
+ public static get instance ( ) : RoomListStore2 {
243
304
if ( ! RoomListStore . internalInstance ) {
244
- RoomListStore . internalInstance = new _RoomListStore ( ) ;
305
+ RoomListStore . internalInstance = new RoomListStore2 ( ) ;
245
306
}
246
307
247
308
return RoomListStore . internalInstance ;
248
309
}
249
310
}
311
+
312
+ window . mx_RoomListStore2 = RoomListStore . instance ;
0 commit comments