2022-09-29 22:23:02 +08:00
|
|
|
/*
|
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
2022-10-10 22:18:38 +08:00
|
|
|
import { mocked, MockedObject } from "jest-mock";
|
2023-08-09 15:18:41 +08:00
|
|
|
import {
|
|
|
|
ClientEvent,
|
|
|
|
MatrixClient,
|
|
|
|
Room,
|
|
|
|
RoomEvent,
|
|
|
|
EventType,
|
|
|
|
MsgType,
|
|
|
|
IContent,
|
|
|
|
MatrixEvent,
|
2023-08-10 16:01:14 +08:00
|
|
|
SyncState,
|
2023-08-09 15:18:41 +08:00
|
|
|
} from "matrix-js-sdk/src/matrix";
|
2022-10-26 00:53:31 +08:00
|
|
|
import { waitFor } from "@testing-library/react";
|
2024-07-25 20:49:41 +08:00
|
|
|
import { CallMembership, MatrixRTCSession } from "matrix-js-sdk/src/matrixrtc";
|
2022-09-29 22:23:02 +08:00
|
|
|
|
2022-10-06 22:27:12 +08:00
|
|
|
import BasePlatform from "../src/BasePlatform";
|
2022-09-29 22:23:02 +08:00
|
|
|
import Notifier from "../src/Notifier";
|
2022-10-06 22:27:12 +08:00
|
|
|
import SettingsStore from "../src/settings/SettingsStore";
|
|
|
|
import ToastStore from "../src/stores/ToastStore";
|
2022-10-10 22:18:38 +08:00
|
|
|
import {
|
|
|
|
createLocalNotificationSettingsIfNeeded,
|
|
|
|
getLocalNotificationAccountDataEventType,
|
|
|
|
} from "../src/utils/notifications";
|
2023-07-17 22:04:09 +08:00
|
|
|
import {
|
|
|
|
getMockClientWithEventEmitter,
|
|
|
|
mkEvent,
|
|
|
|
mkMessage,
|
|
|
|
mockClientMethodsUser,
|
|
|
|
mockPlatformPeg,
|
|
|
|
} from "./test-utils";
|
2023-11-22 01:12:08 +08:00
|
|
|
import { getIncomingCallToastKey, IncomingCallToast } from "../src/toasts/IncomingCallToast";
|
2022-10-26 00:53:31 +08:00
|
|
|
import { SdkContextClass } from "../src/contexts/SDKContext";
|
|
|
|
import UserActivity from "../src/UserActivity";
|
|
|
|
import Modal from "../src/Modal";
|
|
|
|
import { mkThread } from "./test-utils/threads";
|
|
|
|
import dis from "../src/dispatcher/dispatcher";
|
|
|
|
import { ThreadPayload } from "../src/dispatcher/payloads/ThreadPayload";
|
|
|
|
import { Action } from "../src/dispatcher/actions";
|
2023-01-17 17:04:36 +08:00
|
|
|
import { VoiceBroadcastChunkEventType, VoiceBroadcastInfoState } from "../src/voice-broadcast";
|
|
|
|
import { mkVoiceBroadcastInfoStateEvent } from "./voice-broadcast/utils/test-utils";
|
2023-07-17 22:04:09 +08:00
|
|
|
import { addReplyToMessageContent } from "../src/utils/Reply";
|
2022-09-29 22:23:02 +08:00
|
|
|
|
2022-10-10 22:18:38 +08:00
|
|
|
jest.mock("../src/utils/notifications", () => ({
|
|
|
|
// @ts-ignore
|
|
|
|
...jest.requireActual("../src/utils/notifications"),
|
|
|
|
createLocalNotificationSettingsIfNeeded: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
2024-07-05 02:08:06 +08:00
|
|
|
jest.mock("../src/audio/compat", () => ({
|
|
|
|
...jest.requireActual("../src/audio/compat"),
|
|
|
|
createAudioContext: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
2022-09-29 22:23:02 +08:00
|
|
|
describe("Notifier", () => {
|
|
|
|
const roomId = "!room1:server";
|
|
|
|
const testEvent = mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: "m.room.message",
|
|
|
|
user: "@user1:server",
|
|
|
|
room: roomId,
|
|
|
|
content: {},
|
|
|
|
});
|
2022-10-06 22:27:12 +08:00
|
|
|
|
|
|
|
let MockPlatform: MockedObject<BasePlatform>;
|
|
|
|
let mockClient: MockedObject<MatrixClient>;
|
2022-10-26 00:53:31 +08:00
|
|
|
let testRoom: Room;
|
2022-10-06 22:27:12 +08:00
|
|
|
let accountDataEventKey: string;
|
2022-12-01 20:21:56 +08:00
|
|
|
let accountDataStore: Record<string, MatrixEvent | undefined> = {};
|
2022-09-29 22:23:02 +08:00
|
|
|
|
2022-10-26 00:53:31 +08:00
|
|
|
let mockSettings: Record<string, boolean> = {};
|
|
|
|
|
2022-10-13 15:07:48 +08:00
|
|
|
const userId = "@bob:example.org";
|
|
|
|
|
2022-12-01 20:21:56 +08:00
|
|
|
const emitLiveEvent = (event: MatrixEvent): void => {
|
|
|
|
mockClient!.emit(RoomEvent.Timeline, event, testRoom, false, false, {
|
|
|
|
liveEvent: true,
|
|
|
|
timeline: testRoom.getLiveTimeline(),
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-01-17 17:04:36 +08:00
|
|
|
const mkAudioEvent = (broadcastChunkContent?: object): MatrixEvent => {
|
|
|
|
const chunkContent = broadcastChunkContent ? { [VoiceBroadcastChunkEventType]: broadcastChunkContent } : {};
|
2023-01-03 17:02:28 +08:00
|
|
|
|
|
|
|
return mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: EventType.RoomMessage,
|
|
|
|
user: "@user:example.com",
|
|
|
|
room: "!room:example.com",
|
|
|
|
content: {
|
|
|
|
...chunkContent,
|
|
|
|
msgtype: MsgType.Audio,
|
|
|
|
body: "test audio message",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2024-07-05 02:08:06 +08:00
|
|
|
const mockAudioBufferSourceNode = {
|
|
|
|
addEventListener: jest.fn(),
|
|
|
|
connect: jest.fn(),
|
|
|
|
start: jest.fn(),
|
|
|
|
};
|
|
|
|
const mockAudioContext = {
|
|
|
|
decodeAudioData: jest.fn(),
|
|
|
|
suspend: jest.fn(),
|
|
|
|
resume: jest.fn(),
|
|
|
|
createBufferSource: jest.fn().mockReturnValue(mockAudioBufferSourceNode),
|
|
|
|
currentTime: 1337,
|
|
|
|
};
|
|
|
|
|
2022-09-29 22:23:02 +08:00
|
|
|
beforeEach(() => {
|
|
|
|
accountDataStore = {};
|
2022-10-06 22:27:12 +08:00
|
|
|
mockClient = getMockClientWithEventEmitter({
|
2022-10-13 15:07:48 +08:00
|
|
|
...mockClientMethodsUser(userId),
|
2022-10-06 22:27:12 +08:00
|
|
|
isGuest: jest.fn().mockReturnValue(false),
|
|
|
|
getAccountData: jest.fn().mockImplementation((eventType) => accountDataStore[eventType]),
|
|
|
|
setAccountData: jest.fn().mockImplementation((eventType, content) => {
|
2022-10-13 15:07:48 +08:00
|
|
|
accountDataStore[eventType] = content
|
|
|
|
? new MatrixEvent({
|
2022-10-06 22:27:12 +08:00
|
|
|
type: eventType,
|
|
|
|
content,
|
2022-10-13 15:07:48 +08:00
|
|
|
})
|
|
|
|
: undefined;
|
2022-10-06 22:27:12 +08:00
|
|
|
}),
|
|
|
|
decryptEventIfNeeded: jest.fn(),
|
|
|
|
getRoom: jest.fn(),
|
|
|
|
getPushActionsForEvent: jest.fn(),
|
2023-01-30 20:20:11 +08:00
|
|
|
supportsThreads: jest.fn().mockReturnValue(false),
|
2024-07-25 17:34:19 +08:00
|
|
|
matrixRTC: {
|
|
|
|
on: jest.fn(),
|
|
|
|
off: jest.fn(),
|
|
|
|
getRoomSession: jest.fn(),
|
|
|
|
},
|
2022-10-06 22:27:12 +08:00
|
|
|
});
|
2022-10-13 15:07:48 +08:00
|
|
|
|
|
|
|
mockClient.pushRules = {
|
2023-01-03 17:16:46 +08:00
|
|
|
global: {},
|
2022-10-13 15:07:48 +08:00
|
|
|
};
|
2023-01-03 17:16:46 +08:00
|
|
|
accountDataEventKey = getLocalNotificationAccountDataEventType(mockClient.deviceId!);
|
2022-10-06 22:27:12 +08:00
|
|
|
|
2023-01-03 17:16:46 +08:00
|
|
|
testRoom = new Room(roomId, mockClient, mockClient.getSafeUserId());
|
2022-10-06 22:27:12 +08:00
|
|
|
|
2022-09-29 22:23:02 +08:00
|
|
|
MockPlatform = mockPlatformPeg({
|
|
|
|
supportsNotifications: jest.fn().mockReturnValue(true),
|
|
|
|
maySendNotifications: jest.fn().mockReturnValue(true),
|
|
|
|
displayNotification: jest.fn(),
|
2022-10-13 15:07:48 +08:00
|
|
|
loudNotification: jest.fn(),
|
2022-09-29 22:23:02 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
Notifier.isBodyEnabled = jest.fn().mockReturnValue(true);
|
2022-10-06 22:27:12 +08:00
|
|
|
|
2023-01-03 17:16:46 +08:00
|
|
|
mockClient.getRoom.mockImplementation((id: string | undefined): Room | null => {
|
|
|
|
if (id === roomId) return testRoom;
|
|
|
|
if (id) return new Room(id, mockClient, mockClient.getSafeUserId());
|
|
|
|
return null;
|
2022-10-26 00:53:31 +08:00
|
|
|
});
|
2024-07-05 02:08:06 +08:00
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
Notifier.backgroundAudio.audioContext = mockAudioContext;
|
2022-09-29 22:23:02 +08:00
|
|
|
});
|
|
|
|
|
2022-10-13 15:07:48 +08:00
|
|
|
describe("triggering notification from events", () => {
|
|
|
|
let hasStartedNotiferBefore = false;
|
|
|
|
|
|
|
|
const event = new MatrixEvent({
|
|
|
|
sender: "@alice:server.org",
|
|
|
|
type: "m.room.message",
|
|
|
|
room_id: "!room:server.org",
|
|
|
|
content: {
|
|
|
|
body: "hey",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
// notifier defines some listener functions in start
|
|
|
|
// and references them in stop
|
|
|
|
// so blows up if stopped before it was started
|
|
|
|
if (hasStartedNotiferBefore) {
|
|
|
|
Notifier.stop();
|
|
|
|
}
|
|
|
|
Notifier.start();
|
|
|
|
hasStartedNotiferBefore = true;
|
|
|
|
mockClient.getRoom.mockReturnValue(testRoom);
|
|
|
|
mockClient.getPushActionsForEvent.mockReturnValue({
|
|
|
|
notify: true,
|
|
|
|
tweaks: {
|
|
|
|
sound: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-10-26 00:53:31 +08:00
|
|
|
mockSettings = {
|
|
|
|
notificationsEnabled: true,
|
|
|
|
audioNotificationsEnabled: true,
|
|
|
|
};
|
|
|
|
|
2022-10-13 15:07:48 +08:00
|
|
|
// enable notifications by default
|
2022-10-26 00:53:31 +08:00
|
|
|
jest.spyOn(SettingsStore, "getValue")
|
|
|
|
.mockReset()
|
|
|
|
.mockImplementation((settingName) => mockSettings[settingName] ?? false);
|
2022-10-13 15:07:48 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
Notifier.stop();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("does not create notifications before syncing has started", () => {
|
2022-12-01 20:21:56 +08:00
|
|
|
emitLiveEvent(event);
|
2022-10-13 15:07:48 +08:00
|
|
|
|
|
|
|
expect(MockPlatform.displayNotification).not.toHaveBeenCalled();
|
|
|
|
expect(MockPlatform.loudNotification).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("does not create notifications for own event", () => {
|
|
|
|
const ownEvent = new MatrixEvent({ sender: userId });
|
|
|
|
|
2022-11-04 18:48:08 +08:00
|
|
|
mockClient!.emit(ClientEvent.Sync, SyncState.Syncing, null);
|
2022-12-01 20:21:56 +08:00
|
|
|
emitLiveEvent(ownEvent);
|
|
|
|
|
|
|
|
expect(MockPlatform.displayNotification).not.toHaveBeenCalled();
|
|
|
|
expect(MockPlatform.loudNotification).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("does not create notifications for non-live events (scrollback)", () => {
|
|
|
|
mockClient!.emit(ClientEvent.Sync, SyncState.Syncing, null);
|
|
|
|
mockClient!.emit(RoomEvent.Timeline, event, testRoom, false, false, {
|
|
|
|
liveEvent: false,
|
|
|
|
timeline: testRoom.getLiveTimeline(),
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(MockPlatform.displayNotification).not.toHaveBeenCalled();
|
|
|
|
expect(MockPlatform.loudNotification).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("does not create notifications for rooms which cannot be obtained via client.getRoom", () => {
|
|
|
|
mockClient!.emit(ClientEvent.Sync, SyncState.Syncing, null);
|
|
|
|
mockClient.getRoom.mockReturnValue(null);
|
|
|
|
mockClient!.emit(RoomEvent.Timeline, event, testRoom, false, false, {
|
|
|
|
liveEvent: true,
|
|
|
|
timeline: testRoom.getLiveTimeline(),
|
|
|
|
});
|
2022-10-13 15:07:48 +08:00
|
|
|
|
|
|
|
expect(MockPlatform.displayNotification).not.toHaveBeenCalled();
|
|
|
|
expect(MockPlatform.loudNotification).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("does not create notifications when event does not have notify push action", () => {
|
|
|
|
mockClient.getPushActionsForEvent.mockReturnValue({
|
|
|
|
notify: false,
|
|
|
|
tweaks: {
|
|
|
|
sound: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-11-04 18:48:08 +08:00
|
|
|
mockClient!.emit(ClientEvent.Sync, SyncState.Syncing, null);
|
2022-12-01 20:21:56 +08:00
|
|
|
emitLiveEvent(event);
|
2022-10-13 15:07:48 +08:00
|
|
|
|
|
|
|
expect(MockPlatform.displayNotification).not.toHaveBeenCalled();
|
|
|
|
expect(MockPlatform.loudNotification).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("creates desktop notification when enabled", () => {
|
2022-11-04 18:48:08 +08:00
|
|
|
mockClient!.emit(ClientEvent.Sync, SyncState.Syncing, null);
|
2022-12-01 20:21:56 +08:00
|
|
|
emitLiveEvent(event);
|
2022-10-13 15:07:48 +08:00
|
|
|
|
|
|
|
expect(MockPlatform.displayNotification).toHaveBeenCalledWith(testRoom.name, "hey", null, testRoom, event);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("creates a loud notification when enabled", () => {
|
2022-11-04 18:48:08 +08:00
|
|
|
mockClient!.emit(ClientEvent.Sync, SyncState.Syncing, null);
|
2022-12-01 20:21:56 +08:00
|
|
|
emitLiveEvent(event);
|
2022-10-13 15:07:48 +08:00
|
|
|
|
|
|
|
expect(MockPlatform.loudNotification).toHaveBeenCalledWith(event, testRoom);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("does not create loud notification when event does not have sound tweak in push actions", () => {
|
|
|
|
mockClient.getPushActionsForEvent.mockReturnValue({
|
|
|
|
notify: true,
|
|
|
|
tweaks: {
|
|
|
|
sound: false,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-11-04 18:48:08 +08:00
|
|
|
mockClient!.emit(ClientEvent.Sync, SyncState.Syncing, null);
|
2022-12-01 20:21:56 +08:00
|
|
|
emitLiveEvent(event);
|
2022-10-13 15:07:48 +08:00
|
|
|
|
|
|
|
// desktop notification created
|
|
|
|
expect(MockPlatform.displayNotification).toHaveBeenCalled();
|
|
|
|
// without noisy
|
|
|
|
expect(MockPlatform.loudNotification).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-02-13 19:39:16 +08:00
|
|
|
describe("displayPopupNotification", () => {
|
2022-12-01 20:21:56 +08:00
|
|
|
const testCases: { event: IContent | undefined; count: number }[] = [
|
2022-10-13 15:07:48 +08:00
|
|
|
{ event: { is_silenced: true }, count: 0 },
|
|
|
|
{ event: { is_silenced: false }, count: 1 },
|
|
|
|
{ event: undefined, count: 1 },
|
2022-12-01 20:21:56 +08:00
|
|
|
];
|
|
|
|
it.each(testCases)("does not dispatch when notifications are silenced", ({ event, count }) => {
|
|
|
|
mockClient.setAccountData(accountDataEventKey, event!);
|
2023-02-13 19:39:16 +08:00
|
|
|
Notifier.displayPopupNotification(testEvent, testRoom);
|
2022-09-29 22:23:02 +08:00
|
|
|
expect(MockPlatform.displayNotification).toHaveBeenCalledTimes(count);
|
|
|
|
});
|
2023-01-03 17:02:28 +08:00
|
|
|
|
|
|
|
it("should display a notification for a voice message", () => {
|
|
|
|
const audioEvent = mkAudioEvent();
|
2023-02-13 19:39:16 +08:00
|
|
|
Notifier.displayPopupNotification(audioEvent, testRoom);
|
2023-01-03 17:02:28 +08:00
|
|
|
expect(MockPlatform.displayNotification).toHaveBeenCalledWith(
|
|
|
|
"@user:example.com (!room1:server)",
|
|
|
|
"@user:example.com: test audio message",
|
|
|
|
"data:image/png;base64,00",
|
|
|
|
testRoom,
|
|
|
|
audioEvent,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2023-01-17 17:04:36 +08:00
|
|
|
it("should display the expected notification for a broadcast chunk with sequence = 1", () => {
|
|
|
|
const audioEvent = mkAudioEvent({ sequence: 1 });
|
2023-02-13 19:39:16 +08:00
|
|
|
Notifier.displayPopupNotification(audioEvent, testRoom);
|
2023-01-17 17:04:36 +08:00
|
|
|
expect(MockPlatform.displayNotification).toHaveBeenCalledWith(
|
|
|
|
"@user:example.com (!room1:server)",
|
|
|
|
"@user:example.com started a voice broadcast",
|
|
|
|
"data:image/png;base64,00",
|
|
|
|
testRoom,
|
|
|
|
audioEvent,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2023-03-01 23:23:35 +08:00
|
|
|
it("should display the expected notification for a broadcast chunk with sequence = 2", () => {
|
2023-01-17 17:04:36 +08:00
|
|
|
const audioEvent = mkAudioEvent({ sequence: 2 });
|
2023-02-13 19:39:16 +08:00
|
|
|
Notifier.displayPopupNotification(audioEvent, testRoom);
|
2023-01-03 17:02:28 +08:00
|
|
|
expect(MockPlatform.displayNotification).not.toHaveBeenCalled();
|
|
|
|
});
|
2023-07-17 22:04:09 +08:00
|
|
|
|
|
|
|
it("should strip reply fallback", () => {
|
|
|
|
const event = mkMessage({
|
|
|
|
msg: "Test",
|
|
|
|
event: true,
|
|
|
|
user: mockClient.getSafeUserId(),
|
|
|
|
room: testRoom.roomId,
|
|
|
|
});
|
|
|
|
const reply = mkMessage({
|
|
|
|
msg: "This was a triumph",
|
|
|
|
event: true,
|
|
|
|
user: mockClient.getSafeUserId(),
|
|
|
|
room: testRoom.roomId,
|
|
|
|
});
|
|
|
|
addReplyToMessageContent(reply.getContent(), event, { includeLegacyFallback: true });
|
|
|
|
Notifier.displayPopupNotification(reply, testRoom);
|
|
|
|
expect(MockPlatform.displayNotification).toHaveBeenCalledWith(
|
|
|
|
"@bob:example.org (!room1:server)",
|
|
|
|
"This was a triumph",
|
|
|
|
expect.any(String),
|
|
|
|
testRoom,
|
|
|
|
reply,
|
|
|
|
);
|
|
|
|
});
|
2022-09-29 22:23:02 +08:00
|
|
|
});
|
|
|
|
|
2022-11-07 19:56:43 +08:00
|
|
|
describe("getSoundForRoom", () => {
|
|
|
|
it("should not explode if given invalid url", () => {
|
|
|
|
jest.spyOn(SettingsStore, "getValue").mockImplementation((name: string) => {
|
|
|
|
return { url: { content_uri: "foobar" } };
|
|
|
|
});
|
|
|
|
expect(Notifier.getSoundForRoom("!roomId:server")).toBeNull();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-09-29 22:23:02 +08:00
|
|
|
describe("_playAudioNotification", () => {
|
2022-12-01 20:21:56 +08:00
|
|
|
const testCases: { event: IContent | undefined; count: number }[] = [
|
2022-10-13 15:07:48 +08:00
|
|
|
{ event: { is_silenced: true }, count: 0 },
|
|
|
|
{ event: { is_silenced: false }, count: 1 },
|
|
|
|
{ event: undefined, count: 1 },
|
2022-12-01 20:21:56 +08:00
|
|
|
];
|
|
|
|
it.each(testCases)("does not dispatch when notifications are silenced", ({ event, count }) => {
|
2022-09-29 22:23:02 +08:00
|
|
|
// It's not ideal to only look at whether this function has been called
|
|
|
|
// but avoids starting to look into DOM stuff
|
|
|
|
Notifier.getSoundForRoom = jest.fn();
|
|
|
|
|
2022-12-01 20:21:56 +08:00
|
|
|
mockClient.setAccountData(accountDataEventKey, event!);
|
2023-02-13 19:39:16 +08:00
|
|
|
Notifier.playAudioNotification(testEvent, testRoom);
|
2022-09-29 22:23:02 +08:00
|
|
|
expect(Notifier.getSoundForRoom).toHaveBeenCalledTimes(count);
|
|
|
|
});
|
|
|
|
});
|
2022-10-06 22:27:12 +08:00
|
|
|
|
|
|
|
describe("group call notifications", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.spyOn(SettingsStore, "getValue").mockReturnValue(true);
|
|
|
|
jest.spyOn(ToastStore.sharedInstance(), "addOrReplaceToast");
|
|
|
|
|
|
|
|
mockClient.getPushActionsForEvent.mockReturnValue({
|
|
|
|
notify: true,
|
|
|
|
tweaks: {},
|
|
|
|
});
|
2022-12-01 20:21:56 +08:00
|
|
|
Notifier.start();
|
2023-02-15 21:36:22 +08:00
|
|
|
Notifier.onSyncStateChange(SyncState.Syncing, null);
|
2022-10-06 22:27:12 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
jest.resetAllMocks();
|
|
|
|
});
|
|
|
|
|
2023-11-22 01:12:08 +08:00
|
|
|
const emitCallNotifyEvent = (type?: string, roomMention = true) => {
|
2022-10-26 00:53:31 +08:00
|
|
|
const callEvent = mkEvent({
|
2023-11-22 01:12:08 +08:00
|
|
|
type: type ?? EventType.CallNotify,
|
2022-10-26 00:53:31 +08:00
|
|
|
user: "@alice:foo",
|
|
|
|
room: roomId,
|
2023-11-22 01:12:08 +08:00
|
|
|
content: {
|
|
|
|
"application": "m.call",
|
|
|
|
"m.mentions": { user_ids: [], room: roomMention },
|
|
|
|
"notify_type": "ring",
|
|
|
|
"call_id": "abc123",
|
|
|
|
},
|
2022-10-26 00:53:31 +08:00
|
|
|
event: true,
|
|
|
|
});
|
2022-12-01 20:21:56 +08:00
|
|
|
emitLiveEvent(callEvent);
|
2022-10-06 22:27:12 +08:00
|
|
|
return callEvent;
|
|
|
|
};
|
|
|
|
|
|
|
|
const setGroupCallsEnabled = (val: boolean) => {
|
|
|
|
jest.spyOn(SettingsStore, "getValue").mockImplementation((name: string) => {
|
|
|
|
if (name === "feature_group_calls") return val;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
it("should show toast when group calls are supported", () => {
|
|
|
|
setGroupCallsEnabled(true);
|
|
|
|
|
2023-11-22 01:12:08 +08:00
|
|
|
const notifyEvent = emitCallNotifyEvent();
|
2022-10-06 22:27:12 +08:00
|
|
|
|
|
|
|
expect(ToastStore.sharedInstance().addOrReplaceToast).toHaveBeenCalledWith(
|
|
|
|
expect.objectContaining({
|
2023-11-22 01:12:08 +08:00
|
|
|
key: getIncomingCallToastKey(notifyEvent.getContent().call_id ?? "", roomId),
|
2022-10-06 22:27:12 +08:00
|
|
|
priority: 100,
|
|
|
|
component: IncomingCallToast,
|
|
|
|
bodyClassName: "mx_IncomingCallToast",
|
2023-11-22 01:12:08 +08:00
|
|
|
props: { notifyEvent },
|
2022-10-06 22:27:12 +08:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not show toast when group calls are not supported", () => {
|
|
|
|
setGroupCallsEnabled(false);
|
|
|
|
|
2023-11-22 01:12:08 +08:00
|
|
|
emitCallNotifyEvent();
|
2022-10-06 22:27:12 +08:00
|
|
|
|
|
|
|
expect(ToastStore.sharedInstance().addOrReplaceToast).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2024-07-25 17:34:19 +08:00
|
|
|
it("should not show toast when group call is already connected", () => {
|
|
|
|
setGroupCallsEnabled(true);
|
|
|
|
const spyCallMemberships = jest.spyOn(MatrixRTCSession, "callMembershipsForRoom").mockReturnValue([
|
|
|
|
new CallMembership(
|
|
|
|
mkEvent({
|
|
|
|
event: true,
|
|
|
|
room: testRoom.roomId,
|
|
|
|
user: userId,
|
|
|
|
type: EventType.GroupCallMemberPrefix,
|
|
|
|
content: {},
|
|
|
|
}),
|
|
|
|
{
|
|
|
|
call_id: "123",
|
|
|
|
application: "m.call",
|
|
|
|
focus_active: { type: "livekit" },
|
|
|
|
foci_preferred: [],
|
|
|
|
device_id: "DEVICE",
|
|
|
|
},
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
|
|
|
|
const roomSession = MatrixRTCSession.roomSessionForRoom(mockClient, testRoom);
|
|
|
|
|
|
|
|
mockClient.matrixRTC.getRoomSession.mockReturnValue(roomSession);
|
|
|
|
emitCallNotifyEvent();
|
|
|
|
expect(ToastStore.sharedInstance().addOrReplaceToast).not.toHaveBeenCalled();
|
|
|
|
spyCallMemberships.mockRestore();
|
|
|
|
});
|
|
|
|
|
2022-10-06 22:27:12 +08:00
|
|
|
it("should not show toast when calling with non-group call event", () => {
|
|
|
|
setGroupCallsEnabled(true);
|
|
|
|
|
2023-11-22 01:12:08 +08:00
|
|
|
emitCallNotifyEvent("event_type");
|
2022-10-06 22:27:12 +08:00
|
|
|
|
|
|
|
expect(ToastStore.sharedInstance().addOrReplaceToast).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
2022-10-10 22:18:38 +08:00
|
|
|
|
|
|
|
describe("local notification settings", () => {
|
|
|
|
const createLocalNotificationSettingsIfNeededMock = mocked(createLocalNotificationSettingsIfNeeded);
|
|
|
|
let hasStartedNotiferBefore = false;
|
|
|
|
beforeEach(() => {
|
|
|
|
// notifier defines some listener functions in start
|
|
|
|
// and references them in stop
|
|
|
|
// so blows up if stopped before it was started
|
|
|
|
if (hasStartedNotiferBefore) {
|
|
|
|
Notifier.stop();
|
|
|
|
}
|
|
|
|
Notifier.start();
|
|
|
|
hasStartedNotiferBefore = true;
|
|
|
|
createLocalNotificationSettingsIfNeededMock.mockClear();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
Notifier.stop();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("does not create local notifications event after a sync error", () => {
|
|
|
|
mockClient.emit(ClientEvent.Sync, SyncState.Error, SyncState.Syncing);
|
|
|
|
expect(createLocalNotificationSettingsIfNeededMock).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("does not create local notifications event after sync stops", () => {
|
|
|
|
mockClient.emit(ClientEvent.Sync, SyncState.Stopped, SyncState.Syncing);
|
|
|
|
expect(createLocalNotificationSettingsIfNeededMock).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("does not create local notifications event after a cached sync", () => {
|
|
|
|
mockClient.emit(ClientEvent.Sync, SyncState.Syncing, SyncState.Syncing, {
|
|
|
|
fromCache: true,
|
|
|
|
});
|
|
|
|
expect(createLocalNotificationSettingsIfNeededMock).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("creates local notifications event after a non-cached sync", () => {
|
|
|
|
mockClient.emit(ClientEvent.Sync, SyncState.Syncing, SyncState.Syncing, {});
|
|
|
|
expect(createLocalNotificationSettingsIfNeededMock).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
2022-10-26 00:53:31 +08:00
|
|
|
|
2023-02-13 19:39:16 +08:00
|
|
|
describe("evaluateEvent", () => {
|
2022-10-26 00:53:31 +08:00
|
|
|
beforeEach(() => {
|
|
|
|
jest.spyOn(SdkContextClass.instance.roomViewStore, "getRoomId").mockReturnValue(testRoom.roomId);
|
|
|
|
|
|
|
|
jest.spyOn(UserActivity.sharedInstance(), "userActiveRecently").mockReturnValue(true);
|
|
|
|
|
|
|
|
jest.spyOn(Modal, "hasDialogs").mockReturnValue(false);
|
|
|
|
|
2023-02-13 19:39:16 +08:00
|
|
|
jest.spyOn(Notifier, "displayPopupNotification").mockReset();
|
2022-10-26 00:53:31 +08:00
|
|
|
jest.spyOn(Notifier, "isEnabled").mockReturnValue(true);
|
|
|
|
|
|
|
|
mockClient.getPushActionsForEvent.mockReturnValue({
|
|
|
|
notify: true,
|
|
|
|
tweaks: {
|
|
|
|
sound: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should show a pop-up", () => {
|
2023-02-13 19:39:16 +08:00
|
|
|
expect(Notifier.displayPopupNotification).toHaveBeenCalledTimes(0);
|
|
|
|
Notifier.evaluateEvent(testEvent);
|
|
|
|
expect(Notifier.displayPopupNotification).toHaveBeenCalledTimes(0);
|
2022-10-26 00:53:31 +08:00
|
|
|
|
|
|
|
const eventFromOtherRoom = mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: "m.room.message",
|
|
|
|
user: "@user1:server",
|
|
|
|
room: "!otherroom:example.org",
|
|
|
|
content: {},
|
|
|
|
});
|
|
|
|
|
2023-02-13 19:39:16 +08:00
|
|
|
Notifier.evaluateEvent(eventFromOtherRoom);
|
|
|
|
expect(Notifier.displayPopupNotification).toHaveBeenCalledTimes(1);
|
2022-10-26 00:53:31 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should a pop-up for thread event", async () => {
|
|
|
|
const { events, rootEvent } = mkThread({
|
|
|
|
room: testRoom,
|
|
|
|
client: mockClient,
|
|
|
|
authorId: "@bob:example.org",
|
|
|
|
participantUserIds: ["@bob:example.org"],
|
|
|
|
});
|
|
|
|
|
2023-02-13 19:39:16 +08:00
|
|
|
expect(Notifier.displayPopupNotification).toHaveBeenCalledTimes(0);
|
2022-10-26 00:53:31 +08:00
|
|
|
|
2023-02-13 19:39:16 +08:00
|
|
|
Notifier.evaluateEvent(rootEvent);
|
|
|
|
expect(Notifier.displayPopupNotification).toHaveBeenCalledTimes(0);
|
2022-10-26 00:53:31 +08:00
|
|
|
|
2023-02-13 19:39:16 +08:00
|
|
|
Notifier.evaluateEvent(events[1]);
|
|
|
|
expect(Notifier.displayPopupNotification).toHaveBeenCalledTimes(1);
|
2022-10-26 00:53:31 +08:00
|
|
|
|
|
|
|
dis.dispatch<ThreadPayload>({
|
|
|
|
action: Action.ViewThread,
|
2023-01-03 17:16:46 +08:00
|
|
|
thread_id: rootEvent.getId()!,
|
2022-10-26 00:53:31 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
await waitFor(() => expect(SdkContextClass.instance.roomViewStore.getThreadId()).toBe(rootEvent.getId()));
|
|
|
|
|
2023-02-13 19:39:16 +08:00
|
|
|
Notifier.evaluateEvent(events[1]);
|
|
|
|
expect(Notifier.displayPopupNotification).toHaveBeenCalledTimes(1);
|
2022-10-26 00:53:31 +08:00
|
|
|
});
|
2023-01-03 17:02:28 +08:00
|
|
|
|
|
|
|
it("should show a pop-up for an audio message", () => {
|
2023-02-13 19:39:16 +08:00
|
|
|
Notifier.evaluateEvent(mkAudioEvent());
|
|
|
|
expect(Notifier.displayPopupNotification).toHaveBeenCalledTimes(1);
|
2023-01-03 17:02:28 +08:00
|
|
|
});
|
2023-01-17 17:04:36 +08:00
|
|
|
|
|
|
|
it("should not show a notification for broadcast info events in any case", () => {
|
|
|
|
// Let client decide to show a notification
|
|
|
|
mockClient.getPushActionsForEvent.mockReturnValue({
|
|
|
|
notify: true,
|
|
|
|
tweaks: {},
|
|
|
|
});
|
|
|
|
|
|
|
|
const broadcastStartedEvent = mkVoiceBroadcastInfoStateEvent(
|
|
|
|
"!other:example.org",
|
|
|
|
VoiceBroadcastInfoState.Started,
|
|
|
|
"@user:example.com",
|
|
|
|
"ABC123",
|
|
|
|
);
|
|
|
|
|
2023-02-13 19:39:16 +08:00
|
|
|
Notifier.evaluateEvent(broadcastStartedEvent);
|
|
|
|
expect(Notifier.displayPopupNotification).not.toHaveBeenCalled();
|
2023-01-17 17:04:36 +08:00
|
|
|
});
|
2022-10-26 00:53:31 +08:00
|
|
|
});
|
2022-11-21 19:24:59 +08:00
|
|
|
|
|
|
|
describe("setPromptHidden", () => {
|
|
|
|
it("should persist by default", () => {
|
|
|
|
Notifier.setPromptHidden(true);
|
|
|
|
expect(localStorage.getItem("notifications_hidden")).toBeTruthy();
|
|
|
|
});
|
|
|
|
});
|
2023-06-27 17:39:55 +08:00
|
|
|
|
|
|
|
describe("onEvent", () => {
|
|
|
|
it("should not evaluate events from the thread list fake timeline sets", async () => {
|
|
|
|
mockClient.supportsThreads.mockReturnValue(true);
|
|
|
|
|
|
|
|
const fn = jest.spyOn(Notifier, "evaluateEvent");
|
|
|
|
|
|
|
|
await testRoom.createThreadsTimelineSets();
|
|
|
|
testRoom.threadsTimelineSets[0]!.addEventToTimeline(
|
|
|
|
mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: "m.room.message",
|
|
|
|
user: "@user1:server",
|
|
|
|
room: roomId,
|
|
|
|
content: { body: "this is a thread root" },
|
|
|
|
}),
|
|
|
|
testRoom.threadsTimelineSets[0]!.getLiveTimeline(),
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(fn).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
2022-09-29 22:23:02 +08:00
|
|
|
});
|