diff --git a/src/Notifier.ts b/src/Notifier.ts index bc7bae71c4a..3d5e5c03a53 100644 --- a/src/Notifier.ts +++ b/src/Notifier.ts @@ -27,6 +27,7 @@ import { PermissionChanged as PermissionChangedEvent, } from "@matrix-org/analytics-events/types/typescript/PermissionChanged"; import { ISyncStateData, SyncState } from "matrix-js-sdk/src/sync"; +import { IRoomTimelineData } from "matrix-js-sdk/src/matrix"; import { MatrixClientPeg } from './MatrixClientPeg'; import { PosthogAnalytics } from "./PosthogAnalytics"; @@ -217,7 +218,7 @@ export const Notifier = { this.boundOnRoomReceipt = this.boundOnRoomReceipt || this.onRoomReceipt.bind(this); this.boundOnEventDecrypted = this.boundOnEventDecrypted || this.onEventDecrypted.bind(this); - MatrixClientPeg.get().on(ClientEvent.Event, this.boundOnEvent); + MatrixClientPeg.get().on(RoomEvent.Timeline, this.boundOnEvent); MatrixClientPeg.get().on(RoomEvent.Receipt, this.boundOnRoomReceipt); MatrixClientPeg.get().on(MatrixEventEvent.Decrypted, this.boundOnEventDecrypted); MatrixClientPeg.get().on(ClientEvent.Sync, this.boundOnSyncStateChange); @@ -227,7 +228,7 @@ export const Notifier = { stop: function(this: typeof Notifier) { if (MatrixClientPeg.get()) { - MatrixClientPeg.get().removeListener(ClientEvent.Event, this.boundOnEvent); + MatrixClientPeg.get().removeListener(RoomEvent.Timeline, this.boundOnEvent); MatrixClientPeg.get().removeListener(RoomEvent.Receipt, this.boundOnRoomReceipt); MatrixClientPeg.get().removeListener(MatrixEventEvent.Decrypted, this.boundOnEventDecrypted); MatrixClientPeg.get().removeListener(ClientEvent.Sync, this.boundOnSyncStateChange); @@ -368,7 +369,12 @@ export const Notifier = { } }, - onEvent: function(this: typeof Notifier, ev: MatrixEvent) { + onEvent: function(this: typeof Notifier, ev: MatrixEvent, room: Room | undefined, + toStartOfTimeline: boolean | undefined, + removed: boolean, + data: IRoomTimelineData, + ) { + if (!data.liveEvent) return; // on notify for new things, not old. if (!this.isSyncing) return; // don't alert for any messages initially if (ev.getSender() === MatrixClientPeg.get().getUserId()) return; diff --git a/test/Notifier-test.ts b/test/Notifier-test.ts index f15e7984267..9cf7b5ce790 100644 --- a/test/Notifier-test.ts +++ b/test/Notifier-test.ts @@ -16,7 +16,7 @@ limitations under the License. import { mocked, MockedObject } from "jest-mock"; import { ClientEvent, MatrixClient } from "matrix-js-sdk/src/client"; -import { Room } from "matrix-js-sdk/src/models/room"; +import { Room, RoomEvent } from "matrix-js-sdk/src/models/room"; import { MatrixEvent } from "matrix-js-sdk/src/models/event"; import { SyncState } from "matrix-js-sdk/src/sync"; import { waitFor } from "@testing-library/react"; @@ -66,6 +66,13 @@ describe("Notifier", () => { const userId = "@bob:example.org"; + const emitLiveEvent = (event: MatrixEvent) => { + mockClient!.emit(RoomEvent.Timeline, event, testRoom, false, false, { + liveEvent: true, + timeline: testRoom.getLiveTimeline(), + }); + }; + beforeEach(() => { accountDataStore = {}; mockClient = getMockClientWithEventEmitter({ @@ -150,7 +157,7 @@ describe("Notifier", () => { }); it('does not create notifications before syncing has started', () => { - mockClient!.emit(ClientEvent.Event, event); + emitLiveEvent(event); expect(MockPlatform.displayNotification).not.toHaveBeenCalled(); expect(MockPlatform.loudNotification).not.toHaveBeenCalled(); @@ -160,7 +167,7 @@ describe("Notifier", () => { const ownEvent = new MatrixEvent({ sender: userId }); mockClient!.emit(ClientEvent.Sync, SyncState.Syncing, null); - mockClient!.emit(ClientEvent.Event, ownEvent); + emitLiveEvent(ownEvent); expect(MockPlatform.displayNotification).not.toHaveBeenCalled(); expect(MockPlatform.loudNotification).not.toHaveBeenCalled(); @@ -175,7 +182,7 @@ describe("Notifier", () => { }); mockClient!.emit(ClientEvent.Sync, SyncState.Syncing, null); - mockClient!.emit(ClientEvent.Event, event); + emitLiveEvent(event); expect(MockPlatform.displayNotification).not.toHaveBeenCalled(); expect(MockPlatform.loudNotification).not.toHaveBeenCalled(); @@ -183,7 +190,7 @@ describe("Notifier", () => { it('creates desktop notification when enabled', () => { mockClient!.emit(ClientEvent.Sync, SyncState.Syncing, null); - mockClient!.emit(ClientEvent.Event, event); + emitLiveEvent(event); expect(MockPlatform.displayNotification).toHaveBeenCalledWith( testRoom.name, @@ -196,7 +203,7 @@ describe("Notifier", () => { it('creates a loud notification when enabled', () => { mockClient!.emit(ClientEvent.Sync, SyncState.Syncing, null); - mockClient!.emit(ClientEvent.Event, event); + emitLiveEvent(event); expect(MockPlatform.loudNotification).toHaveBeenCalledWith( event, testRoom, @@ -212,7 +219,7 @@ describe("Notifier", () => { }); mockClient!.emit(ClientEvent.Sync, SyncState.Syncing, null); - mockClient!.emit(ClientEvent.Event, event); + emitLiveEvent(event); // desktop notification created expect(MockPlatform.displayNotification).toHaveBeenCalled(); @@ -267,7 +274,7 @@ describe("Notifier", () => { notify: true, tweaks: {}, }); - + Notifier.start(); Notifier.onSyncStateChange(SyncState.Syncing); }); @@ -283,7 +290,7 @@ describe("Notifier", () => { content: {}, event: true, }); - Notifier.onEvent(callEvent); + emitLiveEvent(callEvent); return callEvent; };