Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Consider the unthreaded read receipt for Unread dot state (#11117)
Browse files Browse the repository at this point in the history
* Consider the unthreaded read receipt for Unread dot state also

* Add tests

* Fix strict types
  • Loading branch information
t3chguy authored Jun 21, 2023
1 parent 9b5b053 commit 767cd62
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 12 deletions.
18 changes: 8 additions & 10 deletions src/Unread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,13 @@ function makeHasReceipt(
// If we found an event matching our receipt, then it's easy: this event
// has a receipt if its ID is the same as the one in the receipt.
return (ev) => ev.getId() == readUpToId;
} else {
// If we didn't, we have to guess by saying if this event is before the
// receipt's ts, then it we pretend it has a receipt.
const receipt = roomOrThread.getReadReceiptForUserId(myUserId);
if (receipt) {
const receiptTimestamp = receipt.data.ts;
return (ev) => ev.getTs() < receiptTimestamp;
} else {
return (_ev) => false;
}
}

// If we didn't, we have to guess by saying if this event is before the
// receipt's ts, then it we pretend it has a receipt.
const receiptTs = roomOrThread.getReadReceiptForUserId(myUserId)?.data.ts ?? 0;
const unthreadedReceiptTs = roomOrThread.getLastUnthreadedReceiptFor(myUserId)?.ts ?? 0;
// We pick the more recent of the two receipts as the latest
const receiptTimestamp = Math.max(receiptTs, unthreadedReceiptTs);
return (ev) => ev.getTs() < receiptTimestamp;
}
77 changes: 75 additions & 2 deletions test/Unread-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ import { logger } from "matrix-js-sdk/src/logger";
import { haveRendererForEvent } from "../src/events/EventTileFactory";
import { makeBeaconEvent, mkEvent, stubClient } from "./test-utils";
import { mkThread } from "./test-utils/threads";
import { doesRoomHaveUnreadMessages, eventTriggersUnreadCount } from "../src/Unread";
import {
doesRoomHaveUnreadMessages,
doesRoomOrThreadHaveUnreadMessages,
eventTriggersUnreadCount,
} from "../src/Unread";
import { MatrixClientPeg } from "../src/MatrixClientPeg";

jest.mock("../src/events/EventTileFactory", () => ({
Expand Down Expand Up @@ -122,7 +126,7 @@ describe("Unread", () => {
let room: Room;
let event: MatrixEvent;
const roomId = "!abc:server.org";
const myId = client.getUserId()!;
const myId = client.getSafeUserId();

beforeAll(() => {
client.supportsThreads = () => true;
Expand Down Expand Up @@ -429,4 +433,73 @@ describe("Unread", () => {
);
});
});

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");
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);
});

it("should consider unthreaded read receipts for main timeline", () => {
// 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 },
},
},
},
}),
);

expect(doesRoomOrThreadHaveUnreadMessages(room)).toBe(false);
});

it("should consider unthreaded read receipts for thread timelines", () => {
// Provide an unthreaded read receipt with ts greater than the latest thread event
const receipt = new MatrixEvent({
type: "m.receipt",
room_id: "!foo:bar",
content: {
[event.getId()!]: {
[ReceiptType.Read]: {
[myId]: { ts: 10000000000 },
},
},
},
});
room.addReceipt(receipt);

const { thread } = mkThread({ room, client, authorId: myId, participantUserIds: [aliceId] });

expect(thread.replyToEvent!.getTs()).toBeLessThan(
receipt.getContent()[event.getId()!][ReceiptType.Read][myId].ts,
);
expect(doesRoomOrThreadHaveUnreadMessages(thread)).toBe(false);
});
});
});

0 comments on commit 767cd62

Please sign in to comment.