mirror of
				https://gitlab.com/comunic/comunicapiv2
				synced 2025-11-04 03:24:04 +00:00 
			
		
		
		
	Refactor Notifications structure
This commit is contained in:
		@@ -1,5 +1,5 @@
 | 
			
		||||
import { DatabaseHelper } from "./DatabaseHelper";
 | 
			
		||||
import { Notif, NotifElemType, NotifEventVisibility } from "../entities/Notification";
 | 
			
		||||
import { Notif, NotifElemType, NotifEventVisibility, NotifEventType } from "../entities/Notification";
 | 
			
		||||
import { time } from "../utils/DateUtils";
 | 
			
		||||
import { PostsHelper } from "./PostsHelper";
 | 
			
		||||
import { PostPageKind, PostVisibilityLevel } from "../entities/Post";
 | 
			
		||||
@@ -17,6 +17,79 @@ const NOTIFICATIONS_TABLE = "comunic_notifications";
 | 
			
		||||
 | 
			
		||||
export class NotificationsHelper {
 | 
			
		||||
	
 | 
			
		||||
	/**
 | 
			
		||||
	 * 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 this.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 this.Push(new Notif({
 | 
			
		||||
			fromUserID: fromUser,
 | 
			
		||||
			destUserID: destUser,
 | 
			
		||||
			onElemID: fromUser, // Same as fromUser
 | 
			
		||||
			onElemType: NotifElemType.FRIENDSHIP_REQUEST,
 | 
			
		||||
			type: action
 | 
			
		||||
		}));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * 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 this.Push(n);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Push a new notification
 | 
			
		||||
	 * 
 | 
			
		||||
@@ -309,6 +382,51 @@ export class NotificationsHelper {
 | 
			
		||||
		}));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * 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 this.Delete(new Notif({
 | 
			
		||||
			onElemType: NotifElemType.FRIENDSHIP_REQUEST,
 | 
			
		||||
			destUserID: userOne,
 | 
			
		||||
			fromUserID: userTwo,
 | 
			
		||||
		}));
 | 
			
		||||
 | 
			
		||||
		await this.Delete(new Notif({
 | 
			
		||||
			onElemType: NotifElemType.FRIENDSHIP_REQUEST,
 | 
			
		||||
			destUserID: userTwo,
 | 
			
		||||
			fromUserID: userOne,
 | 
			
		||||
		}));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * 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 this.Delete(n);
 | 
			
		||||
 | 
			
		||||
		n.destUserID = -1;
 | 
			
		||||
		n.fromUserID = userID;
 | 
			
		||||
		await this.Delete(n);
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Count the number of unread notifications of a user
 | 
			
		||||
	 * 
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user