Skip to content

Commit 877e3df

Browse files
t3chguyGermain Souquet
andauthored
[Release] Port multiple threads fixes (matrix-org#2292)
Co-authored-by: Germain Souquet <[email protected]>
1 parent d705a0e commit 877e3df

File tree

12 files changed

+548
-323
lines changed

12 files changed

+548
-323
lines changed

spec/integ/matrix-client-methods.spec.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,14 @@ describe("MatrixClient", function() {
145145
describe("joinRoom", function() {
146146
it("should no-op if you've already joined a room", function() {
147147
const roomId = "!foo:bar";
148-
const room = new Room(roomId, userId);
148+
const room = new Room(roomId, client, userId);
149+
client.fetchRoomEvent = () => Promise.resolve({});
149150
room.addLiveEvents([
150151
utils.mkMembership({
151152
user: userId, room: roomId, mship: "join", event: true,
152153
}),
153154
]);
155+
httpBackend.verifyNoOutstandingRequests();
154156
store.storeRoom(room);
155157
client.joinRoom(roomId);
156158
httpBackend.verifyNoOutstandingRequests();
@@ -556,11 +558,14 @@ describe("MatrixClient", function() {
556558
});
557559

558560
describe("partitionThreadedEvents", function() {
559-
const room = new Room("!STrMRsukXHtqQdSeHa:matrix.org", client, userId);
561+
let room;
562+
beforeEach(() => {
563+
room = new Room("!STrMRsukXHtqQdSeHa:matrix.org", client, userId);
564+
});
560565

561566
it("returns empty arrays when given an empty arrays", function() {
562567
const events = [];
563-
const [timeline, threaded] = client.partitionThreadedEvents(room, events);
568+
const [timeline, threaded] = room.partitionThreadedEvents(events);
564569
expect(timeline).toEqual([]);
565570
expect(threaded).toEqual([]);
566571
});
@@ -580,7 +585,7 @@ describe("MatrixClient", function() {
580585
// Vote has no threadId yet
581586
expect(eventPollResponseReference.threadId).toBeFalsy();
582587

583-
const [timeline, threaded] = client.partitionThreadedEvents(room, events);
588+
const [timeline, threaded] = room.partitionThreadedEvents(events);
584589

585590
expect(timeline).toEqual([
586591
// The message that was sent in a thread is missing
@@ -613,7 +618,7 @@ describe("MatrixClient", function() {
613618
eventReaction,
614619
];
615620

616-
const [timeline, threaded] = client.partitionThreadedEvents(room, events);
621+
const [timeline, threaded] = room.partitionThreadedEvents(events);
617622

618623
expect(timeline).toEqual([
619624
eventPollStartThreadRoot,
@@ -640,7 +645,7 @@ describe("MatrixClient", function() {
640645
eventMessageInThread,
641646
];
642647

643-
const [timeline, threaded] = client.partitionThreadedEvents(room, events);
648+
const [timeline, threaded] = room.partitionThreadedEvents(events);
644649

645650
expect(timeline).toEqual([
646651
eventPollStartThreadRoot,
@@ -667,7 +672,7 @@ describe("MatrixClient", function() {
667672
eventReaction,
668673
];
669674

670-
const [timeline, threaded] = client.partitionThreadedEvents(room, events);
675+
const [timeline, threaded] = room.partitionThreadedEvents(events);
671676

672677
expect(timeline).toEqual([
673678
eventPollStartThreadRoot,
@@ -710,7 +715,7 @@ describe("MatrixClient", function() {
710715
eventMember,
711716
eventCreate,
712717
];
713-
const [timeline, threaded] = client.partitionThreadedEvents(room, events);
718+
const [timeline, threaded] = room.partitionThreadedEvents(events);
714719

715720
expect(timeline).toEqual([
716721
// The message that was sent in a thread is missing
@@ -749,7 +754,7 @@ describe("MatrixClient", function() {
749754
threadedReactionRedaction,
750755
];
751756

752-
const [timeline, threaded] = client.partitionThreadedEvents(room, events);
757+
const [timeline, threaded] = room.partitionThreadedEvents(events);
753758

754759
expect(timeline).toEqual([
755760
threadRootEvent,
@@ -778,7 +783,7 @@ describe("MatrixClient", function() {
778783
replyToReply,
779784
];
780785

781-
const [timeline, threaded] = client.partitionThreadedEvents(room, events);
786+
const [timeline, threaded] = room.partitionThreadedEvents(events);
782787

783788
expect(timeline).toEqual([
784789
threadRootEvent,
@@ -805,7 +810,7 @@ describe("MatrixClient", function() {
805810
replyToThreadResponse,
806811
];
807812

808-
const [timeline, threaded] = client.partitionThreadedEvents(room, events);
813+
const [timeline, threaded] = room.partitionThreadedEvents(events);
809814

810815
expect(timeline).toEqual([
811816
threadRootEvent,

spec/test-utils/test-utils.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { logger } from '../../src/logger';
88
import { IContent, IEvent, IUnsigned, MatrixEvent, MatrixEventEvent } from "../../src/models/event";
99
import { ClientEvent, EventType, MatrixClient } from "../../src";
1010
import { SyncState } from "../../src/sync";
11+
import { eventMapperFor } from "../../src/event-mapper";
1112

1213
/**
1314
* Return a promise that is resolved when the client next emits a
@@ -79,6 +80,7 @@ interface IEventOpts {
7980
redacts?: string;
8081
}
8182

83+
let testEventIndex = 1; // counter for events, easier for comparison of randomly generated events
8284
/**
8385
* Create an Event.
8486
* @param {Object} opts Values for the event.
@@ -88,9 +90,10 @@ interface IEventOpts {
8890
* @param {string} opts.skey Optional. The state key (auto inserts empty string)
8991
* @param {Object} opts.content The event.content
9092
* @param {boolean} opts.event True to make a MatrixEvent.
93+
* @param {MatrixClient} client If passed along with opts.event=true will be used to set up re-emitters.
9194
* @return {Object} a JSON object representing this event.
9295
*/
93-
export function mkEvent(opts: IEventOpts): object | MatrixEvent {
96+
export function mkEvent(opts: IEventOpts, client?: MatrixClient): object | MatrixEvent {
9497
if (!opts.type || !opts.content) {
9598
throw new Error("Missing .type or .content =>" + JSON.stringify(opts));
9699
}
@@ -100,7 +103,8 @@ export function mkEvent(opts: IEventOpts): object | MatrixEvent {
100103
sender: opts.sender || opts.user, // opts.user for backwards-compat
101104
content: opts.content,
102105
unsigned: opts.unsigned || {},
103-
event_id: "$" + Math.random() + "-" + Math.random(),
106+
event_id: "$" + testEventIndex++ + "-" + Math.random() + "-" + Math.random(),
107+
txn_id: "~" + Math.random(),
104108
redacts: opts.redacts,
105109
};
106110
if (opts.skey !== undefined) {
@@ -116,6 +120,11 @@ export function mkEvent(opts: IEventOpts): object | MatrixEvent {
116120
].includes(opts.type)) {
117121
event.state_key = "";
118122
}
123+
124+
if (opts.event && client) {
125+
return eventMapperFor(client, {})(event);
126+
}
127+
119128
return opts.event ? new MatrixEvent(event) : event;
120129
}
121130

@@ -208,9 +217,10 @@ interface IMessageOpts {
208217
* @param {string} opts.user The user ID for the event.
209218
* @param {string} opts.msg Optional. The content.body for the event.
210219
* @param {boolean} opts.event True to make a MatrixEvent.
220+
* @param {MatrixClient} client If passed along with opts.event=true will be used to set up re-emitters.
211221
* @return {Object|MatrixEvent} The event
212222
*/
213-
export function mkMessage(opts: IMessageOpts): object | MatrixEvent {
223+
export function mkMessage(opts: IMessageOpts, client?: MatrixClient): object | MatrixEvent {
214224
const eventOpts: IEventOpts = {
215225
...opts,
216226
type: EventType.RoomMessage,
@@ -223,7 +233,7 @@ export function mkMessage(opts: IMessageOpts): object | MatrixEvent {
223233
if (!eventOpts.content.body) {
224234
eventOpts.content.body = "Random->" + Math.random();
225235
}
226-
return mkEvent(eventOpts);
236+
return mkEvent(eventOpts, client);
227237
}
228238

229239
/**

spec/unit/matrix-client.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -981,7 +981,7 @@ describe("MatrixClient", function() {
981981

982982
expect(rootEvent.isThreadRoot).toBe(true);
983983

984-
const [roomEvents, threadEvents] = client.partitionThreadedEvents(room, [rootEvent]);
984+
const [roomEvents, threadEvents] = room.partitionThreadedEvents([rootEvent]);
985985
expect(roomEvents).toHaveLength(1);
986986
expect(threadEvents).toHaveLength(1);
987987

0 commit comments

Comments
 (0)