From c67112d9268ecfd4cd9f3e71f86499470c7d4917 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Wed, 25 Mar 2020 08:23:09 +0100 Subject: [PATCH] Can mark related notifications as seen --- src/controllers/NotificationsController.ts | 53 ++++++++++++++++++++++ src/controllers/Routes.ts | 2 + src/entities/Notification.ts | 4 +- src/helpers/NotificationsHelper.ts | 21 ++++++++- 4 files changed, 77 insertions(+), 3 deletions(-) diff --git a/src/controllers/NotificationsController.ts b/src/controllers/NotificationsController.ts index a3f85ad..2153e85 100644 --- a/src/controllers/NotificationsController.ts +++ b/src/controllers/NotificationsController.ts @@ -53,6 +53,59 @@ export class NotificationsController { h.send(list.map(this.NotifToAPI)); } + /** + * Mark a notification as seen + * + * @param h Request handler + */ + public static async MarkSeen(h: RequestHandler) { + const notifID = await this.PostNotifyID(h, "notifID") + const deleteSimilar = h.postBool("delete_similar", false) + + // Check if we are targetting a precise notification or + // an unfinite number of similar notifications + if(!deleteSimilar) { + await NotificationsHelper.Delete(new Notif({ + id: notifID + })) + } + + else { + // Get information about the notification + const notif = await NotificationsHelper.GetSingle(notifID); + + await NotificationsHelper.Delete(new Notif({ + onElemType: notif.onElemType, + onElemID: notif.onElemID, + destUserID: h.getUserId() + })); + } + + + h.success() + } + + /** + * The the ID of a notification included in a request + * + * @param h Request handler + * @param name The name of the post field + */ + private static async PostNotifyID(h: RequestHandler, name: string) { + const notifID = h.postInt(name); + + // Check if the notification exists and targets the current user + const n = new Notif({ + id: notifID, + destUserID: h.getUserId() + }); + + if(!await NotificationsHelper.SimilarExists(n)) + h.error(404, "Specified notification not found!"); + + return notifID; + } + /** * Transform a notification into an API entry * diff --git a/src/controllers/Routes.ts b/src/controllers/Routes.ts index ab1ce7b..6c0a9d7 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -262,6 +262,8 @@ export const Routes : Route[] = [ {path: "/notifications/get_list_unread", cb: (h) => NotificationsController.GetListUnread(h)}, + {path: "/notifications/mark_seen", cb: (h) => NotificationsController.MarkSeen(h)}, + // Movies controller diff --git a/src/entities/Notification.ts b/src/entities/Notification.ts index 89f11f9..0bbcf02 100644 --- a/src/entities/Notification.ts +++ b/src/entities/Notification.ts @@ -54,7 +54,7 @@ export interface NotifBuilder { fromUserID ?: number, destUserID ?: number, onElemID ?: number, - onElemType: NotifElemType, + onElemType ?: NotifElemType, type ?: NotifEventType, eventVisibility ?: NotifEventVisibility, fromContainerID ?: number, @@ -68,7 +68,7 @@ export class Notif implements NotifBuilder { fromUserID ?: number; destUserID ?: number; onElemID ?: number; - onElemType: NotifElemType; + onElemType ?: NotifElemType; type ?: NotifEventType; eventVisibility ?: NotifEventVisibility; fromContainerID ?: number; diff --git a/src/helpers/NotificationsHelper.ts b/src/helpers/NotificationsHelper.ts index 3058d6b..86f59c3 100644 --- a/src/helpers/NotificationsHelper.ts +++ b/src/helpers/NotificationsHelper.ts @@ -218,7 +218,7 @@ export class NotificationsHelper { * * @param n The notification */ - private static async SimilarExists(n: Notif) : Promise { + public static async SimilarExists(n: Notif) : Promise { return await DatabaseHelper.Count({ table: NOTIFICATIONS_TABLE, where: this.NotifToDB(n, false) @@ -286,6 +286,25 @@ export class NotificationsHelper { return list.map((e) => this.DBToNotif(e)); } + /** + * Get information about a single notification + * + * @param notifID Target notification id + */ + public static async GetSingle(notifID: number) : Promise { + const row = await DatabaseHelper.QueryRow({ + table: NOTIFICATIONS_TABLE, + where: { + id: notifID + } + }); + + if(row == null) + throw new Error("Could not find notification !"); + + return this.DBToNotif(row); + } + /** * Turn a notification into a database entry *