Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 9398897

Browse files
dbkrrichvdh
authored andcommitted
Fix notifications for encrypted events (#1336)
Remember events that we may need to notify for once decrypted and evaluate them in an Event.decrypted listener.
1 parent 814e08d commit 9398897

File tree

1 file changed

+42
-8
lines changed

1 file changed

+42
-8
lines changed

src/Notifier.js

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,16 @@ import Modal from './Modal';
3333
* }
3434
*/
3535

36+
const MAX_PENDING_ENCRYPTED = 20;
37+
3638
const Notifier = {
3739
notifsByRoom: {},
3840

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+
3946
notificationMessageForEvent: function(ev) {
4047
return TextForEvent.textForEvent(ev);
4148
},
@@ -98,8 +105,10 @@ const Notifier = {
98105
this.boundOnRoomTimeline = this.onRoomTimeline.bind(this);
99106
this.boundOnSyncStateChange = this.onSyncStateChange.bind(this);
100107
this.boundOnRoomReceipt = this.onRoomReceipt.bind(this);
108+
this.boundOnEventDecrypted = this.onEventDecrypted.bind(this);
101109
MatrixClientPeg.get().on('Room.timeline', this.boundOnRoomTimeline);
102110
MatrixClientPeg.get().on('Room.receipt', this.boundOnRoomReceipt);
111+
MatrixClientPeg.get().on('Event.decrypted', this.boundOnEventDecrypted);
103112
MatrixClientPeg.get().on("sync", this.boundOnSyncStateChange);
104113
this.toolbarHidden = false;
105114
this.isSyncing = false;
@@ -109,6 +118,7 @@ const Notifier = {
109118
if (MatrixClientPeg.get() && this.boundOnRoomTimeline) {
110119
MatrixClientPeg.get().removeListener('Room.timeline', this.boundOnRoomTimeline);
111120
MatrixClientPeg.get().removeListener('Room.receipt', this.boundOnRoomReceipt);
121+
MatrixClientPeg.get().removeListener('Event.decrypted', this.boundOnEventDecrypted);
112122
MatrixClientPeg.get().removeListener('sync', this.boundOnSyncStateChange);
113123
}
114124
this.isSyncing = false;
@@ -244,16 +254,26 @@ const Notifier = {
244254
if (ev.sender && ev.sender.userId === MatrixClientPeg.get().credentials.userId) return;
245255
if (data.timeline.getTimelineSet() !== room.getUnfilteredTimelineSet()) return;
246256

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();
255264
}
265+
return;
256266
}
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);
257277
},
258278

259279
onRoomReceipt: function(ev, room) {
@@ -273,6 +293,20 @@ const Notifier = {
273293
delete this.notifsByRoom[room.roomId];
274294
}
275295
},
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+
}
276310
};
277311

278312
if (!global.mxNotifier) {

0 commit comments

Comments
 (0)