Send correct receipt when marking a room as read (#10730)
parent
326e217d3f
commit
d084c34ea2
|
@ -66,14 +66,7 @@ export function localNotificationsAreSilenced(cli: MatrixClient): boolean {
|
||||||
* @returns a promise that resolves when the room has been marked as read
|
* @returns a promise that resolves when the room has been marked as read
|
||||||
*/
|
*/
|
||||||
export async function clearRoomNotification(room: Room, client: MatrixClient): Promise<{} | undefined> {
|
export async function clearRoomNotification(room: Room, client: MatrixClient): Promise<{} | undefined> {
|
||||||
const roomEvents = room.getLiveTimeline().getEvents();
|
const lastEvent = room.getLastLiveEvent();
|
||||||
const lastThreadEvents = room.lastThread?.events;
|
|
||||||
|
|
||||||
const lastRoomEvent = roomEvents?.[roomEvents?.length - 1];
|
|
||||||
const lastThreadLastEvent = lastThreadEvents?.[lastThreadEvents?.length - 1];
|
|
||||||
|
|
||||||
const lastEvent =
|
|
||||||
(lastRoomEvent?.getTs() ?? 0) > (lastThreadLastEvent?.getTs() ?? 0) ? lastRoomEvent : lastThreadLastEvent;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (lastEvent) {
|
if (lastEvent) {
|
||||||
|
|
|
@ -114,31 +114,51 @@ describe("notifications", () => {
|
||||||
let sendReadReceiptSpy: jest.SpyInstance;
|
let sendReadReceiptSpy: jest.SpyInstance;
|
||||||
const ROOM_ID = "123";
|
const ROOM_ID = "123";
|
||||||
const USER_ID = "@bob:example.org";
|
const USER_ID = "@bob:example.org";
|
||||||
|
let message: MatrixEvent;
|
||||||
|
let sendReceiptsSetting = true;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
stubClient();
|
stubClient();
|
||||||
client = mocked(MatrixClientPeg.get());
|
client = mocked(MatrixClientPeg.get());
|
||||||
room = new Room(ROOM_ID, client, USER_ID);
|
room = new Room(ROOM_ID, client, USER_ID);
|
||||||
|
message = mkMessage({
|
||||||
|
event: true,
|
||||||
|
room: ROOM_ID,
|
||||||
|
user: USER_ID,
|
||||||
|
msg: "Hello",
|
||||||
|
});
|
||||||
|
room.addLiveEvents([message]);
|
||||||
sendReadReceiptSpy = jest.spyOn(client, "sendReadReceipt").mockResolvedValue({});
|
sendReadReceiptSpy = jest.spyOn(client, "sendReadReceipt").mockResolvedValue({});
|
||||||
jest.spyOn(client, "getRooms").mockReturnValue([room]);
|
jest.spyOn(client, "getRooms").mockReturnValue([room]);
|
||||||
jest.spyOn(SettingsStore, "getValue").mockImplementation((name) => {
|
jest.spyOn(SettingsStore, "getValue").mockImplementation((name) => {
|
||||||
return name === "sendReadReceipts";
|
return name === "sendReadReceipts" && sendReceiptsSetting;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("sends a request even if everything has been read", () => {
|
it("sends a request even if everything has been read", () => {
|
||||||
clearRoomNotification(room, client);
|
clearRoomNotification(room, client);
|
||||||
expect(sendReadReceiptSpy).not.toHaveBeenCalled();
|
expect(sendReadReceiptSpy).toHaveBeenCalledWith(message, ReceiptType.Read, true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("marks the room as read even if the receipt failed", async () => {
|
it("marks the room as read even if the receipt failed", async () => {
|
||||||
room.setUnreadNotificationCount(NotificationCountType.Total, 5);
|
room.setUnreadNotificationCount(NotificationCountType.Total, 5);
|
||||||
sendReadReceiptSpy = jest.spyOn(client, "sendReadReceipt").mockReset().mockRejectedValue({});
|
sendReadReceiptSpy = jest.spyOn(client, "sendReadReceipt").mockReset().mockRejectedValue({ error: 42 });
|
||||||
try {
|
|
||||||
|
await expect(async () => {
|
||||||
await clearRoomNotification(room, client);
|
await clearRoomNotification(room, client);
|
||||||
} finally {
|
}).rejects.toEqual({ error: 42 });
|
||||||
expect(room.getUnreadNotificationCount(NotificationCountType.Total)).toBe(0);
|
expect(room.getUnreadNotificationCount(NotificationCountType.Total)).toBe(0);
|
||||||
}
|
});
|
||||||
|
|
||||||
|
describe("when sendReadReceipts setting is disabled", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
sendReceiptsSetting = false;
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should send a private read receipt", () => {
|
||||||
|
clearRoomNotification(room, client);
|
||||||
|
expect(sendReadReceiptSpy).toHaveBeenCalledWith(message, ReceiptType.ReadPrivate, true);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue