From 33b01207d369724aa9255892c40cc2644fb1081c Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Thu, 26 Dec 2019 17:55:29 +0100 Subject: [PATCH] Can invite a user to join a group --- src/controllers/GroupsController.ts | 19 +++++++++++++++++++ src/controllers/Routes.ts | 2 ++ src/helpers/GroupsHelper.ts | 17 +++++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/src/controllers/GroupsController.ts b/src/controllers/GroupsController.ts index 5af73a7..2dcae4f 100644 --- a/src/controllers/GroupsController.ts +++ b/src/controllers/GroupsController.ts @@ -289,6 +289,25 @@ export class GroupsController { h.send(members.map((m) => this.GroupMemberToAPI(m))); } + /** + * Invite a user to join the network + * + * @param h Request handler + */ + public static async InviteUser(h: RequestHandler) { + const groupID = await h.postGroupIDWithAccess("group_id", GroupsAccessLevel.MODERATOR_ACCESS); + const userID = await h.postUserId("userID"); + + if(!await GroupsHelper.GetMembershipLevel(groupID, userID)) + h.error(401, "The user is not a visitor of the group!"); + + await GroupsHelper.SendInvitation(groupID, userID); + + // TODO : Create a notification + + h.success("The user has been successfully invited to join the group!"); + } + /** * Turn a GroupInfo object into a valid API object * diff --git a/src/controllers/Routes.ts b/src/controllers/Routes.ts index 515f8f3..bd765db 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -106,4 +106,6 @@ export const Routes : Route[] = [ {path: "/groups/delete_logo", cb: (h) => GroupsController.DeleteLogo(h)}, {path: "/groups/get_members", cb: (h) => GroupsController.GetMembers(h)}, + + {path: "/groups/invite", cb: (h) => GroupsController.InviteUser(h)}, ] \ No newline at end of file diff --git a/src/helpers/GroupsHelper.ts b/src/helpers/GroupsHelper.ts index 2032f85..bb81afa 100644 --- a/src/helpers/GroupsHelper.ts +++ b/src/helpers/GroupsHelper.ts @@ -158,6 +158,23 @@ export class GroupsHelper { return result.visibility; } + /** + * Invite a user to join a group + * + * @param groupID The ID of the target group + * @param userID The ID of the target user + */ + public static async SendInvitation(groupID: number, userID: number) { + await this.InsertMember(new GroupMember({ + id: -1, + userID: userID, + groupID: groupID, + timeCreate: time(), + following: true, + level: GroupMembershipLevels.INVITED + })); + } + /** * Insert a new group member *