Skip to content

Commit 38e04c8

Browse files
authored
Give MatrixClient.invite an options param (#4919)
1 parent 1fcbc6e commit 38e04c8

File tree

3 files changed

+74
-5
lines changed

3 files changed

+74
-5
lines changed

spec/integ/matrix-client-methods.spec.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,59 @@ describe("MatrixClient", function () {
267267
});
268268
});
269269

270+
describe("invite", function () {
271+
it("should send request to /invite", async () => {
272+
const roomId = "!roomId:server";
273+
const userId = "@user:server";
274+
275+
httpBackend
276+
.when("POST", `/rooms/${encodeURIComponent(roomId)}/invite`)
277+
.check((request) => {
278+
expect(request.data).toEqual({ user_id: userId });
279+
})
280+
.respond(200, {});
281+
282+
const prom = client.invite(roomId, userId);
283+
await httpBackend.flushAllExpected();
284+
await prom;
285+
httpBackend.verifyNoOutstandingExpectation();
286+
});
287+
288+
it("accepts a stringy reason argument", async () => {
289+
const roomId = "!roomId:server";
290+
const userId = "@user:server";
291+
292+
httpBackend
293+
.when("POST", `/rooms/${encodeURIComponent(roomId)}/invite`)
294+
.check((request) => {
295+
expect(request.data).toEqual({ user_id: userId, reason: "testreason" });
296+
})
297+
.respond(200, {});
298+
299+
const prom = client.invite(roomId, userId, "testreason");
300+
await httpBackend.flushAllExpected();
301+
await prom;
302+
httpBackend.verifyNoOutstandingExpectation();
303+
});
304+
305+
it("accepts an options object with a reason", async () => {
306+
const roomId = "!roomId:server";
307+
const userId = "@user:server";
308+
309+
httpBackend
310+
.when("POST", `/rooms/${encodeURIComponent(roomId)}/invite`)
311+
.check((request) => {
312+
expect(request.data).toEqual({ user_id: userId, reason: "testreason" });
313+
})
314+
.respond(200, {});
315+
316+
const prom = client.invite(roomId, userId, { reason: "testreason" });
317+
await httpBackend.flushAllExpected();
318+
await prom;
319+
httpBackend.verifyNoOutstandingExpectation();
320+
});
321+
});
322+
270323
describe("knockRoom", function () {
271324
const roomId = "!some-room-id:example.org";
272325
const reason = "some reason";

src/@types/requests.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ export interface IJoinRoomOpts {
4343
viaServers?: string[];
4444
}
4545

46+
/** Options object for {@link MatrixClient.invite}. */
47+
export interface InviteOpts {
48+
/**
49+
* The reason for the invite.
50+
*/
51+
reason?: string;
52+
}
53+
4654
export interface KnockRoomOpts {
4755
/**
4856
* The reason for the knock.

src/client.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ import {
115115
type IGuestAccessOpts,
116116
type IJoinRoomOpts,
117117
type INotificationsResponse,
118+
type InviteOpts,
118119
type IPaginateOpts,
119120
type IPresenceOpts,
120121
type IRedactOpts,
@@ -3755,12 +3756,19 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
37553756
}
37563757

37573758
/**
3758-
* @param reason - Optional.
3759-
* @returns Promise which resolves: `{}` an empty object.
3760-
* @returns Rejects: with an error response.
3759+
* Send an invite to the given user to join the given room.
3760+
*
3761+
* @param roomId - The ID of the room to which the user should be invited.
3762+
* @param userId - The ID of the user that should be invited.
3763+
* @param opts - Optional reason object. For backwards compatibility, a string is also accepted, and will be interpreted as a reason.
3764+
*
3765+
* @returns An empty object.
37613766
*/
3762-
public invite(roomId: string, userId: string, reason?: string): Promise<EmptyObject> {
3763-
return this.membershipChange(roomId, userId, KnownMembership.Invite, reason);
3767+
public invite(roomId: string, userId: string, opts: InviteOpts | string = {}): Promise<EmptyObject> {
3768+
if (typeof opts != "object") {
3769+
opts = { reason: opts };
3770+
}
3771+
return this.membershipChange(roomId, userId, KnownMembership.Invite, opts.reason);
37643772
}
37653773

37663774
/**

0 commit comments

Comments
 (0)