From 8c31a4d8c4269ac5dbd1b7d3886ecd3e01cb7f50 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 27 Dec 2019 09:57:28 +0100 Subject: [PATCH] Can send a request to join a group --- src/controllers/GroupsController.ts | 39 +++++++++++++++++++++++++++++ src/controllers/Routes.ts | 2 ++ 2 files changed, 41 insertions(+) diff --git a/src/controllers/GroupsController.ts b/src/controllers/GroupsController.ts index 6fac2a1..a30f441 100644 --- a/src/controllers/GroupsController.ts +++ b/src/controllers/GroupsController.ts @@ -329,6 +329,45 @@ export class GroupsController { h.success("Response to the invitation was successfully saved!"); } + /** + * Send a request to join a server + * + * @param h Request handler + */ + public static async SendRequest(h: RequestHandler) { + + const groupID = await h.postGroupIDWithAccess("id", GroupsAccessLevel.LIMITED_ACCESS); + + // Check the user is really a visitor of the group + if(await GroupsHelper.GetMembershipLevel(groupID, h.getUserId()) != GroupMembershipLevels.VISITOR) + h.error(401, "You are not currently a visitor of the group!"); + + + // Check the user is allowed to send a request to join the group + const group = await GroupsHelper.GetInfo(groupID); + if(group.registrationLevel == GroupRegistrationLevel.CLOSED_REGISTRATION) + h.error(401, "You are not authorized to send a registration request for this group!"); + + // Create & insert membership + const member = new GroupMember({ + id: -1, + userID: h.getUserId(), + timeCreate: time(), + groupID: groupID, + level: group.registrationLevel == GroupRegistrationLevel.MODERATED_REGISTRATION + ? GroupMembershipLevels.PENDING : GroupMembershipLevels.MEMBER, + following: true, + }); + + await GroupsHelper.InsertMember(member); + + if(group.registrationLevel == GroupRegistrationLevel.MODERATED_REGISTRATION) { + //TODO : Send a notification + } + + h.success("The membership has been successfully saved!"); + } + /** * Turn a GroupInfo object into a valid API object * diff --git a/src/controllers/Routes.ts b/src/controllers/Routes.ts index c7928fc..a100761 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -110,4 +110,6 @@ export const Routes : Route[] = [ {path: "/groups/invite", cb: (h) => GroupsController.InviteUser(h)}, {path: "/groups/respond_invitation", cb: (h) => GroupsController.RespondInvitation(h)}, + + {path: "/groups/send_request", cb: (h) => GroupsController.SendRequest(h)}, ] \ No newline at end of file