2022-03-04 17:39:16 +08:00
|
|
|
/*
|
2023-01-31 17:58:17 +08:00
|
|
|
Copyright 2022 - 2023 The Matrix.org Foundation C.I.C.
|
2022-03-04 17:39:16 +08:00
|
|
|
|
|
|
|
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";
|
2023-08-04 15:36:16 +08:00
|
|
|
import {
|
2023-08-09 23:10:54 +08:00
|
|
|
PushRuleActionName,
|
|
|
|
TweakName,
|
2023-08-04 15:36:16 +08:00
|
|
|
NotificationCountType,
|
|
|
|
Room,
|
|
|
|
EventStatus,
|
|
|
|
EventType,
|
|
|
|
MatrixEvent,
|
|
|
|
PendingEventOrdering,
|
|
|
|
} from "matrix-js-sdk/src/matrix";
|
2024-03-18 22:40:52 +08:00
|
|
|
import { KnownMembership } from "matrix-js-sdk/src/types";
|
2023-01-31 17:58:17 +08:00
|
|
|
|
|
|
|
import type { MatrixClient } from "matrix-js-sdk/src/matrix";
|
2023-09-19 19:24:35 +08:00
|
|
|
import { mkEvent, mkRoom, mkRoomMember, muteRoom, stubClient, upsertRoomStateEvents } from "./test-utils";
|
2023-01-31 17:58:17 +08:00
|
|
|
import {
|
|
|
|
getRoomNotifsState,
|
|
|
|
RoomNotifState,
|
|
|
|
getUnreadNotificationCount,
|
|
|
|
determineUnreadState,
|
|
|
|
} from "../src/RoomNotifs";
|
2024-01-15 23:25:48 +08:00
|
|
|
import { NotificationLevel } from "../src/stores/notifications/NotificationLevel";
|
2023-02-03 20:00:33 +08:00
|
|
|
import SettingsStore from "../src/settings/SettingsStore";
|
2023-09-19 19:24:35 +08:00
|
|
|
import { MatrixClientPeg } from "../src/MatrixClientPeg";
|
2022-03-04 17:39:16 +08:00
|
|
|
|
|
|
|
describe("RoomNotifs test", () => {
|
2023-01-31 17:58:17 +08:00
|
|
|
let client: jest.Mocked<MatrixClient>;
|
|
|
|
|
2022-03-04 17:39:16 +08:00
|
|
|
beforeEach(() => {
|
2023-01-31 17:58:17 +08:00
|
|
|
client = stubClient() as jest.Mocked<MatrixClient>;
|
2022-03-04 17:39:16 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("getRoomNotifsState handles rules with no conditions", () => {
|
2023-01-31 17:58:17 +08:00
|
|
|
mocked(client).pushRules = {
|
2022-03-04 17:39:16 +08:00
|
|
|
global: {
|
|
|
|
override: [
|
|
|
|
{
|
|
|
|
rule_id: "!roomId:server",
|
|
|
|
enabled: true,
|
|
|
|
default: false,
|
|
|
|
actions: [],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
};
|
2023-01-31 17:58:17 +08:00
|
|
|
expect(getRoomNotifsState(client, "!roomId:server")).toBe(null);
|
2022-03-04 17:39:16 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("getRoomNotifsState handles guest users", () => {
|
2023-01-31 17:58:17 +08:00
|
|
|
mocked(client).isGuest.mockReturnValue(true);
|
|
|
|
expect(getRoomNotifsState(client, "!roomId:server")).toBe(RoomNotifState.AllMessages);
|
2022-03-04 17:39:16 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("getRoomNotifsState handles mute state", () => {
|
2023-01-31 17:58:17 +08:00
|
|
|
const room = mkRoom(client, "!roomId:server");
|
|
|
|
muteRoom(room);
|
|
|
|
expect(getRoomNotifsState(client, room.roomId)).toBe(RoomNotifState.Mute);
|
2022-03-04 17:39:16 +08:00
|
|
|
});
|
|
|
|
|
2023-06-28 22:07:02 +08:00
|
|
|
it("getRoomNotifsState handles mute state for legacy DontNotify action", () => {
|
|
|
|
const room = mkRoom(client, "!roomId:server");
|
|
|
|
muteRoom(room);
|
|
|
|
client.pushRules!.global.override![0]!.actions = [PushRuleActionName.DontNotify];
|
|
|
|
expect(getRoomNotifsState(client, room.roomId)).toBe(RoomNotifState.Mute);
|
|
|
|
});
|
|
|
|
|
2022-03-04 17:39:16 +08:00
|
|
|
it("getRoomNotifsState handles mentions only", () => {
|
2023-01-31 17:58:17 +08:00
|
|
|
(client as any).getRoomPushRule = () => ({
|
2022-03-04 17:39:16 +08:00
|
|
|
rule_id: "!roomId:server",
|
|
|
|
enabled: true,
|
|
|
|
default: false,
|
|
|
|
actions: [PushRuleActionName.DontNotify],
|
|
|
|
});
|
2023-01-31 17:58:17 +08:00
|
|
|
expect(getRoomNotifsState(client, "!roomId:server")).toBe(RoomNotifState.MentionsOnly);
|
2022-03-04 17:39:16 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("getRoomNotifsState handles noisy", () => {
|
2023-01-31 17:58:17 +08:00
|
|
|
(client as any).getRoomPushRule = () => ({
|
2022-03-04 17:39:16 +08:00
|
|
|
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
|
|
|
});
|
2023-01-31 17:58:17 +08:00
|
|
|
expect(getRoomNotifsState(client, "!roomId:server")).toBe(RoomNotifState.AllMessagesLoud);
|
2022-03-04 17:39:16 +08:00
|
|
|
});
|
2022-10-24 14:50:21 +08:00
|
|
|
|
|
|
|
describe("getUnreadNotificationCount", () => {
|
|
|
|
const ROOM_ID = "!roomId:example.org";
|
|
|
|
const THREAD_ID = "$threadId";
|
|
|
|
|
|
|
|
let room: Room;
|
|
|
|
beforeEach(() => {
|
2023-01-31 17:58:17 +08:00
|
|
|
room = new Room(ROOM_ID, client, client.getUserId()!);
|
2022-10-24 14:50:21 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("counts room notification type", () => {
|
2024-01-30 01:52:48 +08:00
|
|
|
expect(getUnreadNotificationCount(room, NotificationCountType.Total, false)).toBe(0);
|
|
|
|
expect(getUnreadNotificationCount(room, NotificationCountType.Highlight, false)).toBe(0);
|
2022-10-24 14:50:21 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("counts notifications type", () => {
|
|
|
|
room.setUnreadNotificationCount(NotificationCountType.Total, 2);
|
|
|
|
room.setUnreadNotificationCount(NotificationCountType.Highlight, 1);
|
|
|
|
|
2024-01-30 01:52:48 +08:00
|
|
|
expect(getUnreadNotificationCount(room, NotificationCountType.Total, false)).toBe(2);
|
|
|
|
expect(getUnreadNotificationCount(room, NotificationCountType.Highlight, false)).toBe(1);
|
2022-10-24 14:50:21 +08:00
|
|
|
});
|
|
|
|
|
2023-02-03 20:00:33 +08:00
|
|
|
describe("when there is a room predecessor", () => {
|
2022-10-24 14:50:21 +08:00
|
|
|
const OLD_ROOM_ID = "!oldRoomId:example.org";
|
2023-02-03 20:00:33 +08:00
|
|
|
const mkCreateEvent = (predecessorId?: string): MatrixEvent => {
|
|
|
|
return mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: "m.room.create",
|
|
|
|
room: ROOM_ID,
|
2023-02-09 20:46:17 +08:00
|
|
|
user: "@zoe:localhost",
|
2023-02-03 20:00:33 +08:00
|
|
|
content: {
|
|
|
|
...(predecessorId ? { predecessor: { room_id: predecessorId, event_id: "$someevent" } } : {}),
|
2023-02-09 20:46:17 +08:00
|
|
|
creator: "@zoe:localhost",
|
2023-02-03 20:00:33 +08:00
|
|
|
room_version: "5",
|
|
|
|
},
|
|
|
|
ts: Date.now(),
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const mkPredecessorEvent = (predecessorId: string): MatrixEvent => {
|
|
|
|
return mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: EventType.RoomPredecessor,
|
|
|
|
room: ROOM_ID,
|
2023-02-09 20:46:17 +08:00
|
|
|
user: "@zoe:localhost",
|
2023-02-03 20:00:33 +08:00
|
|
|
skey: "",
|
|
|
|
content: {
|
|
|
|
predecessor_room_id: predecessorId,
|
2022-10-24 14:50:21 +08:00
|
|
|
},
|
2023-02-03 20:00:33 +08:00
|
|
|
ts: Date.now(),
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const itShouldCountPredecessorHighlightWhenThereIsAPredecessorInTheCreateEvent = (): void => {
|
|
|
|
it("and there is a predecessor in the create event, it should count predecessor highlight", () => {
|
|
|
|
room.addLiveEvents([mkCreateEvent(OLD_ROOM_ID)]);
|
|
|
|
|
2024-01-30 01:52:48 +08:00
|
|
|
expect(getUnreadNotificationCount(room, NotificationCountType.Total, false)).toBe(8);
|
|
|
|
expect(getUnreadNotificationCount(room, NotificationCountType.Highlight, false)).toBe(7);
|
2023-02-03 20:00:33 +08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const itShouldCountPredecessorHighlightWhenThereIsAPredecessorEvent = (): void => {
|
|
|
|
it("and there is a predecessor event, it should count predecessor highlight", () => {
|
|
|
|
client.getVisibleRooms();
|
|
|
|
room.addLiveEvents([mkCreateEvent(OLD_ROOM_ID)]);
|
|
|
|
upsertRoomStateEvents(room, [mkPredecessorEvent(OLD_ROOM_ID)]);
|
|
|
|
|
2024-01-30 01:52:48 +08:00
|
|
|
expect(getUnreadNotificationCount(room, NotificationCountType.Total, false)).toBe(8);
|
|
|
|
expect(getUnreadNotificationCount(room, NotificationCountType.Highlight, false)).toBe(7);
|
2023-02-03 20:00:33 +08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
room.setUnreadNotificationCount(NotificationCountType.Total, 2);
|
|
|
|
room.setUnreadNotificationCount(NotificationCountType.Highlight, 1);
|
|
|
|
|
|
|
|
const oldRoom = new Room(OLD_ROOM_ID, client, client.getUserId()!);
|
|
|
|
oldRoom.setUnreadNotificationCount(NotificationCountType.Total, 10);
|
|
|
|
oldRoom.setUnreadNotificationCount(NotificationCountType.Highlight, 6);
|
|
|
|
|
|
|
|
client.getRoom.mockImplementation((roomId: string | undefined): Room | null => {
|
|
|
|
if (roomId === room.roomId) return room;
|
|
|
|
if (roomId === OLD_ROOM_ID) return oldRoom;
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("and dynamic room predecessors are disabled", () => {
|
|
|
|
itShouldCountPredecessorHighlightWhenThereIsAPredecessorInTheCreateEvent();
|
|
|
|
itShouldCountPredecessorHighlightWhenThereIsAPredecessorEvent();
|
|
|
|
|
|
|
|
it("and there is only a predecessor event, it should not count predecessor highlight", () => {
|
|
|
|
room.addLiveEvents([mkCreateEvent()]);
|
|
|
|
upsertRoomStateEvents(room, [mkPredecessorEvent(OLD_ROOM_ID)]);
|
|
|
|
|
2024-01-30 01:52:48 +08:00
|
|
|
expect(getUnreadNotificationCount(room, NotificationCountType.Total, false)).toBe(2);
|
|
|
|
expect(getUnreadNotificationCount(room, NotificationCountType.Highlight, false)).toBe(1);
|
2023-02-03 20:00:33 +08:00
|
|
|
});
|
2022-10-24 14:50:21 +08:00
|
|
|
});
|
|
|
|
|
2023-02-03 20:00:33 +08:00
|
|
|
describe("and dynamic room predecessors are enabled", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.spyOn(SettingsStore, "getValue").mockImplementation(
|
|
|
|
(settingName) => settingName === "feature_dynamic_room_predecessors",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
itShouldCountPredecessorHighlightWhenThereIsAPredecessorInTheCreateEvent();
|
|
|
|
itShouldCountPredecessorHighlightWhenThereIsAPredecessorEvent();
|
|
|
|
|
|
|
|
it("and there is only a predecessor event, it should count predecessor highlight", () => {
|
|
|
|
room.addLiveEvents([mkCreateEvent()]);
|
|
|
|
upsertRoomStateEvents(room, [mkPredecessorEvent(OLD_ROOM_ID)]);
|
|
|
|
|
2024-01-30 01:52:48 +08:00
|
|
|
expect(getUnreadNotificationCount(room, NotificationCountType.Total, false)).toBe(8);
|
|
|
|
expect(getUnreadNotificationCount(room, NotificationCountType.Highlight, false)).toBe(7);
|
2023-02-03 20:00:33 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("and there is an unknown room in the predecessor event, it should not count predecessor highlight", () => {
|
|
|
|
room.addLiveEvents([mkCreateEvent()]);
|
|
|
|
upsertRoomStateEvents(room, [mkPredecessorEvent("!unknon:example.com")]);
|
|
|
|
|
2024-01-30 01:52:48 +08:00
|
|
|
expect(getUnreadNotificationCount(room, NotificationCountType.Total, false)).toBe(2);
|
|
|
|
expect(getUnreadNotificationCount(room, NotificationCountType.Highlight, false)).toBe(1);
|
2023-02-03 20:00:33 +08:00
|
|
|
});
|
|
|
|
});
|
2022-10-24 14:50:21 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("counts thread notification type", () => {
|
2024-01-30 01:52:48 +08:00
|
|
|
expect(getUnreadNotificationCount(room, NotificationCountType.Total, false, THREAD_ID)).toBe(0);
|
|
|
|
expect(getUnreadNotificationCount(room, NotificationCountType.Highlight, false, THREAD_ID)).toBe(0);
|
2022-10-24 14:50:21 +08:00
|
|
|
});
|
|
|
|
|
2023-03-01 23:23:35 +08:00
|
|
|
it("counts thread notifications type", () => {
|
2022-10-24 14:50:21 +08:00
|
|
|
room.setThreadUnreadNotificationCount(THREAD_ID, NotificationCountType.Total, 2);
|
|
|
|
room.setThreadUnreadNotificationCount(THREAD_ID, NotificationCountType.Highlight, 1);
|
|
|
|
|
2024-01-30 01:52:48 +08:00
|
|
|
expect(getUnreadNotificationCount(room, NotificationCountType.Total, false, THREAD_ID)).toBe(2);
|
|
|
|
expect(getUnreadNotificationCount(room, NotificationCountType.Highlight, false, THREAD_ID)).toBe(1);
|
2022-10-24 14:50:21 +08:00
|
|
|
});
|
|
|
|
});
|
2023-01-31 17:58:17 +08:00
|
|
|
|
|
|
|
describe("determineUnreadState", () => {
|
|
|
|
let room: Room;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
room = new Room("!room-id:example.com", client, "@user:example.com", {
|
|
|
|
pendingEventOrdering: PendingEventOrdering.Detached,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("shows nothing by default", async () => {
|
2024-01-15 23:25:48 +08:00
|
|
|
const { level, symbol, count } = determineUnreadState(room);
|
2023-01-31 17:58:17 +08:00
|
|
|
|
|
|
|
expect(symbol).toBe(null);
|
2024-01-15 23:25:48 +08:00
|
|
|
expect(level).toBe(NotificationLevel.None);
|
2023-01-31 17:58:17 +08:00
|
|
|
expect(count).toBe(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("indicates if there are unsent messages", async () => {
|
|
|
|
const event = mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: "m.message",
|
|
|
|
user: "@user:example.org",
|
|
|
|
content: {},
|
|
|
|
});
|
|
|
|
event.status = EventStatus.NOT_SENT;
|
|
|
|
room.addPendingEvent(event, "txn");
|
|
|
|
|
2024-01-15 23:25:48 +08:00
|
|
|
const { level, symbol, count } = determineUnreadState(room);
|
2023-01-31 17:58:17 +08:00
|
|
|
|
|
|
|
expect(symbol).toBe("!");
|
2024-01-15 23:25:48 +08:00
|
|
|
expect(level).toBe(NotificationLevel.Unsent);
|
2023-01-31 17:58:17 +08:00
|
|
|
expect(count).toBeGreaterThan(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("indicates the user has been invited to a channel", async () => {
|
2024-03-12 22:52:54 +08:00
|
|
|
room.updateMyMembership(KnownMembership.Invite);
|
2023-01-31 17:58:17 +08:00
|
|
|
|
2024-01-15 23:25:48 +08:00
|
|
|
const { level, symbol, count } = determineUnreadState(room);
|
2023-01-31 17:58:17 +08:00
|
|
|
|
|
|
|
expect(symbol).toBe("!");
|
2024-01-15 23:25:48 +08:00
|
|
|
expect(level).toBe(NotificationLevel.Highlight);
|
2023-01-31 17:58:17 +08:00
|
|
|
expect(count).toBeGreaterThan(0);
|
|
|
|
});
|
|
|
|
|
2023-09-19 19:24:35 +08:00
|
|
|
it("indicates the user knock has been denied", async () => {
|
|
|
|
jest.spyOn(SettingsStore, "getValue").mockImplementation((name) => {
|
|
|
|
return name === "feature_ask_to_join";
|
|
|
|
});
|
2024-03-12 01:16:53 +08:00
|
|
|
const roomMember = mkRoomMember(
|
|
|
|
room.roomId,
|
|
|
|
MatrixClientPeg.get()!.getSafeUserId(),
|
2024-03-12 22:52:54 +08:00
|
|
|
KnownMembership.Leave,
|
2024-03-12 01:16:53 +08:00
|
|
|
true,
|
|
|
|
{
|
2024-03-12 22:52:54 +08:00
|
|
|
membership: KnownMembership.Knock,
|
2024-03-12 01:16:53 +08:00
|
|
|
},
|
|
|
|
);
|
2023-09-19 19:24:35 +08:00
|
|
|
jest.spyOn(room, "getMember").mockReturnValue(roomMember);
|
2024-01-15 23:25:48 +08:00
|
|
|
const { level, symbol, count } = determineUnreadState(room);
|
2023-09-19 19:24:35 +08:00
|
|
|
|
|
|
|
expect(symbol).toBe("!");
|
2024-01-15 23:25:48 +08:00
|
|
|
expect(level).toBe(NotificationLevel.Highlight);
|
2023-09-19 19:24:35 +08:00
|
|
|
expect(count).toBeGreaterThan(0);
|
|
|
|
});
|
|
|
|
|
2023-01-31 17:58:17 +08:00
|
|
|
it("shows nothing for muted channels", async () => {
|
|
|
|
room.setUnreadNotificationCount(NotificationCountType.Highlight, 99);
|
|
|
|
room.setUnreadNotificationCount(NotificationCountType.Total, 99);
|
|
|
|
muteRoom(room);
|
|
|
|
|
2024-01-15 23:25:48 +08:00
|
|
|
const { level, count } = determineUnreadState(room);
|
2023-01-31 17:58:17 +08:00
|
|
|
|
2024-01-15 23:25:48 +08:00
|
|
|
expect(level).toBe(NotificationLevel.None);
|
2023-01-31 17:58:17 +08:00
|
|
|
expect(count).toBe(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("uses the correct number of unreads", async () => {
|
|
|
|
room.setUnreadNotificationCount(NotificationCountType.Total, 999);
|
|
|
|
|
2024-01-15 23:25:48 +08:00
|
|
|
const { level, count } = determineUnreadState(room);
|
2023-01-31 17:58:17 +08:00
|
|
|
|
2024-01-15 23:25:48 +08:00
|
|
|
expect(level).toBe(NotificationLevel.Notification);
|
2023-01-31 17:58:17 +08:00
|
|
|
expect(count).toBe(999);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("uses the correct number of highlights", async () => {
|
|
|
|
room.setUnreadNotificationCount(NotificationCountType.Highlight, 888);
|
|
|
|
|
2024-01-15 23:25:48 +08:00
|
|
|
const { level, count } = determineUnreadState(room);
|
2023-01-31 17:58:17 +08:00
|
|
|
|
2024-01-15 23:25:48 +08:00
|
|
|
expect(level).toBe(NotificationLevel.Highlight);
|
2023-01-31 17:58:17 +08:00
|
|
|
expect(count).toBe(888);
|
|
|
|
});
|
|
|
|
});
|
2022-03-04 17:39:16 +08:00
|
|
|
});
|