2022-07-18 12:00:56 +00: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 { mocked } from "jest-mock";
|
2022-12-12 14:58:30 +00:00
|
|
|
import { MatrixEvent, EventType, MsgType, Room } from "matrix-js-sdk/src/matrix";
|
2022-12-12 14:34:05 +00:00
|
|
|
import { ReceiptType } from "matrix-js-sdk/src/@types/read_receipts";
|
2022-07-18 12:00:56 +00:00
|
|
|
|
|
|
|
import { haveRendererForEvent } from "../src/events/EventTileFactory";
|
2022-12-12 14:34:05 +00:00
|
|
|
import { makeBeaconEvent, mkEvent, stubClient } from "./test-utils";
|
|
|
|
import { mkThread } from "./test-utils/threads";
|
|
|
|
import { doesRoomHaveUnreadMessages, eventTriggersUnreadCount } from "../src/Unread";
|
|
|
|
import { MatrixClientPeg } from "../src/MatrixClientPeg";
|
2022-07-18 12:00:56 +00:00
|
|
|
|
|
|
|
jest.mock("../src/events/EventTileFactory", () => ({
|
|
|
|
haveRendererForEvent: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
2022-12-12 14:34:05 +00:00
|
|
|
describe("Unread", () => {
|
|
|
|
// A different user.
|
2022-12-12 11:24:14 +00:00
|
|
|
const aliceId = "@alice:server.org";
|
2022-12-12 14:34:05 +00:00
|
|
|
stubClient();
|
|
|
|
const client = MatrixClientPeg.get();
|
2022-07-18 12:00:56 +00:00
|
|
|
|
2022-12-12 14:58:30 +00:00
|
|
|
describe("eventTriggersUnreadCount()", () => {
|
2022-12-12 14:34:05 +00:00
|
|
|
// setup events
|
|
|
|
const alicesMessage = new MatrixEvent({
|
|
|
|
type: EventType.RoomMessage,
|
|
|
|
sender: aliceId,
|
|
|
|
content: {
|
|
|
|
msgtype: MsgType.Text,
|
2022-12-12 14:58:30 +00:00
|
|
|
body: "Hello from Alice",
|
2022-12-12 14:34:05 +00:00
|
|
|
},
|
|
|
|
});
|
2022-07-18 12:00:56 +00:00
|
|
|
|
2022-12-12 14:34:05 +00:00
|
|
|
const ourMessage = new MatrixEvent({
|
|
|
|
type: EventType.RoomMessage,
|
|
|
|
sender: client.getUserId()!,
|
|
|
|
content: {
|
|
|
|
msgtype: MsgType.Text,
|
2022-12-12 14:58:30 +00:00
|
|
|
body: "Hello from Bob",
|
2022-12-12 14:34:05 +00:00
|
|
|
},
|
|
|
|
});
|
2022-07-18 12:00:56 +00:00
|
|
|
|
2022-12-12 14:34:05 +00:00
|
|
|
const redactedEvent = new MatrixEvent({
|
|
|
|
type: EventType.RoomMessage,
|
|
|
|
sender: aliceId,
|
|
|
|
});
|
|
|
|
redactedEvent.makeRedacted(redactedEvent);
|
2022-07-18 12:00:56 +00:00
|
|
|
|
2022-12-12 14:34:05 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
mocked(haveRendererForEvent).mockClear().mockReturnValue(false);
|
|
|
|
});
|
2022-07-18 12:00:56 +00:00
|
|
|
|
2022-12-12 14:58:30 +00:00
|
|
|
it("returns false when the event was sent by the current user", () => {
|
2022-12-12 14:34:05 +00:00
|
|
|
expect(eventTriggersUnreadCount(ourMessage)).toBe(false);
|
|
|
|
// returned early before checking renderer
|
|
|
|
expect(haveRendererForEvent).not.toHaveBeenCalled();
|
|
|
|
});
|
2022-07-18 12:00:56 +00:00
|
|
|
|
2022-12-12 14:58:30 +00:00
|
|
|
it("returns false for a redacted event", () => {
|
2022-12-12 14:34:05 +00:00
|
|
|
expect(eventTriggersUnreadCount(redactedEvent)).toBe(false);
|
|
|
|
// returned early before checking renderer
|
|
|
|
expect(haveRendererForEvent).not.toHaveBeenCalled();
|
|
|
|
});
|
2022-07-18 12:00:56 +00:00
|
|
|
|
2022-12-12 14:58:30 +00:00
|
|
|
it("returns false for an event without a renderer", () => {
|
2022-12-12 14:34:05 +00:00
|
|
|
mocked(haveRendererForEvent).mockReturnValue(false);
|
|
|
|
expect(eventTriggersUnreadCount(alicesMessage)).toBe(false);
|
|
|
|
expect(haveRendererForEvent).toHaveBeenCalledWith(alicesMessage, false);
|
|
|
|
});
|
2022-07-18 12:00:56 +00:00
|
|
|
|
2022-12-12 14:58:30 +00:00
|
|
|
it("returns true for an event with a renderer", () => {
|
2022-12-12 14:34:05 +00:00
|
|
|
mocked(haveRendererForEvent).mockReturnValue(true);
|
|
|
|
expect(eventTriggersUnreadCount(alicesMessage)).toBe(true);
|
|
|
|
expect(haveRendererForEvent).toHaveBeenCalledWith(alicesMessage, false);
|
|
|
|
});
|
2022-07-18 12:00:56 +00:00
|
|
|
|
2022-12-12 14:58:30 +00:00
|
|
|
it("returns false for beacon locations", () => {
|
2022-12-12 14:34:05 +00:00
|
|
|
const beaconLocationEvent = makeBeaconEvent(aliceId);
|
|
|
|
expect(eventTriggersUnreadCount(beaconLocationEvent)).toBe(false);
|
|
|
|
expect(haveRendererForEvent).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
const noUnreadEventTypes = [
|
|
|
|
EventType.RoomMember,
|
|
|
|
EventType.RoomThirdPartyInvite,
|
|
|
|
EventType.CallAnswer,
|
|
|
|
EventType.CallHangup,
|
|
|
|
EventType.RoomCanonicalAlias,
|
|
|
|
EventType.RoomServerAcl,
|
|
|
|
];
|
2022-07-18 12:00:56 +00:00
|
|
|
|
2022-12-12 14:34:05 +00:00
|
|
|
it.each(noUnreadEventTypes)(
|
2022-12-12 14:58:30 +00:00
|
|
|
"returns false without checking for renderer for events with type %s",
|
2022-12-12 14:34:05 +00:00
|
|
|
(eventType) => {
|
|
|
|
const event = new MatrixEvent({
|
|
|
|
type: eventType,
|
|
|
|
sender: aliceId,
|
|
|
|
});
|
|
|
|
expect(eventTriggersUnreadCount(event)).toBe(false);
|
|
|
|
expect(haveRendererForEvent).not.toHaveBeenCalled();
|
|
|
|
},
|
|
|
|
);
|
2022-07-18 14:23:24 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 14:34:05 +00:00
|
|
|
describe("doesRoomHaveUnreadMessages()", () => {
|
|
|
|
let room: Room;
|
|
|
|
let event: MatrixEvent;
|
|
|
|
const roomId = "!abc:server.org";
|
|
|
|
const myId = client.getUserId()!;
|
|
|
|
|
|
|
|
beforeAll(() => {
|
|
|
|
client.supportsExperimentalThreads = () => true;
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
// Create a room and initial event in it.
|
|
|
|
room = new Room(roomId, client, myId);
|
|
|
|
event = mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: "m.room.message",
|
|
|
|
user: aliceId,
|
|
|
|
room: roomId,
|
|
|
|
content: {},
|
|
|
|
});
|
|
|
|
room.addLiveEvents([event]);
|
|
|
|
|
|
|
|
// Don't care about the code path of hidden events.
|
|
|
|
mocked(haveRendererForEvent).mockClear().mockReturnValue(true);
|
|
|
|
});
|
|
|
|
|
2022-12-12 14:58:30 +00:00
|
|
|
it("returns true for a room with no receipts", () => {
|
2022-12-12 14:34:05 +00:00
|
|
|
expect(doesRoomHaveUnreadMessages(room)).toBe(true);
|
|
|
|
});
|
|
|
|
|
2022-12-12 14:58:30 +00:00
|
|
|
it("returns false for a room when the latest event was sent by the current user", () => {
|
2022-12-12 14:34:05 +00:00
|
|
|
event = mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: "m.room.message",
|
|
|
|
user: myId,
|
|
|
|
room: roomId,
|
|
|
|
content: {},
|
|
|
|
});
|
|
|
|
// Only for timeline events.
|
|
|
|
room.addLiveEvents([event]);
|
|
|
|
|
|
|
|
expect(doesRoomHaveUnreadMessages(room)).toBe(false);
|
|
|
|
});
|
|
|
|
|
2022-12-12 14:58:30 +00:00
|
|
|
it("returns false for a room when the read receipt is at the latest event", () => {
|
2022-12-12 14:34:05 +00:00
|
|
|
const receipt = new MatrixEvent({
|
|
|
|
type: "m.receipt",
|
|
|
|
room_id: "!foo:bar",
|
|
|
|
content: {
|
|
|
|
[event.getId()!]: {
|
|
|
|
[ReceiptType.Read]: {
|
|
|
|
[myId]: { ts: 1 },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
room.addReceipt(receipt);
|
|
|
|
|
|
|
|
expect(doesRoomHaveUnreadMessages(room)).toBe(false);
|
|
|
|
});
|
|
|
|
|
2022-12-12 14:58:30 +00:00
|
|
|
it("returns true for a room when the read receipt is earlier than the latest event", () => {
|
2022-12-12 14:34:05 +00:00
|
|
|
const receipt = new MatrixEvent({
|
|
|
|
type: "m.receipt",
|
|
|
|
room_id: "!foo:bar",
|
|
|
|
content: {
|
|
|
|
[event.getId()!]: {
|
|
|
|
[ReceiptType.Read]: {
|
|
|
|
[myId]: { ts: 1 },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
room.addReceipt(receipt);
|
|
|
|
|
|
|
|
const event2 = mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: "m.room.message",
|
|
|
|
user: aliceId,
|
|
|
|
room: roomId,
|
|
|
|
content: {},
|
|
|
|
});
|
|
|
|
// Only for timeline events.
|
|
|
|
room.addLiveEvents([event2]);
|
|
|
|
|
|
|
|
expect(doesRoomHaveUnreadMessages(room)).toBe(true);
|
|
|
|
});
|
|
|
|
|
2022-12-12 14:58:30 +00:00
|
|
|
it("returns true for a room with an unread message in a thread", () => {
|
2022-12-12 14:34:05 +00:00
|
|
|
// Mark the main timeline as read.
|
|
|
|
const receipt = new MatrixEvent({
|
|
|
|
type: "m.receipt",
|
|
|
|
room_id: "!foo:bar",
|
|
|
|
content: {
|
|
|
|
[event.getId()!]: {
|
|
|
|
[ReceiptType.Read]: {
|
|
|
|
[myId]: { ts: 1 },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
room.addReceipt(receipt);
|
|
|
|
|
|
|
|
// Create a thread as a different user.
|
|
|
|
mkThread({ room, client, authorId: myId, participantUserIds: [aliceId] });
|
|
|
|
|
|
|
|
expect(doesRoomHaveUnreadMessages(room)).toBe(true);
|
|
|
|
});
|
|
|
|
|
2022-12-12 14:58:30 +00:00
|
|
|
it("returns false for a room when the latest thread event was sent by the current user", () => {
|
2022-12-12 14:34:05 +00:00
|
|
|
// Mark the main timeline as read.
|
|
|
|
const receipt = new MatrixEvent({
|
|
|
|
type: "m.receipt",
|
|
|
|
room_id: "!foo:bar",
|
|
|
|
content: {
|
|
|
|
[event.getId()!]: {
|
|
|
|
[ReceiptType.Read]: {
|
|
|
|
[myId]: { ts: 1 },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
room.addReceipt(receipt);
|
|
|
|
|
|
|
|
// Create a thread as the current user.
|
|
|
|
mkThread({ room, client, authorId: myId, participantUserIds: [myId] });
|
|
|
|
|
|
|
|
expect(doesRoomHaveUnreadMessages(room)).toBe(false);
|
|
|
|
});
|
|
|
|
|
2022-12-12 14:58:30 +00:00
|
|
|
it("returns false for a room with read thread messages", () => {
|
2022-12-12 14:34:05 +00:00
|
|
|
// Mark the main timeline as read.
|
|
|
|
let receipt = new MatrixEvent({
|
|
|
|
type: "m.receipt",
|
|
|
|
room_id: "!foo:bar",
|
|
|
|
content: {
|
|
|
|
[event.getId()!]: {
|
|
|
|
[ReceiptType.Read]: {
|
|
|
|
[myId]: { ts: 1 },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
room.addReceipt(receipt);
|
|
|
|
|
|
|
|
// Create threads.
|
2022-12-12 14:58:30 +00:00
|
|
|
const { rootEvent, events } = mkThread({ room, client, authorId: myId, participantUserIds: [aliceId] });
|
2022-12-12 14:34:05 +00:00
|
|
|
|
|
|
|
// Mark the thread as read.
|
|
|
|
receipt = new MatrixEvent({
|
|
|
|
type: "m.receipt",
|
|
|
|
room_id: "!foo:bar",
|
|
|
|
content: {
|
|
|
|
[events[events.length - 1].getId()!]: {
|
|
|
|
[ReceiptType.Read]: {
|
|
|
|
[myId]: { ts: 1, thread_id: rootEvent.getId()! },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
room.addReceipt(receipt);
|
|
|
|
|
|
|
|
expect(doesRoomHaveUnreadMessages(room)).toBe(false);
|
|
|
|
});
|
|
|
|
|
2022-12-12 14:58:30 +00:00
|
|
|
it("returns true for a room when read receipt is not on the latest thread messages", () => {
|
2022-12-12 14:34:05 +00:00
|
|
|
// Mark the main timeline as read.
|
|
|
|
let receipt = new MatrixEvent({
|
|
|
|
type: "m.receipt",
|
|
|
|
room_id: "!foo:bar",
|
|
|
|
content: {
|
|
|
|
[event.getId()!]: {
|
|
|
|
[ReceiptType.Read]: {
|
|
|
|
[myId]: { ts: 1 },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
room.addReceipt(receipt);
|
|
|
|
|
|
|
|
// Create threads.
|
2022-12-12 14:58:30 +00:00
|
|
|
const { rootEvent, events } = mkThread({ room, client, authorId: myId, participantUserIds: [aliceId] });
|
2022-12-12 14:34:05 +00:00
|
|
|
|
|
|
|
// Mark the thread as read.
|
|
|
|
receipt = new MatrixEvent({
|
|
|
|
type: "m.receipt",
|
|
|
|
room_id: "!foo:bar",
|
|
|
|
content: {
|
|
|
|
[events[0].getId()!]: {
|
|
|
|
[ReceiptType.Read]: {
|
|
|
|
[myId]: { ts: 1, threadId: rootEvent.getId()! },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
room.addReceipt(receipt);
|
|
|
|
|
|
|
|
expect(doesRoomHaveUnreadMessages(room)).toBe(true);
|
2022-07-18 12:00:56 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|