1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-30 09:16:28 +00:00
comunicapiv3/src/api_data/account_export_api.rs

69 lines
2.7 KiB
Rust
Raw Normal View History

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 06:30:24 +00:00
use crate::api_data::friend_api::FriendAPI;
use crate::api_data::group_api::GroupApi;
2020-07-14 05:48:05 +00:00
use crate::api_data::movie_api::MovieAPI;
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;
use crate::helpers::groups_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:48:05 +00:00
movies: Vec<MovieAPI>,
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-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:48:05 +00:00
movies: MovieAPI::for_list(&export.movies),
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()
.map(|r| (r.0.clone(), ConversationMessageAPI::for_list(r.1)))
.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-13 17:12:39 +00:00
};
Ok(export)
}
}