mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2024-11-22 21:39:21 +00:00
Load post comments
This commit is contained in:
parent
a3bb9685ea
commit
27e3dedf51
55
src/api_data/comment_api.rs
Normal file
55
src/api_data/comment_api.rs
Normal file
@ -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<String>,
|
||||
img_url: Option<String>,
|
||||
likes: u64,
|
||||
userlike: bool,
|
||||
}
|
||||
|
||||
impl CommentAPI {
|
||||
/// Construct a new `CommentAPI` object
|
||||
pub fn new(c: &Comment, curr_user_id: &Option<UserID>) -> ResultBoxError<CommentAPI> {
|
||||
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<Comment>, curr_user_id: &Option<UserID>) -> ResultBoxError<Vec<CommentAPI>> {
|
||||
l.iter().map(|c| Self::new(c, curr_user_id)).collect()
|
||||
}
|
||||
}
|
@ -34,3 +34,4 @@ pub mod post_api;
|
||||
pub mod movie_api;
|
||||
pub mod survey_choice_api;
|
||||
pub mod survey_api;
|
||||
pub mod comment_api;
|
@ -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<Vec<CommentAPI>>,
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
14
src/data/comment.rs
Normal file
14
src/data/comment.rs
Normal file
@ -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<String>,
|
||||
}
|
@ -22,3 +22,4 @@ pub mod friendship_status;
|
||||
pub mod post;
|
||||
pub mod movie;
|
||||
pub mod survey;
|
||||
pub mod comment;
|
@ -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,
|
||||
}
|
||||
}
|
||||
}
|
29
src/helpers/comments_helper.rs
Normal file
29
src/helpers/comments_helper.rs
Normal file
@ -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<Vec<Comment>> {
|
||||
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<Comment> {
|
||||
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")?,
|
||||
})
|
||||
}
|
@ -13,3 +13,4 @@ pub mod conversations_helper;
|
||||
pub mod virtual_directory_helper;
|
||||
pub mod movies_helper;
|
||||
pub mod survey_helper;
|
||||
pub mod comments_helper;
|
@ -177,6 +177,13 @@ pub fn get_access_level(p: &Post, user_id: &Option<UserID>) -> ResultBoxError<Po
|
||||
}
|
||||
}
|
||||
|
||||
/// Check out whether it is possible to create comments on a post or not
|
||||
pub fn allow_comments_on_post(p: &Post) -> ResultBoxError<bool> {
|
||||
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<Post> {
|
||||
let user_id = if res.get_u64("ID_amis")? == 0 {
|
||||
|
@ -111,6 +111,12 @@ pub fn allow_posts_on_his_page(user_id: &UserID) -> ResultBoxError<bool> {
|
||||
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<bool> {
|
||||
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<bool> {
|
||||
Ok(find_user_by_id(user_id)?.public_friends_list)
|
||||
|
Loading…
Reference in New Issue
Block a user