mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2025-06-20 16:35:17 +00:00
Load post comments
This commit is contained in:
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()
|
||||
}
|
||||
}
|
@ -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;
|
||||
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)
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user