2020-07-10 06:21:40 +00:00
|
|
|
//! # Comments controller
|
|
|
|
//!
|
|
|
|
//! @author Pierre Hubert
|
|
|
|
|
|
|
|
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 {
|
|
|
|
(
|
|
|
|
r.post_content("content", 3, true)?,
|
|
|
|
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))
|
|
|
|
}
|