mirror of
https://gitlab.com/comunic/comunicapiv2
synced 2024-11-22 21:39:22 +00:00
Can get the list of notifications of the user
This commit is contained in:
parent
b4c44fff1b
commit
e96cb38942
@ -2,6 +2,7 @@ import { RequestHandler } from "../entities/RequestHandler";
|
|||||||
import { NotificationsHelper } from "../helpers/NotificationsHelper";
|
import { NotificationsHelper } from "../helpers/NotificationsHelper";
|
||||||
import { ConversationsHelper } from "../helpers/ConversationsHelper";
|
import { ConversationsHelper } from "../helpers/ConversationsHelper";
|
||||||
import { FriendsHelper } from "../helpers/FriendsHelper";
|
import { FriendsHelper } from "../helpers/FriendsHelper";
|
||||||
|
import { Notif } from "../entities/Notification";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Notifications controller
|
* Notifications controller
|
||||||
@ -40,4 +41,36 @@ export class NotificationsController {
|
|||||||
|
|
||||||
h.send(data);
|
h.send(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
@ -138,4 +138,6 @@ export const Routes : Route[] = [
|
|||||||
{path: "/notifications/count_unread", cb: (h) => NotificationsController.CountUnread(h)},
|
{path: "/notifications/count_unread", cb: (h) => NotificationsController.CountUnread(h)},
|
||||||
|
|
||||||
{path: "/notifications/count_all_news", cb: (h) => NotificationsController.CountAllNews(h)},
|
{path: "/notifications/count_all_news", cb: (h) => NotificationsController.CountAllNews(h)},
|
||||||
|
|
||||||
|
{path: "/notifications/get_list_unread", cb: (h) => NotificationsController.GetListUnread(h)},
|
||||||
]
|
]
|
82
src/entities/Notification.ts
Normal file
82
src/entities/Notification.ts
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
/**
|
||||||
|
* Notification object
|
||||||
|
*
|
||||||
|
* To avoid conflicts with the native Notification object,
|
||||||
|
* I renamed it Notif
|
||||||
|
*
|
||||||
|
* @author Pierre HUBERT
|
||||||
|
*/
|
||||||
|
|
||||||
|
export enum NotifElemType {
|
||||||
|
USER_PAGE = "user_page",
|
||||||
|
GROUP_PAGE = "group_page",
|
||||||
|
CONVERSATION = "conversation",
|
||||||
|
CONVERSATION_MESSAGE = "conversation_message",
|
||||||
|
POST = "post",
|
||||||
|
POST_TEXT = "post_text",
|
||||||
|
POST_IMAGE = "post_img",
|
||||||
|
POST_YOUTUBE = "post_youtube",
|
||||||
|
POST_MOVIE = "post_movie",
|
||||||
|
POST_WEBLINK = "post_weblink",
|
||||||
|
POST_PDF = "post_pdf",
|
||||||
|
POST_TIMER = "post_timer",
|
||||||
|
POST_SURVEY = "post_survey",
|
||||||
|
COMMENT = "comment",
|
||||||
|
FRIENDSHIP_REQUEST = "friend_request",
|
||||||
|
GROUP_MEMBERSHIP = "group_membership",
|
||||||
|
}
|
||||||
|
|
||||||
|
export enum NotifEventType {
|
||||||
|
COMMENT_CREATED = "comment_created",
|
||||||
|
SENT_FRIEND_REQUEST = "sent_friend_request",
|
||||||
|
ACCEPTED_FRIEND_REQUEST = "accepted_friend_request",
|
||||||
|
REJECTED_FRIEND_REQUEST = "rejected_friend_request",
|
||||||
|
ELEM_CREATED = "elem_created",
|
||||||
|
ELEM_UPDATED = "elem_updated",
|
||||||
|
SENT_GROUP_MEMBERSHIP_INVITATION = "sent_group_membership_invitation",
|
||||||
|
ACCEPTED_GROUP_MEMBERSHIP_INVITATION = "accepted_group_membership_invitation",
|
||||||
|
REJECTED_GROUP_MEMBERSHIP_INVITATION = "rejected_group_membership_invitation",
|
||||||
|
SENT_GROUP_MEMBERSHIP_REQUEST = "sent_group_membership_request",
|
||||||
|
ACCEPTED_GROUP_MEMBERSHIP_REQUEST = "accepted_group_membership_request",
|
||||||
|
REJECTED_GROUP_MEMBERSHIP_REQUEST = "rejected_group_membership_request",
|
||||||
|
}
|
||||||
|
|
||||||
|
export enum NotifEventVisibility {
|
||||||
|
EVENT_PRIVATE = "event_private",
|
||||||
|
EVENT_PUBLIC = "event_public",
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface NotifBuilder {
|
||||||
|
id: number,
|
||||||
|
timeCreate: number,
|
||||||
|
seen: boolean,
|
||||||
|
fromUserID: number,
|
||||||
|
destUserID: number,
|
||||||
|
onElemID: number,
|
||||||
|
onElemType: NotifElemType,
|
||||||
|
type: NotifEventType,
|
||||||
|
eventVisibility: NotifEventVisibility,
|
||||||
|
fromContainerID ?: number,
|
||||||
|
fromContainerType ?: NotifElemType
|
||||||
|
}
|
||||||
|
|
||||||
|
export class Notif implements NotifBuilder {
|
||||||
|
id: number; timeCreate: number;
|
||||||
|
seen: boolean;
|
||||||
|
fromUserID: number;
|
||||||
|
destUserID: number;
|
||||||
|
onElemID: number;
|
||||||
|
onElemType: NotifElemType;
|
||||||
|
type: NotifEventType;
|
||||||
|
eventVisibility: NotifEventVisibility;
|
||||||
|
fromContainerID ?: number;
|
||||||
|
fromContainerType ?: NotifElemType;
|
||||||
|
|
||||||
|
constructor(info: NotifBuilder) {
|
||||||
|
for (const key in info) {
|
||||||
|
if (info.hasOwnProperty(key)) {
|
||||||
|
this[key] = info[key];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,5 @@
|
|||||||
import { DatabaseHelper } from "./DatabaseHelper";
|
import { DatabaseHelper } from "./DatabaseHelper";
|
||||||
|
import { Notif } from "../entities/Notification";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Notifications helper
|
* Notifications helper
|
||||||
@ -25,4 +26,42 @@ export class NotificationsHelper {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the list of unread notifications of a user
|
||||||
|
*
|
||||||
|
* @param userID Target user ID
|
||||||
|
*/
|
||||||
|
public static async GetListUnread(userID: number) : Promise<Array<Notif>> {
|
||||||
|
const list = await DatabaseHelper.Query({
|
||||||
|
table: NOTIFICATIONS_TABLE,
|
||||||
|
where: {
|
||||||
|
dest_user_id: userID,
|
||||||
|
seen: 0
|
||||||
|
},
|
||||||
|
order: "id DESC"
|
||||||
|
});
|
||||||
|
|
||||||
|
return list.map((e) => this.DBToNotif(e));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Database entry to notification
|
||||||
|
*
|
||||||
|
* @param row Database entry
|
||||||
|
*/
|
||||||
|
private static DBToNotif(row: any) : Notif {
|
||||||
|
return new Notif({
|
||||||
|
id: row.id,
|
||||||
|
seen: row.seen == 1,
|
||||||
|
timeCreate: row.time_create,
|
||||||
|
fromUserID: row.from_user_id,
|
||||||
|
destUserID: row.dest_user_id,
|
||||||
|
onElemID: row.on_elem_id,
|
||||||
|
onElemType: row.on_elem_type,
|
||||||
|
type: row.type,
|
||||||
|
eventVisibility: row.visibility,
|
||||||
|
fromContainerID: row.from_container_id,
|
||||||
|
fromContainerType: row.from_container_type
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user