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:
parent
820b861ef8
commit
704e05bafe
23
src/controllers/NotificationsController.ts
Normal file
23
src/controllers/NotificationsController.ts
Normal 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())
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -5,6 +5,7 @@ import { UserController } from "./UserController";
|
|||||||
import { ConversationsController } from "./ConversationsController";
|
import { ConversationsController } from "./ConversationsController";
|
||||||
import { SearchController } from "./SearchController";
|
import { SearchController } from "./SearchController";
|
||||||
import { GroupsController } from "./GroupsController";
|
import { GroupsController } from "./GroupsController";
|
||||||
|
import { NotificationsController } from "./NotificationsController";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Controllers routes
|
* Controllers routes
|
||||||
@ -130,4 +131,9 @@ export const Routes : Route[] = [
|
|||||||
{path: "/groups/set_following", cb: (h) => GroupsController.SetFollowing(h)},
|
{path: "/groups/set_following", cb: (h) => GroupsController.SetFollowing(h)},
|
||||||
|
|
||||||
{path: "/groups/delete", cb: (h) => GroupsController.DeleteGroup(h)},
|
{path: "/groups/delete", cb: (h) => GroupsController.DeleteGroup(h)},
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Notifications controller
|
||||||
|
{path: "/notifications/count_unread", cb: (h) => NotificationsController.CountUnread(h)},
|
||||||
]
|
]
|
28
src/helpers/NotificationsHelper.ts
Normal file
28
src/helpers/NotificationsHelper.ts
Normal 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
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user