1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-27 07:49:21 +00:00
comunicapiv3/src/controllers/posts_controller.rs

155 lines
5.2 KiB
Rust
Raw Normal View History

2020-07-02 16:19:04 +00:00
//! # Posts controller
//!
//! @author Pierre Hubert
use crate::api_data::post_api::PostAPI;
2020-07-07 17:14:16 +00:00
use crate::api_data::res_create_post::ResCreatePost;
2020-07-08 09:03:17 +00:00
use crate::constants::PATH_POST_IMAGES;
2020-07-02 16:19:04 +00:00
use crate::controllers::routes::RequestResult;
2020-07-08 09:03:17 +00:00
use crate::data::error::{ExecError, ResultBoxError};
2020-07-06 07:20:31 +00:00
use crate::data::group::GroupAccessLevel;
2020-07-02 16:19:04 +00:00
use crate::data::http_request_handler::HttpRequestHandler;
2020-07-08 09:03:17 +00:00
use crate::data::post::{Post, PostAccessLevel, PostFile, PostKind, PostPageKind, PostVisibilityLevel};
2020-07-06 15:31:23 +00:00
use crate::helpers::{groups_helper, posts_helper, user_helper};
2020-07-07 17:14:16 +00:00
use crate::utils::date_utils::time;
use crate::utils::string_utils::check_string_before_insert;
2020-07-08 09:03:17 +00:00
use crate::utils::user_data_utils::user_data_path;
impl PostFile {
/// Initialize a `PostFile` instance based on a file that have just been created
pub fn new_from_created_file(path: &str) -> ResultBoxError<PostFile> {
let data = std::fs::metadata(user_data_path(path.as_ref()))?;
Ok(PostFile {
path: path.to_string(),
size: data.len() as usize,
file_type: mime_guess::from_path(path)
.first()
.map(|m| format!("{}/{}", m.type_(), m.subtype())),
})
}
}
2020-07-02 16:19:04 +00:00
/// Get the list of posts of a user
pub fn get_list_user(r: &mut HttpRequestHandler) -> RequestResult {
let user_id = r.post_user_id("userID")?;
let start_from = r.post_u64_opt("startFrom", 0)?;
if !user_helper::can_see_user_page(r.user_id_ref()?, &user_id)? {
r.forbidden("You are not allowed to access this user posts !".to_string())?;
}
let posts = posts_helper::PostsQuery::new(r.user_id_opt())
.set_start_from(start_from)
.get_user(&user_id)?;
2020-07-06 07:20:31 +00:00
r.set_response(PostAPI::for_list(&posts, r.user_id_opt())?)
}
/// Get the list of posts of a group
pub fn get_list_group(r: &mut HttpRequestHandler) -> RequestResult {
let group_id = r.post_group_id_with_access("groupID", GroupAccessLevel::VIEW_ACCESS)?;
let start_from = r.post_u64_opt("startFrom", 0)?;
let posts = posts_helper::PostsQuery::new(r.user_id_opt())
.set_start_from(start_from)
.get_group(&group_id)?;
2020-07-06 08:01:54 +00:00
r.set_response(PostAPI::for_list(&posts, r.user_id_opt())?)
}
/// Get the latest posts of a group
pub fn get_latest(r: &mut HttpRequestHandler) -> RequestResult {
let start_from = r.post_u64_opt("startFrom", 0)?;
let include_groups = r.post_bool_opt("include_groups", false);
let posts = posts_helper::PostsQuery::new(r.user_id_opt())
.set_start_from(start_from)
.get_latest(include_groups)?;
2020-07-04 14:44:42 +00:00
r.set_response(PostAPI::for_list(&posts, r.user_id_opt())?)
}
/// Get information about a single post
pub fn get_single(r: &mut HttpRequestHandler) -> RequestResult {
let post = r.post_post_with_access("postID", PostAccessLevel::BASIC_ACCESS)?;
r.set_response(PostAPI::new(&post, &r.user_id_opt())?)
2020-07-06 15:31:23 +00:00
}
/// Create a new post
pub fn create_post(r: &mut HttpRequestHandler) -> RequestResult {
// Process page target
let target_page = match r.post_string("kind-page")?.as_str() {
"user" => {
let user_id = r.post_user_id("kind-id")?;
if !user_helper::can_create_posts(r.user_id_ref()?, &user_id)? {
r.forbidden("You are not allowed to create posts on this page!".to_string())?;
}
PostPageKind::PAGE_KIND_USER(user_id)
}
"group" => {
let group_id = r.post_group_id_with_access("kind-id", GroupAccessLevel::MEMBER_ACCESS)?;
if !groups_helper::can_user_create_posts(&group_id, r.user_id_ref()?)? {
r.forbidden("You are not allowed to create posts on this group!".to_string())?;
}
PostPageKind::PAGE_KIND_GROUP(group_id)
}
_ => {
r.not_found("Unsupported target page type!".to_string())?;
unreachable!();
}
};
2020-07-07 17:14:16 +00:00
// Start to create post
2020-07-07 17:19:18 +00:00
let mut post = Post {
2020-07-07 17:14:16 +00:00
id: 0,
user_id: r.user_id()?,
time_create: time(),
target_page,
content: Some(r.post_string_opt("content", 0, false)?),
visibility: PostVisibilityLevel::VISIBILITY_PUBLIC,
kind: PostKind::POST_KIND_TEXT,
};
// Handle different post types
2020-07-07 17:19:18 +00:00
post.kind = match r.post_string("kind")?.as_str() {
2020-07-07 17:14:16 +00:00
// Text posts
"text" => {
if !check_string_before_insert(post.content.as_ref().unwrap_or(&String::new())) {
r.forbidden("Specified post content is invalid!".to_string())?;
}
2020-07-07 17:19:18 +00:00
PostKind::POST_KIND_TEXT
2020-07-07 17:14:16 +00:00
}
2020-07-08 09:03:17 +00:00
// Image post
"image" => {
if !r.has_file("image") {
r.bad_request("An error occured while receiving the image!".to_string())?;
}
let path = r.save_post_image("image", PATH_POST_IMAGES, 2000, 2000)?;
PostKind::POST_KIND_IMAGE(PostFile::new_from_created_file(&path)?)
}
2020-07-07 17:14:16 +00:00
_ => {
2020-07-07 17:19:18 +00:00
r.internal_error(ExecError::boxed_new("Unsupported kind of post!"))?;
unreachable!();
2020-07-07 17:14:16 +00:00
}
2020-07-07 17:19:18 +00:00
};
2020-07-07 17:14:16 +00:00
// Create the post
let post_id = posts_helper::create(&post)?;
2020-07-06 15:31:23 +00:00
2020-07-07 17:14:16 +00:00
// TODO : create a notification
r.set_response(ResCreatePost::new(post_id))
2020-07-02 16:19:04 +00:00
}