2022-02-23 17:12:48 +01:00
|
|
|
/*
|
2024-09-09 15:57:16 +02:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-02-23 17:12:48 +01:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 15:57:16 +02:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2022-02-23 17:12:48 +01:00
|
|
|
*/
|
|
|
|
|
2023-08-14 10:58:55 +02:00
|
|
|
import { MatrixClient, MatrixEvent, MatrixEventEvent, RelationType, Room, Thread } from "matrix-js-sdk/src/matrix";
|
2022-02-23 17:12:48 +01:00
|
|
|
|
|
|
|
import { mkMessage, MessageEventProps } from "./test-utils";
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
export const makeThreadEvent = ({
|
|
|
|
rootEventId,
|
|
|
|
replyToEventId,
|
|
|
|
...props
|
|
|
|
}: MessageEventProps & {
|
|
|
|
rootEventId: string;
|
|
|
|
replyToEventId: string;
|
|
|
|
}): MatrixEvent =>
|
|
|
|
mkMessage({
|
|
|
|
...props,
|
|
|
|
relatesTo: {
|
|
|
|
event_id: rootEventId,
|
|
|
|
rel_type: "m.thread",
|
|
|
|
["m.in_reply_to"]: {
|
|
|
|
event_id: replyToEventId,
|
|
|
|
},
|
2022-02-23 17:12:48 +01:00
|
|
|
},
|
2022-12-12 12:24:14 +01:00
|
|
|
});
|
2022-02-23 17:12:48 +01:00
|
|
|
|
|
|
|
type MakeThreadEventsProps = {
|
|
|
|
roomId: Room["roomId"];
|
|
|
|
// root message user id
|
|
|
|
authorId: string;
|
|
|
|
// user ids of thread replies
|
|
|
|
// cycled through until thread length is fulfilled
|
|
|
|
participantUserIds: string[];
|
|
|
|
// number of messages in the thread, root message included
|
|
|
|
// optional, default 2
|
|
|
|
length?: number;
|
|
|
|
ts?: number;
|
|
|
|
// provide to set current_user_participated accurately
|
|
|
|
currentUserId?: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const makeThreadEvents = ({
|
2022-12-12 12:24:14 +01:00
|
|
|
roomId,
|
|
|
|
authorId,
|
|
|
|
participantUserIds,
|
|
|
|
length = 2,
|
|
|
|
ts = 1,
|
|
|
|
currentUserId,
|
|
|
|
}: MakeThreadEventsProps): { rootEvent: MatrixEvent; events: MatrixEvent[] } => {
|
2022-02-23 17:12:48 +01:00
|
|
|
const rootEvent = mkMessage({
|
|
|
|
user: authorId,
|
|
|
|
event: true,
|
|
|
|
room: roomId,
|
2022-12-12 12:24:14 +01:00
|
|
|
msg: "root event message " + Math.random(),
|
2022-02-23 17:12:48 +01:00
|
|
|
ts,
|
|
|
|
});
|
|
|
|
|
2023-02-13 18:01:43 +01:00
|
|
|
const rootEventId = rootEvent.getId()!;
|
2022-02-23 17:12:48 +01:00
|
|
|
const events = [rootEvent];
|
|
|
|
|
|
|
|
for (let i = 1; i < length; i++) {
|
|
|
|
const prevEvent = events[i - 1];
|
2023-02-13 18:01:43 +01:00
|
|
|
const replyToEventId = prevEvent.getId()!;
|
2022-02-23 17:12:48 +01:00
|
|
|
const user = participantUserIds[i % participantUserIds.length];
|
2022-12-12 12:24:14 +01:00
|
|
|
events.push(
|
|
|
|
makeThreadEvent({
|
|
|
|
user,
|
|
|
|
room: roomId,
|
|
|
|
event: true,
|
|
|
|
msg: `reply ${i} by ${user}`,
|
|
|
|
rootEventId,
|
|
|
|
replyToEventId,
|
|
|
|
// replies are 1ms after each other
|
|
|
|
ts: ts + i,
|
|
|
|
}),
|
|
|
|
);
|
2022-02-23 17:12:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
rootEvent.setUnsigned({
|
2022-02-24 09:44:34 +01:00
|
|
|
"m.relations": {
|
2022-10-20 13:48:25 +02:00
|
|
|
[RelationType.Thread]: {
|
2024-11-05 12:34:42 +01:00
|
|
|
latest_event: events[events.length - 1].event,
|
2022-02-24 09:44:34 +01:00
|
|
|
count: length,
|
2023-02-13 18:01:43 +01:00
|
|
|
current_user_participated: [...participantUserIds, authorId].includes(currentUserId!),
|
2022-02-24 09:44:34 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2022-02-23 17:12:48 +01:00
|
|
|
|
|
|
|
return { rootEvent, events };
|
|
|
|
};
|
2022-10-20 13:48:25 +02:00
|
|
|
|
|
|
|
type MakeThreadProps = {
|
|
|
|
room: Room;
|
|
|
|
client: MatrixClient;
|
|
|
|
authorId: string;
|
|
|
|
participantUserIds: string[];
|
|
|
|
length?: number;
|
|
|
|
ts?: number;
|
|
|
|
};
|
|
|
|
|
2023-11-29 14:36:52 +01:00
|
|
|
/**
|
|
|
|
* Create a thread but don't actually populate it with events - see
|
|
|
|
* populateThread for what you probably want to do.
|
|
|
|
*
|
|
|
|
* Leaving this here in case it is needed by some people, but I (andyb) would
|
|
|
|
* expect us to move to use populateThread exclusively.
|
|
|
|
*/
|
2022-10-20 13:48:25 +02:00
|
|
|
export const mkThread = ({
|
|
|
|
room,
|
|
|
|
client,
|
|
|
|
authorId,
|
|
|
|
participantUserIds,
|
|
|
|
length = 2,
|
|
|
|
ts = 1,
|
2022-12-12 12:24:14 +01:00
|
|
|
}: MakeThreadProps): { thread: Thread; rootEvent: MatrixEvent; events: MatrixEvent[] } => {
|
2022-10-20 13:48:25 +02:00
|
|
|
const { rootEvent, events } = makeThreadEvents({
|
|
|
|
roomId: room.roomId,
|
|
|
|
authorId,
|
|
|
|
participantUserIds,
|
|
|
|
length,
|
|
|
|
ts,
|
2023-02-13 18:01:43 +01:00
|
|
|
currentUserId: client.getUserId()!,
|
2022-10-20 13:48:25 +02:00
|
|
|
});
|
2022-10-28 13:48:15 +02:00
|
|
|
expect(rootEvent).toBeTruthy();
|
|
|
|
|
|
|
|
for (const evt of events) {
|
2022-12-12 12:24:14 +01:00
|
|
|
room?.reEmitter.reEmit(evt, [MatrixEventEvent.BeforeRedaction]);
|
2022-10-28 13:48:15 +02:00
|
|
|
}
|
2022-10-20 13:48:25 +02:00
|
|
|
|
2023-02-13 18:01:43 +01:00
|
|
|
const thread = room.createThread(rootEvent.getId()!, rootEvent, events, true);
|
2023-06-01 09:53:48 +02:00
|
|
|
|
2022-10-24 08:50:21 +02:00
|
|
|
return { thread, rootEvent, events };
|
2022-10-20 13:48:25 +02:00
|
|
|
};
|
2023-11-29 14:36:52 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a thread, and make sure the events added to the thread and the room's
|
|
|
|
* timeline as if they came in via sync.
|
|
|
|
*
|
|
|
|
* Note that mkThread doesn't actually add the events properly to the room.
|
|
|
|
*/
|
|
|
|
export const populateThread = async ({
|
|
|
|
room,
|
|
|
|
client,
|
|
|
|
authorId,
|
|
|
|
participantUserIds,
|
|
|
|
length = 2,
|
|
|
|
ts = 1,
|
|
|
|
}: MakeThreadProps): Promise<{ thread: Thread; rootEvent: MatrixEvent; events: MatrixEvent[] }> => {
|
|
|
|
const ret = mkThread({ room, client, authorId, participantUserIds, length, ts });
|
|
|
|
|
|
|
|
// So that we do not have to mock the thread loading, tell the thread
|
|
|
|
// that it is already loaded, and send the events again to the room
|
|
|
|
// so they are added to the thread timeline.
|
|
|
|
ret.thread.initialEventsFetched = true;
|
|
|
|
await room.addLiveEvents(ret.events);
|
|
|
|
return ret;
|
|
|
|
};
|