mirror of
				https://gitlab.com/comunic/comunicapiv3
				synced 2025-10-31 07:34:45 +00:00 
			
		
		
		
	Complete information
This commit is contained in:
		| @@ -4,18 +4,31 @@ | ||||
| use serde::Serialize; | ||||
|  | ||||
| use crate::data::post::Post; | ||||
| use crate::data::user::UserID; | ||||
|  | ||||
| #[derive(Serialize)] | ||||
| #[allow(non_snake_case)] | ||||
| pub struct PostAPI { | ||||
|     ID: u64, | ||||
|     userID: u64, | ||||
|     user_page_id: u64, | ||||
|     group_id: u64, | ||||
|     post_time: u64, | ||||
|     content: Option<String>, | ||||
|     visibility_level: String, | ||||
| } | ||||
|  | ||||
| impl PostAPI { | ||||
|     /// Turn a `Post` entry into an API entry | ||||
|     pub fn new(p: &Post) -> PostAPI { | ||||
|         PostAPI { | ||||
|             ID: p.id | ||||
|             ID: p.id, | ||||
|             userID: p.user_id.id(), | ||||
|             user_page_id: p.user_page_id().unwrap_or(&UserID::invalid()).id(), | ||||
|             group_id: p.group_id().map(|f| f.id()).unwrap_or(0), | ||||
|             post_time: p.time_create, | ||||
|             content: p.content.clone(), | ||||
|             visibility_level: p.visibility.to_api(), | ||||
|         } | ||||
|     } | ||||
|  | ||||
|   | ||||
| @@ -2,6 +2,9 @@ | ||||
| //! | ||||
| //! @author Pierre Hubert | ||||
|  | ||||
| use crate::data::group_id::GroupID; | ||||
| use crate::data::user::UserID; | ||||
|  | ||||
| #[allow(non_camel_case_types)] | ||||
| pub enum PostVisibilityLevel { | ||||
|     //Posts that can be seen by anyone | ||||
| @@ -17,6 +20,46 @@ pub enum PostVisibilityLevel { | ||||
|     VISIBILITY_GROUP_MEMBERS = 50, | ||||
| } | ||||
|  | ||||
| impl PostVisibilityLevel { | ||||
|     pub fn to_api(&self) -> String { | ||||
|         match self { | ||||
|             PostVisibilityLevel::VISIBILITY_PUBLIC => "public", | ||||
|             PostVisibilityLevel::VISIBILITY_FRIENDS => "friends", | ||||
|             PostVisibilityLevel::VISIBILITY_USER => "private", | ||||
|             PostVisibilityLevel::VISIBILITY_GROUP_MEMBERS => "members", | ||||
|         }.to_string() | ||||
|     } | ||||
| } | ||||
|  | ||||
| #[allow(non_camel_case_types)] | ||||
| pub enum PostPageKind { | ||||
|     PAGE_KIND_USER(UserID), | ||||
|     PAGE_KIND_GROUP(GroupID), | ||||
| } | ||||
|  | ||||
| pub struct Post { | ||||
|     pub id: u64 | ||||
|     pub id: u64, | ||||
|     pub user_id: UserID, | ||||
|     pub time_create: u64, | ||||
|     pub target_page: PostPageKind, | ||||
|     pub content: Option<String>, | ||||
|     pub visibility: PostVisibilityLevel, | ||||
| } | ||||
|  | ||||
| impl Post { | ||||
|     /// Get the ID of the target user page, if any | ||||
|     pub fn user_page_id(&self) -> Option<&UserID> { | ||||
|         match &self.target_page { | ||||
|             PostPageKind::PAGE_KIND_USER(id) => Some(id), | ||||
|             _ => None, | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     /// Get the ID of the target group page, if any | ||||
|     pub fn group_id(&self) -> Option<&GroupID> { | ||||
|         match &self.target_page { | ||||
|             PostPageKind::PAGE_KIND_GROUP(id) => Some(id), | ||||
|             _ => None, | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -4,9 +4,10 @@ | ||||
|  | ||||
| use crate::constants::database_tables_names::POSTS_TABLE; | ||||
| use crate::data::error::ResultBoxError; | ||||
| use crate::data::post::{Post, PostVisibilityLevel}; | ||||
| use crate::data::post::{Post, PostPageKind, PostVisibilityLevel}; | ||||
| use crate::data::user::UserID; | ||||
| use crate::helpers::{database, friends_helper}; | ||||
| use crate::utils::date_utils::time; | ||||
|  | ||||
| impl PostVisibilityLevel { | ||||
|     pub fn to_db(&self) -> u32 { | ||||
| @@ -17,6 +18,16 @@ impl PostVisibilityLevel { | ||||
|             PostVisibilityLevel::VISIBILITY_GROUP_MEMBERS => 50, | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     pub fn from_db(val: u32) -> PostVisibilityLevel { | ||||
|         match val { | ||||
|             1 => PostVisibilityLevel::VISIBILITY_PUBLIC, | ||||
|             2 => PostVisibilityLevel::VISIBILITY_FRIENDS, | ||||
|             3 => PostVisibilityLevel::VISIBILITY_USER, | ||||
|             50 => PostVisibilityLevel::VISIBILITY_GROUP_MEMBERS, | ||||
|             _ => PostVisibilityLevel::VISIBILITY_PUBLIC, | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
|  | ||||
| @@ -112,7 +123,25 @@ fn get_user(query: &PostsQuery, target_id: &UserID) -> ResultBoxError<Vec<Post>> | ||||
|  | ||||
| /// 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 { | ||||
|         res.get_user_id("ID_personne") | ||||
|     } else { | ||||
|         res.get_user_id("ID_amis") | ||||
|     }?; | ||||
|  | ||||
|     let target_page = if res.get_u64("group_id")? == 0 { | ||||
|         PostPageKind::PAGE_KIND_USER(res.get_user_id("ID_personne")?) | ||||
|     } else { | ||||
|         PostPageKind::PAGE_KIND_GROUP(res.get_group_id("group_id")?) | ||||
|     }; | ||||
|  | ||||
|     Ok(Post { | ||||
|         id: res.get_u64("ID")? | ||||
|         // General information | ||||
|         id: res.get_u64("ID")?, | ||||
|         user_id: user_id.clone(), | ||||
|         time_create: res.get_u64("time_insert").unwrap_or(time()), | ||||
|         target_page, | ||||
|         content: res.get_optional_str("texte")?, | ||||
|         visibility: PostVisibilityLevel::from_db(res.get_u32("niveau_visibilite")?), | ||||
|     }) | ||||
| } | ||||
		Reference in New Issue
	
	Block a user