From 5e8f48a218b2528080ec9d1d03cea7647aeda5ad Mon Sep 17 00:00:00 2001 From: Germain Date: Mon, 14 Nov 2022 15:46:48 +0000 Subject: [PATCH] Add ability to send unthreaded receipt --- spec/unit/read-receipt.spec.ts | 38 ++++++++++++++++++++++------------ src/client.ts | 18 ++++++++++------ 2 files changed, 37 insertions(+), 19 deletions(-) diff --git a/spec/unit/read-receipt.spec.ts b/spec/unit/read-receipt.spec.ts index 78f57ea7d3c..2e3870a388b 100644 --- a/spec/unit/read-receipt.spec.ts +++ b/spec/unit/read-receipt.spec.ts @@ -18,6 +18,7 @@ import MockHttpBackend from 'matrix-mock-request'; import { ReceiptType } from '../../src/@types/read_receipts'; import { MatrixClient } from "../../src/client"; +import { Feature, ServerSupport } from '../../src/feature'; import { EventType } from '../../src/matrix'; import { MAIN_ROOM_TIMELINE } from '../../src/models/read-receipt'; import { encodeUri } from '../../src/utils'; @@ -69,15 +70,8 @@ const roomEvent = utils.mkEvent({ }, }); -function mockServerSideSupport(client, hasServerSideSupport) { - const doesServerSupportUnstableFeature = client.doesServerSupportUnstableFeature; - client.doesServerSupportUnstableFeature = (unstableFeature) => { - if (unstableFeature === "org.matrix.msc3771") { - return Promise.resolve(hasServerSideSupport); - } else { - return doesServerSupportUnstableFeature(unstableFeature); - } - }; +function mockServerSideSupport(client, serverSideSupport: ServerSupport) { + client.canSupport.set(Feature.ThreadUnreadNotifications, serverSideSupport); } describe("Read receipt", () => { @@ -103,13 +97,31 @@ describe("Read receipt", () => { expect(request.data.thread_id).toEqual(THREAD_ID); }).respond(200, {}); - mockServerSideSupport(client, true); + mockServerSideSupport(client, ServerSupport.Stable); client.sendReceipt(threadEvent, ReceiptType.Read, {}); await httpBackend.flushAllExpected(); await flushPromises(); }); + it("sends an unthreaded receipt", async () => { + httpBackend.when( + "POST", encodeUri("/rooms/$roomId/receipt/$receiptType/$eventId", { + $roomId: ROOM_ID, + $receiptType: ReceiptType.Read, + $eventId: threadEvent.getId()!, + }), + ).check((request) => { + expect(request.data.thread_id).toBeUndefined(); + }).respond(200, {}); + + mockServerSideSupport(client, ServerSupport.Stable); + client.sendReadReceipt(threadEvent, ReceiptType.Read, true); + + await httpBackend.flushAllExpected(); + await flushPromises(); + }); + it("sends a room read receipt", async () => { httpBackend.when( "POST", encodeUri("/rooms/$roomId/receipt/$receiptType/$eventId", { @@ -121,7 +133,7 @@ describe("Read receipt", () => { expect(request.data.thread_id).toEqual(MAIN_ROOM_TIMELINE); }).respond(200, {}); - mockServerSideSupport(client, true); + mockServerSideSupport(client, ServerSupport.Stable); client.sendReceipt(roomEvent, ReceiptType.Read, {}); await httpBackend.flushAllExpected(); @@ -139,7 +151,7 @@ describe("Read receipt", () => { expect(request.data.thread_id).toBeUndefined(); }).respond(200, {}); - mockServerSideSupport(client, false); + mockServerSideSupport(client, ServerSupport.Unsupported); client.sendReceipt(threadEvent, ReceiptType.Read, {}); await httpBackend.flushAllExpected(); @@ -157,7 +169,7 @@ describe("Read receipt", () => { expect(request.data).toEqual({}); }).respond(200, {}); - mockServerSideSupport(client, false); + mockServerSideSupport(client, ServerSupport.Unsupported); client.sendReceipt(threadEvent, ReceiptType.Read, undefined); await httpBackend.flushAllExpected(); diff --git a/src/client.ts b/src/client.ts index 316e554c072..12515e64ccc 100644 --- a/src/client.ts +++ b/src/client.ts @@ -4588,6 +4588,7 @@ export class MatrixClient extends TypedEventEmitter { if (this.isGuest()) { return Promise.resolve({}); // guests cannot send receipts so don't bother. @@ -4606,12 +4608,15 @@ export class MatrixClient extends TypedEventEmitter(Method.Post, path, undefined, body || {}); @@ -4633,6 +4638,7 @@ export class MatrixClient extends TypedEventEmitter { if (!event) return; const eventId = event.getId()!; @@ -4641,7 +4647,7 @@ export class MatrixClient extends TypedEventEmitter