1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2025-06-20 08:35:17 +00:00

Can create text posts

This commit is contained in:
2020-01-04 17:06:43 +01:00
parent a726ba4230
commit 037916e7eb
4 changed files with 127 additions and 5 deletions

View File

@ -4,6 +4,7 @@ import { DatabaseHelper } from "./DatabaseHelper";
import { UserHelper } from "./UserHelper";
import { GroupsHelper } from "./GroupsHelper";
import { GroupMembershipLevels } from "../entities/GroupMember";
import { mysql_date } from "../utils/DateUtils";
/**
* Posts helper
@ -35,6 +36,90 @@ const PostDBTypes : Record<string, PostKind> = {
*/
export class PostsHelper {
/**
* Create a new post
*
* @param p Information about the post
* @returns The ID of the created post
* @throws In case of failure
*/
public static async Create(p: Post) : Promise<number> {
// Extract the kind of post
let kindDb : string = "";
for (const key in PostDBTypes) {
if (PostDBTypes.hasOwnProperty(key) && PostDBTypes[key] == p.kind)
kindDb = key;
}
if(kindDb == "")
throw new Error("Unknown post kind: " + kindDb);
// Prepare post target
let userID: number, friendID: number, groupID: number;
// Post on user page
if(p.isUserPage) {
userID = p.userPageID;
friendID = p.userID;
groupID = 0;
}
// Post on group page
else {
userID = p.userID;
friendID = 0;
groupID = p.groupID;
}
// Generate database entry
let data = {
// Post meta-data & basic info
ID_personne: userID,
ID_amis: friendID,
group_id: groupID,
date_envoi: mysql_date(),
time_insert: p.timeCreate,
niveau_visibilite: p.visibilityLevel,
type: kindDb,
texte: p.hasContent ? p.content : "",
// Generic file infos
size: !p.hasFile ? null : p.file.size,
file_type: !p.hasFile ? null : p.file.type,
path: !p.hasFile ? null : p.file.path,
// Movies post
idvideo: p.hasMovie ? p.movieID : null,
// Countdown timer (TODO : implement)
jour_fin: null,
mois_fin: null,
annee_fin: null,
time_end: p.hasTimeEnd ? p.timeEnd : null,
// Weblink
url_page: !p.hasLink ? null : p.link.url,
titre_page: !p.hasLink ? null : p.link.title,
description_page: !p.hasLink ? null : p.link.description,
image_page: !p.hasLink ? null : p.link.image
}
// Insert the post
const postID = await DatabaseHelper.InsertRow(TABLE_NAME, data);
return postID;
}
/**
* Get the posts of a user
*