diff --git a/src/index.ts b/src/index.ts index e8df699..9214807 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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"; diff --git a/src/matrixHelpers.ts b/src/matrixHelpers.ts new file mode 100644 index 0000000..15cd1cc --- /dev/null +++ b/src/matrixHelpers.ts @@ -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 { + const response = await validatedFetch( + `${matrixUrl}/_matrix/client/v3/login`, + Joi.object({ 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 { + const { room_id: roomId } = await validatedFetch( + `${matrixUrl}/_matrix/client/v3/createRoom?access_token=${params.accessToken}`, + Joi.object({ 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 { + 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 { + 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 { + 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( + `${matrixUrl}/_matrix/client/v3/rooms/${params.roomId}/messages?dir=b&limit=1&access_token=${params.accessToken}`, + Joi.object({ + 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, + }; +}