diff --git a/src/controllers/GroupsController.ts b/src/controllers/GroupsController.ts index 1b7e545..7d92280 100644 --- a/src/controllers/GroupsController.ts +++ b/src/controllers/GroupsController.ts @@ -8,6 +8,8 @@ import { GroupSettings } from "../entities/GroupSettings"; import { removeHTMLNodes, checkURL } from "../utils/StringUtils"; import { findKey } from "../utils/ArrayUtils"; import { checkVirtualDirectoryAvailability, VirtualDirType } from "../utils/VirtualDirsUtils"; +import { NotificationsUtils } from "../utils/NotificationsUtils"; +import { NotifEventType } from "../entities/Notification"; /** * Groups API controller @@ -303,7 +305,9 @@ export class GroupsController { await GroupsHelper.SendInvitation(groupID, userID); - // TODO : Create a notification + // Create a notification + await NotificationsUtils.CreateGroupMembershipNotification( + userID, h.getUserId(), groupID, NotifEventType.SENT_GROUP_MEMBERSHIP_INVITATION) h.success("The user has been successfully invited to join the group!"); } diff --git a/src/entities/Notification.ts b/src/entities/Notification.ts index 5e32d71..89f11f9 100644 --- a/src/entities/Notification.ts +++ b/src/entities/Notification.ts @@ -51,7 +51,7 @@ export interface NotifBuilder { id ?: number, timeCreate ?: number, seen ?: boolean, - fromUserID: number, + fromUserID ?: number, destUserID ?: number, onElemID ?: number, onElemType: NotifElemType, @@ -65,7 +65,7 @@ export class Notif implements NotifBuilder { id ?: number; timeCreate ?: number; seen ?: boolean; - fromUserID: number; + fromUserID ?: number; destUserID ?: number; onElemID ?: number; onElemType: NotifElemType; diff --git a/src/helpers/NotificationsHelper.ts b/src/helpers/NotificationsHelper.ts index 97bca7a..de7313d 100644 --- a/src/helpers/NotificationsHelper.ts +++ b/src/helpers/NotificationsHelper.ts @@ -113,7 +113,30 @@ export class NotificationsHelper { } - // TODO : continue + // Groups membership notifications + else if (n.onElemType == NotifElemType.GROUP_MEMBERSHIP) { + + // Complete the notification + n.fromContainerType = NotifElemType.EMPTY; + n.fromContainerID = 0; + + // Check whether the notification has to be pushed to a single user + // or to all the moderators of the group + if(n.hasDestUserID) { + + //Push the notification in private way (if it has a destination, + //generally the target user of the membership request) + await this.PushPrivate(n); + + } + + else { + + // Push the notification to all the moderators of the group + throw new Error("//TODO Push notification to all members of the group"); + + } + } // Unsupported notification type else { diff --git a/src/utils/NotificationsUtils.ts b/src/utils/NotificationsUtils.ts index 9a4fad6..6bc0feb 100644 --- a/src/utils/NotificationsUtils.ts +++ b/src/utils/NotificationsUtils.ts @@ -70,4 +70,38 @@ export class NotificationsUtils { fromUserID: userOne, })); } + + /** + * Create & push a group membership notification + * + * @param userID The ID of the target user for the membership + * @param moderatorID The ID of the moderator creating the notification (0 if it is the user) + * @param groupID The ID of the target group + * @param kind The kind of notification to create + */ + public static async CreateGroupMembershipNotification(userID: number, moderatorID: number, groupID: number, kind: NotifEventType) { + // TODO : Delete all previous notifications + + const n = new Notif({ + onElemID: groupID, + onElemType: NotifElemType.GROUP_MEMBERSHIP, + type: kind + }); + + + // The notification must be sent to all the moderators of the group + if(moderatorID < 1) { + n.fromUserID = userID; + n.destUserID = -1; + } + + // We specify both the source and the destination of the notification + // not to broadcast the notification to all the group members + else { + n.fromUserID = moderatorID; + n.destUserID = userID; + } + + await NotificationsHelper.Push(n); + } } \ No newline at end of file