Skip to content

Commit d43e664

Browse files
author
Germain
authored
Add ability to send unthreaded receipt (#2878)
1 parent 0e32284 commit d43e664

File tree

2 files changed

+37
-19
lines changed

2 files changed

+37
-19
lines changed

spec/unit/read-receipt.spec.ts

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import MockHttpBackend from 'matrix-mock-request';
1818

1919
import { ReceiptType } from '../../src/@types/read_receipts';
2020
import { MatrixClient } from "../../src/client";
21+
import { Feature, ServerSupport } from '../../src/feature';
2122
import { EventType } from '../../src/matrix';
2223
import { MAIN_ROOM_TIMELINE } from '../../src/models/read-receipt';
2324
import { encodeUri } from '../../src/utils';
@@ -69,15 +70,8 @@ const roomEvent = utils.mkEvent({
6970
},
7071
});
7172

72-
function mockServerSideSupport(client, hasServerSideSupport) {
73-
const doesServerSupportUnstableFeature = client.doesServerSupportUnstableFeature;
74-
client.doesServerSupportUnstableFeature = (unstableFeature) => {
75-
if (unstableFeature === "org.matrix.msc3771") {
76-
return Promise.resolve(hasServerSideSupport);
77-
} else {
78-
return doesServerSupportUnstableFeature(unstableFeature);
79-
}
80-
};
73+
function mockServerSideSupport(client, serverSideSupport: ServerSupport) {
74+
client.canSupport.set(Feature.ThreadUnreadNotifications, serverSideSupport);
8175
}
8276

8377
describe("Read receipt", () => {
@@ -103,13 +97,31 @@ describe("Read receipt", () => {
10397
expect(request.data.thread_id).toEqual(THREAD_ID);
10498
}).respond(200, {});
10599

106-
mockServerSideSupport(client, true);
100+
mockServerSideSupport(client, ServerSupport.Stable);
107101
client.sendReceipt(threadEvent, ReceiptType.Read, {});
108102

109103
await httpBackend.flushAllExpected();
110104
await flushPromises();
111105
});
112106

107+
it("sends an unthreaded receipt", async () => {
108+
httpBackend.when(
109+
"POST", encodeUri("/rooms/$roomId/receipt/$receiptType/$eventId", {
110+
$roomId: ROOM_ID,
111+
$receiptType: ReceiptType.Read,
112+
$eventId: threadEvent.getId()!,
113+
}),
114+
).check((request) => {
115+
expect(request.data.thread_id).toBeUndefined();
116+
}).respond(200, {});
117+
118+
mockServerSideSupport(client, ServerSupport.Stable);
119+
client.sendReadReceipt(threadEvent, ReceiptType.Read, true);
120+
121+
await httpBackend.flushAllExpected();
122+
await flushPromises();
123+
});
124+
113125
it("sends a room read receipt", async () => {
114126
httpBackend.when(
115127
"POST", encodeUri("/rooms/$roomId/receipt/$receiptType/$eventId", {
@@ -121,7 +133,7 @@ describe("Read receipt", () => {
121133
expect(request.data.thread_id).toEqual(MAIN_ROOM_TIMELINE);
122134
}).respond(200, {});
123135

124-
mockServerSideSupport(client, true);
136+
mockServerSideSupport(client, ServerSupport.Stable);
125137
client.sendReceipt(roomEvent, ReceiptType.Read, {});
126138

127139
await httpBackend.flushAllExpected();
@@ -139,7 +151,7 @@ describe("Read receipt", () => {
139151
expect(request.data.thread_id).toBeUndefined();
140152
}).respond(200, {});
141153

142-
mockServerSideSupport(client, false);
154+
mockServerSideSupport(client, ServerSupport.Unsupported);
143155
client.sendReceipt(threadEvent, ReceiptType.Read, {});
144156

145157
await httpBackend.flushAllExpected();
@@ -157,7 +169,7 @@ describe("Read receipt", () => {
157169
expect(request.data).toEqual({});
158170
}).respond(200, {});
159171

160-
mockServerSideSupport(client, false);
172+
mockServerSideSupport(client, ServerSupport.Unsupported);
161173
client.sendReceipt(threadEvent, ReceiptType.Read, undefined);
162174

163175
await httpBackend.flushAllExpected();

src/client.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4588,13 +4588,15 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
45884588
* @param {ReceiptType} receiptType The kind of receipt e.g. "m.read". Other than
45894589
* ReceiptType.Read are experimental!
45904590
* @param {object} body Additional content to send alongside the receipt.
4591+
* @param {boolean} unthreaded An unthreaded receipt will clear room+thread notifications
45914592
* @return {Promise} Resolves: to an empty object {}
45924593
* @return {module:http-api.MatrixError} Rejects: with an error response.
45934594
*/
45944595
public async sendReceipt(
45954596
event: MatrixEvent,
45964597
receiptType: ReceiptType,
45974598
body: any,
4599+
unthreaded = false,
45984600
): Promise<{}> {
45994601
if (this.isGuest()) {
46004602
return Promise.resolve({}); // guests cannot send receipts so don't bother.
@@ -4606,12 +4608,15 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
46064608
$eventId: event.getId()!,
46074609
});
46084610

4609-
// TODO: Add a check for which spec version this will be released in
4610-
if (await this.doesServerSupportUnstableFeature("org.matrix.msc3771")) {
4611+
const supportsThreadRR = this.canSupport.get(Feature.ThreadUnreadNotifications) !== ServerSupport.Unsupported;
4612+
if (supportsThreadRR && !unthreaded) {
46114613
const isThread = !!event.threadRootId;
4612-
body.thread_id = isThread
4613-
? event.threadRootId
4614-
: MAIN_ROOM_TIMELINE;
4614+
body = {
4615+
...body,
4616+
thread_id: isThread
4617+
? event.threadRootId
4618+
: MAIN_ROOM_TIMELINE,
4619+
};
46154620
}
46164621

46174622
const promise = this.http.authedRequest<{}>(Method.Post, path, undefined, body || {});
@@ -4633,6 +4638,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
46334638
public async sendReadReceipt(
46344639
event: MatrixEvent | null,
46354640
receiptType = ReceiptType.Read,
4641+
unthreaded = false,
46364642
): Promise<{} | undefined> {
46374643
if (!event) return;
46384644
const eventId = event.getId()!;
@@ -4641,7 +4647,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
46414647
throw new Error(`Cannot set read receipt to a pending event (${eventId})`);
46424648
}
46434649

4644-
return this.sendReceipt(event, receiptType, {});
4650+
return this.sendReceipt(event, receiptType, {}, unthreaded);
46454651
}
46464652

46474653
/**

0 commit comments

Comments
 (0)