2020-07-10 06:21:40 +00:00
|
|
|
//! # Comments controller
|
|
|
|
//!
|
|
|
|
//! @author Pierre Hubert
|
|
|
|
|
2020-07-10 06:45:53 +00:00
|
|
|
use crate::api_data::comment_api::CommentAPI;
|
2020-07-10 06:21:40 +00:00
|
|
|
use crate::api_data::res_create_comment::ResCreateComment;
|
|
|
|
use crate::constants::PATH_COMMENTS_IMAGES;
|
|
|
|
use crate::controllers::routes::RequestResult;
|
|
|
|
use crate::data::comment::Comment;
|
|
|
|
use crate::data::http_request_handler::HttpRequestHandler;
|
|
|
|
use crate::data::post::PostAccessLevel;
|
|
|
|
use crate::helpers::{comments_helper, posts_helper};
|
|
|
|
use crate::utils::date_utils::time;
|
|
|
|
use crate::utils::string_utils::remove_html_nodes;
|
|
|
|
|
|
|
|
/// Create a new comment
|
|
|
|
pub fn create(r: &mut HttpRequestHandler) -> RequestResult {
|
|
|
|
let post = r.post_post_with_access("postID", PostAccessLevel::BASIC_ACCESS)?;
|
|
|
|
|
|
|
|
if !posts_helper::allow_comments_on_post(&post)? {
|
|
|
|
r.forbidden("You can not create comments on this post!".to_string())?;
|
|
|
|
}
|
|
|
|
|
|
|
|
let (content, image) = if r.has_file("image") {
|
|
|
|
(
|
2020-07-10 06:25:11 +00:00
|
|
|
remove_html_nodes(&r.post_string_opt("content", 0, false)?),
|
2020-07-10 06:21:40 +00:00
|
|
|
Some(r.save_post_image("image", PATH_COMMENTS_IMAGES, 700, 700)?)
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
(
|
2020-07-10 06:57:46 +00:00
|
|
|
r.post_content("content", 2, true)?,
|
2020-07-10 06:21:40 +00:00
|
|
|
None
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
let comment = Comment {
|
|
|
|
id: 0,
|
|
|
|
time_sent: time(),
|
|
|
|
user_id: r.user_id()?,
|
|
|
|
post_id: post.id,
|
|
|
|
content,
|
|
|
|
image_path: image,
|
|
|
|
};
|
|
|
|
|
|
|
|
let comment_id = comments_helper::create(&comment)?;
|
|
|
|
|
|
|
|
// TODO : Create notifications
|
|
|
|
// TODO : Remove notifications targeting current user about the post
|
|
|
|
|
|
|
|
r.set_response(ResCreateComment::new(comment_id))
|
2020-07-10 06:45:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get information about a single comment
|
|
|
|
pub fn get_single(r: &mut HttpRequestHandler) -> RequestResult {
|
|
|
|
let comment = r.post_comment_with_access("commentID")?;
|
|
|
|
|
|
|
|
r.set_response(CommentAPI::new(&comment, &r.user_id_opt())?)
|
2020-07-10 06:57:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Change a comment's content
|
|
|
|
pub fn edit(r: &mut HttpRequestHandler) -> RequestResult {
|
|
|
|
let comment = r.post_comment_with_full_access("commentID")?;
|
|
|
|
let new_content = r.post_content("content", 2, true)?;
|
|
|
|
|
|
|
|
comments_helper::edit(comment.id, &new_content)?;
|
|
|
|
|
|
|
|
r.success("Content updated.")
|
2020-07-10 07:02:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Delete a comment
|
|
|
|
pub fn delete(r: &mut HttpRequestHandler) -> RequestResult {
|
|
|
|
let comment = r.post_comment_with_full_access("commentID")?;
|
|
|
|
|
|
|
|
comments_helper::delete(&comment)?;
|
|
|
|
|
|
|
|
r.success("Comment deleted.")
|
2020-07-10 06:21:40 +00:00
|
|
|
}
|