@@ -33,9 +33,16 @@ import Modal from './Modal';
33
33
* }
34
34
*/
35
35
36
+ const MAX_PENDING_ENCRYPTED = 20 ;
37
+
36
38
const Notifier = {
37
39
notifsByRoom : { } ,
38
40
41
+ // A list of event IDs that we've received but need to wait until
42
+ // they're decrypted until we decide whether to notify for them
43
+ // or not
44
+ pendingEncryptedEventIds : [ ] ,
45
+
39
46
notificationMessageForEvent : function ( ev ) {
40
47
return TextForEvent . textForEvent ( ev ) ;
41
48
} ,
@@ -98,8 +105,10 @@ const Notifier = {
98
105
this . boundOnRoomTimeline = this . onRoomTimeline . bind ( this ) ;
99
106
this . boundOnSyncStateChange = this . onSyncStateChange . bind ( this ) ;
100
107
this . boundOnRoomReceipt = this . onRoomReceipt . bind ( this ) ;
108
+ this . boundOnEventDecrypted = this . onEventDecrypted . bind ( this ) ;
101
109
MatrixClientPeg . get ( ) . on ( 'Room.timeline' , this . boundOnRoomTimeline ) ;
102
110
MatrixClientPeg . get ( ) . on ( 'Room.receipt' , this . boundOnRoomReceipt ) ;
111
+ MatrixClientPeg . get ( ) . on ( 'Event.decrypted' , this . boundOnEventDecrypted ) ;
103
112
MatrixClientPeg . get ( ) . on ( "sync" , this . boundOnSyncStateChange ) ;
104
113
this . toolbarHidden = false ;
105
114
this . isSyncing = false ;
@@ -109,6 +118,7 @@ const Notifier = {
109
118
if ( MatrixClientPeg . get ( ) && this . boundOnRoomTimeline ) {
110
119
MatrixClientPeg . get ( ) . removeListener ( 'Room.timeline' , this . boundOnRoomTimeline ) ;
111
120
MatrixClientPeg . get ( ) . removeListener ( 'Room.receipt' , this . boundOnRoomReceipt ) ;
121
+ MatrixClientPeg . get ( ) . removeListener ( 'Event.decrypted' , this . boundOnEventDecrypted ) ;
112
122
MatrixClientPeg . get ( ) . removeListener ( 'sync' , this . boundOnSyncStateChange ) ;
113
123
}
114
124
this . isSyncing = false ;
@@ -244,16 +254,26 @@ const Notifier = {
244
254
if ( ev . sender && ev . sender . userId === MatrixClientPeg . get ( ) . credentials . userId ) return ;
245
255
if ( data . timeline . getTimelineSet ( ) !== room . getUnfilteredTimelineSet ( ) ) return ;
246
256
247
- const actions = MatrixClientPeg . get ( ) . getPushActionsForEvent ( ev ) ;
248
- if ( actions && actions . notify ) {
249
- if ( this . isEnabled ( ) ) {
250
- this . _displayPopupNotification ( ev , room ) ;
251
- }
252
- if ( actions . tweaks . sound && this . isAudioEnabled ( ) ) {
253
- PlatformPeg . get ( ) . loudNotification ( ev , room ) ;
254
- this . _playAudioNotification ( ev , room ) ;
257
+ // If it's an encrypted event and the type is still 'm.room.encrypted',
258
+ // it hasn't yet been decrypted, so wait until it is.
259
+ if ( event . isBeingDecrypted ( ) || event . isDecryptionFailure ( ) ) {
260
+ this . pendingEncryptedEventIds . push ( ev . getId ( ) ) ;
261
+ // don't let the list fill up indefinitely
262
+ while ( this . pendingEncryptedEventIds . length > MAX_PENDING_ENCRYPTED ) {
263
+ this . pendingEncryptedEventIds . shift ( ) ;
255
264
}
265
+ return ;
256
266
}
267
+
268
+ this . _evaluateEvent ( ev ) ;
269
+ } ,
270
+
271
+ onEventDecrypted : function ( ev ) {
272
+ const idx = this . pendingEncryptedEventIds . indexOf ( ev . getId ( ) ) ;
273
+ if ( idx === - 1 ) return ;
274
+
275
+ this . pendingEncryptedEventIds . splice ( idx , 1 ) ;
276
+ this . _evaluateEvent ( ev ) ;
257
277
} ,
258
278
259
279
onRoomReceipt : function ( ev , room ) {
@@ -273,6 +293,20 @@ const Notifier = {
273
293
delete this . notifsByRoom [ room . roomId ] ;
274
294
}
275
295
} ,
296
+
297
+ _evaluateEvent : function ( ev ) {
298
+ const room = MatrixClientPeg . get ( ) . getRoom ( ev . getRoomId ( ) ) ;
299
+ const actions = MatrixClientPeg . get ( ) . getPushActionsForEvent ( ev ) ;
300
+ if ( actions && actions . notify ) {
301
+ if ( this . isEnabled ( ) ) {
302
+ this . _displayPopupNotification ( ev , room ) ;
303
+ }
304
+ if ( actions . tweaks . sound && this . isAudioEnabled ( ) ) {
305
+ PlatformPeg . get ( ) . loudNotification ( ev , room ) ;
306
+ this . _playAudioNotification ( ev , room ) ;
307
+ }
308
+ }
309
+ }
276
310
} ;
277
311
278
312
if ( ! global . mxNotifier ) {
0 commit comments