1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-22 13:29:21 +00:00

Export related user information

This commit is contained in:
Pierre HUBERT 2020-07-14 09:10:10 +02:00
parent 36f879f23f
commit 5405aa5420
3 changed files with 65 additions and 3 deletions

View File

@ -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<u64, Vec<ConversationMessageAPI>>,
friends_list: Vec<FriendAPI>,
groups: Vec<GroupApi>,
users_info: HashMap<u64, APIUserInfo>,
}
impl AccountExportAPI {
@ -62,6 +65,16 @@ impl AccountExportAPI {
.iter()
.map(|g| GroupApi::new(&g, curr_user_id.as_option()))
.collect::<Result<Vec<GroupApi>, _>>()?,
users_info: export.get_related_users_ids()?
.iter()
.map(|u| user_helper::find_user_by_id(u))
.collect::<Result<Vec<User>, _>>()?
.iter()
.map(|u| {
APIUserInfo::new(&curr_user_id.as_option(), u)
.map(|r| (u.id.id(), r))
})
.collect::<Result<HashMap<u64, APIUserInfo>, _>>()?,
};
Ok(export)

View File

@ -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<u64, Vec<ConversationMessage>>,
pub friends_list: Vec<Friend>,
pub groups: Vec<GroupID>,
}
impl AccountExport {
/// Get the IDs of the related users
pub fn get_related_users_ids(&self) -> ResultBoxError<HashSet<UserID>> {
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)
}
}

View File

@ -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<H: Hasher>(&self, state: &mut H) {
self.0.hash(state)
}
}
#[derive(Debug, PartialEq)]
pub enum UserPageStatus {
OPEN,