diff --git a/src/controllers/posts_controller.rs b/src/controllers/posts_controller.rs index 062ae94..172b5c2 100644 --- a/src/controllers/posts_controller.rs +++ b/src/controllers/posts_controller.rs @@ -141,6 +141,7 @@ pub fn create_post(r: &mut HttpRequestHandler) -> RequestResult { PostKind::POST_KIND_IMAGE(PostFile::new_from_created_file(&path)?) } + // YouTube posts "youtube" => { let youtube = r.post_string("youtube_id")?; @@ -151,6 +152,13 @@ pub fn create_post(r: &mut HttpRequestHandler) -> RequestResult { PostKind::POST_KIND_YOUTUBE(youtube) } + // Movies posts + "movie" => { + let movie_id = r.post_movie_id("movieID")?; + + PostKind::POST_KIND_MOVIE(movie_id) + } + // TODO : add support for next types _ => { diff --git a/src/data/http_request_handler.rs b/src/data/http_request_handler.rs index 6d5f35a..53bfd2a 100644 --- a/src/data/http_request_handler.rs +++ b/src/data/http_request_handler.rs @@ -18,7 +18,7 @@ use crate::data::group::GroupAccessLevel; use crate::data::group_id::GroupID; use crate::data::post::{Post, PostAccessLevel}; use crate::data::user::UserID; -use crate::helpers::{account_helper, api_helper, conversations_helper, friends_helper, groups_helper, posts_helper, user_helper, virtual_directory_helper}; +use crate::helpers::{account_helper, api_helper, conversations_helper, friends_helper, groups_helper, movies_helper, posts_helper, user_helper, virtual_directory_helper}; use crate::helpers::virtual_directory_helper::VirtualDirType; use crate::utils::string_utils::{check_url, remove_html_nodes}; use crate::utils::user_data_utils::{generate_new_user_data_file_name, prepare_file_creation, user_data_path}; @@ -561,4 +561,15 @@ impl HttpRequestHandler { Ok(post) } + + /// Get the ID of a movie included in the request + pub fn post_movie_id(&mut self, name: &str) -> ResultBoxError { + let movie_id = self.post_u64(name)?; + + if !movies_helper::does_user_has(self.user_id_ref()?, movie_id)? { + self.forbidden("You are not authorized to use this movie!".to_string())?; + } + + Ok(movie_id) + } } \ No newline at end of file diff --git a/src/helpers/movies_helper.rs b/src/helpers/movies_helper.rs index 05f5a3e..abb945f 100644 --- a/src/helpers/movies_helper.rs +++ b/src/helpers/movies_helper.rs @@ -23,6 +23,11 @@ pub fn get_info(movie_id: u64) -> ResultBoxError { .query_row(db_to_movie) } +/// Check out whether a user own a movie or not +pub fn does_user_has(user_id: &UserID, movie_id: u64) -> ResultBoxError { + Ok(get_info(movie_id).map(|m| &m.user_id == user_id).unwrap_or(false)) +} + /// Turn a database entry into a movie object fn db_to_movie(row: &database::RowResult) -> ResultBoxError { Ok(Movie { diff --git a/src/helpers/posts_helper.rs b/src/helpers/posts_helper.rs index c488def..a8799d7 100644 --- a/src/helpers/posts_helper.rs +++ b/src/helpers/posts_helper.rs @@ -90,6 +90,11 @@ pub fn create(p: &Post) -> ResultBoxError { .add_str("type", "youtube"); } + // Movie post + POST_KIND_MOVIE(id) => { + insert_query = insert_query.add_u64("idvideo", *id); + } + _ => unimplemented!() /* POST_KIND_WEBLINK(_) => {},