From e96cb389428e92c78ac4d645feb38dab5b331c62 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 28 Dec 2019 12:57:55 +0100 Subject: [PATCH] Can get the list of notifications of the user --- src/controllers/NotificationsController.ts | 33 +++++++++ src/controllers/Routes.ts | 2 + src/entities/Notification.ts | 82 ++++++++++++++++++++++ src/helpers/NotificationsHelper.ts | 39 ++++++++++ 4 files changed, 156 insertions(+) create mode 100644 src/entities/Notification.ts diff --git a/src/controllers/NotificationsController.ts b/src/controllers/NotificationsController.ts index b0d5b36..a3f85ad 100644 --- a/src/controllers/NotificationsController.ts +++ b/src/controllers/NotificationsController.ts @@ -2,6 +2,7 @@ import { RequestHandler } from "../entities/RequestHandler"; import { NotificationsHelper } from "../helpers/NotificationsHelper"; import { ConversationsHelper } from "../helpers/ConversationsHelper"; import { FriendsHelper } from "../helpers/FriendsHelper"; +import { Notif } from "../entities/Notification"; /** * Notifications controller @@ -40,4 +41,36 @@ export class NotificationsController { h.send(data); } + + /** + * Get the list of unread notifications + * + * @param h Request handler + */ + public static async GetListUnread(h: RequestHandler) { + const list = await NotificationsHelper.GetListUnread(h.getUserId()); + + h.send(list.map(this.NotifToAPI)); + } + + /** + * Transform a notification into an API entry + * + * @param n The notification to transform + */ + private static NotifToAPI(n: Notif) : Object { + return { + id: n.id, + time_create: n.timeCreate, + seen: n.seen, + from_user_id: n.fromUserID, + dest_user_id: n.destUserID, + on_elem_id: n.onElemID, + on_elem_type: n.onElemType, + type: n.type, + event_visibility: n.eventVisibility, + from_container_id: n.fromContainerID, + from_container_type: n.fromContainerType + }; + } } \ No newline at end of file diff --git a/src/controllers/Routes.ts b/src/controllers/Routes.ts index c4bc353..594da4e 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -138,4 +138,6 @@ export const Routes : Route[] = [ {path: "/notifications/count_unread", cb: (h) => NotificationsController.CountUnread(h)}, {path: "/notifications/count_all_news", cb: (h) => NotificationsController.CountAllNews(h)}, + + {path: "/notifications/get_list_unread", cb: (h) => NotificationsController.GetListUnread(h)}, ] \ No newline at end of file diff --git a/src/entities/Notification.ts b/src/entities/Notification.ts new file mode 100644 index 0000000..bdc6f05 --- /dev/null +++ b/src/entities/Notification.ts @@ -0,0 +1,82 @@ +/** + * Notification object + * + * To avoid conflicts with the native Notification object, + * I renamed it Notif + * + * @author Pierre HUBERT + */ + +export enum NotifElemType { + USER_PAGE = "user_page", + GROUP_PAGE = "group_page", + CONVERSATION = "conversation", + CONVERSATION_MESSAGE = "conversation_message", + POST = "post", + POST_TEXT = "post_text", + POST_IMAGE = "post_img", + POST_YOUTUBE = "post_youtube", + POST_MOVIE = "post_movie", + POST_WEBLINK = "post_weblink", + POST_PDF = "post_pdf", + POST_TIMER = "post_timer", + POST_SURVEY = "post_survey", + COMMENT = "comment", + FRIENDSHIP_REQUEST = "friend_request", + GROUP_MEMBERSHIP = "group_membership", +} + +export enum NotifEventType { + COMMENT_CREATED = "comment_created", + SENT_FRIEND_REQUEST = "sent_friend_request", + ACCEPTED_FRIEND_REQUEST = "accepted_friend_request", + REJECTED_FRIEND_REQUEST = "rejected_friend_request", + ELEM_CREATED = "elem_created", + ELEM_UPDATED = "elem_updated", + SENT_GROUP_MEMBERSHIP_INVITATION = "sent_group_membership_invitation", + ACCEPTED_GROUP_MEMBERSHIP_INVITATION = "accepted_group_membership_invitation", + REJECTED_GROUP_MEMBERSHIP_INVITATION = "rejected_group_membership_invitation", + SENT_GROUP_MEMBERSHIP_REQUEST = "sent_group_membership_request", + ACCEPTED_GROUP_MEMBERSHIP_REQUEST = "accepted_group_membership_request", + REJECTED_GROUP_MEMBERSHIP_REQUEST = "rejected_group_membership_request", +} + +export enum NotifEventVisibility { + EVENT_PRIVATE = "event_private", + EVENT_PUBLIC = "event_public", +} + +export interface NotifBuilder { + id: number, + timeCreate: number, + seen: boolean, + fromUserID: number, + destUserID: number, + onElemID: number, + onElemType: NotifElemType, + type: NotifEventType, + eventVisibility: NotifEventVisibility, + fromContainerID ?: number, + fromContainerType ?: NotifElemType +} + +export class Notif implements NotifBuilder { + id: number; timeCreate: number; + seen: boolean; + fromUserID: number; + destUserID: number; + onElemID: number; + onElemType: NotifElemType; + type: NotifEventType; + eventVisibility: NotifEventVisibility; + fromContainerID ?: number; + fromContainerType ?: NotifElemType; + + constructor(info: NotifBuilder) { + for (const key in info) { + if (info.hasOwnProperty(key)) { + this[key] = info[key]; + } + } + } +} \ No newline at end of file diff --git a/src/helpers/NotificationsHelper.ts b/src/helpers/NotificationsHelper.ts index 35a8e97..e474e36 100644 --- a/src/helpers/NotificationsHelper.ts +++ b/src/helpers/NotificationsHelper.ts @@ -1,4 +1,5 @@ import { DatabaseHelper } from "./DatabaseHelper"; +import { Notif } from "../entities/Notification"; /** * Notifications helper @@ -25,4 +26,42 @@ export class NotificationsHelper { }); } + /** + * Get the list of unread notifications of a user + * + * @param userID Target user ID + */ + public static async GetListUnread(userID: number) : Promise> { + const list = await DatabaseHelper.Query({ + table: NOTIFICATIONS_TABLE, + where: { + dest_user_id: userID, + seen: 0 + }, + order: "id DESC" + }); + + return list.map((e) => this.DBToNotif(e)); + } + + /** + * Database entry to notification + * + * @param row Database entry + */ + private static DBToNotif(row: any) : Notif { + return new Notif({ + id: row.id, + seen: row.seen == 1, + timeCreate: row.time_create, + fromUserID: row.from_user_id, + destUserID: row.dest_user_id, + onElemID: row.on_elem_id, + onElemType: row.on_elem_type, + type: row.type, + eventVisibility: row.visibility, + fromContainerID: row.from_container_id, + fromContainerType: row.from_container_type + }); + } } \ No newline at end of file