2018-03-24 23:09:34 +00:00
|
|
|
import { doClientApiCall } from "./helpers";
|
2017-12-24 09:02:57 +00:00
|
|
|
import AppService from "../db/models/AppService";
|
|
|
|
|
|
|
|
export interface MatrixUserResponse {
|
|
|
|
access_token: string;
|
|
|
|
device_id: string;
|
|
|
|
home_server: string;
|
|
|
|
user_id: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class MatrixAppserviceClient {
|
|
|
|
|
2018-03-24 23:09:34 +00:00
|
|
|
constructor(private appservice: AppService) {
|
2017-12-24 09:02:57 +00:00
|
|
|
}
|
|
|
|
|
2018-03-24 03:26:14 +00:00
|
|
|
public async registerUser(localpart: string): Promise<MatrixUserResponse> {
|
2018-03-24 23:09:34 +00:00
|
|
|
return doClientApiCall(
|
2017-12-24 09:02:57 +00:00
|
|
|
"POST",
|
|
|
|
"/_matrix/client/r0/register",
|
|
|
|
{access_token: this.appservice.asToken},
|
|
|
|
{type: "m.login.application_service", username: localpart},
|
|
|
|
);
|
|
|
|
}
|
2018-03-24 18:18:38 +00:00
|
|
|
|
|
|
|
public async whoAmI(): Promise<string> {
|
2018-03-24 23:09:34 +00:00
|
|
|
const response = await doClientApiCall(
|
2018-03-24 18:18:38 +00:00
|
|
|
"GET",
|
|
|
|
"/_matrix/client/r0/account/whoami",
|
|
|
|
{access_token: this.appservice.asToken},
|
|
|
|
);
|
|
|
|
return response['user_id'];
|
|
|
|
}
|
2017-12-24 09:02:57 +00:00
|
|
|
}
|