1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2025-06-20 16:45:16 +00:00

Refactor Notifications structure

This commit is contained in:
2020-03-28 14:18:37 +01:00
parent bef85d2d91
commit 0882ac76b3
6 changed files with 131 additions and 143 deletions

View File

@ -1,130 +0,0 @@
/**
* Notifications utilities
*
* @author Pierre Hubert
*/
import { NotifEventType, Notif, NotifElemType } from "../entities/Notification";
import { NotificationsHelper } from "../helpers/NotificationsHelper";
import { time } from "./DateUtils";
export class NotificationsUtils {
/**
* Create a notification about a post
*
* @param fromUser Source user ID
* @param postID Target post ID
* @param action Action to notify
*/
public static async CreatePostNotification(fromUser: number, postID: number, action: NotifEventType) {
await NotificationsHelper.Push(new Notif({
timeCreate: time(),
fromUserID: fromUser,
onElemID: postID,
onElemType: NotifElemType.POST,
type: action
}));
}
/**
* Create & push friendship request notification
*
* @param fromUser Source user ID
* @param destUser Destination user ID
* @param action The kind of action
*/
public static async CreateFriendsNotifications(fromUser: number, destUser: number, action: NotifEventType) {
await this.DeleteNotificationsFrienshipRequest(fromUser, destUser);
// Push the notification
await NotificationsHelper.Push(new Notif({
fromUserID: fromUser,
destUserID: destUser,
onElemID: fromUser, // Same as fromUser
onElemType: NotifElemType.FRIENDSHIP_REQUEST,
type: action
}));
}
/**
* Delete all the notifications related to a friendship request between two users
*
* @param userOne First user
* @param userTwo Second user
*/
public static async DeleteNotificationsFrienshipRequest(userOne: number, userTwo: number) {
// Delete notifications in two ways
await NotificationsHelper.Delete(new Notif({
onElemType: NotifElemType.FRIENDSHIP_REQUEST,
destUserID: userOne,
fromUserID: userTwo,
}));
await NotificationsHelper.Delete(new Notif({
onElemType: NotifElemType.FRIENDSHIP_REQUEST,
destUserID: userTwo,
fromUserID: userOne,
}));
}
/**
* Create & push a group membership notification
*
* @param userID The ID of the target user for the membership
* @param moderatorID The ID of the moderator creating the notification (0 if it is the user)
* @param groupID The ID of the target group
* @param kind The kind of notification to create
*/
public static async CreateGroupMembershipNotification(userID: number, moderatorID: number, groupID: number, kind: NotifEventType) {
await this.DeleteNotificationsGroupsMembership(userID, groupID);
const n = new Notif({
onElemID: groupID,
onElemType: NotifElemType.GROUP_MEMBERSHIP,
type: kind
});
// The notification must be sent to all the moderators of the group
if(moderatorID < 1) {
n.fromUserID = userID;
n.destUserID = -1;
}
// We specify both the source and the destination of the notification
// not to broadcast the notification to all the group members
else {
n.fromUserID = moderatorID;
n.destUserID = userID;
}
await NotificationsHelper.Push(n);
}
/**
* Delete all the notifications related to a specific group membership
*
* @param userID Target user
* @param groupID The ID of the target group
*/
public static async DeleteNotificationsGroupsMembership(userID: number, groupID: number) {
const n = new Notif({
onElemType: NotifElemType.GROUP_MEMBERSHIP,
onElemID: groupID
});
n.destUserID = userID;
n.fromUserID = -1;
await NotificationsHelper.Delete(n);
n.destUserID = -1;
n.fromUserID = userID;
await NotificationsHelper.Delete(n);
}
}