Skip to content

Commit 07189f0

Browse files
authored
Add tests for sendEvent threadId handling (#2435)
* Add tests for sendEvent threadId handling * Fix sendEvent threadId relation support not adding `is_falling_back` field
1 parent aa94d5d commit 07189f0

File tree

3 files changed

+93
-11
lines changed

3 files changed

+93
-11
lines changed

spec/unit/matrix-client.spec.ts

Lines changed: 85 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ describe("MatrixClient", function() {
8787
// }
8888
// items are popped off when processed and block if no items left.
8989
];
90-
let acceptKeepalives;
90+
let acceptKeepalives: boolean;
9191
let pendingLookup = null;
9292
function httpReq(cb, method, path, qp, data, prefix) {
9393
if (path === KEEP_ALIVE_PATH && acceptKeepalives) {
@@ -127,7 +127,7 @@ describe("MatrixClient", function() {
127127
(next.error ? "BAD" : "GOOD") + " response",
128128
);
129129
if (next.expectBody) {
130-
expect(next.expectBody).toEqual(data);
130+
expect(data).toEqual(next.expectBody);
131131
}
132132
if (next.expectQueryParams) {
133133
Object.keys(next.expectQueryParams).forEach(function(k) {
@@ -777,7 +777,7 @@ describe("MatrixClient", function() {
777777
expectBody: content,
778778
}];
779779

780-
await client.sendEvent(roomId, EventType.RoomMessage, content, txnId);
780+
await client.sendEvent(roomId, EventType.RoomMessage, { ...content }, txnId);
781781
});
782782

783783
it("overload with null threadId works", async () => {
@@ -790,20 +790,99 @@ describe("MatrixClient", function() {
790790
expectBody: content,
791791
}];
792792

793-
await client.sendEvent(roomId, null, EventType.RoomMessage, content, txnId);
793+
await client.sendEvent(roomId, null, EventType.RoomMessage, { ...content }, txnId);
794794
});
795795

796796
it("overload with threadId works", async () => {
797797
const eventId = "$eventId:example.org";
798798
const txnId = client.makeTxnId();
799+
const threadId = "$threadId:server";
799800
httpLookups = [{
800801
method: "PUT",
801802
path: `/rooms/${encodeURIComponent(roomId)}/send/m.room.message/${txnId}`,
802803
data: { event_id: eventId },
803-
expectBody: content,
804+
expectBody: {
805+
...content,
806+
"m.relates_to": {
807+
"event_id": threadId,
808+
"is_falling_back": true,
809+
"rel_type": "m.thread",
810+
},
811+
},
812+
}];
813+
814+
await client.sendEvent(roomId, threadId, EventType.RoomMessage, { ...content }, txnId);
815+
});
816+
817+
it("should add thread relation if threadId is passed and the relation is missing", async () => {
818+
const eventId = "$eventId:example.org";
819+
const threadId = "$threadId:server";
820+
const txnId = client.makeTxnId();
821+
822+
const room = new Room(roomId, client, userId);
823+
store.getRoom.mockReturnValue(room);
824+
825+
const rootEvent = new MatrixEvent({ event_id: threadId });
826+
room.createThread(threadId, rootEvent, [rootEvent], false);
827+
828+
httpLookups = [{
829+
method: "PUT",
830+
path: `/rooms/${encodeURIComponent(roomId)}/send/m.room.message/${txnId}`,
831+
data: { event_id: eventId },
832+
expectBody: {
833+
...content,
834+
"m.relates_to": {
835+
"m.in_reply_to": {
836+
event_id: threadId,
837+
},
838+
"event_id": threadId,
839+
"is_falling_back": true,
840+
"rel_type": "m.thread",
841+
},
842+
},
843+
}];
844+
845+
await client.sendEvent(roomId, threadId, EventType.RoomMessage, { ...content }, txnId);
846+
});
847+
848+
it("should add thread relation if threadId is passed and the relation is missing with reply", async () => {
849+
const eventId = "$eventId:example.org";
850+
const threadId = "$threadId:server";
851+
const txnId = client.makeTxnId();
852+
853+
const content = {
854+
body,
855+
"m.relates_to": {
856+
"m.in_reply_to": {
857+
event_id: "$other:event",
858+
},
859+
},
860+
};
861+
862+
const room = new Room(roomId, client, userId);
863+
store.getRoom.mockReturnValue(room);
864+
865+
const rootEvent = new MatrixEvent({ event_id: threadId });
866+
room.createThread(threadId, rootEvent, [rootEvent], false);
867+
868+
httpLookups = [{
869+
method: "PUT",
870+
path: `/rooms/${encodeURIComponent(roomId)}/send/m.room.message/${txnId}`,
871+
data: { event_id: eventId },
872+
expectBody: {
873+
...content,
874+
"m.relates_to": {
875+
"m.in_reply_to": {
876+
event_id: "$other:event",
877+
},
878+
"event_id": threadId,
879+
"is_falling_back": false,
880+
"rel_type": "m.thread",
881+
},
882+
},
804883
}];
805884

806-
await client.sendEvent(roomId, "$threadId:server", EventType.RoomMessage, content, txnId);
885+
await client.sendEvent(roomId, threadId, EventType.RoomMessage, { ...content }, txnId);
807886
});
808887
});
809888

src/client.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3772,17 +3772,20 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
37723772
// If we expect that an event is part of a thread but is missing the relation
37733773
// we need to add it manually, as well as the reply fallback
37743774
if (threadId && !content["m.relates_to"]?.rel_type) {
3775+
const isReply = !!content["m.relates_to"]?.["m.in_reply_to"];
37753776
content["m.relates_to"] = {
37763777
...content["m.relates_to"],
37773778
"rel_type": THREAD_RELATION_TYPE.name,
37783779
"event_id": threadId,
3780+
// Set is_falling_back to true unless this is actually intended to be a reply
3781+
"is_falling_back": !isReply,
37793782
};
37803783
const thread = this.getRoom(roomId)?.getThread(threadId);
3781-
if (thread) {
3784+
if (thread && !isReply) {
37823785
content["m.relates_to"]["m.in_reply_to"] = {
37833786
"event_id": thread.lastReply((ev: MatrixEvent) => {
37843787
return ev.isRelation(THREAD_RELATION_TYPE.name) && !ev.status;
3785-
})?.getId(),
3788+
})?.getId() ?? threadId,
37863789
};
37873790
}
37883791
}
@@ -4031,7 +4034,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
40314034
$txnId: txnId,
40324035
};
40334036

4034-
let path;
4037+
let path: string;
40354038

40364039
if (event.isState()) {
40374040
let pathTemplate = "/rooms/$roomId/state/$eventType";

src/content-helpers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,10 @@ export const getTextForLocationEvent = (
139139
/**
140140
* Generates the content for a Location event
141141
* @param uri a geo:// uri for the location
142-
* @param ts the timestamp when the location was correct (milliseconds since
142+
* @param timestamp the timestamp when the location was correct (milliseconds since
143143
* the UNIX epoch)
144144
* @param description the (optional) label for this location on the map
145-
* @param asset_type the (optional) asset type of this location e.g. "m.self"
145+
* @param assetType the (optional) asset type of this location e.g. "m.self"
146146
* @param text optional. A text for the location
147147
*/
148148
export const makeLocationContent = (

0 commit comments

Comments
 (0)