1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2024-11-22 13:29:22 +00:00

Can respond to membership invitation

This commit is contained in:
Pierre HUBERT 2019-12-26 18:16:44 +01:00
parent 33b01207d3
commit 5bb65e70bb
3 changed files with 87 additions and 0 deletions

View File

@ -308,6 +308,27 @@ export class GroupsController {
h.success("The user has been successfully invited to join the group!");
}
/**
* Respond to a user invitation
*
* @param h Request handler
*/
public static async RespondInvitation(h: RequestHandler) {
const groupID = await h.postGroupIDWithAccess("id", GroupsAccessLevel.LIMITED_ACCESS);
const accept = h.postBool("accept");
// Check if the user really received an invitation to join the group
if(!await GroupsHelper.ReceivedInvitation(groupID, h.getUserId()))
h.error(404, "Invitation not found!");
// Respond to the invitation
await GroupsHelper.RespondInvitation(groupID, h.getUserId(), accept);
// TODO : Create a notification
h.success("Response to the invitation was successfully saved!");
}
/**
* Turn a GroupInfo object into a valid API object
*

View File

@ -108,4 +108,6 @@ export const Routes : Route[] = [
{path: "/groups/get_members", cb: (h) => GroupsController.GetMembers(h)},
{path: "/groups/invite", cb: (h) => GroupsController.InviteUser(h)},
{path: "/groups/respond_invitation", cb: (h) => GroupsController.RespondInvitation(h)},
]

View File

@ -175,6 +175,37 @@ export class GroupsHelper {
}));
}
/**
* Check if a user received an invitation to join a group or not
*
* @param groupID Target group ID
* @param userID Target user ID
*/
public static async ReceivedInvitation(groupID: number, userID: number) : Promise<boolean> {
return await DatabaseHelper.Count({
table: GROUPS_MEMBERS_TABLE,
where: {
groups_id: groupID,
user_ID: userID,
level: GroupMembershipLevels.INVITED
}
}) > 0;
}
/**
* Respond to a membership invitation
*
* @param groupID Target group ID
* @param userID Target user ID
* @param accept true to accept invitation / FALSE else
*/
public static async RespondInvitation(groupID: number, userID: number, accept: boolean) {
if(!accept)
await this.DeleteMember(groupID, userID);
else
await this.UpdateMembershipLevel(groupID, userID, GroupMembershipLevels.MEMBER);
}
/**
* Insert a new group member
*
@ -189,6 +220,39 @@ export class GroupsHelper {
});
}
/**
* Update a user membership level
*
* @param groupID Target group ID
* @param userID Target user ID
* @param level New membership level
*/
public static async UpdateMembershipLevel(groupID: number, userID: number, level: GroupMembershipLevels) {
await DatabaseHelper.UpdateRows({
table: GROUPS_MEMBERS_TABLE,
where: {
user_id: userID,
groups_id: groupID
},
set: {
level: level
}
});
}
/**
* Delete completely a user membership
*
* @param groupID Target group ID
* @param userID Target user ID
*/
public static async DeleteMember(groupID: number, userID: number) {
await DatabaseHelper.DeleteRows(GROUPS_MEMBERS_TABLE, {
groups_id: groupID,
user_id: userID
});
}
/**
* Get the membership level of a user for a group