1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2024-11-25 23:09:22 +00:00

Ready to create the notification

This commit is contained in:
Pierre HUBERT 2020-03-24 08:07:50 +01:00
parent edc68a0275
commit e6c610a3c0
2 changed files with 143 additions and 4 deletions

View File

@ -48,7 +48,7 @@ export enum NotifEventVisibility {
export interface NotifBuilder { export interface NotifBuilder {
id ?: number, id ?: number,
timeCreate: number, timeCreate ?: number,
seen ?: boolean, seen ?: boolean,
fromUserID: number, fromUserID: number,
destUserID ?: number, destUserID ?: number,
@ -61,7 +61,8 @@ export interface NotifBuilder {
} }
export class Notif implements NotifBuilder { export class Notif implements NotifBuilder {
id ?: number; timeCreate: number; id ?: number;
timeCreate ?: number;
seen ?: boolean; seen ?: boolean;
fromUserID: number; fromUserID: number;
destUserID ?: number; destUserID ?: number;
@ -79,4 +80,38 @@ export class Notif implements NotifBuilder {
} }
} }
} }
get hasId() : boolean {
return this.id > 0
}
get hasSeen() : boolean {
return this.seen === true || this.seen === false
}
get hasFromUserID() : boolean {
return this.fromUserID > 0
}
get hasDestUserID() : boolean {
return this.destUserID > 0
}
get hasType() : boolean {
return this.type != null && this.type != undefined
}
get hasOnElemID() : boolean {
return this.onElemID > 0
}
get hasOnElemType() : boolean {
return this.onElemType != null && this.onElemType != undefined
}
get hasTimeCreate() : boolean {
return this.timeCreate != null
&& this.timeCreate != undefined
&& this.timeCreate > 0;
}
} }

View File

@ -1,5 +1,8 @@
import { DatabaseHelper } from "./DatabaseHelper"; import { DatabaseHelper } from "./DatabaseHelper";
import { Notif } from "../entities/Notification"; import { Notif, NotifElemType, NotifEventVisibility } from "../entities/Notification";
import { time } from "../utils/DateUtils";
import { PostsHelper } from "./PostsHelper";
import { PostPageKind, PostVisibilityLevel } from "../entities/Post";
/** /**
* Notifications helper * Notifications helper
@ -17,7 +20,81 @@ export class NotificationsHelper {
* @param n Notification to push * @param n Notification to push
*/ */
public static async Push(n: Notif) { public static async Push(n: Notif) {
// TODO : push notification
if(!n.hasTimeCreate)
n.timeCreate = time();
// Determine the visibility level of the notification
if(n.onElemType == NotifElemType.POST) {
const post = await PostsHelper.GetSingle(n.onElemID);
// Determine post container
switch(post.kindPage) {
case PostPageKind.PAGE_KIND_USER:
n.fromContainerType = NotifElemType.USER_PAGE
n.fromContainerID = post.userPageID
break;
case PostPageKind.PAGE_KIND_GROUP:
n.fromContainerType = NotifElemType.GROUP_PAGE
n.fromContainerID = post.groupID
break;
default:
throw new Error("Unsupported page kind: " + post.kindPage)
}
// Check the scope of the notification
if(post.visibilityLevel == PostVisibilityLevel.VISIBILITY_USER) {
// Check if the user does not own the post
if(post.userID == post.userPageID)
return;
// If the personn who posted that is not the owner of the page
else if(post.userPageID != n.fromUserID)
n.destUserID = post.userPageID
// If the user is the one who own the page
else
n.destUserID = post.userID
await this.PushPrivate(n)
}
// TODO : continue
}
}
/**
* Push a private notification
*
* @param n The notification
*/
private static async PushPrivate(n: Notif) {
n.eventVisibility = NotifEventVisibility.EVENT_PRIVATE
if(await this.SimilarExists(n))
return;
throw new Error("Create the notification!")
//await this.Create(n)
}
/**
* Check out whether a similar notification exists
* for a given notification
*
* @param n The notification
*/
private static async SimilarExists(n: Notif) : Promise<boolean> {
return await DatabaseHelper.Count({
table: NOTIFICATIONS_TABLE,
where: this.NotifToDB(n)
}) > 0;
} }
/** /**
@ -53,6 +130,33 @@ export class NotificationsHelper {
return list.map((e) => this.DBToNotif(e)); return list.map((e) => this.DBToNotif(e));
} }
/**
* Turn a notification into a database entry
*
* @param n The notification
* @param allInfo True to return a full notification entry
*/
private static NotifToDB(n: Notif, allInfo = true) : any {
let data: any = {}
if(n.hasId) data.id = n.id
if(n.hasSeen) data.seen = n.seen ? 1 : 0
if(n.hasFromUserID) data.from_user_id = n.fromUserID
if(n.hasDestUserID) data.dest_user_id = n.destUserID
if(n.hasType) data.type = n.type
if(n.hasOnElemID) data.on_elem_id = n.onElemID
if(n.hasOnElemType) data.on_elem_type = n.onElemType
if(allInfo) {
data.from_container_id = n.fromContainerID
data.from_container_type = n.fromContainerType
data.time_create = n.timeCreate
data.visibility = n.eventVisibility
}
return data;
}
/** /**
* Database entry to notification * Database entry to notification
* *