2019-02-11 02:10:11 +05:30
|
|
|
import {
|
2019-06-27 02:01:36 +05:30
|
|
|
HomeServerError,
|
2019-02-11 02:10:11 +05:30
|
|
|
} from "./error.js";
|
2019-02-11 01:55:29 +05:30
|
|
|
|
2019-02-05 03:56:24 +05:30
|
|
|
class RequestWrapper {
|
2019-12-23 18:58:27 +05:30
|
|
|
constructor(method, url, requestResult) {
|
|
|
|
this._requestResult = requestResult;
|
|
|
|
this._promise = this._requestResult.response().then(response => {
|
|
|
|
// ok?
|
|
|
|
if (response.status >= 200 && response.status < 300) {
|
|
|
|
return response.body;
|
|
|
|
} else {
|
|
|
|
switch (response.status) {
|
|
|
|
default:
|
2020-03-17 04:37:54 +05:30
|
|
|
throw new HomeServerError(method, url, response.body, response.status);
|
2019-12-23 18:58:27 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2019-06-27 02:01:36 +05:30
|
|
|
}
|
2018-12-21 19:05:24 +05:30
|
|
|
|
2019-06-27 02:01:36 +05:30
|
|
|
abort() {
|
2019-12-23 18:58:27 +05:30
|
|
|
return this._requestResult.abort();
|
2019-06-27 02:01:36 +05:30
|
|
|
}
|
2018-12-21 19:05:24 +05:30
|
|
|
|
2019-06-27 02:01:36 +05:30
|
|
|
response() {
|
|
|
|
return this._promise;
|
|
|
|
}
|
2018-12-21 19:05:24 +05:30
|
|
|
}
|
|
|
|
|
2019-02-07 05:55:12 +05:30
|
|
|
export default class HomeServerApi {
|
2019-12-23 18:58:27 +05:30
|
|
|
constructor({homeServer, accessToken, request}) {
|
2019-03-09 00:33:47 +05:30
|
|
|
// store these both in a closure somehow so it's harder to get at in case of XSS?
|
|
|
|
// one could change the homeserver as well so the token gets sent there, so both must be protected from read/write
|
2019-12-23 18:58:27 +05:30
|
|
|
this._homeserver = homeServer;
|
2019-06-27 02:01:36 +05:30
|
|
|
this._accessToken = accessToken;
|
2019-12-23 18:58:27 +05:30
|
|
|
this._requestFn = request;
|
2019-06-27 02:01:36 +05:30
|
|
|
}
|
2018-12-21 19:05:24 +05:30
|
|
|
|
2019-06-27 02:01:36 +05:30
|
|
|
_url(csPath) {
|
|
|
|
return `${this._homeserver}/_matrix/client/r0${csPath}`;
|
|
|
|
}
|
2018-12-21 19:05:24 +05:30
|
|
|
|
2020-03-31 03:26:03 +05:30
|
|
|
_request(method, url, queryParams = {}, body) {
|
2019-06-27 02:01:36 +05:30
|
|
|
const queryString = Object.entries(queryParams)
|
|
|
|
.filter(([, value]) => value !== undefined)
|
2019-10-12 23:54:09 +05:30
|
|
|
.map(([name, value]) => {
|
|
|
|
if (typeof value === "object") {
|
|
|
|
value = JSON.stringify(value);
|
|
|
|
}
|
|
|
|
return `${encodeURIComponent(name)}=${encodeURIComponent(value)}`;
|
|
|
|
})
|
2019-06-27 02:01:36 +05:30
|
|
|
.join("&");
|
2020-03-31 03:26:03 +05:30
|
|
|
url = `${url}?${queryString}`;
|
2019-06-27 02:01:36 +05:30
|
|
|
let bodyString;
|
|
|
|
const headers = new Headers();
|
|
|
|
if (this._accessToken) {
|
|
|
|
headers.append("Authorization", `Bearer ${this._accessToken}`);
|
|
|
|
}
|
|
|
|
headers.append("Accept", "application/json");
|
|
|
|
if (body) {
|
|
|
|
headers.append("Content-Type", "application/json");
|
|
|
|
bodyString = JSON.stringify(body);
|
|
|
|
}
|
2019-12-23 18:58:27 +05:30
|
|
|
const requestResult = this._requestFn(url, {
|
2019-06-27 02:01:36 +05:30
|
|
|
method,
|
|
|
|
headers,
|
|
|
|
body: bodyString,
|
|
|
|
});
|
2019-12-23 18:58:27 +05:30
|
|
|
return new RequestWrapper(method, url, requestResult);
|
2019-06-27 02:01:36 +05:30
|
|
|
}
|
2019-02-05 03:56:24 +05:30
|
|
|
|
2019-06-27 02:01:36 +05:30
|
|
|
_post(csPath, queryParams, body) {
|
2020-03-31 03:26:03 +05:30
|
|
|
return this._request("POST", this._url(csPath), queryParams, body);
|
2019-06-27 02:01:36 +05:30
|
|
|
}
|
2019-02-05 03:56:24 +05:30
|
|
|
|
2019-07-27 01:33:57 +05:30
|
|
|
_put(csPath, queryParams, body) {
|
2020-03-31 03:26:03 +05:30
|
|
|
return this._request("PUT", this._url(csPath), queryParams, body);
|
2019-07-27 01:33:57 +05:30
|
|
|
}
|
|
|
|
|
2019-06-27 02:01:36 +05:30
|
|
|
_get(csPath, queryParams, body) {
|
2020-03-31 03:26:03 +05:30
|
|
|
return this._request("GET", this._url(csPath), queryParams, body);
|
2019-06-27 02:01:36 +05:30
|
|
|
}
|
2018-12-21 19:05:24 +05:30
|
|
|
|
2019-06-27 02:01:36 +05:30
|
|
|
sync(since, filter, timeout) {
|
|
|
|
return this._get("/sync", {since, timeout, filter});
|
|
|
|
}
|
2019-02-05 03:56:24 +05:30
|
|
|
|
2019-03-09 05:11:06 +05:30
|
|
|
// params is from, dir and optionally to, limit, filter.
|
|
|
|
messages(roomId, params) {
|
2019-10-12 23:54:09 +05:30
|
|
|
return this._get(`/rooms/${encodeURIComponent(roomId)}/messages`, params);
|
2019-03-09 05:11:06 +05:30
|
|
|
}
|
|
|
|
|
2019-07-27 01:33:57 +05:30
|
|
|
send(roomId, eventType, txnId, content) {
|
2019-10-12 23:54:09 +05:30
|
|
|
return this._put(`/rooms/${encodeURIComponent(roomId)}/send/${encodeURIComponent(eventType)}/${encodeURIComponent(txnId)}`, {}, content);
|
2019-07-27 01:33:57 +05:30
|
|
|
}
|
|
|
|
|
2019-06-27 02:01:36 +05:30
|
|
|
passwordLogin(username, password) {
|
2019-02-05 03:56:24 +05:30
|
|
|
return this._post("/login", undefined, {
|
|
|
|
"type": "m.login.password",
|
|
|
|
"identifier": {
|
|
|
|
"type": "m.id.user",
|
|
|
|
"user": username
|
|
|
|
},
|
|
|
|
"password": password
|
|
|
|
});
|
2019-06-27 02:01:36 +05:30
|
|
|
}
|
2019-10-12 23:54:09 +05:30
|
|
|
|
|
|
|
createFilter(userId, filter) {
|
|
|
|
return this._post(`/user/${encodeURIComponent(userId)}/filter`, undefined, filter);
|
|
|
|
}
|
2020-03-31 03:26:03 +05:30
|
|
|
|
|
|
|
versions(timeout) {
|
|
|
|
// TODO: implement timeout
|
|
|
|
return this._request("GET", `${this._homeserver}/_matrix/client/versions`);
|
|
|
|
}
|
2019-03-08 16:56:59 +05:30
|
|
|
}
|