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

Commit 1f703b8

Browse files
kegsayt3chguy
andauthored
bugfix: fix an issue where the Notifier would incorrectly fire for non-timeline events (#9664)
* bugfix: fix an issue where the Notifier would incorrectly fire for non-timeline events This was caused by listening for ClientEvent.Event, not RoomEvent.Timeline. Fixes element-hq/element-web#17263 * tsc strict checks maybe * More types? * Update src/Notifier.ts Co-authored-by: Michael Telatynski <[email protected]> * Update src/Notifier.ts * fix LL test; review comments * More tests * More tsc strict checks.. * More strict ts.. * More ts Co-authored-by: Michael Telatynski <[email protected]>
1 parent ccfb455 commit 1f703b8

File tree

2 files changed

+66
-20
lines changed

2 files changed

+66
-20
lines changed

src/Notifier.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
PermissionChanged as PermissionChangedEvent,
2828
} from "@matrix-org/analytics-events/types/typescript/PermissionChanged";
2929
import { ISyncStateData, SyncState } from "matrix-js-sdk/src/sync";
30+
import { IRoomTimelineData } from "matrix-js-sdk/src/matrix";
3031

3132
import { MatrixClientPeg } from './MatrixClientPeg';
3233
import { PosthogAnalytics } from "./PosthogAnalytics";
@@ -217,7 +218,7 @@ export const Notifier = {
217218
this.boundOnRoomReceipt = this.boundOnRoomReceipt || this.onRoomReceipt.bind(this);
218219
this.boundOnEventDecrypted = this.boundOnEventDecrypted || this.onEventDecrypted.bind(this);
219220

220-
MatrixClientPeg.get().on(ClientEvent.Event, this.boundOnEvent);
221+
MatrixClientPeg.get().on(RoomEvent.Timeline, this.boundOnEvent);
221222
MatrixClientPeg.get().on(RoomEvent.Receipt, this.boundOnRoomReceipt);
222223
MatrixClientPeg.get().on(MatrixEventEvent.Decrypted, this.boundOnEventDecrypted);
223224
MatrixClientPeg.get().on(ClientEvent.Sync, this.boundOnSyncStateChange);
@@ -227,7 +228,7 @@ export const Notifier = {
227228

228229
stop: function(this: typeof Notifier) {
229230
if (MatrixClientPeg.get()) {
230-
MatrixClientPeg.get().removeListener(ClientEvent.Event, this.boundOnEvent);
231+
MatrixClientPeg.get().removeListener(RoomEvent.Timeline, this.boundOnEvent);
231232
MatrixClientPeg.get().removeListener(RoomEvent.Receipt, this.boundOnRoomReceipt);
232233
MatrixClientPeg.get().removeListener(MatrixEventEvent.Decrypted, this.boundOnEventDecrypted);
233234
MatrixClientPeg.get().removeListener(ClientEvent.Sync, this.boundOnSyncStateChange);
@@ -368,7 +369,15 @@ export const Notifier = {
368369
}
369370
},
370371

371-
onEvent: function(this: typeof Notifier, ev: MatrixEvent) {
372+
onEvent: function(
373+
this: typeof Notifier,
374+
ev: MatrixEvent,
375+
room: Room | undefined,
376+
toStartOfTimeline: boolean | undefined,
377+
removed: boolean,
378+
data: IRoomTimelineData,
379+
) {
380+
if (!data.liveEvent) return; // only notify for new things, not old.
372381
if (!this.isSyncing) return; // don't alert for any messages initially
373382
if (ev.getSender() === MatrixClientPeg.get().getUserId()) return;
374383

@@ -428,6 +437,11 @@ export const Notifier = {
428437
}
429438
}
430439
const room = MatrixClientPeg.get().getRoom(roomId);
440+
if (!room) {
441+
// e.g we are in the process of joining a room.
442+
// Seen in the cypress lazy-loading test.
443+
return;
444+
}
431445

432446
const actions = MatrixClientPeg.get().getPushActionsForEvent(ev);
433447

test/Notifier-test.ts

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ limitations under the License.
1616

1717
import { mocked, MockedObject } from "jest-mock";
1818
import { ClientEvent, MatrixClient } from "matrix-js-sdk/src/client";
19-
import { Room } from "matrix-js-sdk/src/models/room";
20-
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
19+
import { Room, RoomEvent } from "matrix-js-sdk/src/models/room";
20+
import { IContent, MatrixEvent } from "matrix-js-sdk/src/models/event";
2121
import { SyncState } from "matrix-js-sdk/src/sync";
2222
import { waitFor } from "@testing-library/react";
2323

@@ -60,12 +60,19 @@ describe("Notifier", () => {
6060
let mockClient: MockedObject<MatrixClient>;
6161
let testRoom: Room;
6262
let accountDataEventKey: string;
63-
let accountDataStore = {};
63+
let accountDataStore: Record<string, MatrixEvent | undefined> = {};
6464

6565
let mockSettings: Record<string, boolean> = {};
6666

6767
const userId = "@bob:example.org";
6868

69+
const emitLiveEvent = (event: MatrixEvent): void => {
70+
mockClient!.emit(RoomEvent.Timeline, event, testRoom, false, false, {
71+
liveEvent: true,
72+
timeline: testRoom.getLiveTimeline(),
73+
});
74+
};
75+
6976
beforeEach(() => {
7077
accountDataStore = {};
7178
mockClient = getMockClientWithEventEmitter({
@@ -150,7 +157,7 @@ describe("Notifier", () => {
150157
});
151158

152159
it('does not create notifications before syncing has started', () => {
153-
mockClient!.emit(ClientEvent.Event, event);
160+
emitLiveEvent(event);
154161

155162
expect(MockPlatform.displayNotification).not.toHaveBeenCalled();
156163
expect(MockPlatform.loudNotification).not.toHaveBeenCalled();
@@ -160,7 +167,30 @@ describe("Notifier", () => {
160167
const ownEvent = new MatrixEvent({ sender: userId });
161168

162169
mockClient!.emit(ClientEvent.Sync, SyncState.Syncing, null);
163-
mockClient!.emit(ClientEvent.Event, ownEvent);
170+
emitLiveEvent(ownEvent);
171+
172+
expect(MockPlatform.displayNotification).not.toHaveBeenCalled();
173+
expect(MockPlatform.loudNotification).not.toHaveBeenCalled();
174+
});
175+
176+
it('does not create notifications for non-live events (scrollback)', () => {
177+
mockClient!.emit(ClientEvent.Sync, SyncState.Syncing, null);
178+
mockClient!.emit(RoomEvent.Timeline, event, testRoom, false, false, {
179+
liveEvent: false,
180+
timeline: testRoom.getLiveTimeline(),
181+
});
182+
183+
expect(MockPlatform.displayNotification).not.toHaveBeenCalled();
184+
expect(MockPlatform.loudNotification).not.toHaveBeenCalled();
185+
});
186+
187+
it('does not create notifications for rooms which cannot be obtained via client.getRoom', () => {
188+
mockClient!.emit(ClientEvent.Sync, SyncState.Syncing, null);
189+
mockClient.getRoom.mockReturnValue(null);
190+
mockClient!.emit(RoomEvent.Timeline, event, testRoom, false, false, {
191+
liveEvent: true,
192+
timeline: testRoom.getLiveTimeline(),
193+
});
164194

165195
expect(MockPlatform.displayNotification).not.toHaveBeenCalled();
166196
expect(MockPlatform.loudNotification).not.toHaveBeenCalled();
@@ -175,15 +205,15 @@ describe("Notifier", () => {
175205
});
176206

177207
mockClient!.emit(ClientEvent.Sync, SyncState.Syncing, null);
178-
mockClient!.emit(ClientEvent.Event, event);
208+
emitLiveEvent(event);
179209

180210
expect(MockPlatform.displayNotification).not.toHaveBeenCalled();
181211
expect(MockPlatform.loudNotification).not.toHaveBeenCalled();
182212
});
183213

184214
it('creates desktop notification when enabled', () => {
185215
mockClient!.emit(ClientEvent.Sync, SyncState.Syncing, null);
186-
mockClient!.emit(ClientEvent.Event, event);
216+
emitLiveEvent(event);
187217

188218
expect(MockPlatform.displayNotification).toHaveBeenCalledWith(
189219
testRoom.name,
@@ -196,7 +226,7 @@ describe("Notifier", () => {
196226

197227
it('creates a loud notification when enabled', () => {
198228
mockClient!.emit(ClientEvent.Sync, SyncState.Syncing, null);
199-
mockClient!.emit(ClientEvent.Event, event);
229+
emitLiveEvent(event);
200230

201231
expect(MockPlatform.loudNotification).toHaveBeenCalledWith(
202232
event, testRoom,
@@ -212,7 +242,7 @@ describe("Notifier", () => {
212242
});
213243

214244
mockClient!.emit(ClientEvent.Sync, SyncState.Syncing, null);
215-
mockClient!.emit(ClientEvent.Event, event);
245+
emitLiveEvent(event);
216246

217247
// desktop notification created
218248
expect(MockPlatform.displayNotification).toHaveBeenCalled();
@@ -222,12 +252,13 @@ describe("Notifier", () => {
222252
});
223253

224254
describe("_displayPopupNotification", () => {
225-
it.each([
255+
const testCases: {event: IContent | undefined, count: number}[] = [
226256
{ event: { is_silenced: true }, count: 0 },
227257
{ event: { is_silenced: false }, count: 1 },
228258
{ event: undefined, count: 1 },
229-
])("does not dispatch when notifications are silenced", ({ event, count }) => {
230-
mockClient.setAccountData(accountDataEventKey, event);
259+
];
260+
it.each(testCases)("does not dispatch when notifications are silenced", ({ event, count }) => {
261+
mockClient.setAccountData(accountDataEventKey, event!);
231262
Notifier._displayPopupNotification(testEvent, testRoom);
232263
expect(MockPlatform.displayNotification).toHaveBeenCalledTimes(count);
233264
});
@@ -243,16 +274,17 @@ describe("Notifier", () => {
243274
});
244275

245276
describe("_playAudioNotification", () => {
246-
it.each([
277+
const testCases: {event: IContent | undefined, count: number}[] = [
247278
{ event: { is_silenced: true }, count: 0 },
248279
{ event: { is_silenced: false }, count: 1 },
249280
{ event: undefined, count: 1 },
250-
])("does not dispatch when notifications are silenced", ({ event, count }) => {
281+
];
282+
it.each(testCases)("does not dispatch when notifications are silenced", ({ event, count }) => {
251283
// It's not ideal to only look at whether this function has been called
252284
// but avoids starting to look into DOM stuff
253285
Notifier.getSoundForRoom = jest.fn();
254286

255-
mockClient.setAccountData(accountDataEventKey, event);
287+
mockClient.setAccountData(accountDataEventKey, event!);
256288
Notifier._playAudioNotification(testEvent, testRoom);
257289
expect(Notifier.getSoundForRoom).toHaveBeenCalledTimes(count);
258290
});
@@ -267,7 +299,7 @@ describe("Notifier", () => {
267299
notify: true,
268300
tweaks: {},
269301
});
270-
302+
Notifier.start();
271303
Notifier.onSyncStateChange(SyncState.Syncing);
272304
});
273305

@@ -283,7 +315,7 @@ describe("Notifier", () => {
283315
content: {},
284316
event: true,
285317
});
286-
Notifier.onEvent(callEvent);
318+
emitLiveEvent(callEvent);
287319
return callEvent;
288320
};
289321

0 commit comments

Comments
 (0)