From 47bbeb2e5bb176945f5c676ba6cd7080e8662149 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Wed, 25 May 2022 11:03:59 -0600 Subject: [PATCH 1/2] Convert getLocalAliases to a stable API call --- spec/unit/matrix-client.spec.ts | 25 +++++++++++++++++++++++++ src/client.ts | 15 ++++++++------- src/http-api.ts | 7 ++++++- 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/spec/unit/matrix-client.spec.ts b/spec/unit/matrix-client.spec.ts index 489aa167950..b01187e7c7d 100644 --- a/spec/unit/matrix-client.spec.ts +++ b/spec/unit/matrix-client.spec.ts @@ -150,6 +150,10 @@ describe("MatrixClient", function() { } return Promise.resolve(next.data); } + // Jest doesn't let us have custom expectation errors, so if you're seeing this then + // you forgot to handle at least 1 pending request. Check your tests to ensure your + // number of expectations lines up with your number of requests made, and that those + // requests match your expectations. expect(true).toBe(false); return new Promise(() => {}); } @@ -1191,4 +1195,25 @@ describe("MatrixClient", function() { passwordTest({ auth, new_password: newPassword, logout_devices: false }, callback); }); }); + + describe("getLocalAliases", () => { + it("should call the right endpoint", async () => { + const response = { + aliases: ["#woop:example.org", "#another:example.org"], + }; + client.http.authedRequest.mockClear().mockResolvedValue(response); + + const roomId = "!whatever:example.org"; + const result = await client.getLocalAliases(roomId); + + // Current version of the endpoint we support is v3 + const [callback, method, path, queryParams, _, opts] = client.http.authedRequest.mock.calls[0]; + expect(callback).toBeFalsy(); + expect(method).toBe('GET'); + expect(path).toEqual(`/rooms/${encodeURIComponent(roomId)}/aliases`); + expect(opts).toMatchObject({ prefix: "/_matrix/client/v3" }); + expect(queryParams).toBeFalsy(); + expect(result!.aliases).toEqual(response.aliases); + }); + }); }); diff --git a/src/client.ts b/src/client.ts index 7d1787a2b08..f678eefdffe 100644 --- a/src/client.ts +++ b/src/client.ts @@ -61,6 +61,7 @@ import { PREFIX_R0, PREFIX_UNSTABLE, PREFIX_V1, + PREFIX_V3, retryNetworkOperation, UploadContentResponseType, } from "./http-api"; @@ -7531,16 +7532,16 @@ export class MatrixClient extends TypedEventEmitter { - const path = utils.encodeUri("/rooms/$roomId/aliases", - { $roomId: roomId }); - const prefix = PREFIX_UNSTABLE + "/org.matrix.msc2432"; - return this.http.authedRequest(callback, Method.Get, path, null, null, { prefix }); + public getLocalAliases(roomId: string): Promise<{ aliases: string[] }> { + const path = utils.encodeUri("/rooms/$roomId/aliases", { $roomId: roomId }); + const prefix = PREFIX_V3; + return this.http.authedRequest(undefined, Method.Get, path, null, null, { prefix }); } /** diff --git a/src/http-api.ts b/src/http-api.ts index b6a5abb544b..23c692b1fb0 100644 --- a/src/http-api.ts +++ b/src/http-api.ts @@ -48,10 +48,15 @@ TODO: export const PREFIX_R0 = "/_matrix/client/r0"; /** - * A constant representing the URI path for release v1 of the Client-Server HTTP API. + * A constant representing the URI path for the legacy release v1 of the Client-Server HTTP API. */ export const PREFIX_V1 = "/_matrix/client/v1"; +/** + * A constant representing the URI path for Client-Server API endpoints versioned at v3. + */ +export const PREFIX_V3 = "/_matrix/client/v3"; + /** * A constant representing the URI path for as-yet unspecified Client-Server HTTP APIs. */ From 8dab0461ff483d21aac52751b703ec59557ea4b8 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Wed, 25 May 2022 11:14:48 -0600 Subject: [PATCH 2/2] Appease the linter --- .eslintrc.js | 2 ++ spec/unit/matrix-client.spec.ts | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/.eslintrc.js b/.eslintrc.js index 6fc5b99a671..1f5ce5cbd1e 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -52,6 +52,8 @@ module.exports = { "@typescript-eslint/no-explicit-any": "off", // We'd rather not do this but we do "@typescript-eslint/ban-ts-comment": "off", + // We're okay with assertion errors when we ask for them + "@typescript-eslint/no-non-null-assertion": "off", "quotes": "off", // We use a `logger` intermediary module diff --git a/spec/unit/matrix-client.spec.ts b/spec/unit/matrix-client.spec.ts index b01187e7c7d..795d1ae5c43 100644 --- a/spec/unit/matrix-client.spec.ts +++ b/spec/unit/matrix-client.spec.ts @@ -1207,8 +1207,9 @@ describe("MatrixClient", function() { const result = await client.getLocalAliases(roomId); // Current version of the endpoint we support is v3 - const [callback, method, path, queryParams, _, opts] = client.http.authedRequest.mock.calls[0]; + const [callback, method, path, queryParams, data, opts] = client.http.authedRequest.mock.calls[0]; expect(callback).toBeFalsy(); + expect(data).toBeFalsy(); expect(method).toBe('GET'); expect(path).toEqual(`/rooms/${encodeURIComponent(roomId)}/aliases`); expect(opts).toMatchObject({ prefix: "/_matrix/client/v3" });