From 037916e7eb6defeeff74f77f2f03ffa9618fec64 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 4 Jan 2020 17:06:43 +0100 Subject: [PATCH] Can create text posts --- src/controllers/PostsController.ts | 30 +++++++++-- src/entities/RequestHandler.ts | 5 +- src/helpers/PostsHelper.ts | 85 ++++++++++++++++++++++++++++++ src/utils/StringUtils.ts | 12 +++++ 4 files changed, 127 insertions(+), 5 deletions(-) diff --git a/src/controllers/PostsController.ts b/src/controllers/PostsController.ts index f2c0def..41bee1b 100644 --- a/src/controllers/PostsController.ts +++ b/src/controllers/PostsController.ts @@ -13,6 +13,7 @@ import { GroupsAccessLevel } from "../entities/Group"; import { GroupsHelper } from "../helpers/GroupsHelper"; import { time } from "../utils/DateUtils"; import { findKey } from "../utils/ArrayUtils"; +import { check_string_before_insert } from "../utils/StringUtils"; /** * Posts controller @@ -139,7 +140,7 @@ export class PostsController { userID: h.getUserId(), timeCreate: time(), kind: h.postString("kind"), - content: h.postContent("content"), + content: h.postContent("content", 0), visibilityLevel: this.PostVisibilityLevel(h, "visibility"), // Post target @@ -148,8 +149,31 @@ export class PostsController { }); - console.info(newPost); - h.success("Go on."); + // Process each kind of post + switch(newPost.kind) { + + // Text posts + case PostKind.POST_KIND_TEXT: + + // Check the string + if(!check_string_before_insert(newPost.content)) + h.error(400, "Specified post content is invalid!"); + + break; + + default: + h.error(500, "Unsupported kind of post!"); + } + + // Create the post + const postID = await PostsHelper.Create(newPost); + + // TODO : create a notification + + h.send({ + success: "The post has been created!", + postID: postID + }); } /** diff --git a/src/entities/RequestHandler.ts b/src/entities/RequestHandler.ts index 00a3f2c..4816a4a 100644 --- a/src/entities/RequestHandler.ts +++ b/src/entities/RequestHandler.ts @@ -85,9 +85,10 @@ export class RequestHandler { * Get some content for post and satinize it (remove HTML nodes) * * @param name The name of the POST field + * @param minLength Optionnal minimal length for the post */ - public postContent(name: string) : string { - const content = this.postString(name); + public postContent(name: string, minLength ?: number) : string { + const content = this.postString(name, minLength); if(content.match(/data:image/)) this.error(401, "Please do not include inline images!"); diff --git a/src/helpers/PostsHelper.ts b/src/helpers/PostsHelper.ts index 714d049..83bc805 100644 --- a/src/helpers/PostsHelper.ts +++ b/src/helpers/PostsHelper.ts @@ -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 = { */ 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 { + + // 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 * diff --git a/src/utils/StringUtils.ts b/src/utils/StringUtils.ts index cefb068..64aef91 100644 --- a/src/utils/StringUtils.ts +++ b/src/utils/StringUtils.ts @@ -45,4 +45,16 @@ export function fixEncoding(input : string) : string { export function removeHTMLNodes(input : string) : string { return input.replace(//g, ">"); +} + +/** + * Check a string before inserting it + * + * Legacy function that might be completed + * / replaced in the future + * + * @param s The string to check + */ +export function check_string_before_insert(s: string) : boolean { + return s.trim().length >= 3; } \ No newline at end of file