2023-11-30 18:18:18 +08:00
|
|
|
/*
|
2024-09-09 21:57:16 +08:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2023-11-30 18:18:18 +08:00
|
|
|
Copyright 2023 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 21:57:16 +08:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2023-11-30 18:18:18 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
import { JSHandle, Page } from "@playwright/test";
|
|
|
|
import { PageFunctionOn } from "playwright-core/types/structs";
|
|
|
|
|
2023-12-19 16:36:54 +08:00
|
|
|
import { Network } from "./network";
|
2023-12-05 21:24:10 +08:00
|
|
|
import type {
|
|
|
|
IContent,
|
|
|
|
ICreateRoomOpts,
|
|
|
|
ISendEventResponse,
|
|
|
|
MatrixClient,
|
|
|
|
Room,
|
|
|
|
MatrixEvent,
|
|
|
|
ReceiptType,
|
2023-12-05 23:20:20 +08:00
|
|
|
IRoomDirectoryOptions,
|
2023-12-12 22:08:36 +08:00
|
|
|
KnockRoomOpts,
|
|
|
|
Visibility,
|
2023-12-13 22:59:08 +08:00
|
|
|
UploadOpts,
|
|
|
|
Upload,
|
2024-03-20 22:27:29 +08:00
|
|
|
StateEvents,
|
2024-03-25 20:48:48 +08:00
|
|
|
TimelineEvents,
|
2023-12-05 21:24:10 +08:00
|
|
|
} from "matrix-js-sdk/src/matrix";
|
2024-03-25 20:48:48 +08:00
|
|
|
import type { RoomMessageEventContent } from "matrix-js-sdk/src/types";
|
2023-12-12 16:55:29 +08:00
|
|
|
import { Credentials } from "../plugins/homeserver";
|
2023-11-30 18:18:18 +08:00
|
|
|
|
|
|
|
export class Client {
|
2023-12-19 16:36:54 +08:00
|
|
|
public network: Network;
|
2023-11-30 18:18:18 +08:00
|
|
|
protected client: JSHandle<MatrixClient>;
|
|
|
|
|
|
|
|
protected getClientHandle(): Promise<JSHandle<MatrixClient>> {
|
|
|
|
return this.page.evaluateHandle(() => window.mxMatrixClientPeg.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
public async prepareClient(): Promise<JSHandle<MatrixClient>> {
|
|
|
|
if (!this.client) {
|
|
|
|
this.client = await this.getClientHandle();
|
|
|
|
}
|
|
|
|
return this.client;
|
|
|
|
}
|
|
|
|
|
|
|
|
public constructor(protected readonly page: Page) {
|
|
|
|
page.on("framenavigated", async () => {
|
|
|
|
this.client = null;
|
|
|
|
});
|
2023-12-19 16:36:54 +08:00
|
|
|
this.network = new Network(page, this);
|
2023-11-30 18:18:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public evaluate<R, Arg, O extends MatrixClient = MatrixClient>(
|
|
|
|
pageFunction: PageFunctionOn<O, Arg, R>,
|
|
|
|
arg: Arg,
|
|
|
|
): Promise<R>;
|
|
|
|
public evaluate<R, O extends MatrixClient = MatrixClient>(
|
|
|
|
pageFunction: PageFunctionOn<O, void, R>,
|
|
|
|
arg?: any,
|
|
|
|
): Promise<R>;
|
|
|
|
public async evaluate<T>(fn: (client: MatrixClient) => T, arg?: any): Promise<T> {
|
|
|
|
await this.prepareClient();
|
|
|
|
return this.client.evaluate(fn, arg);
|
|
|
|
}
|
|
|
|
|
2023-12-05 18:37:23 +08:00
|
|
|
public evaluateHandle<R, Arg, O extends MatrixClient = MatrixClient>(
|
|
|
|
pageFunction: PageFunctionOn<O, Arg, R>,
|
|
|
|
arg: Arg,
|
|
|
|
): Promise<JSHandle<R>>;
|
|
|
|
public evaluateHandle<R, O extends MatrixClient = MatrixClient>(
|
|
|
|
pageFunction: PageFunctionOn<O, void, R>,
|
|
|
|
arg?: any,
|
|
|
|
): Promise<JSHandle<R>>;
|
|
|
|
public async evaluateHandle<T>(fn: (client: MatrixClient) => T, arg?: any): Promise<JSHandle<T>> {
|
|
|
|
await this.prepareClient();
|
|
|
|
return this.client.evaluateHandle(fn, arg);
|
|
|
|
}
|
|
|
|
|
2023-11-30 18:18:18 +08:00
|
|
|
/**
|
|
|
|
* @param roomId ID of the room to send the event into
|
|
|
|
* @param threadId ID of the thread to send into or null for main timeline
|
|
|
|
* @param eventType type of event to send
|
|
|
|
* @param content the event content to send
|
|
|
|
*/
|
|
|
|
public async sendEvent(
|
|
|
|
roomId: string,
|
|
|
|
threadId: string | null,
|
|
|
|
eventType: string,
|
|
|
|
content: IContent,
|
|
|
|
): Promise<ISendEventResponse> {
|
|
|
|
const client = await this.prepareClient();
|
|
|
|
return client.evaluate(
|
|
|
|
async (client, { roomId, threadId, eventType, content }) => {
|
2024-03-25 20:48:48 +08:00
|
|
|
return client.sendEvent(
|
|
|
|
roomId,
|
|
|
|
threadId,
|
|
|
|
eventType as keyof TimelineEvents,
|
|
|
|
content as TimelineEvents[keyof TimelineEvents],
|
|
|
|
);
|
2023-11-30 18:18:18 +08:00
|
|
|
},
|
|
|
|
{ roomId, threadId, eventType, content },
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-12-16 18:01:26 +08:00
|
|
|
* Send a message into a room
|
2023-11-30 18:18:18 +08:00
|
|
|
* @param roomId ID of the room to send the message into
|
|
|
|
* @param content the event content to send
|
2024-01-05 17:56:54 +08:00
|
|
|
* @param threadId optional thread id
|
2023-11-30 18:18:18 +08:00
|
|
|
*/
|
2024-01-05 17:56:54 +08:00
|
|
|
public async sendMessage(
|
|
|
|
roomId: string,
|
|
|
|
content: IContent | string,
|
|
|
|
threadId: string | null = null,
|
|
|
|
): Promise<ISendEventResponse> {
|
2023-12-12 16:55:29 +08:00
|
|
|
if (typeof content === "string") {
|
|
|
|
content = {
|
|
|
|
msgtype: "m.text",
|
2023-12-12 17:56:55 +08:00
|
|
|
body: content,
|
2023-12-12 16:55:29 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-11-30 18:18:18 +08:00
|
|
|
const client = await this.prepareClient();
|
|
|
|
return client.evaluate(
|
2024-01-05 17:56:54 +08:00
|
|
|
(client, { roomId, content, threadId }) => {
|
2024-03-25 20:48:48 +08:00
|
|
|
return client.sendMessage(roomId, threadId, content as RoomMessageEventContent);
|
2023-11-30 18:18:18 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
roomId,
|
|
|
|
content,
|
2024-01-05 17:56:54 +08:00
|
|
|
threadId,
|
2023-11-30 18:18:18 +08:00
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-12-05 18:37:23 +08:00
|
|
|
public async redactEvent(roomId: string, eventId: string, reason?: string): Promise<ISendEventResponse> {
|
|
|
|
return this.evaluate(
|
|
|
|
async (client, { roomId, eventId, reason }) => {
|
|
|
|
return client.redactEvent(roomId, eventId, reason);
|
|
|
|
},
|
|
|
|
{ roomId, eventId, reason },
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-11-30 18:18:18 +08:00
|
|
|
/**
|
|
|
|
* Create a room with given options.
|
|
|
|
* @param options the options to apply when creating the room
|
|
|
|
* @return the ID of the newly created room
|
|
|
|
*/
|
|
|
|
public async createRoom(options: ICreateRoomOpts): Promise<string> {
|
|
|
|
const client = await this.prepareClient();
|
|
|
|
return await client.evaluate(async (cli, options) => {
|
|
|
|
const resp = await cli.createRoom(options);
|
|
|
|
const roomId = resp.room_id;
|
|
|
|
if (!cli.getRoom(roomId)) {
|
|
|
|
await new Promise<void>((resolve) => {
|
|
|
|
const onRoom = (room: Room) => {
|
|
|
|
if (room.roomId === roomId) {
|
|
|
|
cli.off(window.matrixcs.ClientEvent.Room, onRoom);
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
cli.on(window.matrixcs.ClientEvent.Room, onRoom);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return roomId;
|
|
|
|
}, options);
|
|
|
|
}
|
|
|
|
|
2023-12-01 20:24:49 +08:00
|
|
|
/**
|
|
|
|
* Create a space with given options.
|
|
|
|
* @param options the options to apply when creating the space
|
|
|
|
* @return the ID of the newly created space (room)
|
|
|
|
*/
|
|
|
|
public async createSpace(options: ICreateRoomOpts): Promise<string> {
|
|
|
|
return this.createRoom({
|
|
|
|
...options,
|
|
|
|
creation_content: {
|
|
|
|
...options.creation_content,
|
|
|
|
type: "m.space",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-11-30 18:18:18 +08:00
|
|
|
/**
|
|
|
|
* Joins the given room by alias or ID
|
|
|
|
* @param roomIdOrAlias the id or alias of the room to join
|
|
|
|
*/
|
|
|
|
public async joinRoom(roomIdOrAlias: string): Promise<void> {
|
|
|
|
const client = await this.prepareClient();
|
|
|
|
await client.evaluate(async (client, roomIdOrAlias) => {
|
|
|
|
return await client.joinRoom(roomIdOrAlias);
|
|
|
|
}, roomIdOrAlias);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make this bot join a room by name
|
|
|
|
* @param roomName Name of the room to join
|
|
|
|
*/
|
2023-12-12 16:55:29 +08:00
|
|
|
public async joinRoomByName(roomName: string): Promise<string> {
|
2023-11-30 18:18:18 +08:00
|
|
|
const client = await this.prepareClient();
|
2023-12-12 16:55:29 +08:00
|
|
|
return client.evaluate(
|
|
|
|
async (client, { roomName }) => {
|
2023-11-30 18:18:18 +08:00
|
|
|
const room = client.getRooms().find((r) => r.getDefaultRoomName(client.getUserId()) === roomName);
|
|
|
|
if (room) {
|
2023-12-12 16:55:29 +08:00
|
|
|
await client.joinRoom(room.roomId);
|
|
|
|
return room.roomId;
|
2023-11-30 18:18:18 +08:00
|
|
|
}
|
|
|
|
throw new Error(`Bot room join failed. Cannot find room '${roomName}'`);
|
|
|
|
},
|
|
|
|
{
|
|
|
|
roomName,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2023-12-04 19:56:48 +08:00
|
|
|
|
2023-12-19 16:36:54 +08:00
|
|
|
/**
|
|
|
|
* Wait until next sync from this client
|
|
|
|
*/
|
|
|
|
public async waitForNextSync(): Promise<void> {
|
|
|
|
await this.page.waitForResponse(async (response) => {
|
|
|
|
const accessToken = await this.evaluate((client) => client.getAccessToken());
|
|
|
|
const authHeader = await response.request().headerValue("authorization");
|
|
|
|
return response.url().includes("/sync") && authHeader.includes(accessToken);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-12-04 19:56:48 +08:00
|
|
|
/**
|
|
|
|
* Invites the given user to the given room.
|
|
|
|
* @param roomId the id of the room to invite to
|
|
|
|
* @param userId the id of the user to invite
|
|
|
|
*/
|
|
|
|
public async inviteUser(roomId: string, userId: string): Promise<void> {
|
|
|
|
const client = await this.prepareClient();
|
|
|
|
await client.evaluate((client, { roomId, userId }) => client.invite(roomId, userId), {
|
|
|
|
roomId,
|
|
|
|
userId,
|
|
|
|
});
|
|
|
|
}
|
2023-12-05 21:24:10 +08:00
|
|
|
|
2023-12-12 22:08:36 +08:00
|
|
|
/**
|
|
|
|
* Knocks the given room.
|
|
|
|
* @param roomId the id of the room to knock
|
|
|
|
* @param opts the options to use when knocking
|
|
|
|
*/
|
|
|
|
public async knockRoom(roomId: string, opts?: KnockRoomOpts): Promise<void> {
|
|
|
|
const client = await this.prepareClient();
|
|
|
|
await client.evaluate((client, { roomId, opts }) => client.knockRoom(roomId, opts), { roomId, opts });
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Kicks the given user from the given room.
|
|
|
|
* @param roomId the id of the room to kick from
|
|
|
|
* @param userId the id of the user to kick
|
|
|
|
* @param reason the reason for the kick
|
|
|
|
*/
|
|
|
|
public async kick(roomId: string, userId: string, reason?: string): Promise<void> {
|
|
|
|
const client = await this.prepareClient();
|
|
|
|
await client.evaluate((client, { roomId, userId, reason }) => client.kick(roomId, userId, reason), {
|
|
|
|
roomId,
|
|
|
|
userId,
|
|
|
|
reason,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bans the given user from the given room.
|
|
|
|
* @param roomId the id of the room to ban from
|
|
|
|
* @param userId the id of the user to ban
|
|
|
|
* @param reason the reason for the ban
|
|
|
|
*/
|
|
|
|
public async ban(roomId: string, userId: string, reason?: string): Promise<void> {
|
|
|
|
const client = await this.prepareClient();
|
|
|
|
await client.evaluate((client, { roomId, userId, reason }) => client.ban(roomId, userId, reason), {
|
|
|
|
roomId,
|
|
|
|
userId,
|
|
|
|
reason,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unban the given user from the given room.
|
|
|
|
* @param roomId the id of the room to unban from
|
|
|
|
* @param userId the id of the user to unban
|
|
|
|
*/
|
|
|
|
public async unban(roomId: string, userId: string): Promise<void> {
|
|
|
|
const client = await this.prepareClient();
|
|
|
|
await client.evaluate((client, { roomId, userId }) => client.unban(roomId, userId), { roomId, userId });
|
|
|
|
}
|
|
|
|
|
2023-12-05 21:24:10 +08:00
|
|
|
/**
|
|
|
|
* @param {MatrixEvent} event
|
|
|
|
* @param {ReceiptType} receiptType
|
|
|
|
* @param {boolean} unthreaded
|
|
|
|
*/
|
|
|
|
public async sendReadReceipt(
|
|
|
|
event: JSHandle<MatrixEvent>,
|
|
|
|
receiptType?: ReceiptType,
|
|
|
|
unthreaded?: boolean,
|
|
|
|
): Promise<{}> {
|
|
|
|
const client = await this.prepareClient();
|
|
|
|
return client.evaluate(
|
|
|
|
(client, { event, receiptType, unthreaded }) => {
|
|
|
|
return client.sendReadReceipt(event, receiptType, unthreaded);
|
|
|
|
},
|
|
|
|
{ event, receiptType, unthreaded },
|
|
|
|
);
|
|
|
|
}
|
2023-12-05 23:20:20 +08:00
|
|
|
|
|
|
|
public async publicRooms(options?: IRoomDirectoryOptions): ReturnType<MatrixClient["publicRooms"]> {
|
|
|
|
const client = await this.prepareClient();
|
2023-12-12 16:55:29 +08:00
|
|
|
return client.evaluate((client, options) => {
|
2023-12-05 23:20:20 +08:00
|
|
|
return client.publicRooms(options);
|
|
|
|
}, options);
|
|
|
|
}
|
2023-12-12 16:55:29 +08:00
|
|
|
|
2023-12-13 22:59:08 +08:00
|
|
|
/**
|
|
|
|
* @param {string} name
|
|
|
|
* @param {module:client.callback} callback Optional.
|
|
|
|
* @return {Promise} Resolves: {} an empty object.
|
|
|
|
* @return {module:http-api.MatrixError} Rejects: with an error response.
|
|
|
|
*/
|
|
|
|
public async setDisplayName(name: string): Promise<{}> {
|
|
|
|
const client = await this.prepareClient();
|
|
|
|
return client.evaluate(async (cli: MatrixClient, name) => cli.setDisplayName(name), name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} url
|
|
|
|
* @param {module:client.callback} callback Optional.
|
|
|
|
* @return {Promise} Resolves: {} an empty object.
|
|
|
|
* @return {module:http-api.MatrixError} Rejects: with an error response.
|
|
|
|
*/
|
|
|
|
public async setAvatarUrl(url: string): Promise<{}> {
|
|
|
|
const client = await this.prepareClient();
|
|
|
|
return client.evaluate(async (cli: MatrixClient, url) => cli.setAvatarUrl(url), url);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Upload a file to the media repository on the homeserver.
|
|
|
|
*
|
|
|
|
* @param {object} file The object to upload. On a browser, something that
|
|
|
|
* can be sent to XMLHttpRequest.send (typically a File). Under node.js,
|
|
|
|
* a Buffer, String or ReadStream.
|
|
|
|
*/
|
|
|
|
public async uploadContent(file: Buffer, opts?: UploadOpts): Promise<Awaited<Upload["promise"]>> {
|
|
|
|
const client = await this.prepareClient();
|
|
|
|
return client.evaluate(
|
|
|
|
async (cli: MatrixClient, { file, opts }) => cli.uploadContent(new Uint8Array(file), opts),
|
|
|
|
{
|
|
|
|
file: [...file],
|
|
|
|
opts,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-12-12 16:55:29 +08:00
|
|
|
/**
|
2024-09-19 15:13:04 +08:00
|
|
|
* Bootstraps cross-signing.
|
2023-12-12 16:55:29 +08:00
|
|
|
*/
|
|
|
|
public async bootstrapCrossSigning(credentials: Credentials): Promise<void> {
|
|
|
|
const client = await this.prepareClient();
|
2024-09-19 15:13:04 +08:00
|
|
|
return bootstrapCrossSigningForClient(client, credentials);
|
2023-12-12 16:55:29 +08:00
|
|
|
}
|
2023-12-12 22:08:36 +08:00
|
|
|
|
2023-12-13 01:26:08 +08:00
|
|
|
/**
|
|
|
|
* Sets account data for the user.
|
|
|
|
* @param type The type of account data to set
|
|
|
|
* @param content The content to set
|
|
|
|
*/
|
|
|
|
public async setAccountData(type: string, content: IContent): Promise<void> {
|
|
|
|
const client = await this.prepareClient();
|
|
|
|
return client.evaluate(
|
|
|
|
async (client, { type, content }) => {
|
|
|
|
await client.setAccountData(type, content);
|
|
|
|
},
|
|
|
|
{ type, content },
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a state event into the room.
|
|
|
|
* @param roomId ID of the room to send the event into
|
|
|
|
* @param eventType type of event to send
|
|
|
|
* @param content the event content to send
|
|
|
|
* @param stateKey the state key to use
|
|
|
|
*/
|
|
|
|
public async sendStateEvent(
|
|
|
|
roomId: string,
|
|
|
|
eventType: string,
|
|
|
|
content: IContent,
|
|
|
|
stateKey?: string,
|
|
|
|
): Promise<ISendEventResponse> {
|
|
|
|
const client = await this.prepareClient();
|
|
|
|
return client.evaluate(
|
|
|
|
async (client, { roomId, eventType, content, stateKey }) => {
|
2024-03-20 22:27:29 +08:00
|
|
|
return client.sendStateEvent(roomId, eventType as keyof StateEvents, content, stateKey);
|
2023-12-13 01:26:08 +08:00
|
|
|
},
|
|
|
|
{ roomId, eventType, content, stateKey },
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Leaves the given room.
|
|
|
|
* @param roomId ID of the room to leave
|
|
|
|
*/
|
|
|
|
public async leave(roomId: string): Promise<void> {
|
|
|
|
const client = await this.prepareClient();
|
|
|
|
return client.evaluate(async (client, roomId) => {
|
|
|
|
await client.leave(roomId);
|
|
|
|
}, roomId);
|
|
|
|
}
|
|
|
|
|
2023-12-12 22:08:36 +08:00
|
|
|
/**
|
|
|
|
* Sets the directory visibility for a room.
|
|
|
|
* @param roomId ID of the room to set the directory visibility for
|
|
|
|
* @param visibility The new visibility for the room
|
|
|
|
*/
|
|
|
|
public async setRoomDirectoryVisibility(roomId: string, visibility: Visibility): Promise<void> {
|
|
|
|
const client = await this.prepareClient();
|
|
|
|
return client.evaluate(
|
|
|
|
async (client, { roomId, visibility }) => {
|
|
|
|
await client.setRoomDirectoryVisibility(roomId, visibility);
|
|
|
|
},
|
|
|
|
{ roomId, visibility },
|
|
|
|
);
|
|
|
|
}
|
2023-11-30 18:18:18 +08:00
|
|
|
}
|
2024-09-19 15:13:04 +08:00
|
|
|
|
|
|
|
/** Call `CryptoApi.bootstrapCrossSigning` on the given Matrix client, using the given credentials to authenticate
|
|
|
|
* the UIA request.
|
|
|
|
*/
|
|
|
|
export function bootstrapCrossSigningForClient(
|
|
|
|
client: JSHandle<MatrixClient>,
|
|
|
|
credentials: Credentials,
|
|
|
|
resetKeys: boolean = false,
|
|
|
|
) {
|
|
|
|
return client.evaluate(
|
|
|
|
async (client, { credentials, resetKeys }) => {
|
|
|
|
await client.getCrypto().bootstrapCrossSigning({
|
|
|
|
authUploadDeviceSigningKeys: async (func) => {
|
|
|
|
await func({
|
|
|
|
type: "m.login.password",
|
|
|
|
identifier: {
|
|
|
|
type: "m.id.user",
|
|
|
|
user: credentials.userId,
|
|
|
|
},
|
|
|
|
password: credentials.password,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
setupNewCrossSigning: resetKeys,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
{ credentials, resetKeys },
|
|
|
|
);
|
|
|
|
}
|