From 4f058e3b2c9af3b253f25f9fbadfaafab6e64a4f Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Fri, 18 Aug 2023 23:13:26 +0530 Subject: [PATCH] Implement invite from command --- src/domain/session/room/RoomViewModel.js | 8 ++++++++ src/matrix/net/HomeServerApi.ts | 9 +++++++++ src/matrix/room/Room.js | 7 +++++++ 3 files changed, 24 insertions(+) diff --git a/src/domain/session/room/RoomViewModel.js b/src/domain/session/room/RoomViewModel.js index 81835fb0..1e2c9af6 100644 --- a/src/domain/session/room/RoomViewModel.js +++ b/src/domain/session/room/RoomViewModel.js @@ -273,6 +273,14 @@ export class RoomViewModel extends ErrorReportViewModel { this.reportError(new Error("join syntax: /join ")); } break; + case "invite": + if (args.length === 1) { + const userId = args[0]; + await this._room.inviteUser(userId); + } else { + this.reportError(new Error("invite syntax: /invite ")); + } + break; case "shrug": message = "¯\\_(ツ)_/¯ " + args.join(" "); msgtype = "m.text"; diff --git a/src/matrix/net/HomeServerApi.ts b/src/matrix/net/HomeServerApi.ts index c5f90555..eebc692a 100644 --- a/src/matrix/net/HomeServerApi.ts +++ b/src/matrix/net/HomeServerApi.ts @@ -267,6 +267,15 @@ export class HomeServerApi { return this._get("/pushers", undefined, undefined, options); } + invite(roomId: string, userId: string, reason?: string, options?: BaseRequestOptions): IHomeServerRequest { + return this._post( + `/rooms/${encodeURIComponent(roomId)}/invite`, + {}, + { user_id: userId, reason }, + options + ); + } + join(roomId: string, options?: BaseRequestOptions): IHomeServerRequest { return this._post(`/rooms/${encodeURIComponent(roomId)}/join`, {}, {}, options); } diff --git a/src/matrix/room/Room.js b/src/matrix/room/Room.js index 47da3c03..73074f7a 100644 --- a/src/matrix/room/Room.js +++ b/src/matrix/room/Room.js @@ -470,6 +470,13 @@ export class Room extends BaseRoom { }); } + async inviteUser(userId, reason) { + if (!userId) { + throw new Error("userId is null/undefined"); + } + await this._hsApi.invite(this.id, userId, reason).response(); + } + /* called by BaseRoom to pass pendingEvents when opening the timeline */ _getPendingEvents() { return this._sendQueue.pendingEvents;