From edc68a027562706a89ea4edba51d55c45a54c847 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Mon, 23 Mar 2020 19:08:04 +0100 Subject: [PATCH] Create notification when posting a new comment --- src/controllers/CommentsController.ts | 5 ++++- src/entities/Notification.ts | 16 +++++++------- src/helpers/NotificationsHelper.ts | 9 ++++++++ src/utils/NotificationsUtils.ts | 30 +++++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 9 deletions(-) create mode 100644 src/utils/NotificationsUtils.ts diff --git a/src/controllers/CommentsController.ts b/src/controllers/CommentsController.ts index 5f81064..5c795ff 100644 --- a/src/controllers/CommentsController.ts +++ b/src/controllers/CommentsController.ts @@ -4,6 +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"; /** * Comments controller @@ -43,7 +45,8 @@ export class CommentsController { const commentID = await CommentsHelper.Create(newComment); - // TODO : Create notifications + // Create notifications + await NotificationsUtils.CreatePostNotification(h.getUserId(), postID, NotifEventType.COMMENT_CREATED); // TODO : Delete any notifications targetting this user about the post diff --git a/src/entities/Notification.ts b/src/entities/Notification.ts index bdc6f05..012aebc 100644 --- a/src/entities/Notification.ts +++ b/src/entities/Notification.ts @@ -47,28 +47,28 @@ export enum NotifEventVisibility { } export interface NotifBuilder { - id: number, + id ?: number, timeCreate: number, - seen: boolean, + seen ?: boolean, fromUserID: number, - destUserID: number, + destUserID ?: number, onElemID: number, onElemType: NotifElemType, type: NotifEventType, - eventVisibility: NotifEventVisibility, + eventVisibility ?: NotifEventVisibility, fromContainerID ?: number, fromContainerType ?: NotifElemType } export class Notif implements NotifBuilder { - id: number; timeCreate: number; - seen: boolean; + id ?: number; timeCreate: number; + seen ?: boolean; fromUserID: number; - destUserID: number; + destUserID ?: number; onElemID: number; onElemType: NotifElemType; type: NotifEventType; - eventVisibility: NotifEventVisibility; + eventVisibility ?: NotifEventVisibility; fromContainerID ?: number; fromContainerType ?: NotifElemType; diff --git a/src/helpers/NotificationsHelper.ts b/src/helpers/NotificationsHelper.ts index e474e36..aba146f 100644 --- a/src/helpers/NotificationsHelper.ts +++ b/src/helpers/NotificationsHelper.ts @@ -11,6 +11,15 @@ const NOTIFICATIONS_TABLE = "comunic_notifications"; export class NotificationsHelper { + /** + * Push a new notification + * + * @param n Notification to push + */ + public static async Push(n: Notif) { + // TODO : push notification + } + /** * Count the number of unread notifications of a user * diff --git a/src/utils/NotificationsUtils.ts b/src/utils/NotificationsUtils.ts new file mode 100644 index 0000000..157668e --- /dev/null +++ b/src/utils/NotificationsUtils.ts @@ -0,0 +1,30 @@ +/** + * 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 + })); + } + +} \ No newline at end of file