Skip to content

Commit

Permalink
Consolidate API prefixes into enums
Browse files Browse the repository at this point in the history
  • Loading branch information
t3chguy committed Sep 30, 2022
1 parent 7231c68 commit 689d072
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 75 deletions.
68 changes: 31 additions & 37 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,10 @@ import {
MatrixError,
MatrixHttpApi,
Method,
PREFIX_IDENTITY_V2,
PREFIX_MEDIA_R0,
PREFIX_R0,
PREFIX_UNSTABLE,
PREFIX_V1,
PREFIX_V3,
retryNetworkOperation,
UploadContentResponseType,
} from "./http-api";
import { ClientPrefix, MediaPrefix, IdentityPrefix } from "./http-api/prefix";
import {
Crypto,
CryptoEvent,
Expand Down Expand Up @@ -1001,7 +996,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
idBaseUrl: opts.idBaseUrl,
accessToken: opts.accessToken,
request: opts.request,
prefix: PREFIX_R0,
prefix: ClientPrefix.R0,
onlyData: true,
extraParams: opts.queryParams,
localTimeoutMs: opts.localTimeoutMs,
Expand Down Expand Up @@ -2693,7 +2688,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
try {
res = await this.http.authedRequest<IKeyBackupInfo>(
undefined, Method.Get, "/room_keys/version", undefined, undefined,
{ prefix: PREFIX_UNSTABLE },
{ prefix: ClientPrefix.Unstable },
);
} catch (e) {
if (e.errcode === 'M_NOT_FOUND') {
Expand Down Expand Up @@ -2849,7 +2844,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa

const res = await this.http.authedRequest<IKeyBackupInfo>(
undefined, Method.Post, "/room_keys/version", undefined, data,
{ prefix: PREFIX_UNSTABLE },
{ prefix: ClientPrefix.Unstable },
);

// We could assume everything's okay and enable directly, but this ensures
Expand Down Expand Up @@ -2881,7 +2876,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa

return this.http.authedRequest(
undefined, Method.Delete, path, undefined, undefined,
{ prefix: PREFIX_UNSTABLE },
{ prefix: ClientPrefix.Unstable },
);
}

Expand Down Expand Up @@ -2935,7 +2930,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
const path = this.makeKeyBackupPath(roomId, sessionId, version);
return this.http.authedRequest(
undefined, Method.Put, path.path, path.queryData, data,
{ prefix: PREFIX_UNSTABLE },
{ prefix: ClientPrefix.Unstable },
);
}

Expand Down Expand Up @@ -3226,7 +3221,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa

const res = await this.http.authedRequest<IRoomsKeysResponse | IRoomKeysResponse | IKeyBackupSession>(
undefined, Method.Get, path.path, path.queryData, undefined,
{ prefix: PREFIX_UNSTABLE },
{ prefix: ClientPrefix.Unstable },
);

if ((res as IRoomsKeysResponse).rooms) {
Expand Down Expand Up @@ -3291,7 +3286,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
const path = this.makeKeyBackupPath(roomId, sessionId, version);
return this.http.authedRequest(
undefined, Method.Delete, path.path, path.queryData, undefined,
{ prefix: PREFIX_UNSTABLE },
{ prefix: ClientPrefix.Unstable },
);
}

Expand Down Expand Up @@ -3337,7 +3332,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
public getMediaConfig(callback?: Callback): Promise<IMediaConfig> {
return this.http.authedRequest(
callback, Method.Get, "/config", undefined, undefined, {
prefix: PREFIX_MEDIA_R0,
prefix: MediaPrefix.R0,
},
);
}
Expand Down Expand Up @@ -4760,7 +4755,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
url,
ts: ts.toString(),
}, undefined, {
prefix: PREFIX_MEDIA_R0,
prefix: MediaPrefix.R0,
},
);
// TODO: Expire the URL preview cache sometimes
Expand Down Expand Up @@ -6586,7 +6581,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa

