From 5405aa54206fb2794b51d7a7ed993bb1c99056de Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Tue, 14 Jul 2020 09:10:10 +0200 Subject: [PATCH] Export related user information --- src/api_data/account_export_api.rs | 13 +++++++++ src/data/account_export.rs | 47 ++++++++++++++++++++++++++++-- src/data/user.rs | 8 +++++ 3 files changed, 65 insertions(+), 3 deletions(-) diff --git a/src/api_data/account_export_api.rs b/src/api_data/account_export_api.rs index 5ec2e26..2fd47fe 100644 --- a/src/api_data/account_export_api.rs +++ b/src/api_data/account_export_api.rs @@ -18,7 +18,9 @@ use crate::api_data::user_like_api::UserLikeAPI; use crate::data::account_export::AccountExport; use crate::data::error::ResultBoxError; use crate::data::group::Group; +use crate::data::user::User; use crate::helpers::groups_helper; +use crate::helpers::user_helper; #[derive(Serialize)] #[allow(non_snake_case)] @@ -35,6 +37,7 @@ pub struct AccountExportAPI { conversations_messages: HashMap>, friends_list: Vec, groups: Vec, + users_info: HashMap, } impl AccountExportAPI { @@ -62,6 +65,16 @@ impl AccountExportAPI { .iter() .map(|g| GroupApi::new(&g, curr_user_id.as_option())) .collect::, _>>()?, + users_info: export.get_related_users_ids()? + .iter() + .map(|u| user_helper::find_user_by_id(u)) + .collect::, _>>()? + .iter() + .map(|u| { + APIUserInfo::new(&curr_user_id.as_option(), u) + .map(|r| (u.id.id(), r)) + }) + .collect::, _>>()?, }; Ok(export) diff --git a/src/data/account_export.rs b/src/data/account_export.rs index 0748783..196ace2 100644 --- a/src/data/account_export.rs +++ b/src/data/account_export.rs @@ -2,18 +2,20 @@ //! //! @author Pierre Hubert -use std::collections::HashMap; +use std::collections::{HashMap, HashSet}; use crate::data::comment::Comment; use crate::data::conversation::Conversation; use crate::data::conversation_message::ConversationMessage; +use crate::data::error::ResultBoxError; use crate::data::friend::Friend; use crate::data::group_id::GroupID; use crate::data::movie::Movie; -use crate::data::post::Post; +use crate::data::post::{Post, PostPageKind}; use crate::data::survey_response::SurveyResponse; -use crate::data::user::User; +use crate::data::user::{User, UserID}; use crate::data::user_like::UserLike; +use crate::helpers::comments_helper; pub struct AccountExport { pub user: User, @@ -27,4 +29,43 @@ pub struct AccountExport { pub conversation_messages: HashMap>, pub friends_list: Vec, pub groups: Vec, +} + +impl AccountExport { + /// Get the IDs of the related users + pub fn get_related_users_ids(&self) -> ResultBoxError> { + let mut set = HashSet::new(); + + // Own user + set.insert(self.user.id.clone()); + + // Friends + self.friends_list.iter().for_each(|f| { set.insert(f.friend_id.clone()); }); + + // Posts + for post in &self.posts { + set.insert(post.user_id.clone()); + + if let PostPageKind::PAGE_KIND_USER(id) = &post.target_page { + set.insert(id.clone()); + } + + comments_helper::get(post.id)?.iter().for_each(|f| { set.insert(f.user_id.clone()); }) + } + + // Comments + self.comments.iter().for_each(|f| { set.insert(f.user_id.clone()); }); + + // Conversation members + for conv in &self.conversations { + conv.members.iter().for_each(|f| { set.insert(f.clone()); }); + } + + // Conversation messages + for (_, conv_messages) in &self.conversation_messages { + conv_messages.iter().for_each(|f| { set.insert(f.user_id.clone()); }) + } + + Ok(set) + } } \ No newline at end of file diff --git a/src/data/user.rs b/src/data/user.rs index 9be0145..882a220 100644 --- a/src/data/user.rs +++ b/src/data/user.rs @@ -1,3 +1,5 @@ +use std::hash::{Hash, Hasher}; + use crate::utils::user_data_utils::user_data_url; ///! User information @@ -36,6 +38,12 @@ impl UserID { } } +impl Hash for UserID { + fn hash(&self, state: &mut H) { + self.0.hash(state) + } +} + #[derive(Debug, PartialEq)] pub enum UserPageStatus { OPEN,