-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} |