1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2024-11-22 13:29:22 +00:00

Can get the number of unread notifications

This commit is contained in:
Pierre HUBERT 2019-12-27 18:56:22 +01:00
parent 820b861ef8
commit 704e05bafe
3 changed files with 57 additions and 0 deletions

View File

@ -0,0 +1,23 @@
import { RequestHandler } from "../entities/RequestHandler";
import { NotificationsHelper } from "../helpers/NotificationsHelper";
/**
* Notifications controller
*
* @author Pierre HUBERT
*/
export class NotificationsController {
/**
* Get the number of unread notifications
*
* @param h Request handler
*/
public static async CountUnread(h: RequestHandler) {
h.send({
number: await NotificationsHelper.CountUnread(h.getUserId())
});
}
}

View File

@ -5,6 +5,7 @@ import { UserController } from "./UserController";
import { ConversationsController } from "./ConversationsController";
import { SearchController } from "./SearchController";
import { GroupsController } from "./GroupsController";
import { NotificationsController } from "./NotificationsController";
/**
* Controllers routes
@ -130,4 +131,9 @@ export const Routes : Route[] = [
{path: "/groups/set_following", cb: (h) => GroupsController.SetFollowing(h)},
{path: "/groups/delete", cb: (h) => GroupsController.DeleteGroup(h)},
// Notifications controller
{path: "/notifications/count_unread", cb: (h) => NotificationsController.CountUnread(h)},
]

View File

@ -0,0 +1,28 @@
import { DatabaseHelper } from "./DatabaseHelper";
/**
* Notifications helper
*
* @author Pierre HUBERT
*/
const NOTIFICATIONS_TABLE = "comunic_notifications";
export class NotificationsHelper {
/**
* Count the number of unread notifications of a user
*
* @param userID Target user ID
*/
public static async CountUnread(userID: number) : Promise<number> {
return await DatabaseHelper.Count({
table: NOTIFICATIONS_TABLE,
where: {
dest_user_id: userID,
seen: 0
}
});
}
}