mirror of
https://gitlab.com/comunic/comunicapiv2
synced 2024-11-22 05:19:22 +00:00
Can mark related notifications as seen
This commit is contained in:
parent
890e49a4ae
commit
c67112d926
@ -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
|
||||
*
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
|
@ -218,7 +218,7 @@ export class NotificationsHelper {
|
||||
*
|
||||
* @param n The notification
|
||||
*/
|
||||
private static async SimilarExists(n: Notif) : Promise<boolean> {
|
||||
public static async SimilarExists(n: Notif) : Promise<boolean> {
|
||||
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<Notif> {
|
||||
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
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user