2022-09-28 19:13:09 +02: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
|
|
|
|
import { mocked } from "jest-mock";
|
2022-11-15 11:27:13 +01:00
|
|
|
import { MatrixClient } from "matrix-js-sdk/src/client";
|
|
|
|
import { NotificationCountType, Room } from "matrix-js-sdk/src/models/room";
|
|
|
|
import { ReceiptType } from "matrix-js-sdk/src/@types/read_receipts";
|
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,
|
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";
|
2022-09-28 19:13:09 +02:00
|
|
|
|
|
|
|
jest.mock("../../src/settings/SettingsStore");
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
describe("notifications", () => {
|
2022-09-29 16:23:02 +02:00
|
|
|
let accountDataStore = {};
|
2022-10-13 14:15:34 +02:00
|
|
|
let mockClient;
|
|
|
|
let accountDataEventKey;
|
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 = {};
|
2022-10-13 14:15:34 +02: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";
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
stubClient();
|
|
|
|
client = mocked(MatrixClientPeg.get());
|
|
|
|
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("sends a request even if everything has been read", () => {
|
|
|
|
clearRoomNotification(room, client);
|
|
|
|
expect(sendReadReceiptSpy).not.toBeCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("marks the room as read even if the receipt failed", async () => {
|
|
|
|
room.setUnreadNotificationCount(NotificationCountType.Total, 5);
|
|
|
|
sendReadReceiptSpy = jest.spyOn(client, "sendReadReceipt").mockReset().mockRejectedValue({});
|
|
|
|
try {
|
|
|
|
await clearRoomNotification(room, client);
|
|
|
|
} finally {
|
|
|
|
expect(room.getUnreadNotificationCount(NotificationCountType.Total)).toBe(0);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
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();
|
|
|
|
client = mocked(MatrixClientPeg.get());
|
|
|
|
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);
|
|
|
|
expect(sendReadReceiptSpy).not.toBeCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("sends unthreaded receipt requests", () => {
|
|
|
|
const message = mkMessage({
|
|
|
|
event: true,
|
|
|
|
room: ROOM_ID,
|
|
|
|
user: USER_ID,
|
|
|
|
ts: 1,
|
|
|
|
});
|
|
|
|
room.addLiveEvents([message]);
|
|
|
|
room.setUnreadNotificationCount(NotificationCountType.Total, 1);
|
|
|
|
|
|
|
|
clearAllNotifications(client);
|
|
|
|
|
|
|
|
expect(sendReadReceiptSpy).toBeCalledWith(message, ReceiptType.Read, true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("sends private read receipts", () => {
|
|
|
|
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);
|
|
|
|
|
|
|
|
clearAllNotifications(client);
|
|
|
|
|
|
|
|
expect(sendReadReceiptSpy).toBeCalledWith(message, ReceiptType.ReadPrivate, true);
|
|
|
|
});
|
|
|
|
});
|
2022-09-28 19:13:09 +02:00
|
|
|
});
|