2020-01-03 16:31:39 +00:00
|
|
|
import { RequestHandler } from "../entities/RequestHandler";
|
|
|
|
import { Comment } from "../entities/Comment";
|
|
|
|
import { LikesHelper, LikesType } from "../helpers/LikesHelper";
|
2020-03-21 10:28:32 +00:00
|
|
|
import { check_string_before_insert, removeHTMLNodes } from "../utils/StringUtils";
|
|
|
|
import { time } from "../utils/DateUtils";
|
|
|
|
import { CommentsHelper } from "../helpers/CommentsHelper";
|
2020-03-23 18:08:04 +00:00
|
|
|
import { NotifEventType } from "../entities/Notification";
|
2020-03-28 13:18:37 +00:00
|
|
|
import { NotificationsHelper } from "../helpers/NotificationsHelper";
|
2020-04-01 15:51:33 +00:00
|
|
|
import { AbstractUserConnectionContainer } from "../entities/UserConnectionContainer";
|
2020-01-03 16:31:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Comments controller
|
|
|
|
*
|
|
|
|
* @author Pierre HUBERT
|
|
|
|
*/
|
|
|
|
|
|
|
|
export class CommentsController {
|
|
|
|
|
2020-03-21 08:20:34 +00:00
|
|
|
/**
|
|
|
|
* Create a new comment
|
|
|
|
*
|
|
|
|
* @param h Request handler
|
|
|
|
*/
|
|
|
|
public static async Create(h: RequestHandler) {
|
|
|
|
const postID = await h.postPostIDWithAccess("postID");
|
|
|
|
|
2020-03-21 10:28:32 +00:00
|
|
|
let content: string, image_path: string;
|
|
|
|
|
|
|
|
// Check if an image was included in the request or not
|
|
|
|
if(h.hasFile("image")) {
|
|
|
|
content = this.GetCommentContent(h, "content", false);
|
|
|
|
image_path = await h.savePostImage("image", "imgcommentaire", 700, 700);
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
content = this.GetCommentContent(h, "content", true);
|
|
|
|
|
|
|
|
const newComment = new Comment({
|
|
|
|
id: -1,
|
|
|
|
timeSent: time(),
|
|
|
|
postID: postID,
|
|
|
|
userID: h.getUserId(),
|
|
|
|
content: content,
|
|
|
|
imagePath: image_path
|
|
|
|
});
|
|
|
|
|
|
|
|
const commentID = await CommentsHelper.Create(newComment);
|
|
|
|
|
2020-03-23 18:08:04 +00:00
|
|
|
// Create notifications
|
2020-03-28 13:18:37 +00:00
|
|
|
await NotificationsHelper.CreatePostNotification(h.getUserId(), postID, NotifEventType.COMMENT_CREATED);
|
2020-03-21 10:28:32 +00:00
|
|
|
|
2020-03-28 13:25:58 +00:00
|
|
|
// Delete any notifications targetting this user about the post
|
|
|
|
await NotificationsHelper.DeleteAllPostsNotificationsTargetingUser(h.getUserId(), postID);
|
2020-03-21 10:28:32 +00:00
|
|
|
|
|
|
|
h.send({success: true, commentID: commentID});
|
|
|
|
}
|
|
|
|
|
2020-03-21 10:49:52 +00:00
|
|
|
/**
|
|
|
|
* Get information about a single comment
|
|
|
|
*
|
|
|
|
* @param h Request handler
|
|
|
|
*/
|
|
|
|
public static async GetSingle(h: RequestHandler) {
|
2020-03-21 10:51:51 +00:00
|
|
|
const commentID = await h.postCommentIDWithAccess("commentID");
|
|
|
|
const comment = await CommentsHelper.GetSingle(commentID);
|
2020-03-21 10:49:52 +00:00
|
|
|
|
2020-03-21 10:51:51 +00:00
|
|
|
h.send(await this.CommentToAPI(h, comment))
|
2020-03-21 10:49:52 +00:00
|
|
|
}
|
|
|
|
|
2020-03-21 11:01:42 +00:00
|
|
|
/**
|
|
|
|
* Edit (update) a comment content
|
|
|
|
*
|
|
|
|
* @param h Request handler
|
|
|
|
*/
|
|
|
|
public static async Edit(h: RequestHandler) {
|
|
|
|
const commentID = await this.GetPostCommentIDWithFullAccess(h, "commentID");
|
|
|
|
const newContent = this.GetCommentContent(h, "content", true);
|
|
|
|
|
|
|
|
await CommentsHelper.Edit(commentID, newContent);
|
|
|
|
|
|
|
|
h.success()
|
|
|
|
}
|
|
|
|
|
2020-03-21 11:04:04 +00:00
|
|
|
/**
|
|
|
|
* Delete a comment
|
|
|
|
*
|
|
|
|
* @param h Request handler
|
|
|
|
*/
|
|
|
|
public static async Delete(h: RequestHandler) {
|
|
|
|
const commentID = await this.GetPostCommentIDWithFullAccess(h, "commentID");
|
|
|
|
|
|
|
|
await CommentsHelper.Delete(await CommentsHelper.GetSingle(commentID));
|
|
|
|
|
|
|
|
h.success();
|
|
|
|
}
|
|
|
|
|
2020-03-21 10:28:32 +00:00
|
|
|
/**
|
|
|
|
* Get the content of a comment included in a POST field
|
|
|
|
*
|
|
|
|
* @param h Request handler
|
|
|
|
* @param name The name of the post field
|
|
|
|
* @param need_check True if the comment must have valid content / false else
|
|
|
|
*/
|
|
|
|
private static GetCommentContent(h: RequestHandler, name: string, need_check = true) : string {
|
2020-03-21 10:29:56 +00:00
|
|
|
const content = h.postContent(name, need_check ? 3 : 0);
|
2020-03-21 10:28:32 +00:00
|
|
|
|
|
|
|
if(need_check && !check_string_before_insert(content))
|
|
|
|
h.error(400, "Please check new comment content!");
|
2020-03-21 08:20:34 +00:00
|
|
|
|
2020-03-21 10:28:32 +00:00
|
|
|
return content;
|
2020-03-21 08:20:34 +00:00
|
|
|
}
|
|
|
|
|
2020-03-21 11:01:42 +00:00
|
|
|
/**
|
|
|
|
* Get a comment ID on which current user has full access
|
|
|
|
*
|
|
|
|
* @param h Request handler
|
|
|
|
* @param name The name of the POST field containing the comment ID
|
|
|
|
*/
|
|
|
|
private static async GetPostCommentIDWithFullAccess(h: RequestHandler, name: string) : Promise<number> {
|
|
|
|
const commentID = h.postInt(name);
|
|
|
|
|
|
|
|
if(!await CommentsHelper.IsOwner(h.getUserId(), commentID))
|
|
|
|
h.error(401, "You are not the owner of this comment!");
|
|
|
|
|
|
|
|
return commentID;
|
|
|
|
}
|
|
|
|
|
2020-01-03 16:31:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Turn a list of comment object into API entries
|
|
|
|
*
|
|
|
|
* @param h Request handler
|
|
|
|
* @param l List of comments
|
|
|
|
*/
|
|
|
|
public static async CommentsToAPI(h: RequestHandler, l: Comment[]) : Promise<Array<any>> {
|
|
|
|
const list = [];
|
|
|
|
for (const comment of l) {
|
|
|
|
list.push(await this.CommentToAPI(h, comment));
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Turn a comment into an API object
|
|
|
|
*
|
|
|
|
* @param h Request handler
|
|
|
|
* @param c Comment
|
2020-04-02 07:47:07 +00:00
|
|
|
* @param assumeNoLike Assume there are not any likes on this comment (false by default)
|
2020-01-03 16:31:39 +00:00
|
|
|
*/
|
2020-04-02 07:47:07 +00:00
|
|
|
public static async CommentToAPI(h: AbstractUserConnectionContainer, c: Comment, assumeNoLike: boolean = false) : Promise<any> {
|
|
|
|
const data = {
|
2020-01-03 16:31:39 +00:00
|
|
|
ID: c.id,
|
|
|
|
userID: c.userID,
|
|
|
|
postID: c.postID,
|
|
|
|
time_sent: c.timeSent,
|
|
|
|
content: c.content,
|
|
|
|
img_path: c.hasImage ? c.imagePath : null,
|
|
|
|
img_url: c.hasImage ? c.imageURL : null,
|
2020-04-02 07:47:07 +00:00
|
|
|
likes: 0,
|
|
|
|
userlike: false
|
2020-01-03 16:31:39 +00:00
|
|
|
}
|
2020-04-02 07:47:07 +00:00
|
|
|
|
|
|
|
// Check if we have to load like information
|
|
|
|
if(!assumeNoLike) {
|
|
|
|
data.likes = await LikesHelper.Count(c.id, LikesType.COMMENT);
|
2020-04-02 08:59:00 +00:00
|
|
|
data.userlike = data.likes > 0 && h.signedIn ? await LikesHelper.IsLiking(h.getUserId(), c.id, LikesType.COMMENT) : false;
|
2020-04-02 07:47:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
2020-01-03 16:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|