2022-03-04 17:39:16 +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-03-23 18:27:28 +08:00
|
|
|
import { mocked } from 'jest-mock';
|
2022-03-04 17:39:16 +08:00
|
|
|
import { ConditionKind, PushRuleActionName, TweakName } from "matrix-js-sdk/src/@types/PushRules";
|
2022-10-24 14:50:21 +08:00
|
|
|
import { NotificationCountType, Room } from 'matrix-js-sdk/src/models/room';
|
2022-03-04 17:39:16 +08:00
|
|
|
|
2022-10-24 14:50:21 +08:00
|
|
|
import { mkEvent, stubClient } from "./test-utils";
|
2022-03-04 17:39:16 +08:00
|
|
|
import { MatrixClientPeg } from "../src/MatrixClientPeg";
|
2022-10-24 14:50:21 +08:00
|
|
|
import {
|
|
|
|
getRoomNotifsState,
|
|
|
|
RoomNotifState,
|
|
|
|
getUnreadNotificationCount,
|
|
|
|
} from "../src/RoomNotifs";
|
2022-03-04 17:39:16 +08:00
|
|
|
|
|
|
|
describe("RoomNotifs test", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
stubClient();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("getRoomNotifsState handles rules with no conditions", () => {
|
2022-03-23 18:27:28 +08:00
|
|
|
mocked(MatrixClientPeg.get()).pushRules = {
|
2022-03-04 17:39:16 +08:00
|
|
|
global: {
|
|
|
|
override: [{
|
|
|
|
rule_id: "!roomId:server",
|
|
|
|
enabled: true,
|
|
|
|
default: false,
|
|
|
|
actions: [],
|
|
|
|
}],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
expect(getRoomNotifsState("!roomId:server")).toBe(null);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("getRoomNotifsState handles guest users", () => {
|
2022-03-23 18:27:28 +08:00
|
|
|
mocked(MatrixClientPeg.get()).isGuest.mockReturnValue(true);
|
2022-03-04 17:39:16 +08:00
|
|
|
expect(getRoomNotifsState("!roomId:server")).toBe(RoomNotifState.AllMessages);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("getRoomNotifsState handles mute state", () => {
|
|
|
|
MatrixClientPeg.get().pushRules = {
|
|
|
|
global: {
|
|
|
|
override: [{
|
|
|
|
rule_id: "!roomId:server",
|
|
|
|
enabled: true,
|
|
|
|
default: false,
|
|
|
|
conditions: [{
|
|
|
|
kind: ConditionKind.EventMatch,
|
|
|
|
key: "room_id",
|
|
|
|
pattern: "!roomId:server",
|
|
|
|
}],
|
|
|
|
actions: [PushRuleActionName.DontNotify],
|
|
|
|
}],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
expect(getRoomNotifsState("!roomId:server")).toBe(RoomNotifState.Mute);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("getRoomNotifsState handles mentions only", () => {
|
|
|
|
MatrixClientPeg.get().getRoomPushRule = () => ({
|
|
|
|
rule_id: "!roomId:server",
|
|
|
|
enabled: true,
|
|
|
|
default: false,
|
|
|
|
actions: [PushRuleActionName.DontNotify],
|
|
|
|
});
|
|
|
|
expect(getRoomNotifsState("!roomId:server")).toBe(RoomNotifState.MentionsOnly);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("getRoomNotifsState handles noisy", () => {
|
|
|
|
MatrixClientPeg.get().getRoomPushRule = () => ({
|
|
|
|
rule_id: "!roomId:server",
|
|
|
|
enabled: true,
|
|
|
|
default: false,
|
2022-05-14 03:13:21 +08:00
|
|
|
actions: [{ set_tweak: TweakName.Sound, value: "default" }],
|
2022-03-04 17:39:16 +08:00
|
|
|
});
|
|
|
|
expect(getRoomNotifsState("!roomId:server")).toBe(RoomNotifState.AllMessagesLoud);
|
|
|
|
});
|
2022-10-24 14:50:21 +08: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 17:39:16 +08:00
|
|
|
});
|