2022-03-04 10:39:16 +01: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-12-12 12:24:14 +01:00
|
|
|
import { mocked } from "jest-mock";
|
2022-03-04 10:39:16 +01:00
|
|
|
import { ConditionKind, PushRuleActionName, TweakName } from "matrix-js-sdk/src/@types/PushRules";
|
2022-12-12 12:24:14 +01:00
|
|
|
import { NotificationCountType, Room } from "matrix-js-sdk/src/models/room";
|
2022-03-04 10:39:16 +01:00
|
|
|
|
2022-10-24 08:50:21 +02:00
|
|
|
import { mkEvent, stubClient } from "./test-utils";
|
2022-03-04 10:39:16 +01:00
|
|
|
import { MatrixClientPeg } from "../src/MatrixClientPeg";
|
2022-12-12 12:24:14 +01:00
|
|
|
import { getRoomNotifsState, RoomNotifState, getUnreadNotificationCount } from "../src/RoomNotifs";
|
2022-03-04 10:39:16 +01:00
|
|
|
|
|
|
|
describe("RoomNotifs test", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
stubClient();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("getRoomNotifsState handles rules with no conditions", () => {
|
2022-11-29 11:55:15 +01:00
|
|
|
const cli = MatrixClientPeg.get();
|
|
|
|
mocked(cli).pushRules = {
|
2022-03-04 10:39:16 +01:00
|
|
|
global: {
|
2022-12-12 12:24:14 +01:00
|
|
|
override: [
|
|
|
|
{
|
|
|
|
rule_id: "!roomId:server",
|
|
|
|
enabled: true,
|
|
|
|
default: false,
|
|
|
|
actions: [],
|
|
|
|
},
|
|
|
|
],
|
2022-03-04 10:39:16 +01:00
|
|
|
},
|
|
|
|
};
|
2022-11-29 11:55:15 +01:00
|
|
|
expect(getRoomNotifsState(cli, "!roomId:server")).toBe(null);
|
2022-03-04 10:39:16 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it("getRoomNotifsState handles guest users", () => {
|
2022-11-29 11:55:15 +01:00
|
|
|
const cli = MatrixClientPeg.get();
|
|
|
|
mocked(cli).isGuest.mockReturnValue(true);
|
|
|
|
expect(getRoomNotifsState(cli, "!roomId:server")).toBe(RoomNotifState.AllMessages);
|
2022-03-04 10:39:16 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it("getRoomNotifsState handles mute state", () => {
|
2022-11-29 11:55:15 +01:00
|
|
|
const cli = MatrixClientPeg.get();
|
|
|
|
cli.pushRules = {
|
2022-03-04 10:39:16 +01:00
|
|
|
global: {
|
2022-12-12 12:24:14 +01:00
|
|
|
override: [
|
|
|
|
{
|
|
|
|
rule_id: "!roomId:server",
|
|
|
|
enabled: true,
|
|
|
|
default: false,
|
|
|
|
conditions: [
|
|
|
|
{
|
|
|
|
kind: ConditionKind.EventMatch,
|
|
|
|
key: "room_id",
|
|
|
|
pattern: "!roomId:server",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
actions: [PushRuleActionName.DontNotify],
|
|
|
|
},
|
|
|
|
],
|
2022-03-04 10:39:16 +01:00
|
|
|
},
|
|
|
|
};
|
2022-11-29 11:55:15 +01:00
|
|
|
expect(getRoomNotifsState(cli, "!roomId:server")).toBe(RoomNotifState.Mute);
|
2022-03-04 10:39:16 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it("getRoomNotifsState handles mentions only", () => {
|
2022-11-29 11:55:15 +01:00
|
|
|
const cli = MatrixClientPeg.get();
|
|
|
|
cli.getRoomPushRule = () => ({
|
2022-03-04 10:39:16 +01:00
|
|
|
rule_id: "!roomId:server",
|
|
|
|
enabled: true,
|
|
|
|
default: false,
|
|
|
|
actions: [PushRuleActionName.DontNotify],
|
|
|
|
});
|
2022-11-29 11:55:15 +01:00
|
|
|
expect(getRoomNotifsState(cli, "!roomId:server")).toBe(RoomNotifState.MentionsOnly);
|
2022-03-04 10:39:16 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it("getRoomNotifsState handles noisy", () => {
|
2022-11-29 11:55:15 +01:00
|
|
|
const cli = MatrixClientPeg.get();
|
|
|
|
cli.getRoomPushRule = () => ({
|
2022-03-04 10:39:16 +01:00
|
|
|
rule_id: "!roomId:server",
|
|
|
|
enabled: true,
|
|
|
|
default: false,
|
2022-05-13 21:13:21 +02:00
|
|
|
actions: [{ set_tweak: TweakName.Sound, value: "default" }],
|
2022-03-04 10:39:16 +01:00
|
|
|
});
|
2022-11-29 11:55:15 +01:00
|
|
|
expect(getRoomNotifsState(cli, "!roomId:server")).toBe(RoomNotifState.AllMessagesLoud);
|
2022-03-04 10:39:16 +01:00
|
|
|
});
|
2022-10-24 08:50:21 +02:00
|
|
|
|
|
|
|
describe("getUnreadNotificationCount", () => {
|
|
|
|
const ROOM_ID = "!roomId:example.org";
|
|
|
|
const THREAD_ID = "$threadId";
|
|
|
|
|
|
|
|
let cli;
|
|
|
|
let room: Room;
|
|
|
|
beforeEach(() => {
|
|
|
|
cli = MatrixClientPeg.get();
|
|
|
|
room = new Room(ROOM_ID, cli, cli.getUserId());
|
|
|
|
});
|
|
|
|
|
|
|
|
it("counts room notification type", () => {
|
|
|
|
expect(getUnreadNotificationCount(room, NotificationCountType.Total)).toBe(0);
|
|
|
|
expect(getUnreadNotificationCount(room, NotificationCountType.Highlight)).toBe(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("counts notifications type", () => {
|
|
|
|
room.setUnreadNotificationCount(NotificationCountType.Total, 2);
|
|
|
|
room.setUnreadNotificationCount(NotificationCountType.Highlight, 1);
|
|
|
|
|
|
|
|
expect(getUnreadNotificationCount(room, NotificationCountType.Total)).toBe(2);
|
|
|
|
expect(getUnreadNotificationCount(room, NotificationCountType.Highlight)).toBe(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("counts predecessor highlight", () => {
|
|
|
|
room.setUnreadNotificationCount(NotificationCountType.Total, 2);
|
|
|
|
room.setUnreadNotificationCount(NotificationCountType.Highlight, 1);
|
|
|
|
|
|
|
|
const OLD_ROOM_ID = "!oldRoomId:example.org";
|
|
|
|
const oldRoom = new Room(OLD_ROOM_ID, cli, cli.getUserId());
|
|
|
|
oldRoom.setUnreadNotificationCount(NotificationCountType.Total, 10);
|
|
|
|
oldRoom.setUnreadNotificationCount(NotificationCountType.Highlight, 6);
|
|
|
|
|
|
|
|
cli.getRoom.mockReset().mockReturnValue(oldRoom);
|
|
|
|
|
|
|
|
const predecessorEvent = mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: "m.room.create",
|
|
|
|
room: ROOM_ID,
|
|
|
|
user: cli.getUserId(),
|
|
|
|
content: {
|
|
|
|
creator: cli.getUserId(),
|
|
|
|
room_version: "5",
|
|
|
|
predecessor: {
|
|
|
|
room_id: OLD_ROOM_ID,
|
|
|
|
event_id: "$someevent",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ts: Date.now(),
|
|
|
|
});
|
|
|
|
room.addLiveEvents([predecessorEvent]);
|
|
|
|
|
|
|
|
expect(getUnreadNotificationCount(room, NotificationCountType.Total)).toBe(8);
|
|
|
|
expect(getUnreadNotificationCount(room, NotificationCountType.Highlight)).toBe(7);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("counts thread notification type", () => {
|
|
|
|
expect(getUnreadNotificationCount(room, NotificationCountType.Total, THREAD_ID)).toBe(0);
|
|
|
|
expect(getUnreadNotificationCount(room, NotificationCountType.Highlight, THREAD_ID)).toBe(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("counts notifications type", () => {
|
|
|
|
room.setThreadUnreadNotificationCount(THREAD_ID, NotificationCountType.Total, 2);
|
|
|
|
room.setThreadUnreadNotificationCount(THREAD_ID, NotificationCountType.Highlight, 1);
|
|
|
|
|
|
|
|
expect(getUnreadNotificationCount(room, NotificationCountType.Total, THREAD_ID)).toBe(2);
|
|
|
|
expect(getUnreadNotificationCount(room, NotificationCountType.Highlight, THREAD_ID)).toBe(1);
|
|
|
|
});
|
|
|
|
});
|
2022-03-04 10:39:16 +01:00
|
|
|
});
|