mirror of
https://gitlab.com/comunic/comunicapiv2
synced 2024-11-26 07:19:23 +00:00
162 lines
4.0 KiB
TypeScript
162 lines
4.0 KiB
TypeScript
|
import { PostKind, PostVisibilityLevel, Post, PostPageKind, PostFile, PostLink } from "../entities/Post";
|
||
|
import { FriendsHelper } from "./FriendsHelper";
|
||
|
import { DatabaseHelper } from "./DatabaseHelper";
|
||
|
|
||
|
/**
|
||
|
* Posts helper
|
||
|
*
|
||
|
* @author Pierre HUBERT
|
||
|
*/
|
||
|
|
||
|
/**
|
||
|
* Table name
|
||
|
*/
|
||
|
const TABLE_NAME = "texte";
|
||
|
|
||
|
/**
|
||
|
* Database mapping
|
||
|
*/
|
||
|
const PostDBTypes : Record<string, PostKind> = {
|
||
|
"texte": PostKind.POST_KIND_IMAGE,
|
||
|
"image": PostKind.POST_KIND_IMAGE,
|
||
|
"webpage_link": PostKind.POST_KIND_WEBLINK,
|
||
|
"pdf": PostKind.POST_KIND_PDF,
|
||
|
"video": PostKind.POST_KIND_MOVIE,
|
||
|
"count_down": PostKind.POST_KIND_COUNTDOWN,
|
||
|
"sondage": PostKind.POST_KIND_SURVEY,
|
||
|
"youtube": PostKind.POST_KIND_YOUTUBE
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Posts helper
|
||
|
*/
|
||
|
export class PostsHelper {
|
||
|
|
||
|
/**
|
||
|
* Get the posts of a user
|
||
|
*
|
||
|
* @param userID The ID of the user making the request
|
||
|
* @param targetID The ID of the target user
|
||
|
* @param startFrom Start point (0 = none)
|
||
|
* @param limit Maximum number of messages to fetch
|
||
|
*/
|
||
|
public static async GetUserPosts(userID: number, targetID: number,
|
||
|
startFrom: number = 0, limit: number = 10) : Promise<Array<Post>> {
|
||
|
|
||
|
if(limit < 1)
|
||
|
throw Error("Limit of post query must be greater or equal to one!");
|
||
|
|
||
|
|
||
|
// Determine max user visibility
|
||
|
let level : PostVisibilityLevel = PostVisibilityLevel.VISIBILITY_PUBLIC;
|
||
|
|
||
|
if(userID == targetID)
|
||
|
level = PostVisibilityLevel.VISIBILITY_USER;
|
||
|
|
||
|
else if(userID > 0 && await FriendsHelper.AreFriend(userID, targetID))
|
||
|
level = PostVisibilityLevel.VISIBILITY_FRIENDS;
|
||
|
|
||
|
|
||
|
|
||
|
// Preprocess conditions
|
||
|
|
||
|
/// ============= PERMISSION CONDITIONS =================
|
||
|
// Visibility level
|
||
|
let customWhere = "((niveau_visibilite <= ?) ";
|
||
|
let customWhereArgs = [level.toString()];
|
||
|
|
||
|
// Add user post (if user signed in)
|
||
|
if(userID > 0) {
|
||
|
customWhere += " OR (ID_amis = ?) ";
|
||
|
customWhereArgs.push(userID.toString());
|
||
|
}
|
||
|
|
||
|
customWhere += ")"
|
||
|
/// ============= /PERMISSION CONDITIONS ================
|
||
|
|
||
|
|
||
|
|
||
|
// ============== START POINT CONDITION =================
|
||
|
if(startFrom != 0) {
|
||
|
customWhere += " AND ID <= ?";
|
||
|
customWhereArgs.push(startFrom.toString());
|
||
|
}
|
||
|
// ============== /START POINT CONDITION ================
|
||
|
|
||
|
|
||
|
|
||
|
// Perform the request
|
||
|
const entries = await DatabaseHelper.Query({
|
||
|
table: TABLE_NAME,
|
||
|
|
||
|
// Base conditions
|
||
|
where: {
|
||
|
ID_personne: targetID,
|
||
|
group_id: 0
|
||
|
},
|
||
|
|
||
|
customWhere: customWhere,
|
||
|
customWhereArgs: customWhereArgs,
|
||
|
|
||
|
order: "ID DESC",
|
||
|
limit: limit,
|
||
|
});
|
||
|
|
||
|
return entries.map((r) => this.DBToPost(r));
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Turn a database entry into a row object
|
||
|
*
|
||
|
* @param row The database entry
|
||
|
*/
|
||
|
private static DBToPost(row: any) : Post {
|
||
|
|
||
|
const postPageKind = row.group_id == 0 ? PostPageKind.PAGE_KIND_USER : PostPageKind.PAGE_KIND_GROUP;
|
||
|
|
||
|
const postType = PostDBTypes[row.type];
|
||
|
|
||
|
return new Post({
|
||
|
// General info
|
||
|
id: row.ID,
|
||
|
userID: row.ID_amis == 0 ? row.ID_personne : row.ID_amis,
|
||
|
|
||
|
|
||
|
// Kind of target of the page and its ID
|
||
|
kindPage: postPageKind,
|
||
|
pageID: postPageKind == PostPageKind.PAGE_KIND_USER ? row.ID_personne : row.group_id,
|
||
|
|
||
|
// Basic info
|
||
|
timeCreate: row.time_insert == null ? (new Date(row.date_envoi).getTime() / 1000) : row.time_insert,
|
||
|
content: row.texte,
|
||
|
visibilityLevel: row.niveau_visibilite,
|
||
|
kind: postType,
|
||
|
|
||
|
// Files specific
|
||
|
file: row.path == null ? undefined : new PostFile({
|
||
|
size: Number(row.size),
|
||
|
type: row.file_type,
|
||
|
path: row.path
|
||
|
}),
|
||
|
|
||
|
// Movies specific
|
||
|
movieID: row.idvideo == null ? undefined : row.idvideo,
|
||
|
|
||
|
// Coutdown timer specific
|
||
|
timeEnd: (row.time_end && row.time_end) > 0 ? row.time_end
|
||
|
|
||
|
// Coutdown legacy support
|
||
|
: (row.annee_fin && row.annee_fin > 2010 ? new Date(row.annee_fin + "/" + row.mois_fin + "/" + row.jour_fin).getTime() / 1000 : undefined),
|
||
|
|
||
|
|
||
|
// Weblink - specific
|
||
|
link: row.url_page == null ? undefined :
|
||
|
new PostLink({
|
||
|
url: row.url_page,
|
||
|
title: row.titre_page,
|
||
|
description: row.description_page,
|
||
|
image: row.image_page
|
||
|
}),
|
||
|
});
|
||
|
}
|
||
|
}
|