diff --git a/spec/unit/matrix-client.spec.ts b/spec/unit/matrix-client.spec.ts index c685ea3a2..fbe8c67d7 100644 --- a/spec/unit/matrix-client.spec.ts +++ b/spec/unit/matrix-client.spec.ts @@ -87,7 +87,7 @@ describe("MatrixClient", function() { // } // items are popped off when processed and block if no items left. ]; - let acceptKeepalives; + let acceptKeepalives: boolean; let pendingLookup = null; function httpReq(cb, method, path, qp, data, prefix) { if (path === KEEP_ALIVE_PATH && acceptKeepalives) { @@ -127,7 +127,7 @@ describe("MatrixClient", function() { (next.error ? "BAD" : "GOOD") + " response", ); if (next.expectBody) { - expect(next.expectBody).toEqual(data); + expect(data).toEqual(next.expectBody); } if (next.expectQueryParams) { Object.keys(next.expectQueryParams).forEach(function(k) { @@ -777,7 +777,7 @@ describe("MatrixClient", function() { expectBody: content, }]; - await client.sendEvent(roomId, EventType.RoomMessage, content, txnId); + await client.sendEvent(roomId, EventType.RoomMessage, { ...content }, txnId); }); it("overload with null threadId works", async () => { @@ -790,20 +790,99 @@ describe("MatrixClient", function() { expectBody: content, }]; - await client.sendEvent(roomId, null, EventType.RoomMessage, content, txnId); + await client.sendEvent(roomId, null, EventType.RoomMessage, { ...content }, txnId); }); it("overload with threadId works", async () => { const eventId = "$eventId:example.org"; const txnId = client.makeTxnId(); + const threadId = "$threadId:server"; httpLookups = [{ method: "PUT", path: `/rooms/${encodeURIComponent(roomId)}/send/m.room.message/${txnId}`, data: { event_id: eventId }, - expectBody: content, + expectBody: { + ...content, + "m.relates_to": { + "event_id": threadId, + "is_falling_back": true, + "rel_type": "m.thread", + }, + }, }]; - await client.sendEvent(roomId, "$threadId:server", EventType.RoomMessage, content, txnId); + await client.sendEvent(roomId, threadId, EventType.RoomMessage, { ...content }, txnId); + }); + + it("should add thread relation if threadId is passed and the relation is missing", async () => { + const eventId = "$eventId:example.org"; + const threadId = "$threadId:server"; + const txnId = client.makeTxnId(); + + const room = new Room(roomId, client, userId); + store.getRoom.mockReturnValue(room); + + const rootEvent = new MatrixEvent({ event_id: threadId }); + room.createThread(threadId, rootEvent, [rootEvent], false); + + httpLookups = [{ + method: "PUT", + path: `/rooms/${encodeURIComponent(roomId)}/send/m.room.message/${txnId}`, + data: { event_id: eventId }, + expectBody: { + ...content, + "m.relates_to": { + "m.in_reply_to": { + event_id: threadId, + }, + "event_id": threadId, + "is_falling_back": true, + "rel_type": "m.thread", + }, + }, + }]; + + await client.sendEvent(roomId, threadId, EventType.RoomMessage, { ...content }, txnId); + }); + + it("should add thread relation if threadId is passed and the relation is missing with reply", async () => { + const eventId = "$eventId:example.org"; + const threadId = "$threadId:server"; + const txnId = client.makeTxnId(); + + const content = { + body, + "m.relates_to": { + "m.in_reply_to": { + event_id: "$other:event", + }, + }, + }; + + const room = new Room(roomId, client, userId); + store.getRoom.mockReturnValue(room); + + const rootEvent = new MatrixEvent({ event_id: threadId }); + room.createThread(threadId, rootEvent, [rootEvent], false); + + httpLookups = [{ + method: "PUT", + path: `/rooms/${encodeURIComponent(roomId)}/send/m.room.message/${txnId}`, + data: { event_id: eventId }, + expectBody: { + ...content, + "m.relates_to": { + "m.in_reply_to": { + event_id: "$other:event", + }, + "event_id": threadId, + "is_falling_back": false, + "rel_type": "m.thread", + }, + }, + }]; + + await client.sendEvent(roomId, threadId, EventType.RoomMessage, { ...content }, txnId); }); }); diff --git a/src/client.ts b/src/client.ts index 92d989816..9a2a0e27d 100644 --- a/src/client.ts +++ b/src/client.ts @@ -3772,17 +3772,20 @@ export class MatrixClient extends TypedEventEmitter { return ev.isRelation(THREAD_RELATION_TYPE.name) && !ev.status; - })?.getId(), + })?.getId() ?? threadId, }; } } @@ -4031,7 +4034,7 @@ export class MatrixClient extends TypedEventEmitter