2022-07-18 20:00:56 +08:00
|
|
|
/*
|
2024-09-09 21:57:16 +08:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-07-18 20:00:56 +08:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 21:57:16 +08:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2022-07-18 20:00:56 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
import { mocked } from "jest-mock";
|
2023-08-23 17:04:25 +08:00
|
|
|
import { MatrixEvent, EventType, MsgType, Room, ReceiptType } from "matrix-js-sdk/src/matrix";
|
2023-04-11 15:41:59 +08:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2022-07-18 20:00:56 +08:00
|
|
|
|
2024-10-15 21:57:26 +08:00
|
|
|
import { haveRendererForEvent } from "../../src/events/EventTileFactory";
|
|
|
|
import { makeBeaconEvent, mkEvent, stubClient } from "../test-utils";
|
|
|
|
import { makeThreadEvents, mkThread, populateThread } from "../test-utils/threads";
|
2023-06-21 23:07:16 +08:00
|
|
|
import {
|
|
|
|
doesRoomHaveUnreadMessages,
|
2024-02-07 21:49:40 +08:00
|
|
|
doesRoomHaveUnreadThreads,
|
2023-06-21 23:07:16 +08:00
|
|
|
doesRoomOrThreadHaveUnreadMessages,
|
|
|
|
eventTriggersUnreadCount,
|
2024-10-15 21:57:26 +08:00
|
|
|
} from "../../src/Unread";
|
|
|
|
import { MatrixClientPeg } from "../../src/MatrixClientPeg";
|
2022-07-18 20:00:56 +08:00
|
|
|
|
2024-10-15 21:57:26 +08:00
|
|
|
jest.mock("../../src/events/EventTileFactory", () => ({
|
2022-07-18 20:00:56 +08:00
|
|
|
haveRendererForEvent: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
2023-01-11 19:49:03 +08:00
|
|
|
describe("Unread", () => {
|
|
|
|
// A different user.
|
2022-12-12 19:24:14 +08:00
|
|
|
const aliceId = "@alice:server.org";
|
2023-01-11 19:49:03 +08:00
|
|
|
stubClient();
|
2023-06-06 01:12:23 +08:00
|
|
|
const client = MatrixClientPeg.safeGet();
|
2022-07-18 20:00:56 +08:00
|
|
|
|
2023-01-11 19:49:03 +08:00
|
|
|
describe("eventTriggersUnreadCount()", () => {
|
|
|
|
// setup events
|
|
|
|
const alicesMessage = new MatrixEvent({
|
|
|
|
type: EventType.RoomMessage,
|
|
|
|
sender: aliceId,
|
|
|
|
content: {
|
|
|
|
msgtype: MsgType.Text,
|
|
|
|
body: "Hello from Alice",
|
|
|
|
},
|
|
|
|
});
|
2022-07-18 22:23:24 +08:00
|
|
|
|
2023-01-11 19:49:03 +08:00
|
|
|
const ourMessage = new MatrixEvent({
|
|
|
|
type: EventType.RoomMessage,
|
|
|
|
sender: client.getUserId()!,
|
|
|
|
content: {
|
|
|
|
msgtype: MsgType.Text,
|
|
|
|
body: "Hello from Bob",
|
|
|
|
},
|
|
|
|
});
|
2022-12-12 22:34:05 +08:00
|
|
|
|
2023-01-11 19:49:03 +08:00
|
|
|
const redactedEvent = new MatrixEvent({
|
|
|
|
type: EventType.RoomMessage,
|
|
|
|
sender: aliceId,
|
|
|
|
});
|
2024-03-25 20:21:02 +08:00
|
|
|
redactedEvent.makeRedacted(redactedEvent, new Room(redactedEvent.getRoomId()!, client, aliceId));
|
2022-12-12 22:34:05 +08:00
|
|
|
|
2023-01-11 19:49:03 +08:00
|
|
|
beforeEach(() => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
mocked(haveRendererForEvent).mockClear().mockReturnValue(false);
|
|
|
|
});
|
2022-12-12 22:34:05 +08:00
|
|
|
|
2023-01-11 19:49:03 +08:00
|
|
|
it("returns false when the event was sent by the current user", () => {
|
2023-05-30 17:36:34 +08:00
|
|
|
expect(eventTriggersUnreadCount(client, ourMessage)).toBe(false);
|
2023-01-11 19:49:03 +08:00
|
|
|
// returned early before checking renderer
|
|
|
|
expect(haveRendererForEvent).not.toHaveBeenCalled();
|
|
|
|
});
|
2022-12-12 22:34:05 +08:00
|
|
|
|
2023-01-11 19:49:03 +08:00
|
|
|
it("returns false for a redacted event", () => {
|
2023-05-30 17:36:34 +08:00
|
|
|
expect(eventTriggersUnreadCount(client, redactedEvent)).toBe(false);
|
2023-01-11 19:49:03 +08:00
|
|
|
// returned early before checking renderer
|
|
|
|
expect(haveRendererForEvent).not.toHaveBeenCalled();
|
|
|
|
});
|
2022-12-12 22:34:05 +08:00
|
|
|
|
2023-01-11 19:49:03 +08:00
|
|
|
it("returns false for an event without a renderer", () => {
|
|
|
|
mocked(haveRendererForEvent).mockReturnValue(false);
|
2023-05-30 17:36:34 +08:00
|
|
|
expect(eventTriggersUnreadCount(client, alicesMessage)).toBe(false);
|
2023-06-22 00:29:44 +08:00
|
|
|
expect(haveRendererForEvent).toHaveBeenCalledWith(alicesMessage, client, false);
|
2023-01-11 19:49:03 +08:00
|
|
|
});
|
2022-12-12 22:34:05 +08:00
|
|
|
|
2023-01-11 19:49:03 +08:00
|
|
|
it("returns true for an event with a renderer", () => {
|
|
|
|
mocked(haveRendererForEvent).mockReturnValue(true);
|
2023-05-30 17:36:34 +08:00
|
|
|
expect(eventTriggersUnreadCount(client, alicesMessage)).toBe(true);
|
2023-06-22 00:29:44 +08:00
|
|
|
expect(haveRendererForEvent).toHaveBeenCalledWith(alicesMessage, client, false);
|
2023-01-11 19:49:03 +08:00
|
|
|
});
|
2022-12-12 22:34:05 +08:00
|
|
|
|
2023-01-11 19:49:03 +08:00
|
|
|
it("returns false for beacon locations", () => {
|
|
|
|
const beaconLocationEvent = makeBeaconEvent(aliceId);
|
2023-05-30 17:36:34 +08:00
|
|
|
expect(eventTriggersUnreadCount(client, beaconLocationEvent)).toBe(false);
|
2023-01-11 19:49:03 +08:00
|
|
|
expect(haveRendererForEvent).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
const noUnreadEventTypes = [
|
|
|
|
EventType.RoomMember,
|
|
|
|
EventType.RoomThirdPartyInvite,
|
|
|
|
EventType.CallAnswer,
|
|
|
|
EventType.CallHangup,
|
|
|
|
EventType.RoomCanonicalAlias,
|
|
|
|
EventType.RoomServerAcl,
|
|
|
|
];
|
2022-12-12 22:34:05 +08:00
|
|
|
|
2023-01-11 19:49:03 +08:00
|
|
|
it.each(noUnreadEventTypes)(
|
|
|
|
"returns false without checking for renderer for events with type %s",
|
|
|
|
(eventType) => {
|
|
|
|
const event = new MatrixEvent({
|
|
|
|
type: eventType,
|
|
|
|
sender: aliceId,
|
|
|
|
});
|
2023-05-30 17:36:34 +08:00
|
|
|
expect(eventTriggersUnreadCount(client, event)).toBe(false);
|
2023-01-11 19:49:03 +08:00
|
|
|
expect(haveRendererForEvent).not.toHaveBeenCalled();
|
|
|
|
},
|
|
|
|
);
|
2022-12-13 22:59:52 +08:00
|
|
|
});
|
2022-12-12 22:34:05 +08:00
|
|
|
|
2023-01-11 19:49:03 +08:00
|
|
|
describe("doesRoomHaveUnreadMessages()", () => {
|
|
|
|
let room: Room;
|
|
|
|
let event: MatrixEvent;
|
|
|
|
const roomId = "!abc:server.org";
|
2023-06-21 23:07:16 +08:00
|
|
|
const myId = client.getSafeUserId();
|
2023-01-11 19:49:03 +08:00
|
|
|
|
|
|
|
beforeAll(() => {
|
2023-01-30 20:20:11 +08:00
|
|
|
client.supportsThreads = () => true;
|
2023-01-11 19:49:03 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
room = new Room(roomId, client, myId);
|
2023-04-11 15:41:59 +08:00
|
|
|
jest.spyOn(logger, "warn");
|
2023-01-11 19:49:03 +08:00
|
|
|
});
|
|
|
|
|
2023-04-11 15:41:59 +08:00
|
|
|
describe("when there is an initial event in the room", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
event = mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: "m.room.message",
|
|
|
|
user: aliceId,
|
|
|
|
room: roomId,
|
|
|
|
content: {},
|
|
|
|
});
|
|
|
|
room.addLiveEvents([event]);
|
2023-01-11 19:49:03 +08:00
|
|
|
|
2023-04-11 15:41:59 +08:00
|
|
|
// Don't care about the code path of hidden events.
|
|
|
|
mocked(haveRendererForEvent).mockClear().mockReturnValue(true);
|
2023-01-11 19:49:03 +08:00
|
|
|
});
|
|
|
|
|
2023-04-11 15:41:59 +08:00
|
|
|
it("returns true for a room with no receipts", () => {
|
2024-01-30 01:52:48 +08:00
|
|
|
expect(doesRoomHaveUnreadMessages(room, false)).toBe(true);
|
2023-04-11 15:41:59 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("returns false for a room when the latest event was sent by the current user", () => {
|
|
|
|
event = mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: "m.room.message",
|
|
|
|
user: myId,
|
|
|
|
room: roomId,
|
|
|
|
content: {},
|
|
|
|
});
|
|
|
|
// Only for timeline events.
|
|
|
|
room.addLiveEvents([event]);
|
2023-01-11 19:49:03 +08:00
|
|
|
|
2024-01-30 01:52:48 +08:00
|
|
|
expect(doesRoomHaveUnreadMessages(room, false)).toBe(false);
|
2023-04-11 15:41:59 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("returns false for a room when the read receipt is at the latest event", () => {
|
|
|
|
const receipt = new MatrixEvent({
|
|
|
|
type: "m.receipt",
|
|
|
|
room_id: "!foo:bar",
|
|
|
|
content: {
|
|
|
|
[event.getId()!]: {
|
|
|
|
[ReceiptType.Read]: {
|
|
|
|
[myId]: { ts: 1 },
|
|
|
|
},
|
2023-01-11 19:49:03 +08:00
|
|
|
},
|
|
|
|
},
|
2023-04-11 15:41:59 +08:00
|
|
|
});
|
|
|
|
room.addReceipt(receipt);
|
2023-01-11 19:49:03 +08:00
|
|
|
|
2024-01-30 01:52:48 +08:00
|
|
|
expect(doesRoomHaveUnreadMessages(room, false)).toBe(false);
|
2023-04-11 15:41:59 +08:00
|
|
|
});
|
2023-01-11 19:49:03 +08:00
|
|
|
|
2023-04-11 15:41:59 +08:00
|
|
|
it("returns true for a room when the read receipt is earlier than the latest event", () => {
|
|
|
|
const receipt = new MatrixEvent({
|
|
|
|
type: "m.receipt",
|
|
|
|
room_id: "!foo:bar",
|
|
|
|
content: {
|
|
|
|
[event.getId()!]: {
|
|
|
|
[ReceiptType.Read]: {
|
|
|
|
[myId]: { ts: 1 },
|
|
|
|
},
|
2023-01-11 19:49:03 +08:00
|
|
|
},
|
|
|
|
},
|
2023-04-11 15:41:59 +08:00
|
|
|
});
|
|
|
|
room.addReceipt(receipt);
|
|
|
|
|
|
|
|
const event2 = mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: "m.room.message",
|
|
|
|
user: aliceId,
|
|
|
|
room: roomId,
|
|
|
|
content: {},
|
|
|
|
});
|
|
|
|
// Only for timeline events.
|
|
|
|
room.addLiveEvents([event2]);
|
2023-01-11 19:49:03 +08:00
|
|
|
|
2024-01-30 01:52:48 +08:00
|
|
|
expect(doesRoomHaveUnreadMessages(room, false)).toBe(true);
|
2023-01-11 19:49:03 +08:00
|
|
|
});
|
|
|
|
|
2023-11-29 21:36:52 +08:00
|
|
|
it("returns true for a room with an unread message in a thread", async () => {
|
2023-04-11 15:41:59 +08: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 },
|
|
|
|
},
|
2023-01-11 19:49:03 +08:00
|
|
|
},
|
|
|
|
},
|
2023-04-11 15:41:59 +08:00
|
|
|
});
|
|
|
|
room.addReceipt(receipt);
|
2023-01-11 19:49:03 +08:00
|
|
|
|
2023-11-28 21:59:03 +08:00
|
|
|
// Create a read thread, so we don't consider all threads read
|
|
|
|
// because there are no threaded read receipts.
|
|
|
|
const { rootEvent, events } = mkThread({ room, client, authorId: myId, participantUserIds: [aliceId] });
|
|
|
|
const receipt2 = 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(receipt2);
|
|
|
|
|
2023-04-11 15:41:59 +08:00
|
|
|
// Create a thread as a different user.
|
2023-11-29 21:36:52 +08:00
|
|
|
await populateThread({ room, client, authorId: myId, participantUserIds: [aliceId] });
|
2023-01-11 19:49:03 +08:00
|
|
|
|
2024-01-30 01:52:48 +08:00
|
|
|
expect(doesRoomHaveUnreadMessages(room, true)).toBe(true);
|
2023-04-11 15:41:59 +08:00
|
|
|
});
|
2023-01-11 19:49:03 +08:00
|
|
|
|
2023-11-29 21:36:52 +08:00
|
|
|
it("returns false for a room when the latest thread event was sent by the current user", async () => {
|
2023-04-11 15:41:59 +08: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 },
|
|
|
|
},
|
2023-01-11 19:49:03 +08:00
|
|
|
},
|
|
|
|
},
|
2023-04-11 15:41:59 +08:00
|
|
|
});
|
|
|
|
room.addReceipt(receipt);
|
2023-01-11 19:49:03 +08:00
|
|
|
|
2023-04-11 15:41:59 +08:00
|
|
|
// Create a thread as the current user.
|
2023-11-29 21:36:52 +08:00
|
|
|
await populateThread({ room, client, authorId: myId, participantUserIds: [myId] });
|
2023-01-11 19:49:03 +08:00
|
|
|
|
2024-01-30 01:52:48 +08:00
|
|
|
expect(doesRoomHaveUnreadMessages(room, true)).toBe(false);
|
2023-04-11 15:41:59 +08:00
|
|
|
});
|
2023-01-11 19:49:03 +08:00
|
|
|
|
2023-11-29 21:36:52 +08:00
|
|
|
it("returns false for a room with read thread messages", async () => {
|
2023-04-11 15:41:59 +08: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 },
|
|
|
|
},
|
2023-01-11 19:49:03 +08:00
|
|
|
},
|
|
|
|
},
|
2023-04-11 15:41:59 +08:00
|
|
|
});
|
|
|
|
room.addReceipt(receipt);
|
|
|
|
|
|
|
|
// Create threads.
|
2023-11-29 21:36:52 +08:00
|
|
|
const { rootEvent, events } = await populateThread({
|
|
|
|
room,
|
|
|
|
client,
|
|
|
|
authorId: myId,
|
|
|
|
participantUserIds: [aliceId],
|
|
|
|
});
|
2023-04-11 15:41:59 +08: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()! },
|
|
|
|
},
|
2023-01-11 19:49:03 +08:00
|
|
|
},
|
|
|
|
},
|
2023-04-11 15:41:59 +08:00
|
|
|
});
|
|
|
|
room.addReceipt(receipt);
|
2023-01-11 19:49:03 +08:00
|
|
|
|
2024-01-30 01:52:48 +08:00
|
|
|
expect(doesRoomHaveUnreadMessages(room, true)).toBe(false);
|
2023-04-11 15:41:59 +08:00
|
|
|
});
|
2023-01-11 19:49:03 +08:00
|
|
|
|
2023-11-29 21:36:52 +08:00
|
|
|
it("returns true for a room when read receipt is not on the latest thread messages", async () => {
|
2023-04-11 15:41:59 +08: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 },
|
|
|
|
},
|
2023-01-11 19:49:03 +08:00
|
|
|
},
|
|
|
|
},
|
2023-04-11 15:41:59 +08:00
|
|
|
});
|
|
|
|
room.addReceipt(receipt);
|
|
|
|
|
|
|
|
// Create threads.
|
2023-11-29 21:36:52 +08:00
|
|
|
const { rootEvent, events } = await populateThread({
|
|
|
|
room,
|
|
|
|
client,
|
|
|
|
authorId: myId,
|
|
|
|
participantUserIds: [aliceId],
|
|
|
|
});
|
2023-04-11 15:41:59 +08:00
|
|
|
|
|
|
|
// Mark the thread as read.
|
|
|
|
receipt = new MatrixEvent({
|
|
|
|
type: "m.receipt",
|
|
|
|
room_id: "!foo:bar",
|
|
|
|
content: {
|
|
|
|
[events[0].getId()!]: {
|
|
|
|
[ReceiptType.Read]: {
|
2023-11-28 21:59:03 +08:00
|
|
|
[myId]: { ts: 1, thread_id: rootEvent.getId()! },
|
2023-04-11 15:41:59 +08:00
|
|
|
},
|
2023-01-11 19:49:03 +08:00
|
|
|
},
|
|
|
|
},
|
2023-04-11 15:41:59 +08:00
|
|
|
});
|
|
|
|
room.addReceipt(receipt);
|
|
|
|
|
2024-01-30 01:52:48 +08:00
|
|
|
expect(doesRoomHaveUnreadMessages(room, true)).toBe(true);
|
2023-05-26 22:14:47 +08:00
|
|
|
});
|
|
|
|
|
2023-11-29 21:36:52 +08:00
|
|
|
it("returns true when the event for a thread receipt can't be found", async () => {
|
2023-05-26 22:14:47 +08:00
|
|
|
// Given a room that is read
|
|
|
|
let receipt = new MatrixEvent({
|
|
|
|
type: "m.receipt",
|
|
|
|
room_id: "!foo:bar",
|
|
|
|
content: {
|
|
|
|
[event.getId()!]: {
|
|
|
|
[ReceiptType.Read]: {
|
|
|
|
[myId]: { ts: 1 },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
room.addReceipt(receipt);
|
|
|
|
|
|
|
|
// And a thread
|
2023-11-29 21:36:52 +08:00
|
|
|
const { rootEvent, events } = await populateThread({
|
2023-11-28 21:59:03 +08:00
|
|
|
room,
|
|
|
|
client,
|
|
|
|
authorId: myId,
|
|
|
|
participantUserIds: [aliceId],
|
|
|
|
});
|
2023-05-26 22:14:47 +08:00
|
|
|
|
|
|
|
// When we provide a receipt that points at an unknown event,
|
|
|
|
// but its timestamp is before some of the events in the thread
|
|
|
|
//
|
|
|
|
// (This could happen if we mis-filed a reaction into the main
|
|
|
|
// thread when it should actually have gone into this thread, or
|
|
|
|
// maybe the event is just not loaded for some reason.)
|
|
|
|
const receiptTs = (events.at(-1)?.getTs() ?? 0) - 100;
|
|
|
|
receipt = new MatrixEvent({
|
|
|
|
type: "m.receipt",
|
|
|
|
room_id: "!foo:bar",
|
|
|
|
content: {
|
|
|
|
["UNKNOWN_EVENT_ID"]: {
|
|
|
|
[ReceiptType.Read]: {
|
2023-11-28 21:59:03 +08:00
|
|
|
[myId]: { ts: receiptTs, thread_id: rootEvent.getId()! },
|
2023-05-26 22:14:47 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
room.addReceipt(receipt);
|
|
|
|
|
2024-01-30 01:52:48 +08:00
|
|
|
expect(doesRoomHaveUnreadMessages(room, true)).toBe(true);
|
2023-04-11 15:41:59 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("returns true for a room that only contains a hidden event", () => {
|
|
|
|
const redactedEvent = mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: "m.room.message",
|
|
|
|
user: aliceId,
|
|
|
|
room: roomId,
|
|
|
|
content: {},
|
2023-01-11 19:49:03 +08:00
|
|
|
});
|
2023-04-11 15:41:59 +08:00
|
|
|
console.log("Event Id", redactedEvent.getId());
|
2024-03-25 20:21:02 +08:00
|
|
|
redactedEvent.makeRedacted(redactedEvent, room);
|
2023-04-11 15:41:59 +08:00
|
|
|
console.log("Event Id", redactedEvent.getId());
|
|
|
|
// Only for timeline events.
|
|
|
|
room.addLiveEvents([redactedEvent]);
|
2023-01-11 19:49:03 +08:00
|
|
|
|
2024-01-30 01:52:48 +08:00
|
|
|
expect(doesRoomHaveUnreadMessages(room, true)).toBe(true);
|
2023-04-11 15:41:59 +08:00
|
|
|
expect(logger.warn).toHaveBeenCalledWith(
|
|
|
|
"Falling back to unread room because of no read receipt or counting message found",
|
|
|
|
{
|
2023-11-29 21:36:52 +08:00
|
|
|
roomId: room.roomId,
|
|
|
|
earliestUnimportantEventId: redactedEvent.getId(),
|
2023-04-11 15:41:59 +08:00
|
|
|
},
|
|
|
|
);
|
2022-07-18 20:00:56 +08:00
|
|
|
});
|
2024-02-02 01:58:57 +08:00
|
|
|
|
|
|
|
it("returns false for space", () => {
|
|
|
|
jest.spyOn(room, "isSpaceRoom").mockReturnValue(true);
|
|
|
|
expect(doesRoomHaveUnreadMessages(room, false)).toBe(false);
|
|
|
|
});
|
2022-07-18 20:00:56 +08:00
|
|
|
});
|
2023-06-21 23:07:16 +08:00
|
|
|
|
|
|
|
describe("doesRoomOrThreadHaveUnreadMessages()", () => {
|
|
|
|
let room: Room;
|
|
|
|
let event: MatrixEvent;
|
|
|
|
const roomId = "!abc:server.org";
|
|
|
|
const myId = client.getSafeUserId();
|
|
|
|
|
|
|
|
beforeAll(() => {
|
|
|
|
client.supportsThreads = () => true;
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
room = new Room(roomId, client, myId);
|
|
|
|
jest.spyOn(logger, "warn");
|
|
|
|
|
|
|
|
// Don't care about the code path of hidden events.
|
|
|
|
mocked(haveRendererForEvent).mockClear().mockReturnValue(true);
|
|
|
|
});
|
|
|
|
|
2023-11-29 21:36:52 +08:00
|
|
|
describe("with a single event on the main timeline", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
event = mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: "m.room.message",
|
|
|
|
user: aliceId,
|
|
|
|
room: roomId,
|
|
|
|
content: {},
|
|
|
|
});
|
|
|
|
room.addLiveEvents([event]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("an unthreaded receipt for the event makes the room read", () => {
|
|
|
|
// Send unthreaded receipt into room pointing at the latest event
|
|
|
|
room.addReceipt(
|
|
|
|
new MatrixEvent({
|
|
|
|
type: "m.receipt",
|
|
|
|
room_id: "!foo:bar",
|
|
|
|
content: {
|
|
|
|
[event.getId()!]: {
|
|
|
|
[ReceiptType.Read]: {
|
|
|
|
[myId]: { ts: 1 },
|
|
|
|
},
|
2023-06-21 23:07:16 +08:00
|
|
|
},
|
|
|
|
},
|
2023-11-29 21:36:52 +08:00
|
|
|
}),
|
|
|
|
);
|
2023-06-21 23:07:16 +08:00
|
|
|
|
2023-11-29 21:36:52 +08:00
|
|
|
expect(doesRoomOrThreadHaveUnreadMessages(room)).toBe(false);
|
|
|
|
});
|
2023-06-21 23:07:16 +08:00
|
|
|
|
2023-11-29 21:36:52 +08:00
|
|
|
it("a threaded receipt for the event makes the room read", () => {
|
|
|
|
// Send threaded receipt into room pointing at the latest event
|
|
|
|
room.addReceipt(
|
|
|
|
new MatrixEvent({
|
|
|
|
type: "m.receipt",
|
|
|
|
room_id: "!foo:bar",
|
|
|
|
content: {
|
|
|
|
[event.getId()!]: {
|
|
|
|
[ReceiptType.Read]: {
|
|
|
|
[myId]: { ts: 1, thread_id: "main" },
|
|
|
|
},
|
|
|
|
},
|
2023-06-21 23:07:16 +08:00
|
|
|
},
|
2023-11-29 21:36:52 +08:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(doesRoomOrThreadHaveUnreadMessages(room)).toBe(false);
|
2023-06-21 23:07:16 +08:00
|
|
|
});
|
2023-11-29 21:36:52 +08:00
|
|
|
});
|
2023-06-21 23:07:16 +08:00
|
|
|
|
2023-11-29 21:36:52 +08:00
|
|
|
describe("with an event on the main timeline and a later one in a thread", () => {
|
|
|
|
let threadEvent: MatrixEvent;
|
2023-06-21 23:07:16 +08:00
|
|
|
|
2023-11-29 21:36:52 +08:00
|
|
|
beforeEach(() => {
|
|
|
|
const { events } = makeThreadEvents({
|
|
|
|
roomId: roomId,
|
|
|
|
authorId: aliceId,
|
|
|
|
participantUserIds: ["@x:s.co"],
|
|
|
|
length: 2,
|
|
|
|
ts: 100,
|
|
|
|
currentUserId: myId,
|
|
|
|
});
|
|
|
|
room.addLiveEvents(events);
|
|
|
|
threadEvent = events[1];
|
|
|
|
});
|
|
|
|
|
|
|
|
it("an unthreaded receipt for the later threaded event makes the room read", () => {
|
|
|
|
// Send unthreaded receipt into room pointing at the latest event
|
|
|
|
room.addReceipt(
|
|
|
|
new MatrixEvent({
|
|
|
|
type: "m.receipt",
|
|
|
|
room_id: roomId,
|
|
|
|
content: {
|
|
|
|
[threadEvent.getId()!]: {
|
|
|
|
[ReceiptType.Read]: {
|
|
|
|
[myId]: { ts: 1 },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(doesRoomOrThreadHaveUnreadMessages(room)).toBe(false);
|
|
|
|
});
|
2023-06-21 23:07:16 +08:00
|
|
|
});
|
|
|
|
});
|
2024-02-07 21:49:40 +08:00
|
|
|
|
|
|
|
describe("doesRoomHaveUnreadThreads()", () => {
|
|
|
|
let room: Room;
|
|
|
|
const roomId = "!abc:server.org";
|
|
|
|
const myId = client.getSafeUserId();
|
|
|
|
|
|
|
|
beforeAll(() => {
|
|
|
|
client.supportsThreads = () => true;
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
room = new Room(roomId, client, myId);
|
|
|
|
jest.spyOn(logger, "warn");
|
|
|
|
|
|
|
|
// Don't care about the code path of hidden events.
|
|
|
|
mocked(haveRendererForEvent).mockClear().mockReturnValue(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("returns false when no threads", () => {
|
|
|
|
expect(doesRoomHaveUnreadThreads(room)).toBe(false);
|
|
|
|
|
|
|
|
// Add event to the room
|
|
|
|
const event = mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: "m.room.message",
|
|
|
|
user: aliceId,
|
|
|
|
room: roomId,
|
|
|
|
content: {},
|
|
|
|
});
|
|
|
|
room.addLiveEvents([event]);
|
|
|
|
|
|
|
|
// It still returns false
|
|
|
|
expect(doesRoomHaveUnreadThreads(room)).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("return true when we don't have any receipt for the thread", async () => {
|
|
|
|
await populateThread({
|
|
|
|
room,
|
|
|
|
client,
|
|
|
|
authorId: myId,
|
|
|
|
participantUserIds: [aliceId],
|
|
|
|
});
|
|
|
|
|
|
|
|
// There is no receipt for the thread, it should be unread
|
|
|
|
expect(doesRoomHaveUnreadThreads(room)).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("return false when we have a receipt for the thread", async () => {
|
|
|
|
const { events, rootEvent } = await populateThread({
|
|
|
|
room,
|
|
|
|
client,
|
|
|
|
authorId: myId,
|
|
|
|
participantUserIds: [aliceId],
|
|
|
|
});
|
|
|
|
|
|
|
|
// Mark the thread as read.
|
|
|
|
const 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);
|
|
|
|
|
|
|
|
// There is a receipt for the thread, it should be read
|
|
|
|
expect(doesRoomHaveUnreadThreads(room)).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("return true when only of the threads has a receipt", async () => {
|
|
|
|
// Create a first thread
|
|
|
|
await populateThread({
|
|
|
|
room,
|
|
|
|
client,
|
|
|
|
authorId: myId,
|
|
|
|
participantUserIds: [aliceId],
|
|
|
|
});
|
|
|
|
|
|
|
|
// Create a second thread
|
|
|
|
const { events, rootEvent } = await populateThread({
|
|
|
|
room,
|
|
|
|
client,
|
|
|
|
authorId: myId,
|
|
|
|
participantUserIds: [aliceId],
|
|
|
|
});
|
|
|
|
|
|
|
|
// Mark the thread as read.
|
|
|
|
const 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);
|
|
|
|
|
|
|
|
// The first thread doesn't have a receipt, it should be unread
|
|
|
|
expect(doesRoomHaveUnreadThreads(room)).toBe(true);
|
|
|
|
});
|
|
|
|
});
|
2022-07-18 20:00:56 +08:00
|
|
|
});
|