From 87d81e0f9602500a0ed5fcee9e5c8e75f1378bd1 Mon Sep 17 00:00:00 2001 From: Half-Shot Date: Mon, 13 Mar 2023 13:59:28 +0000 Subject: [PATCH] Add "reason" to leave. --- src/MatrixClient.ts | 5 +++-- src/appservice/Intent.ts | 5 +++-- test/MatrixClientTest.ts | 15 +++++++++++++++ 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/MatrixClient.ts b/src/MatrixClient.ts index 9058fdea..43f4a6b2 100644 --- a/src/MatrixClient.ts +++ b/src/MatrixClient.ts @@ -1162,11 +1162,12 @@ export class MatrixClient extends EventEmitter { /** * Leaves the given room * @param {string} roomId the room ID to leave + * @param {string=} reason Optional reason to be included as the reason for leaving the room. * @returns {Promise} resolves when left */ @timedMatrixClientFunctionCall() - public leaveRoom(roomId: string): Promise { - return this.doRequest("POST", "/_matrix/client/v3/rooms/" + encodeURIComponent(roomId) + "/leave", null, {}); + public leaveRoom(roomId: string, reason?: string): Promise { + return this.doRequest("POST", "/_matrix/client/v3/rooms/" + encodeURIComponent(roomId) + "/leave", null, { reason }); } /** diff --git a/src/appservice/Intent.ts b/src/appservice/Intent.ts index 13b0d575..961d1752 100644 --- a/src/appservice/Intent.ts +++ b/src/appservice/Intent.ts @@ -201,12 +201,13 @@ export class Intent { /** * Leaves the given room. * @param {string} roomId The room ID to leave + * @param {string=} reason Optional reason to be included as the reason for leaving the room. * @returns {Promise} Resolves when the room has been left. */ @timedIntentFunctionCall() - public async leaveRoom(roomId: string): Promise { + public async leaveRoom(roomId: string, reason?: string): Promise { await this.ensureRegistered(); - return this.client.leaveRoom(roomId).then(async () => { + return this.client.leaveRoom(roomId, reason).then(async () => { // Recalculate joined rooms now that we've left a room await this.refreshJoinedRooms(); }); diff --git a/test/MatrixClientTest.ts b/test/MatrixClientTest.ts index fd07051c..102cee59 100644 --- a/test/MatrixClientTest.ts +++ b/test/MatrixClientTest.ts @@ -3340,6 +3340,21 @@ describe('MatrixClient', () => { await Promise.all([client.leaveRoom(roomId), http.flushAllExpected()]); }); + it('should include a reason if provided', async () => { + const { client, http, hsUrl } = createTestClient(); + + const roomId = "!testing:example.org"; + const reason = "I am done testing here"; + + // noinspection TypeScriptValidateJSTypes + http.when("POST", "/_matrix/client/v3/rooms").respond(200, (path, content) => { + expect(content).toEqual({ reason }); + expect(path).toEqual(`${hsUrl}/_matrix/client/v3/rooms/${encodeURIComponent(roomId)}/leave`); + return {}; + }); + + await Promise.all([client.leaveRoom(roomId, reason), http.flushAllExpected()]); + }); }); describe('forgetRoom', () => {