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

Commit 892c3e2

Browse files
committed
Send correct receipt when marking a room as read
1 parent a629ce3 commit 892c3e2

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

src/utils/notifications.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,7 @@ export function localNotificationsAreSilenced(cli: MatrixClient): boolean {
6666
* @returns a promise that resolves when the room has been marked as read
6767
*/
6868
export async function clearRoomNotification(room: Room, client: MatrixClient): Promise<{} | undefined> {
69-
const roomEvents = room.getLiveTimeline().getEvents();
70-
const lastThreadEvents = room.lastThread?.events;
71-
72-
const lastRoomEvent = roomEvents?.[roomEvents?.length - 1];
73-
const lastThreadLastEvent = lastThreadEvents?.[lastThreadEvents?.length - 1];
74-
75-
const lastEvent =
76-
(lastRoomEvent?.getTs() ?? 0) > (lastThreadLastEvent?.getTs() ?? 0) ? lastRoomEvent : lastThreadLastEvent;
69+
const lastEvent = room.getLastLiveEvent();
7770

7871
try {
7972
if (lastEvent) {

test/utils/notifications-test.ts

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,31 +114,51 @@ describe("notifications", () => {
114114
let sendReadReceiptSpy: jest.SpyInstance;
115115
const ROOM_ID = "123";
116116
const USER_ID = "@bob:example.org";
117+
let message: MatrixEvent;
118+
let sendReceiptsSetting = true;
117119

118120
beforeEach(() => {
119121
stubClient();
120122
client = mocked(MatrixClientPeg.get());
121123
room = new Room(ROOM_ID, client, USER_ID);
124+
message = mkMessage({
125+
event: true,
126+
room: ROOM_ID,
127+
user: USER_ID,
128+
msg: "Hello",
129+
});
130+
room.addLiveEvents([message]);
122131
sendReadReceiptSpy = jest.spyOn(client, "sendReadReceipt").mockResolvedValue({});
123132
jest.spyOn(client, "getRooms").mockReturnValue([room]);
124133
jest.spyOn(SettingsStore, "getValue").mockImplementation((name) => {
125-
return name === "sendReadReceipts";
134+
return name === "sendReadReceipts" && sendReceiptsSetting;
126135
});
127136
});
128137

129138
it("sends a request even if everything has been read", () => {
130139
clearRoomNotification(room, client);
131-
expect(sendReadReceiptSpy).not.toHaveBeenCalled();
140+
expect(sendReadReceiptSpy).toHaveBeenCalledWith(message, ReceiptType.Read, true);
132141
});
133142

134143
it("marks the room as read even if the receipt failed", async () => {
135144
room.setUnreadNotificationCount(NotificationCountType.Total, 5);
136-
sendReadReceiptSpy = jest.spyOn(client, "sendReadReceipt").mockReset().mockRejectedValue({});
137-
try {
145+
sendReadReceiptSpy = jest.spyOn(client, "sendReadReceipt").mockReset().mockRejectedValue({ error: 42 });
146+
147+
await expect(async () => {
138148
await clearRoomNotification(room, client);
139-
} finally {
140-
expect(room.getUnreadNotificationCount(NotificationCountType.Total)).toBe(0);
141-
}
149+
}).rejects.toEqual({ error: 42 });
150+
expect(room.getUnreadNotificationCount(NotificationCountType.Total)).toBe(0);
151+
});
152+
153+
describe("when sendReadReceipts setting is disabled", () => {
154+
beforeEach(() => {
155+
sendReceiptsSetting = false;
156+
});
157+
158+
it("should send a private read receipt", () => {
159+
clearRoomNotification(room, client);
160+
expect(sendReadReceiptSpy).toHaveBeenCalledWith(message, ReceiptType.ReadPrivate, true);
161+
});
142162
});
143163
});
144164

0 commit comments

Comments
 (0)