Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce matrix helpers #19

Merged
merged 4 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * as fixtures from "./fixtures";
export * as gitDaemon from "./gitDaemon";
export * as githubWebhooks from "./githubWebhooks";
export * as matrixHelpers from "./matrixHelpers";
export * as mockServer from "./mockServer";
141 changes: 141 additions & 0 deletions src/matrixHelpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import { validatedFetch } from "@eng-automation/js";
import Joi from "joi";

type AccessTokenResponse = { access_token: string };

export async function getAccessToken(matrixUrl: string, params: { user: string; password: string }): Promise<string> {
const response = await validatedFetch<AccessTokenResponse>(
`${matrixUrl}/_matrix/client/v3/login`,
Joi.object<AccessTokenResponse>({ access_token: Joi.string().required() }),
{
init: {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ type: "m.login.password", user: params.user, password: params.password }),
},
},
);

return response.access_token;
}

type RoomResponse = { room_id: string };

export async function createRoom(
matrixUrl: string,
params: {
roomAliasName: string;
accessToken: string;
},
): Promise<string> {
const { room_id: roomId } = await validatedFetch<RoomResponse>(
`${matrixUrl}/_matrix/client/v3/createRoom?access_token=${params.accessToken}`,
Joi.object<RoomResponse>({ room_id: Joi.string().required() }),
{
init: {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ room_alias_name: params.roomAliasName }),
},
},
);

return roomId;
}

export async function inviteUser(
matrixUrl: string,
params: {
roomId: string;
accessToken: string;
userId: string;
},
): Promise<void> {
await validatedFetch(
`${matrixUrl}/_matrix/client/v3/rooms/${params.roomId}/invite?access_token=${params.accessToken}`,
Joi.any(),
{
init: {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ user_id: params.userId }),
},
},
);
}

export async function joinRoom(
matrixUrl: string,
params: {
roomId: string;
accessToken: string;
},
): Promise<void> {
await validatedFetch(
`${matrixUrl}/_matrix/client/v3/rooms/${params.roomId}/join?access_token=${params.accessToken}`,
Joi.any(),
{ init: { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({}) } },
);
}

export async function postMessage(
matrixUrl: string,
params: {
roomId: string;
accessToken: string;
body: string;
},
): Promise<void> {
await validatedFetch(
`${matrixUrl}/_matrix/client/v3/rooms/${params.roomId}/send/m.room.message?access_token=${params.accessToken}`,
Joi.any(),
{
init: {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ msgtype: "m.text", body: params.body }),
},
},
);
}

type LatestMessageResponse = {
chunk: [
{
sender: string;
content: { body: string; formatted_body: string };
},
];
};

export async function getLatestMessage(
matrixUrl: string,
params: {
roomId: string;
accessToken: string;
},
): Promise<{
sender: string;
body: string;
formattedBody: string;
}> {
const res = await validatedFetch<LatestMessageResponse>(
`${matrixUrl}/_matrix/client/v3/rooms/${params.roomId}/messages?dir=b&limit=1&access_token=${params.accessToken}`,
Joi.object<LatestMessageResponse>({
chunk: Joi.array().items(
Joi.object({
sender: Joi.string().required(),
content: Joi.object({ body: Joi.string().required() }),
formatted_body: Joi.object({ formatted_body: Joi.string().required() }),
}).required(),
),
}),
{ init: { headers: { "Content-Type": "application/json" } } },
);

return {
sender: res.chunk[0].sender,
body: res.chunk[0].content.body,
formattedBody: res.chunk[0].content.formatted_body,
};
}
Loading