Skip to content

Commit

Permalink
Add "reason" to leaveRoom. (#301)
Browse files Browse the repository at this point in the history
Add "reason" to leave.
  • Loading branch information
Half-Shot authored Mar 19, 2023
1 parent c7d1677 commit b9911ef
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/MatrixClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>} resolves when left
*/
@timedMatrixClientFunctionCall()
public leaveRoom(roomId: string): Promise<any> {
return this.doRequest("POST", "/_matrix/client/v3/rooms/" + encodeURIComponent(roomId) + "/leave", null, {});
public leaveRoom(roomId: string, reason?: string): Promise<any> {
return this.doRequest("POST", "/_matrix/client/v3/rooms/" + encodeURIComponent(roomId) + "/leave", null, { reason });
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/appservice/Intent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>} Resolves when the room has been left.
*/
@timedIntentFunctionCall()
public async leaveRoom(roomId: string): Promise<any> {
public async leaveRoom(roomId: string, reason?: string): Promise<any> {
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();
});
Expand Down
15 changes: 15 additions & 0 deletions test/MatrixClientTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit b9911ef

Please sign in to comment.