2020-07-13 17:12:39 +00:00
|
|
|
//! # Export account API entry
|
|
|
|
//!
|
|
|
|
//! @author Pierre Hubert
|
2020-07-14 06:07:55 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2020-07-13 17:12:39 +00:00
|
|
|
use serde::Serialize;
|
|
|
|
|
|
|
|
use crate::api_data::comment_api::CommentAPI;
|
2020-07-14 06:07:55 +00:00
|
|
|
use crate::api_data::conversation_api::ConversationAPI;
|
2020-07-14 05:54:40 +00:00
|
|
|
use crate::api_data::conversation_message_api::ConversationMessageAPI;
|
2020-07-14 08:08:30 +00:00
|
|
|
use crate::api_data::entities_constructor::EntitiesConstructor;
|
2020-07-14 06:30:24 +00:00
|
|
|
use crate::api_data::friend_api::FriendAPI;
|
|
|
|
use crate::api_data::group_api::GroupApi;
|
2020-07-13 17:12:39 +00:00
|
|
|
use crate::api_data::post_api::PostAPI;
|
2020-07-13 17:38:51 +00:00
|
|
|
use crate::api_data::survey_response_api::SurveyResponseAPI;
|
2020-07-13 17:12:39 +00:00
|
|
|
use crate::api_data::user_info::APIUserInfo;
|
2020-07-13 17:26:19 +00:00
|
|
|
use crate::api_data::user_like_api::UserLikeAPI;
|
2020-07-13 17:12:39 +00:00
|
|
|
use crate::data::account_export::AccountExport;
|
|
|
|
use crate::data::error::ResultBoxError;
|
2020-07-14 06:30:24 +00:00
|
|
|
use crate::data::group::Group;
|
2020-07-14 07:10:10 +00:00
|
|
|
use crate::data::user::User;
|
2020-07-14 06:30:24 +00:00
|
|
|
use crate::helpers::groups_helper;
|
2020-07-14 07:10:10 +00:00
|
|
|
use crate::helpers::user_helper;
|
2020-07-13 17:12:39 +00:00
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
pub struct AccountExportAPI {
|
|
|
|
userID: u64,
|
|
|
|
advanced_info: APIUserInfo,
|
|
|
|
posts: Vec<PostAPI>,
|
|
|
|
comments: Vec<CommentAPI>,
|
2020-07-13 17:26:19 +00:00
|
|
|
likes: Vec<UserLikeAPI>,
|
2020-07-13 17:38:51 +00:00
|
|
|
survey_responses: Vec<SurveyResponseAPI>,
|
2020-07-14 05:54:40 +00:00
|
|
|
all_conversation_messages: Vec<ConversationMessageAPI>,
|
2020-07-14 06:07:55 +00:00
|
|
|
conversations_list: Vec<ConversationAPI>,
|
|
|
|
conversations_messages: HashMap<u64, Vec<ConversationMessageAPI>>,
|
2020-07-14 06:30:24 +00:00
|
|
|
friends_list: Vec<FriendAPI>,
|
|
|
|
groups: Vec<GroupApi>,
|
2020-07-14 07:10:10 +00:00
|
|
|
users_info: HashMap<u64, APIUserInfo>,
|
2020-07-13 17:12:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AccountExportAPI {
|
|
|
|
pub fn new(export: &AccountExport) -> ResultBoxError<AccountExportAPI> {
|
|
|
|
let curr_user_id = &export.user.id;
|
|
|
|
let export = AccountExportAPI {
|
|
|
|
userID: curr_user_id.id(),
|
|
|
|
advanced_info: APIUserInfo::new_advanced_info(&curr_user_id.as_option(), &export.user)?,
|
|
|
|
posts: PostAPI::for_list(&export.posts, curr_user_id.as_option())?,
|
|
|
|
comments: CommentAPI::for_list(&export.comments, &curr_user_id.as_option())?,
|
2020-07-13 17:26:19 +00:00
|
|
|
likes: UserLikeAPI::for_list(&export.likes),
|
2020-07-13 17:38:51 +00:00
|
|
|
survey_responses: SurveyResponseAPI::for_list(&export.survey_responses),
|
2020-07-14 05:54:40 +00:00
|
|
|
all_conversation_messages: ConversationMessageAPI::for_list(&export.all_conversation_messages),
|
2020-07-14 06:07:55 +00:00
|
|
|
conversations_list: ConversationAPI::for_list(&export.conversations),
|
|
|
|
conversations_messages: export.conversation_messages
|
|
|
|
.iter()
|
2021-03-04 17:51:52 +00:00
|
|
|
.map(|r| (r.0.id(), ConversationMessageAPI::for_list(r.1)))
|
2020-07-14 06:07:55 +00:00
|
|
|
.collect(),
|
2020-07-14 06:30:24 +00:00
|
|
|
friends_list: FriendAPI::from_list(&export.friends_list),
|
|
|
|
groups: export.groups
|
|
|
|
.iter()
|
|
|
|
.map(|g| groups_helper::get_info(g))
|
|
|
|
.collect::<Result<Vec<Group>, _>>()?
|
|
|
|
.iter()
|
|
|
|
.map(|g| GroupApi::new(&g, curr_user_id.as_option()))
|
|
|
|
.collect::<Result<Vec<GroupApi>, _>>()?,
|
2020-07-14 07:10:10 +00:00
|
|
|
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>, _>>()?,
|
2020-07-13 17:12:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok(export)
|
|
|
|
}
|
|
|
|
}
|