const res = await this.http.authedRequest<{ joined: string[] }>(
undefined, Method.Get, path, undefined, undefined,
{ prefix: PREFIX_UNSTABLE },
{ prefix: ClientPrefix.Unstable },
);
return res.joined;
}
Expand Down Expand Up @@ -6888,7 +6883,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
private termsUrlForService(serviceType: SERVICE_TYPES, baseUrl: string) {
switch (serviceType) {
case SERVICE_TYPES.IS:
return baseUrl + PREFIX_IDENTITY_V2 + '/terms';
return baseUrl + IdentityPrefix.V2 + '/terms';
case SERVICE_TYPES.IM:
return baseUrl + '/_matrix/integrations/v1/terms';
default:
Expand Down Expand Up @@ -7115,7 +7110,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
undefined,
{ refresh_token: refreshToken },
{
prefix: PREFIX_V1,
prefix: ClientPrefix.V1,
inhibitLogoutEmit: true, // we don't want to cause logout loops
},
);
Expand Down Expand Up @@ -7221,7 +7216,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
[SSO_ACTION_PARAM.unstable!]: action,
};

return this.http.getUrl(url, params, PREFIX_R0);
return this.http.getUrl(url, params, ClientPrefix.R0);
}

/**
Expand Down Expand Up @@ -7312,7 +7307,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
"/org.matrix.msc3882/login/token",
undefined, // no query params
body,
{ prefix: PREFIX_UNSTABLE },
{ prefix: ClientPrefix.Unstable },
);
}

Expand All @@ -7331,7 +7326,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa

return this.http.getUrl(path, {
session: authSessionId,
}, PREFIX_R0);
}, ClientPrefix.R0);
}

/**
Expand Down Expand Up @@ -7411,7 +7406,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
});
return this.http.authedRequest(
undefined, Method.Get, path, undefined, undefined, {
prefix: PREFIX_UNSTABLE,
prefix: ClientPrefix.Unstable,
},
);
}
Expand Down Expand Up @@ -7714,7 +7709,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
*/
public getLocalAliases(roomId: string): Promise<{ aliases: string[] }> {
const path = utils.encodeUri("/rooms/$roomId/aliases", { $roomId: roomId });
const prefix = PREFIX_V3;
const prefix = ClientPrefix.V3;
return this.http.authedRequest(undefined, Method.Get, path, undefined, undefined, { prefix });
}

Expand Down Expand Up @@ -7970,7 +7965,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
*/
public async addThreePidOnly(data: IAddThreePidOnlyBody): Promise<{}> {
const path = "/account/3pid/add";
const prefix = await this.isVersionSupported("r0.6.0") ? PREFIX_R0 : PREFIX_UNSTABLE;
const prefix = await this.isVersionSupported("r0.6.0") ? ClientPrefix.R0 : ClientPrefix.Unstable;
return this.http.authedRequest(undefined, Method.Post, path, undefined, data, { prefix });
}

Expand All @@ -7990,8 +7985,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
*/
public async bindThreePid(data: IBindThreePidBody): Promise<{}> {
const path = "/account/3pid/bind";
const prefix = await this.isVersionSupported("r0.6.0") ?
PREFIX_R0 : PREFIX_UNSTABLE;
const prefix = await this.isVersionSupported("r0.6.0") ? ClientPrefix.R0 : ClientPrefix.Unstable;
return this.http.authedRequest(
undefined, Method.Post, path, undefined, data, { prefix },
);
Expand Down Expand Up @@ -8019,7 +8013,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
address,
id_server: this.getIdentityServerUrl(true),
};
const prefix = await this.isVersionSupported("r0.6.0") ? PREFIX_R0 : PREFIX_UNSTABLE;
const prefix = await this.isVersionSupported("r0.6.0") ? ClientPrefix.R0 : ClientPrefix.Unstable;
return this.http.authedRequest(undefined, Method.Post, path, undefined, data, { prefix });
}

Expand Down Expand Up @@ -8374,7 +8368,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
return this.http.authedRequest(
undefined, Method.Post, '/keys/signatures/upload', undefined,
content, {
prefix: PREFIX_UNSTABLE,
prefix: ClientPrefix.Unstable,
},
);
}
Expand Down Expand Up @@ -8475,7 +8469,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
if (auth) Object.assign(data, { auth });
return this.http.authedRequest(
undefined, Method.Post, "/keys/device_signing/upload", undefined, data, {
prefix: PREFIX_UNSTABLE,
prefix: ClientPrefix.Unstable,
},
);
}
Expand All @@ -8498,7 +8492,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
throw new Error("No identity server base URL set");
}

