2019-12-27 17:56:22 +00:00
|
|
|
import { RequestHandler } from "../entities/RequestHandler";
|
|
|
|
import { NotificationsHelper } from "../helpers/NotificationsHelper";
|
2019-12-27 18:05:29 +00:00
|
|
|
import { ConversationsHelper } from "../helpers/ConversationsHelper";
|
|
|
|
import { FriendsHelper } from "../helpers/FriendsHelper";
|
2019-12-28 11:57:55 +00:00
|
|
|
import { Notif } from "../entities/Notification";
|
2019-12-27 17:56:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-12-27 18:05:29 +00:00
|
|
|
/**
|
|
|
|
* Count the entire number of unread notifications
|
|
|
|
*
|
|
|
|
* @param h Request handler
|
|
|
|
*/
|
|
|
|
public static async CountAllNews(h: RequestHandler) {
|
|
|
|
let data = {
|
|
|
|
notifications: await NotificationsHelper.CountUnread(h.getUserId()),
|
|
|
|
conversations: await ConversationsHelper.CountUnreadForUser(h.getUserId())
|
|
|
|
}
|
|
|
|
|
|
|
|
if(h.postBool("friends_request", false))
|
|
|
|
data["friends_requests"] = await FriendsHelper.CountRequests(h.getUserId());
|
|
|
|
|
|
|
|
// TODO : add pending calls
|
|
|
|
|
|
|
|
h.send(data);
|
|
|
|
}
|
2019-12-28 11:57:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
};
|
|
|
|
}
|
2019-12-27 17:56:22 +00:00
|
|
|
}
|