diff --git a/src/controllers/FriendsController.ts b/src/controllers/FriendsController.ts index aeaf33d..1042ad7 100644 --- a/src/controllers/FriendsController.ts +++ b/src/controllers/FriendsController.ts @@ -2,6 +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"; /** * Friends controller @@ -110,7 +112,9 @@ export class FriendsController { // Send the request await FriendsHelper.SendRequest(h.getUserId(), friendID); - // TODO : create the notification + // Create the notification + await NotificationsUtils.CreateFriendsNotifications( + h.getUserId(), friendID, NotifEventType.SENT_FRIEND_REQUEST) h.success("Send (create) the friendship request"); } diff --git a/src/entities/Notification.ts b/src/entities/Notification.ts index 636034e..eb112ff 100644 --- a/src/entities/Notification.ts +++ b/src/entities/Notification.ts @@ -8,6 +8,7 @@ */ export enum NotifElemType { + EMPTY = "", USER_PAGE = "user_page", GROUP_PAGE = "group_page", CONVERSATION = "conversation", diff --git a/src/helpers/NotificationsHelper.ts b/src/helpers/NotificationsHelper.ts index fbe2230..90436ea 100644 --- a/src/helpers/NotificationsHelper.ts +++ b/src/helpers/NotificationsHelper.ts @@ -105,7 +105,21 @@ export class NotificationsHelper { } } + // Frienship notifications + else if(n.onElemType == NotifElemType.FRIENDSHIP_REQUEST) { + n.fromContainerID = 0 + n.fromContainerType = NotifElemType.EMPTY; + await this.PushPrivate(n); + } + + // TODO : continue + + // Unsupported notification type + else { + console.error(n) + throw new Error("Type of notification not supported!") + } } /** diff --git a/src/utils/NotificationsUtils.ts b/src/utils/NotificationsUtils.ts index 157668e..1ca8cff 100644 --- a/src/utils/NotificationsUtils.ts +++ b/src/utils/NotificationsUtils.ts @@ -27,4 +27,25 @@ export class NotificationsUtils { })); } + + /** + * Create & push friendship request notification + * + * @param fromUser Source user ID + * @param toUser Destination user ID + * @param action The kind of action + */ + public static async CreateFriendsNotifications(fromUser: number, toUser: number, action: NotifEventType) { + // TODO : Delete all the previous notifications + + // PUsh the notification + await NotificationsHelper.Push(new Notif({ + fromUserID: fromUser, + destUserID: toUser, + onElemID: fromUser, // Same as fromUser + onElemType: NotifElemType.FRIENDSHIP_REQUEST, + type: action + })); + } + } \ No newline at end of file