const uri = this.idBaseUrl + PREFIX_IDENTITY_V2 + "/account/register";
const uri = this.idBaseUrl + IdentityPrefix.V2 + "/account/register";
return this.http.requestOtherUrl(
undefined, Method.Post, uri,
null, hsOpenIdToken,
Expand Down Expand Up @@ -8546,7 +8540,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa

return this.http.idServerRequest(
callback, Method.Post, "/validate/email/requestToken",
params, PREFIX_IDENTITY_V2, identityAccessToken,
params, IdentityPrefix.V2, identityAccessToken,
);
}

Expand Down Expand Up @@ -8596,7 +8590,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa

return this.http.idServerRequest(
callback, Method.Post, "/validate/msisdn/requestToken",
params, PREFIX_IDENTITY_V2, identityAccessToken,
params, IdentityPrefix.V2, identityAccessToken,
);
}

Expand Down Expand Up @@ -8633,7 +8627,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa

return this.http.idServerRequest(
undefined, Method.Post, "/validate/msisdn/submitToken",
params, PREFIX_IDENTITY_V2, identityAccessToken,
params, IdentityPrefix.V2, identityAccessToken,
);
}

Expand Down Expand Up @@ -8681,7 +8675,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
public getIdentityHashDetails(identityAccessToken: string): Promise<any> { // TODO: Types
return this.http.idServerRequest(
undefined, Method.Get, "/hash_details",
null, PREFIX_IDENTITY_V2, identityAccessToken,
null, IdentityPrefix.V2, identityAccessToken,
);
}

Expand Down Expand Up @@ -8750,7 +8744,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa

const response = await this.http.idServerRequest(
undefined, Method.Post, "/lookup",
params, PREFIX_IDENTITY_V2, identityAccessToken,
params, IdentityPrefix.V2, identityAccessToken,
);

if (!response || !response['mappings']) return []; // no results
Expand Down Expand Up @@ -8868,7 +8862,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
public getIdentityAccount(identityAccessToken: string): Promise<any> { // TODO: Types
return this.http.idServerRequest(
undefined, Method.Get, "/account",
undefined, PREFIX_IDENTITY_V2, identityAccessToken,
undefined, IdentityPrefix.V2, identityAccessToken,
);
}

Expand Down Expand Up @@ -9035,7 +9029,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
};

return this.http.authedRequest<IRoomHierarchy>(undefined, Method.Get, path, queryParams, undefined, {
prefix: PREFIX_V1,
prefix: ClientPrefix.V1,
}).catch(e => {
if (e.errcode === "M_UNRECOGNIZED") {
// fall back to the prefixed hierarchy API.
Expand Down
7 changes: 4 additions & 3 deletions src/crypto/EncryptionSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import { logger } from "../logger";
import { MatrixEvent } from "../models/event";
import { createCryptoStoreCacheCallbacks, ICacheCallbacks } from "./CrossSigning";
import { IndexedDBCryptoStore } from './store/indexeddb-crypto-store';
import { Method, PREFIX_UNSTABLE } from "../http-api";
import { Method } from "../http-api";
import { ClientPrefix } from "../http-api/prefix";
import { Crypto, IBootstrapCrossSigningOpts } from "./index";
import {
ClientEvent,
Expand Down Expand Up @@ -246,14 +247,14 @@ export class EncryptionSetupOperation {
algorithm: this.keyBackupInfo.algorithm,
auth_data: this.keyBackupInfo.auth_data,
},
{ prefix: PREFIX_UNSTABLE },
{ prefix: ClientPrefix.Unstable },
);
} else {
// add new key backup
await baseApis.http.authedRequest(
undefined, Method.Post, "/room_keys/version",
undefined, this.keyBackupInfo,
{ prefix: PREFIX_UNSTABLE },
{ prefix: ClientPrefix.Unstable },
);
}
}
Expand Down
Loading

0 comments on commit 689d072

Please sign in to comment.