|
|
|
@ -17,12 +17,23 @@ limitations under the License.
|
|
|
|
|
|
|
|
|
|
import {encodeQueryParams, encodeBody} from "./common";
|
|
|
|
|
import {HomeServerRequest} from "./HomeServerRequest";
|
|
|
|
|
import type {Reconnector} from "./Reconnector";
|
|
|
|
|
import type {IEncodedBody} from "./common";
|
|
|
|
|
import type {IRequestOptions, RequestFunction} from "../../platform/types/Platform";
|
|
|
|
|
import type {LogItem} from "../../logging/LogItem";
|
|
|
|
|
|
|
|
|
|
type RequestMethod = "POST" | "GET" | "PUT";
|
|
|
|
|
|
|
|
|
|
const CS_R0_PREFIX = "/_matrix/client/r0";
|
|
|
|
|
const DEHYDRATION_PREFIX = "/_matrix/client/unstable/org.matrix.msc2697.v2";
|
|
|
|
|
|
|
|
|
|
export class HomeServerApi {
|
|
|
|
|
constructor({homeserver, accessToken, request, reconnector}) {
|
|
|
|
|
private readonly _homeserver: string;
|
|
|
|
|
private readonly _accessToken: string;
|
|
|
|
|
private readonly _requestFn: RequestFunction;
|
|
|
|
|
private readonly _reconnector: Reconnector;
|
|
|
|
|
|
|
|
|
|
constructor({homeserver, accessToken, request, reconnector}: {homeserver: string, accessToken: string, request: RequestFunction, reconnector: Reconnector}) {
|
|
|
|
|
// 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
|
|
|
|
|
this._homeserver = homeserver;
|
|
|
|
@ -31,14 +42,14 @@ export class HomeServerApi {
|
|
|
|
|
this._reconnector = reconnector;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_url(csPath, prefix = CS_R0_PREFIX) {
|
|
|
|
|
_url(csPath: string, prefix: string = CS_R0_PREFIX): string {
|
|
|
|
|
return this._homeserver + prefix + csPath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_baseRequest(method, url, queryParams, body, options, accessToken) {
|
|
|
|
|
_baseRequest(method: RequestMethod, url: string, queryParams?: object, body?: object, options?: IRequestOptions, accessToken?: string): HomeServerRequest {
|
|
|
|
|
const queryString = encodeQueryParams(queryParams);
|
|
|
|
|
url = `${url}?${queryString}`;
|
|
|
|
|
let log;
|
|
|
|
|
let log: LogItem | undefined;
|
|
|
|
|
if (options?.log) {
|
|
|
|
|
const parent = options?.log;
|
|
|
|
|
log = parent.child({
|
|
|
|
@ -47,8 +58,8 @@ export class HomeServerApi {
|
|
|
|
|
method,
|
|
|
|
|
}, parent.level.Info);
|
|
|
|
|
}
|
|
|
|
|
let encodedBody;
|
|
|
|
|
const headers = new Map();
|
|
|
|
|
let encodedBody: IEncodedBody["body"];
|
|
|
|
|
const headers: Map<string, string | number> = new Map();
|
|
|
|
|
if (accessToken) {
|
|
|
|
|
headers.set("Authorization", `Bearer ${accessToken}`);
|
|
|
|
|
}
|
|
|
|
@ -56,6 +67,7 @@ export class HomeServerApi {
|
|
|
|
|
if (body) {
|
|
|
|
|
const encoded = encodeBody(body);
|
|
|
|
|
headers.set("Content-Type", encoded.mimeType);
|
|
|
|
|
//todo: remove this?
|
|
|
|
|
headers.set("Content-Length", encoded.length);
|
|
|
|
|
encodedBody = encoded.body;
|
|
|
|
|
}
|
|
|
|
@ -86,63 +98,63 @@ export class HomeServerApi {
|
|
|
|
|
return hsRequest;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_unauthedRequest(method, url, queryParams, body, options) {
|
|
|
|
|
return this._baseRequest(method, url, queryParams, body, options, null);
|
|
|
|
|
_unauthedRequest(method: RequestMethod, url: string, queryParams?: object, body?: object, options?: IRequestOptions): HomeServerRequest {
|
|
|
|
|
return this._baseRequest(method, url, queryParams, body, options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_authedRequest(method, url, queryParams, body, options) {
|
|
|
|
|
_authedRequest(method: RequestMethod, url: string, queryParams?: object, body?: object, options?: IRequestOptions): HomeServerRequest {
|
|
|
|
|
return this._baseRequest(method, url, queryParams, body, options, this._accessToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_post(csPath, queryParams, body, options) {
|
|
|
|
|
_post(csPath: string, queryParams: object, body: object, options?: IRequestOptions): HomeServerRequest {
|
|
|
|
|
return this._authedRequest("POST", this._url(csPath, options?.prefix || CS_R0_PREFIX), queryParams, body, options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_put(csPath, queryParams, body, options) {
|
|
|
|
|
_put(csPath: string, queryParams: object, body?: object, options?: IRequestOptions): HomeServerRequest {
|
|
|
|
|
return this._authedRequest("PUT", this._url(csPath, options?.prefix || CS_R0_PREFIX), queryParams, body, options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_get(csPath, queryParams, body, options) {
|
|
|
|
|
_get(csPath: string, queryParams?: object, body?: object, options?: IRequestOptions): HomeServerRequest {
|
|
|
|
|
return this._authedRequest("GET", this._url(csPath, options?.prefix || CS_R0_PREFIX), queryParams, body, options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sync(since, filter, timeout, options = null) {
|
|
|
|
|
return this._get("/sync", {since, timeout, filter}, null, options);
|
|
|
|
|
sync(since: string, filter: string, timeout: number, options?: IRequestOptions) {
|
|
|
|
|
return this._get("/sync", {since, timeout, filter}, undefined, options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// params is from, dir and optionally to, limit, filter.
|
|
|
|
|
messages(roomId, params, options = null) {
|
|
|
|
|
return this._get(`/rooms/${encodeURIComponent(roomId)}/messages`, params, null, options);
|
|
|
|
|
messages(roomId: string, params: object, options?: IRequestOptions) {
|
|
|
|
|
return this._get(`/rooms/${encodeURIComponent(roomId)}/messages`, params, undefined, options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// params is at, membership and not_membership
|
|
|
|
|
members(roomId, params, options = null) {
|
|
|
|
|
return this._get(`/rooms/${encodeURIComponent(roomId)}/members`, params, null, options);
|
|
|
|
|
members(roomId: string, params: object, options?: IRequestOptions) {
|
|
|
|
|
return this._get(`/rooms/${encodeURIComponent(roomId)}/members`, params, undefined, options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
send(roomId, eventType, txnId, content, options = null) {
|
|
|
|
|
send(roomId: string, eventType: string, txnId: string, content: object, options?: IRequestOptions) {
|
|
|
|
|
return this._put(`/rooms/${encodeURIComponent(roomId)}/send/${encodeURIComponent(eventType)}/${encodeURIComponent(txnId)}`, {}, content, options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
redact(roomId, eventId, txnId, content, options = null) {
|
|
|
|
|
redact(roomId: string, eventId: string, txnId: string, content: object, options?: IRequestOptions) {
|
|
|
|
|
return this._put(`/rooms/${encodeURIComponent(roomId)}/redact/${encodeURIComponent(eventId)}/${encodeURIComponent(txnId)}`, {}, content, options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
receipt(roomId, receiptType, eventId, options = null) {
|
|
|
|
|
receipt(roomId: string, receiptType: string, eventId: string, options?: IRequestOptions) {
|
|
|
|
|
return this._post(`/rooms/${encodeURIComponent(roomId)}/receipt/${encodeURIComponent(receiptType)}/${encodeURIComponent(eventId)}`,
|
|
|
|
|
{}, {}, options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
state(roomId, eventType, stateKey, options = null) {
|
|
|
|
|
return this._get(`/rooms/${encodeURIComponent(roomId)}/state/${encodeURIComponent(eventType)}/${encodeURIComponent(stateKey)}`, {}, null, options);
|
|
|
|
|
state(roomId: string, eventType: string, stateKey: string, options?: IRequestOptions) {
|
|
|
|
|
return this._get(`/rooms/${encodeURIComponent(roomId)}/state/${encodeURIComponent(eventType)}/${encodeURIComponent(stateKey)}`, {}, undefined, options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getLoginFlows() {
|
|
|
|
|
return this._unauthedRequest("GET", this._url("/login"), null, null, null);
|
|
|
|
|
return this._unauthedRequest("GET", this._url("/login"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
passwordLogin(username, password, initialDeviceDisplayName, options = null) {
|
|
|
|
|
return this._unauthedRequest("POST", this._url("/login"), null, {
|
|
|
|
|
passwordLogin(username: string, password: string, initialDeviceDisplayName: string, options?: IRequestOptions) {
|
|
|
|
|
return this._unauthedRequest("POST", this._url("/login"), undefined, {
|
|
|
|
|
"type": "m.login.password",
|
|
|
|
|
"identifier": {
|
|
|
|
|
"type": "m.id.user",
|
|
|
|
@ -153,8 +165,8 @@ export class HomeServerApi {
|
|
|
|
|
}, options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tokenLogin(loginToken, txnId, initialDeviceDisplayName, options = null) {
|
|
|
|
|
return this._unauthedRequest("POST", this._url("/login"), null, {
|
|
|
|
|
tokenLogin(loginToken: string, txnId: string, initialDeviceDisplayName: string, options?: IRequestOptions) {
|
|
|
|
|
return this._unauthedRequest("POST", this._url("/login"), undefined, {
|
|
|
|
|
"type": "m.login.token",
|
|
|
|
|
"identifier": {
|
|
|
|
|
"type": "m.id.user",
|
|
|
|
@ -165,91 +177,91 @@ export class HomeServerApi {
|
|
|
|
|
}, options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
createFilter(userId, filter, options = null) {
|
|
|
|
|
return this._post(`/user/${encodeURIComponent(userId)}/filter`, null, filter, options);
|
|
|
|
|
createFilter(userId: string, filter: object, options?: IRequestOptions) {
|
|
|
|
|
return this._post(`/user/${encodeURIComponent(userId)}/filter`, {}, filter, options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
versions(options = null) {
|
|
|
|
|
return this._unauthedRequest("GET", `${this._homeserver}/_matrix/client/versions`, null, null, options);
|
|
|
|
|
versions(options?: IRequestOptions) {
|
|
|
|
|
return this._unauthedRequest("GET", `${this._homeserver}/_matrix/client/versions`, undefined, undefined, options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uploadKeys(dehydratedDeviceId, payload, options = null) {
|
|
|
|
|
uploadKeys(dehydratedDeviceId: string, payload: object, options?: IRequestOptions) {
|
|
|
|
|
let path = "/keys/upload";
|
|
|
|
|
if (dehydratedDeviceId) {
|
|
|
|
|
path = path + `/${encodeURIComponent(dehydratedDeviceId)}`;
|
|
|
|
|
}
|
|
|
|
|
return this._post(path, null, payload, options);
|
|
|
|
|
return this._post(path, {}, payload, options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
queryKeys(queryRequest, options = null) {
|
|
|
|
|
return this._post("/keys/query", null, queryRequest, options);
|
|
|
|
|
queryKeys(queryRequest: object, options?: IRequestOptions) {
|
|
|
|
|
return this._post("/keys/query", {}, queryRequest, options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
claimKeys(payload, options = null) {
|
|
|
|
|
return this._post("/keys/claim", null, payload, options);
|
|
|
|
|
claimKeys(payload: object, options?: IRequestOptions) {
|
|
|
|
|
return this._post("/keys/claim", {}, payload, options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sendToDevice(type, payload, txnId, options = null) {
|
|
|
|
|
return this._put(`/sendToDevice/${encodeURIComponent(type)}/${encodeURIComponent(txnId)}`, null, payload, options);
|
|
|
|
|
sendToDevice(type: string, payload: object, txnId: string, options?: IRequestOptions) {
|
|
|
|
|
return this._put(`/sendToDevice/${encodeURIComponent(type)}/${encodeURIComponent(txnId)}`, {}, payload, options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
roomKeysVersion(version = null, options = null) {
|
|
|
|
|
roomKeysVersion(version?: string, options?: IRequestOptions) {
|
|
|
|
|
let versionPart = "";
|
|
|
|
|
if (version) {
|
|
|
|
|
versionPart = `/${encodeURIComponent(version)}`;
|
|
|
|
|
}
|
|
|
|
|
return this._get(`/room_keys/version${versionPart}`, null, null, options);
|
|
|
|
|
return this._get(`/room_keys/version${versionPart}`, undefined, undefined, options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
roomKeyForRoomAndSession(version, roomId, sessionId, options = null) {
|
|
|
|
|
return this._get(`/room_keys/keys/${encodeURIComponent(roomId)}/${encodeURIComponent(sessionId)}`, {version}, null, options);
|
|
|
|
|
roomKeyForRoomAndSession(version: string, roomId: string, sessionId: string, options?: IRequestOptions) {
|
|
|
|
|
return this._get(`/room_keys/keys/${encodeURIComponent(roomId)}/${encodeURIComponent(sessionId)}`, {version}, undefined, options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uploadAttachment(blob, filename, options = null) {
|
|
|
|
|
uploadAttachment(blob: Blob, filename: string, options?: IRequestOptions) {
|
|
|
|
|
return this._authedRequest("POST", `${this._homeserver}/_matrix/media/r0/upload`, {filename}, blob, options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setPusher(pusher, options = null) {
|
|
|
|
|
return this._post("/pushers/set", null, pusher, options);
|
|
|
|
|
setPusher(pusher: object, options?: IRequestOptions) {
|
|
|
|
|
return this._post("/pushers/set", {}, pusher, options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getPushers(options = null) {
|
|
|
|
|
return this._get("/pushers", null, null, options);
|
|
|
|
|
getPushers(options?: IRequestOptions) {
|
|
|
|
|
return this._get("/pushers", undefined, undefined, options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
join(roomId, options = null) {
|
|
|
|
|
return this._post(`/rooms/${encodeURIComponent(roomId)}/join`, null, null, options);
|
|
|
|
|
join(roomId: string, options?: IRequestOptions) {
|
|
|
|
|
return this._post(`/rooms/${encodeURIComponent(roomId)}/join`, {}, {}, options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
joinIdOrAlias(roomIdOrAlias, options = null) {
|
|
|
|
|
return this._post(`/join/${encodeURIComponent(roomIdOrAlias)}`, null, null, options);
|
|
|
|
|
joinIdOrAlias(roomIdOrAlias: string, options?: IRequestOptions) {
|
|
|
|
|
return this._post(`/join/${encodeURIComponent(roomIdOrAlias)}`, {}, {}, options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
leave(roomId, options = null) {
|
|
|
|
|
return this._post(`/rooms/${encodeURIComponent(roomId)}/leave`, null, null, options);
|
|
|
|
|
leave(roomId: string, options?: IRequestOptions) {
|
|
|
|
|
return this._post(`/rooms/${encodeURIComponent(roomId)}/leave`, {}, {}, options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
forget(roomId, options = null) {
|
|
|
|
|
return this._post(`/rooms/${encodeURIComponent(roomId)}/forget`, null, null, options);
|
|
|
|
|
forget(roomId: string, options?: IRequestOptions) {
|
|
|
|
|
return this._post(`/rooms/${encodeURIComponent(roomId)}/forget`, {}, {}, options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logout(options = null) {
|
|
|
|
|
return this._post(`/logout`, null, null, options);
|
|
|
|
|
logout(options?: IRequestOptions) {
|
|
|
|
|
return this._post(`/logout`, {}, {}, options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getDehydratedDevice(options = {}) {
|
|
|
|
|
getDehydratedDevice(options: IRequestOptions) {
|
|
|
|
|
options.prefix = DEHYDRATION_PREFIX;
|
|
|
|
|
return this._get(`/dehydrated_device`, null, null, options);
|
|
|
|
|
return this._get(`/dehydrated_device`, undefined, undefined, options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
createDehydratedDevice(payload, options = {}) {
|
|
|
|
|
createDehydratedDevice(payload: object, options: IRequestOptions) {
|
|
|
|
|
options.prefix = DEHYDRATION_PREFIX;
|
|
|
|
|
return this._put(`/dehydrated_device`, null, payload, options);
|
|
|
|
|
return this._put(`/dehydrated_device`, {}, payload, options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
claimDehydratedDevice(deviceId, options = {}) {
|
|
|
|
|
claimDehydratedDevice(deviceId: string, options: IRequestOptions) {
|
|
|
|
|
options.prefix = DEHYDRATION_PREFIX;
|
|
|
|
|
return this._post(`/dehydrated_device/claim`, null, {device_id: deviceId}, options);
|
|
|
|
|
return this._post(`/dehydrated_device/claim`, {}, {device_id: deviceId}, options);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -258,11 +270,14 @@ import {Request as MockRequest} from "../../mocks/Request.js";
|
|
|
|
|
export function tests() {
|
|
|
|
|
return {
|
|
|
|
|
"superficial happy path for GET": async assert => {
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
const hsApi = new HomeServerApi({
|
|
|
|
|
request: () => new MockRequest().respond(200, 42),
|
|
|
|
|
homeserver: "https://hs.tld"
|
|
|
|
|
homeserver: "https://hs.tld",
|
|
|
|
|
});
|
|
|
|
|
const result = await hsApi._get("foo", null, null, null).response();
|
|
|
|
|
const result = await hsApi
|
|
|
|
|
._get("foo", undefined, undefined, undefined)
|
|
|
|
|
.response();
|
|
|
|
|
assert.strictEqual(result, 42);
|
|
|
|
|
}
|
|
|
|
|
}
|