mirror of
https://gitlab.com/comunic/comunicapiv2
synced 2024-11-22 13:29:22 +00:00
Can create text posts
This commit is contained in:
parent
a726ba4230
commit
037916e7eb
@ -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: <PostKind>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
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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!");
|
||||
|
@ -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
|
||||
*
|
||||
|
@ -46,3 +46,15 @@ export function removeHTMLNodes(input : string) : string {
|
||||
return input.replace(/</g, "<")
|
||||
.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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user