diff --git a/src/api_data/comment_api.rs b/src/api_data/comment_api.rs new file mode 100644 index 0000000..524a67c --- /dev/null +++ b/src/api_data/comment_api.rs @@ -0,0 +1,55 @@ +//! # Comments API entry +//! +//! @author Pierre Hubert +use serde::Serialize; + +use crate::data::comment::Comment; +use crate::data::error::ResultBoxError; +use crate::data::user::UserID; +use crate::helpers::likes_helper; +use crate::helpers::likes_helper::LikeType; +use crate::utils::user_data_utils::user_data_url; + +#[derive(Serialize)] +#[allow(non_snake_case)] +pub struct CommentAPI { + ID: u64, + userID: u64, + postID: u64, + time_sent: u64, + content: String, + image_path: Option, + img_url: Option, + likes: u64, + userlike: bool, +} + +impl CommentAPI { + /// Construct a new `CommentAPI` object + pub fn new(c: &Comment, curr_user_id: &Option) -> ResultBoxError { + let mut c_api = CommentAPI { + ID: c.id, + userID: c.user_id.id(), + postID: c.post_id, + time_sent: c.time_sent, + content: c.content.clone(), + image_path: c.image_path.clone(), + img_url: c.image_path.as_ref().map(|f| user_data_url(f)), + likes: likes_helper::count(c.id, LikeType::COMMENT)? as u64, + userlike: false, + }; + + // If required, check if current user is liking the comment + if let Some(user_id) = curr_user_id { + if c_api.likes > 0 { + c_api.userlike = likes_helper::is_liking(user_id, c.id, LikeType::COMMENT)?; + } + } + + Ok(c_api) + } + + pub fn for_list(l: &Vec, curr_user_id: &Option) -> ResultBoxError> { + l.iter().map(|c| Self::new(c, curr_user_id)).collect() + } +} \ No newline at end of file diff --git a/src/api_data/mod.rs b/src/api_data/mod.rs index ffdc496..5450729 100644 --- a/src/api_data/mod.rs +++ b/src/api_data/mod.rs @@ -33,4 +33,5 @@ pub mod friendship_status_api; pub mod post_api; pub mod movie_api; pub mod survey_choice_api; -pub mod survey_api; \ No newline at end of file +pub mod survey_api; +pub mod comment_api; \ No newline at end of file diff --git a/src/api_data/post_api.rs b/src/api_data/post_api.rs index 05eecbe..f0a766e 100644 --- a/src/api_data/post_api.rs +++ b/src/api_data/post_api.rs @@ -3,12 +3,13 @@ //! @author Pierre Hubert use serde::Serialize; +use crate::api_data::comment_api::CommentAPI; use crate::api_data::movie_api::MovieAPI; use crate::api_data::survey_api::SurveyAPI; use crate::data::error::ResultBoxError; use crate::data::post::{Post, PostKind}; use crate::data::user::UserID; -use crate::helpers::{likes_helper, movies_helper, survey_helper, posts_helper}; +use crate::helpers::{comments_helper, likes_helper, movies_helper, posts_helper, survey_helper}; use crate::helpers::likes_helper::LikeType; use crate::utils::user_data_utils::user_data_url; @@ -50,6 +51,9 @@ pub struct PostAPI { // Likes information likes: u64, userlike: bool, + + // Comemnts + comments: Option>, } impl PostAPI { @@ -94,6 +98,9 @@ impl PostAPI { .as_ref() .map(|user_id| likes_helper::is_liking(user_id, p.id, LikeType::POST)) .unwrap_or(Ok(false))?, + + // Comments + comments: None, }; match &p.kind { @@ -129,6 +136,10 @@ impl PostAPI { } } + if posts_helper::allow_comments_on_post(p)? { + post.comments = Some(CommentAPI::for_list(&comments_helper::get(p.id)?, user)?); + } + Ok(post) } diff --git a/src/constants.rs b/src/constants.rs index a45273d..155e900 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -49,6 +49,9 @@ pub mod database_tables_names { /// Survey responses table pub const SURVEY_RESPONSE_TABLE: &str = "sondage_reponse"; + + /// Comments table + pub const COMMENTS_TABLE: &str = "commentaires"; } /// The account image to show for user who do not have any diff --git a/src/data/comment.rs b/src/data/comment.rs new file mode 100644 index 0000000..2149ee1 --- /dev/null +++ b/src/data/comment.rs @@ -0,0 +1,14 @@ +//! # Comment information +//! +//! @author Pierre Hubert + +use crate::data::user::UserID; + +pub struct Comment { + pub id: u64, + pub time_sent: u64, + pub user_id: UserID, + pub post_id: u64, + pub content: String, + pub image_path: Option, +} diff --git a/src/data/mod.rs b/src/data/mod.rs index 13e827d..d5c9d83 100644 --- a/src/data/mod.rs +++ b/src/data/mod.rs @@ -21,4 +21,5 @@ pub mod friend; pub mod friendship_status; pub mod post; pub mod movie; -pub mod survey; \ No newline at end of file +pub mod survey; +pub mod comment; \ No newline at end of file diff --git a/src/data/post.rs b/src/data/post.rs index 1535edc..aee6bbb 100644 --- a/src/data/post.rs +++ b/src/data/post.rs @@ -133,4 +133,12 @@ impl Post { _ => None, } } + + /// Check out whether a post is targeting a user page or not + pub fn is_on_user_page(&self) -> bool { + match &self.target_page { + PostPageKind::PAGE_KIND_USER(_) => true, + _ => false, + } + } } \ No newline at end of file diff --git a/src/helpers/comments_helper.rs b/src/helpers/comments_helper.rs new file mode 100644 index 0000000..a41f082 --- /dev/null +++ b/src/helpers/comments_helper.rs @@ -0,0 +1,29 @@ +//! # Comments helper +//! +//! @author Pierre Hubert + +use crate::constants::database_tables_names::COMMENTS_TABLE; +use crate::data::comment::Comment; +use crate::data::error::ResultBoxError; +use crate::helpers::database; + +/// Get the comments of a post +pub fn get(post_id: u64) -> ResultBoxError> { + database::QueryInfo::new(COMMENTS_TABLE) + .cond_u64("ID_texte", post_id) + .set_order("ID") + .exec(db_to_comment) +} + + +/// Turn a database entry into a comment object +fn db_to_comment(row: &database::RowResult) -> ResultBoxError { + Ok(Comment { + id: row.get_u64("ID")?, + time_sent: row.get_u64("time_insert").unwrap_or(0), + user_id: row.get_user_id("ID_personne")?, + post_id: row.get_u64("ID_texte")?, + content: row.get_str("commentaire")?, + image_path: row.get_optional_str("image_commentaire")?, + }) +} \ No newline at end of file diff --git a/src/helpers/mod.rs b/src/helpers/mod.rs index 41675b3..d764aba 100644 --- a/src/helpers/mod.rs +++ b/src/helpers/mod.rs @@ -12,4 +12,5 @@ pub mod posts_helper; pub mod conversations_helper; pub mod virtual_directory_helper; pub mod movies_helper; -pub mod survey_helper; \ No newline at end of file +pub mod survey_helper; +pub mod comments_helper; \ No newline at end of file diff --git a/src/helpers/posts_helper.rs b/src/helpers/posts_helper.rs index 87e6c00..5a56135 100644 --- a/src/helpers/posts_helper.rs +++ b/src/helpers/posts_helper.rs @@ -177,6 +177,13 @@ pub fn get_access_level(p: &Post, user_id: &Option) -> ResultBoxError ResultBoxError { + Ok( + !p.is_on_user_page() || + user_helper::allow_comments(p.user_page_id().unwrap_or(&UserID::invalid()))?) +} + /// Turn a post into a database entry fn db_to_post(res: &database::RowResult) -> ResultBoxError { let user_id = if res.get_u64("ID_amis")? == 0 { diff --git a/src/helpers/user_helper.rs b/src/helpers/user_helper.rs index 9e66e61..488c9b4 100644 --- a/src/helpers/user_helper.rs +++ b/src/helpers/user_helper.rs @@ -111,6 +111,12 @@ pub fn allow_posts_on_his_page(user_id: &UserID) -> ResultBoxError { Ok(find_user_by_id(user_id)?.allow_posts_from_friends) } +/// Check out whether a user has blocked comments on his / her page +pub fn allow_comments(user_id: &UserID) -> ResultBoxError { + Ok(!find_user_by_id(user_id)?.block_comments_on_his_page) +} + + /// Check out whether the friends list of a user is public or not pub fn is_user_friends_list_public(user_id: &UserID) -> ResultBoxError { Ok(find_user_by_id(user_id)?.public_friends_list)