diff --git a/src/controllers/CommentsController.ts b/src/controllers/CommentsController.ts index 5c795ff..a5732be 100644 --- a/src/controllers/CommentsController.ts +++ b/src/controllers/CommentsController.ts @@ -4,8 +4,8 @@ import { LikesHelper, LikesType } from "../helpers/LikesHelper"; import { check_string_before_insert, removeHTMLNodes } from "../utils/StringUtils"; import { time } from "../utils/DateUtils"; import { CommentsHelper } from "../helpers/CommentsHelper"; -import { NotificationsUtils } from "../utils/NotificationsUtils"; import { NotifEventType } from "../entities/Notification"; +import { NotificationsHelper } from "../helpers/NotificationsHelper"; /** * Comments controller @@ -46,7 +46,7 @@ export class CommentsController { const commentID = await CommentsHelper.Create(newComment); // Create notifications - await NotificationsUtils.CreatePostNotification(h.getUserId(), postID, NotifEventType.COMMENT_CREATED); + await NotificationsHelper.CreatePostNotification(h.getUserId(), postID, NotifEventType.COMMENT_CREATED); // TODO : Delete any notifications targetting this user about the post diff --git a/src/controllers/FriendsController.ts b/src/controllers/FriendsController.ts index 5392fb8..3c385e0 100644 --- a/src/controllers/FriendsController.ts +++ b/src/controllers/FriendsController.ts @@ -2,8 +2,8 @@ import { Friend } from "../entities/Friend"; import { RequestHandler } from "../entities/RequestHandler"; import { FriendsHelper } from "../helpers/FriendsHelper"; import { UserHelper } from "../helpers/UserHelper"; -import { NotificationsUtils } from "../utils/NotificationsUtils"; import { NotifEventType } from "../entities/Notification"; +import { NotificationsHelper } from "../helpers/NotificationsHelper"; /** * Friends controller @@ -113,7 +113,7 @@ export class FriendsController { await FriendsHelper.SendRequest(h.getUserId(), friendID); // Create the notification - await NotificationsUtils.CreateFriendsNotifications( + await NotificationsHelper.CreateFriendsNotifications( h.getUserId(), friendID, NotifEventType.SENT_FRIEND_REQUEST) h.success("Send (create) the friendship request"); @@ -133,7 +133,7 @@ export class FriendsController { await FriendsHelper.RemoveRequest(h.getUserId(), friendID); // Delete any related notification - await NotificationsUtils.DeleteNotificationsFrienshipRequest(h.getUserId(), friendID); + await NotificationsHelper.DeleteNotificationsFrienshipRequest(h.getUserId(), friendID); h.success("Friendship request removed!"); } @@ -153,7 +153,7 @@ export class FriendsController { await FriendsHelper.RespondRequest(h.getUserId(), friendID, accept); // Create notification - await NotificationsUtils.CreateFriendsNotifications(h.getUserId(), friendID, + await NotificationsHelper.CreateFriendsNotifications(h.getUserId(), friendID, accept ? NotifEventType.ACCEPTED_FRIEND_REQUEST : NotifEventType.REJECTED_FRIEND_REQUEST); h.success("Response to the friendship request successfully saved!"); @@ -170,7 +170,7 @@ export class FriendsController { await FriendsHelper.RemoveFriendship(h.getUserId(), friendID); // Delete any related notification - await NotificationsUtils.DeleteNotificationsFrienshipRequest( + await NotificationsHelper.DeleteNotificationsFrienshipRequest( h.getUserId(), friendID); h.success("The friend was removed from the list!"); diff --git a/src/controllers/GroupsController.ts b/src/controllers/GroupsController.ts index 36446f9..0d4704d 100644 --- a/src/controllers/GroupsController.ts +++ b/src/controllers/GroupsController.ts @@ -8,8 +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"; +import { NotificationsHelper } from "../helpers/NotificationsHelper"; /** * Groups API controller @@ -306,7 +306,7 @@ export class GroupsController { await GroupsHelper.SendInvitation(groupID, userID); // Create a notification - await NotificationsUtils.CreateGroupMembershipNotification( + await NotificationsHelper.CreateGroupMembershipNotification( userID, h.getUserId(), groupID, NotifEventType.SENT_GROUP_MEMBERSHIP_INVITATION) h.success("The user has been successfully invited to join the group!"); @@ -329,7 +329,7 @@ export class GroupsController { await GroupsHelper.RespondInvitation(groupID, h.getUserId(), accept); // Create a notification - await NotificationsUtils.CreateGroupMembershipNotification( + await NotificationsHelper.CreateGroupMembershipNotification( h.getUserId(), 0, groupID, accept ? NotifEventType.ACCEPTED_GROUP_MEMBERSHIP_INVITATION : NotifEventType.REJECTED_GROUP_MEMBERSHIP_INVITATION ) diff --git a/src/controllers/PostsController.ts b/src/controllers/PostsController.ts index 11372f3..f0f5370 100644 --- a/src/controllers/PostsController.ts +++ b/src/controllers/PostsController.ts @@ -19,8 +19,8 @@ import { statSync } from "fs"; import { lookup } from "mime-types"; import { NewSurvey } from "../entities/NewSurvey"; import { FriendsHelper } from "../helpers/FriendsHelper"; -import { NotificationsUtils } from "../utils/NotificationsUtils"; import { NotifEventType } from "../entities/Notification"; +import { NotificationsHelper } from "../helpers/NotificationsHelper"; /** * Posts controller @@ -294,7 +294,7 @@ export class PostsController { // Create a notification - await NotificationsUtils.CreatePostNotification( + await NotificationsHelper.CreatePostNotification( h.getUserId(), postID, NotifEventType.ELEM_CREATED); h.send({ diff --git a/src/helpers/NotificationsHelper.ts b/src/helpers/NotificationsHelper.ts index 768d4d4..c72c6f2 100644 --- a/src/helpers/NotificationsHelper.ts +++ b/src/helpers/NotificationsHelper.ts @@ -1,5 +1,5 @@ import { DatabaseHelper } from "./DatabaseHelper"; -import { Notif, NotifElemType, NotifEventVisibility } from "../entities/Notification"; +import { Notif, NotifElemType, NotifEventVisibility, NotifEventType } from "../entities/Notification"; import { time } from "../utils/DateUtils"; import { PostsHelper } from "./PostsHelper"; import { PostPageKind, PostVisibilityLevel } from "../entities/Post"; @@ -17,6 +17,79 @@ const NOTIFICATIONS_TABLE = "comunic_notifications"; export class NotificationsHelper { + /** + * Create a notification about a post + * + * @param fromUser Source user ID + * @param postID Target post ID + * @param action Action to notify + */ + public static async CreatePostNotification(fromUser: number, postID: number, action: NotifEventType) { + await this.Push(new Notif({ + timeCreate: time(), + fromUserID: fromUser, + onElemID: postID, + onElemType: NotifElemType.POST, + type: action + })); + } + + + /** + * Create & push friendship request notification + * + * @param fromUser Source user ID + * @param destUser Destination user ID + * @param action The kind of action + */ + public static async CreateFriendsNotifications(fromUser: number, destUser: number, action: NotifEventType) { + await this.DeleteNotificationsFrienshipRequest(fromUser, destUser); + + // Push the notification + await this.Push(new Notif({ + fromUserID: fromUser, + destUserID: destUser, + onElemID: fromUser, // Same as fromUser + onElemType: NotifElemType.FRIENDSHIP_REQUEST, + type: action + })); + } + + + /** + * 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) { + await this.DeleteNotificationsGroupsMembership(userID, groupID); + + 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 this.Push(n); + } + /** * Push a new notification * @@ -309,6 +382,51 @@ export class NotificationsHelper { })); } + /** + * Delete all the notifications related to a friendship request between two users + * + * @param userOne First user + * @param userTwo Second user + */ + public static async DeleteNotificationsFrienshipRequest(userOne: number, userTwo: number) { + + // Delete notifications in two ways + await this.Delete(new Notif({ + onElemType: NotifElemType.FRIENDSHIP_REQUEST, + destUserID: userOne, + fromUserID: userTwo, + })); + + await this.Delete(new Notif({ + onElemType: NotifElemType.FRIENDSHIP_REQUEST, + destUserID: userTwo, + fromUserID: userOne, + })); + } + + /** + * Delete all the notifications related to a specific group membership + * + * @param userID Target user + * @param groupID The ID of the target group + */ + public static async DeleteNotificationsGroupsMembership(userID: number, groupID: number) { + + const n = new Notif({ + onElemType: NotifElemType.GROUP_MEMBERSHIP, + onElemID: groupID + }); + + n.destUserID = userID; + n.fromUserID = -1; + await this.Delete(n); + + n.destUserID = -1; + n.fromUserID = userID; + await this.Delete(n); + + } + /** * Count the number of unread notifications of a user * diff --git a/src/utils/NotificationsUtils.ts b/src/utils/NotificationsUtils.ts deleted file mode 100644 index 5810c2c..0000000 --- a/src/utils/NotificationsUtils.ts +++ /dev/null @@ -1,130 +0,0 @@ -/** - * Notifications utilities - * - * @author Pierre Hubert - */ - -import { NotifEventType, Notif, NotifElemType } from "../entities/Notification"; -import { NotificationsHelper } from "../helpers/NotificationsHelper"; -import { time } from "./DateUtils"; - -export class NotificationsUtils { - - /** - * Create a notification about a post - * - * @param fromUser Source user ID - * @param postID Target post ID - * @param action Action to notify - */ - public static async CreatePostNotification(fromUser: number, postID: number, action: NotifEventType) { - await NotificationsHelper.Push(new Notif({ - timeCreate: time(), - fromUserID: fromUser, - onElemID: postID, - onElemType: NotifElemType.POST, - type: action - })); - } - - - /** - * Create & push friendship request notification - * - * @param fromUser Source user ID - * @param destUser Destination user ID - * @param action The kind of action - */ - public static async CreateFriendsNotifications(fromUser: number, destUser: number, action: NotifEventType) { - await this.DeleteNotificationsFrienshipRequest(fromUser, destUser); - - // Push the notification - await NotificationsHelper.Push(new Notif({ - fromUserID: fromUser, - destUserID: destUser, - onElemID: fromUser, // Same as fromUser - onElemType: NotifElemType.FRIENDSHIP_REQUEST, - type: action - })); - } - - - /** - * Delete all the notifications related to a friendship request between two users - * - * @param userOne First user - * @param userTwo Second user - */ - public static async DeleteNotificationsFrienshipRequest(userOne: number, userTwo: number) { - - // Delete notifications in two ways - await NotificationsHelper.Delete(new Notif({ - onElemType: NotifElemType.FRIENDSHIP_REQUEST, - destUserID: userOne, - fromUserID: userTwo, - })); - - await NotificationsHelper.Delete(new Notif({ - onElemType: NotifElemType.FRIENDSHIP_REQUEST, - destUserID: userTwo, - 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) { - await this.DeleteNotificationsGroupsMembership(userID, groupID); - - 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); - } - - /** - * Delete all the notifications related to a specific group membership - * - * @param userID Target user - * @param groupID The ID of the target group - */ - public static async DeleteNotificationsGroupsMembership(userID: number, groupID: number) { - - const n = new Notif({ - onElemType: NotifElemType.GROUP_MEMBERSHIP, - onElemID: groupID - }); - - n.destUserID = userID; - n.fromUserID = -1; - await NotificationsHelper.Delete(n); - - n.destUserID = -1; - n.fromUserID = userID; - await NotificationsHelper.Delete(n); - - } -} \ No newline at end of file