2022-09-28 19:13:09 +02:00
|
|
|
/*
|
2024-09-09 15:57:16 +02:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-09-28 19:13:09 +02:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 15:57:16 +02:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2022-09-28 19:13:09 +02:00
|
|
|
*/
|
|
|
|
|
2023-08-23 11:04:25 +02:00
|
|
|
import { MatrixEvent, NotificationCountType, Room, MatrixClient, ReceiptType } from "matrix-js-sdk/src/matrix";
|
2023-02-13 12:39:16 +01:00
|
|
|
import { Mocked, mocked } from "jest-mock";
|
2022-09-28 19:13:09 +02:00
|
|
|
|
|
|
|
import {
|
2022-09-29 16:23:02 +02:00
|
|
|
localNotificationsAreSilenced,
|
2022-09-28 19:13:09 +02:00
|
|
|
getLocalNotificationAccountDataEventType,
|
2022-10-10 16:18:38 +02:00
|
|
|
createLocalNotificationSettingsIfNeeded,
|
|
|
|
deviceNotificationSettingsKeys,
|
2022-11-15 11:27:13 +01:00
|
|
|
clearAllNotifications,
|
2022-12-22 14:18:38 +01:00
|
|
|
clearRoomNotification,
|
2024-01-25 17:53:41 +01:00
|
|
|
notificationLevelToIndicator,
|
2024-02-07 14:49:40 +01:00
|
|
|
getThreadNotificationLevel,
|
2024-03-19 14:28:20 +01:00
|
|
|
getMarkedUnreadState,
|
|
|
|
setMarkedUnreadState,
|
2022-09-28 19:13:09 +02:00
|
|
|
} from "../../src/utils/notifications";
|
|
|
|
import SettingsStore from "../../src/settings/SettingsStore";
|
|
|
|
import { getMockClientWithEventEmitter } from "../test-utils/client";
|
2022-11-15 11:27:13 +01:00
|
|
|
import { mkMessage, stubClient } from "../test-utils/test-utils";
|
|
|
|
import { MatrixClientPeg } from "../../src/MatrixClientPeg";
|
2024-01-25 17:53:41 +01:00
|
|
|
import { NotificationLevel } from "../../src/stores/notifications/NotificationLevel";
|
2022-09-28 19:13:09 +02:00
|
|
|
|
|
|
|
jest.mock("../../src/settings/SettingsStore");
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
describe("notifications", () => {
|
2023-02-13 12:39:16 +01:00
|
|
|
let accountDataStore: Record<string, MatrixEvent> = {};
|
|
|
|
let mockClient: Mocked<MatrixClient>;
|
|
|
|
let accountDataEventKey: string;
|
2022-09-28 19:13:09 +02:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2022-10-13 14:15:34 +02:00
|
|
|
jest.clearAllMocks();
|
|
|
|
mockClient = getMockClientWithEventEmitter({
|
|
|
|
isGuest: jest.fn().mockReturnValue(false),
|
2022-12-12 12:24:14 +01:00
|
|
|
getAccountData: jest.fn().mockImplementation((eventType) => accountDataStore[eventType]),
|
2022-10-13 14:15:34 +02:00
|
|
|
setAccountData: jest.fn().mockImplementation((eventType, content) => {
|
|
|
|
accountDataStore[eventType] = new MatrixEvent({
|
|
|
|
type: eventType,
|
|
|
|
content,
|
|
|
|
});
|
|
|
|
}),
|
|
|
|
});
|
2022-09-29 16:23:02 +02:00
|
|
|
accountDataStore = {};
|
2023-02-13 18:01:43 +01:00
|
|
|
accountDataEventKey = getLocalNotificationAccountDataEventType(mockClient.deviceId!);
|
2022-09-28 19:13:09 +02:00
|
|
|
mocked(SettingsStore).getValue.mockReturnValue(false);
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
describe("createLocalNotification", () => {
|
|
|
|
it("creates account data event", async () => {
|
2022-10-10 16:18:38 +02:00
|
|
|
await createLocalNotificationSettingsIfNeeded(mockClient);
|
|
|
|
const event = mockClient.getAccountData(accountDataEventKey);
|
|
|
|
expect(event?.getContent().is_silenced).toBe(true);
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("does not do anything for guests", async () => {
|
2022-10-13 14:15:34 +02:00
|
|
|
mockClient.isGuest.mockReset().mockReturnValue(true);
|
|
|
|
await createLocalNotificationSettingsIfNeeded(mockClient);
|
|
|
|
const event = mockClient.getAccountData(accountDataEventKey);
|
|
|
|
expect(event).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
2022-10-10 16:18:38 +02:00
|
|
|
it.each(deviceNotificationSettingsKeys)(
|
2022-12-12 12:24:14 +01:00
|
|
|
"unsilenced for existing sessions when %s setting is truthy",
|
2022-10-10 16:18:38 +02:00
|
|
|
async (settingKey) => {
|
2022-11-04 11:48:08 +01:00
|
|
|
mocked(SettingsStore).getValue.mockImplementation((key): any => {
|
|
|
|
return key === settingKey;
|
|
|
|
});
|
2022-10-10 16:18:38 +02:00
|
|
|
|
|
|
|
await createLocalNotificationSettingsIfNeeded(mockClient);
|
|
|
|
const event = mockClient.getAccountData(accountDataEventKey);
|
|
|
|
expect(event?.getContent().is_silenced).toBe(false);
|
2022-12-12 12:24:14 +01:00
|
|
|
},
|
|
|
|
);
|
2022-10-10 16:18:38 +02:00
|
|
|
|
|
|
|
it("does not override an existing account event data", async () => {
|
|
|
|
mockClient.setAccountData(accountDataEventKey, {
|
|
|
|
is_silenced: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
await createLocalNotificationSettingsIfNeeded(mockClient);
|
|
|
|
const event = mockClient.getAccountData(accountDataEventKey);
|
|
|
|
expect(event?.getContent().is_silenced).toBe(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
describe("localNotificationsAreSilenced", () => {
|
|
|
|
it("defaults to false when no setting exists", () => {
|
2022-10-11 18:11:11 +02:00
|
|
|
expect(localNotificationsAreSilenced(mockClient)).toBeFalsy();
|
2022-09-29 16:23:02 +02:00
|
|
|
});
|
2022-12-12 12:24:14 +01:00
|
|
|
it("checks the persisted value", () => {
|
2022-09-29 16:23:02 +02:00
|
|
|
mockClient.setAccountData(accountDataEventKey, { is_silenced: true });
|
|
|
|
expect(localNotificationsAreSilenced(mockClient)).toBeTruthy();
|
|
|
|
|
|
|
|
mockClient.setAccountData(accountDataEventKey, { is_silenced: false });
|
|
|
|
expect(localNotificationsAreSilenced(mockClient)).toBeFalsy();
|
|
|
|
});
|
|
|
|
});
|
2022-11-15 11:27:13 +01:00
|
|
|
|
2022-12-22 14:18:38 +01:00
|
|
|
describe("clearRoomNotification", () => {
|
|
|
|
let client: MatrixClient;
|
|
|
|
let room: Room;
|
|
|
|
let sendReadReceiptSpy: jest.SpyInstance;
|
|
|
|
const ROOM_ID = "123";
|
|
|
|
const USER_ID = "@bob:example.org";
|
2023-04-28 08:07:25 +02:00
|
|
|
let message: MatrixEvent;
|
|
|
|
let sendReceiptsSetting = true;
|
2022-12-22 14:18:38 +01:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
stubClient();
|
2023-06-05 19:12:23 +02:00
|
|
|
client = mocked(MatrixClientPeg.safeGet());
|
2022-12-22 14:18:38 +01:00
|
|
|
room = new Room(ROOM_ID, client, USER_ID);
|
2023-04-28 08:07:25 +02:00
|
|
|
message = mkMessage({
|
|
|
|
event: true,
|
|
|
|
room: ROOM_ID,
|
|
|
|
user: USER_ID,
|
|
|
|
msg: "Hello",
|
|
|
|
});
|
|
|
|
room.addLiveEvents([message]);
|
2022-12-22 14:18:38 +01:00
|
|
|
sendReadReceiptSpy = jest.spyOn(client, "sendReadReceipt").mockResolvedValue({});
|
|
|
|
jest.spyOn(client, "getRooms").mockReturnValue([room]);
|
|
|
|
jest.spyOn(SettingsStore, "getValue").mockImplementation((name) => {
|
2023-04-28 08:07:25 +02:00
|
|
|
return name === "sendReadReceipts" && sendReceiptsSetting;
|
2022-12-22 14:18:38 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-03-19 14:28:20 +01:00
|
|
|
it("sends a request even if everything has been read", async () => {
|
|
|
|
await clearRoomNotification(room, client);
|
2023-04-28 08:07:25 +02:00
|
|
|
expect(sendReadReceiptSpy).toHaveBeenCalledWith(message, ReceiptType.Read, true);
|
2022-12-22 14:18:38 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it("marks the room as read even if the receipt failed", async () => {
|
|
|
|
room.setUnreadNotificationCount(NotificationCountType.Total, 5);
|
2023-04-28 08:07:25 +02:00
|
|
|
sendReadReceiptSpy = jest.spyOn(client, "sendReadReceipt").mockReset().mockRejectedValue({ error: 42 });
|
|
|
|
|
|
|
|
await expect(async () => {
|
2022-12-22 14:18:38 +01:00
|
|
|
await clearRoomNotification(room, client);
|
2023-04-28 08:07:25 +02:00
|
|
|
}).rejects.toEqual({ error: 42 });
|
|
|
|
expect(room.getUnreadNotificationCount(NotificationCountType.Total)).toBe(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when sendReadReceipts setting is disabled", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
sendReceiptsSetting = false;
|
|
|
|
});
|
|
|
|
|
2024-03-19 14:28:20 +01:00
|
|
|
it("should send a private read receipt", async () => {
|
|
|
|
await clearRoomNotification(room, client);
|
2023-04-28 08:07:25 +02:00
|
|
|
expect(sendReadReceiptSpy).toHaveBeenCalledWith(message, ReceiptType.ReadPrivate, true);
|
|
|
|
});
|
2022-12-22 14:18:38 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-11-15 11:27:13 +01:00
|
|
|
describe("clearAllNotifications", () => {
|
|
|
|
let client: MatrixClient;
|
|
|
|
let room: Room;
|
2022-12-22 14:18:38 +01:00
|
|
|
let sendReadReceiptSpy: jest.SpyInstance;
|
2022-11-15 11:27:13 +01:00
|
|
|
|
|
|
|
const ROOM_ID = "123";
|
|
|
|
const USER_ID = "@bob:example.org";
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
stubClient();
|
2023-06-05 19:12:23 +02:00
|
|
|
client = mocked(MatrixClientPeg.safeGet());
|
2022-11-15 11:27:13 +01:00
|
|
|
room = new Room(ROOM_ID, client, USER_ID);
|
|
|
|
sendReadReceiptSpy = jest.spyOn(client, "sendReadReceipt").mockResolvedValue({});
|
|
|
|
jest.spyOn(client, "getRooms").mockReturnValue([room]);
|
|
|
|
jest.spyOn(SettingsStore, "getValue").mockImplementation((name) => {
|
|
|
|
return name === "sendReadReceipts";
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("does not send any requests if everything has been read", () => {
|
|
|
|
clearAllNotifications(client);
|
2023-03-01 16:23:35 +01:00
|
|
|
expect(sendReadReceiptSpy).not.toHaveBeenCalled();
|
2022-11-15 11:27:13 +01:00
|
|
|
});
|
|
|
|
|
2024-03-19 14:28:20 +01:00
|
|
|
it("sends unthreaded receipt requests", async () => {
|
2022-11-15 11:27:13 +01:00
|
|
|
const message = mkMessage({
|
|
|
|
event: true,
|
|
|
|
room: ROOM_ID,
|
|
|
|
user: USER_ID,
|
|
|
|
ts: 1,
|
|
|
|
});
|
|
|
|
room.addLiveEvents([message]);
|
|
|
|
room.setUnreadNotificationCount(NotificationCountType.Total, 1);
|
|
|
|
|
2024-03-19 14:28:20 +01:00
|
|
|
await clearAllNotifications(client);
|
2022-11-15 11:27:13 +01:00
|
|
|
|
2023-03-01 16:23:35 +01:00
|
|
|
expect(sendReadReceiptSpy).toHaveBeenCalledWith(message, ReceiptType.Read, true);
|
2022-11-15 11:27:13 +01:00
|
|
|
});
|
|
|
|
|
2024-03-19 14:28:20 +01:00
|
|
|
it("sends private read receipts", async () => {
|
2022-11-15 11:27:13 +01:00
|
|
|
const message = mkMessage({
|
|
|
|
event: true,
|
|
|
|
room: ROOM_ID,
|
|
|
|
user: USER_ID,
|
|
|
|
ts: 1,
|
|
|
|
});
|
|
|
|
room.addLiveEvents([message]);
|
|
|
|
room.setUnreadNotificationCount(NotificationCountType.Total, 1);
|
|
|
|
|
|
|
|
jest.spyOn(SettingsStore, "getValue").mockReset().mockReturnValue(false);
|
|
|
|
|
2024-03-19 14:28:20 +01:00
|
|
|
await clearAllNotifications(client);
|
2022-11-15 11:27:13 +01:00
|
|
|
|
2023-03-01 16:23:35 +01:00
|
|
|
expect(sendReadReceiptSpy).toHaveBeenCalledWith(message, ReceiptType.ReadPrivate, true);
|
2022-11-15 11:27:13 +01:00
|
|
|
});
|
|
|
|
});
|
2024-01-25 17:53:41 +01:00
|
|
|
|
2024-03-19 14:28:20 +01:00
|
|
|
describe("getMarkedUnreadState", () => {
|
|
|
|
let client: MatrixClient;
|
|
|
|
let room: Room;
|
|
|
|
|
|
|
|
const ROOM_ID = "123";
|
|
|
|
const USER_ID = "@bob:example.org";
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
stubClient();
|
|
|
|
client = mocked(MatrixClientPeg.safeGet());
|
|
|
|
room = new Room(ROOM_ID, client, USER_ID);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("reads from stable prefix", async () => {
|
|
|
|
room.getAccountData = jest.fn().mockImplementation((eventType: string) => {
|
|
|
|
if (eventType === "m.marked_unread") {
|
|
|
|
return { getContent: jest.fn().mockReturnValue({ unread: true }) };
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
expect(getMarkedUnreadState(room)).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("reads from unstable prefix", async () => {
|
|
|
|
room.getAccountData = jest.fn().mockImplementation((eventType: string) => {
|
|
|
|
if (eventType === "com.famedly.marked_unread") {
|
|
|
|
return { getContent: jest.fn().mockReturnValue({ unread: true }) };
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
expect(getMarkedUnreadState(room)).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("returns undefined if neither prefix is present", async () => {
|
|
|
|
room.getAccountData = jest.fn().mockImplementation((eventType: string) => {
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
expect(getMarkedUnreadState(room)).toBe(undefined);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("setUnreadMarker", () => {
|
|
|
|
let client: MatrixClient;
|
|
|
|
let room: Room;
|
|
|
|
|
|
|
|
const ROOM_ID = "123";
|
|
|
|
const USER_ID = "@bob:example.org";
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
stubClient();
|
|
|
|
client = mocked(MatrixClientPeg.safeGet());
|
|
|
|
room = new Room(ROOM_ID, client, USER_ID);
|
|
|
|
});
|
|
|
|
|
|
|
|
// set true, no existing event
|
|
|
|
it("sets unread flag if event doesn't exist", async () => {
|
|
|
|
await setMarkedUnreadState(room, client, true);
|
|
|
|
expect(client.setRoomAccountData).toHaveBeenCalledWith(ROOM_ID, "com.famedly.marked_unread", {
|
|
|
|
unread: true,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// set false, no existing event
|
|
|
|
it("does nothing when clearing if flag is false", async () => {
|
|
|
|
await setMarkedUnreadState(room, client, false);
|
|
|
|
expect(client.setRoomAccountData).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
// set true, existing event = false
|
|
|
|
it("sets unread flag to if existing event is false", async () => {
|
|
|
|
room.getAccountData = jest
|
|
|
|
.fn()
|
|
|
|
.mockReturnValue({ getContent: jest.fn().mockReturnValue({ unread: false }) });
|
|
|
|
await setMarkedUnreadState(room, client, true);
|
|
|
|
expect(client.setRoomAccountData).toHaveBeenCalledWith(ROOM_ID, "com.famedly.marked_unread", {
|
|
|
|
unread: true,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// set false, existing event = false
|
|
|
|
it("does nothing if set false and existing event is false", async () => {
|
|
|
|
room.getAccountData = jest
|
|
|
|
.fn()
|
|
|
|
.mockReturnValue({ getContent: jest.fn().mockReturnValue({ unread: false }) });
|
|
|
|
await setMarkedUnreadState(room, client, false);
|
|
|
|
expect(client.setRoomAccountData).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
// set true, existing event = true
|
|
|
|
it("does nothing if setting true and existing event is true", async () => {
|
|
|
|
room.getAccountData = jest
|
|
|
|
.fn()
|
|
|
|
.mockReturnValue({ getContent: jest.fn().mockReturnValue({ unread: true }) });
|
|
|
|
await setMarkedUnreadState(room, client, true);
|
|
|
|
expect(client.setRoomAccountData).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
// set false, existing event = true
|
|
|
|
it("sets flag if setting false and existing event is true", async () => {
|
|
|
|
room.getAccountData = jest
|
|
|
|
.fn()
|
|
|
|
.mockReturnValue({ getContent: jest.fn().mockReturnValue({ unread: true }) });
|
|
|
|
await setMarkedUnreadState(room, client, false);
|
|
|
|
expect(client.setRoomAccountData).toHaveBeenCalledWith(ROOM_ID, "com.famedly.marked_unread", {
|
|
|
|
unread: false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-01-25 17:53:41 +01:00
|
|
|
describe("notificationLevelToIndicator", () => {
|
|
|
|
it("returns undefined if notification level is None", () => {
|
|
|
|
expect(notificationLevelToIndicator(NotificationLevel.None)).toBeUndefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("returns default if notification level is Activity", () => {
|
|
|
|
expect(notificationLevelToIndicator(NotificationLevel.Activity)).toEqual("default");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("returns success if notification level is Notification", () => {
|
|
|
|
expect(notificationLevelToIndicator(NotificationLevel.Notification)).toEqual("success");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("returns critical if notification level is Highlight", () => {
|
|
|
|
expect(notificationLevelToIndicator(NotificationLevel.Highlight)).toEqual("critical");
|
|
|
|
});
|
|
|
|
});
|
2024-02-07 14:49:40 +01:00
|
|
|
|
|
|
|
describe("getThreadNotificationLevel", () => {
|
|
|
|
let room: Room;
|
|
|
|
|
|
|
|
const ROOM_ID = "123";
|
|
|
|
const USER_ID = "@bob:example.org";
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
room = new Room(ROOM_ID, MatrixClientPeg.safeGet(), USER_ID);
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each([
|
|
|
|
{ notificationCountType: NotificationCountType.Highlight, expected: NotificationLevel.Highlight },
|
|
|
|
{ notificationCountType: NotificationCountType.Total, expected: NotificationLevel.Notification },
|
|
|
|
{ notificationCountType: null, expected: NotificationLevel.Activity },
|
|
|
|
])(
|
|
|
|
"returns NotificationLevel $expected when notificationCountType is $expected",
|
|
|
|
({ notificationCountType, expected }) => {
|
|
|
|
jest.spyOn(room, "threadsAggregateNotificationType", "get").mockReturnValue(notificationCountType);
|
|
|
|
expect(getThreadNotificationLevel(room)).toEqual(expected);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
2022-09-28 19:13:09 +02:00
|
|
|
});
|