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

69 lines
2.1 KiB
Rust
Raw Normal View History

2020-07-13 17:12:39 +00:00
//! # Export of all account's data
//!
//! @author Pierre Hubert
2020-07-14 07:10:10 +00:00
use std::collections::{HashMap, HashSet};
2020-07-14 06:07:55 +00:00
2020-07-13 17:12:39 +00:00
use crate::data::comment::Comment;
2020-07-14 06:07:55 +00:00
use crate::data::conversation::Conversation;
2020-07-14 05:54:40 +00:00
use crate::data::conversation_message::ConversationMessage;
2020-07-14 07:10:10 +00:00
use crate::data::error::ResultBoxError;
2020-07-14 06:30:24 +00:00
use crate::data::friend::Friend;
use crate::data::group_id::GroupID;
2020-07-14 07:10:10 +00:00
use crate::data::post::{Post, PostPageKind};
2020-07-13 17:38:51 +00:00
use crate::data::survey_response::SurveyResponse;
2020-07-14 07:10:10 +00:00
use crate::data::user::{User, UserID};
2020-07-13 17:26:19 +00:00
use crate::data::user_like::UserLike;
2020-07-14 07:10:10 +00:00
use crate::helpers::comments_helper;
2020-07-13 17:12:39 +00:00
pub struct AccountExport {
pub user: User,
pub posts: Vec<Post>,
pub comments: Vec<Comment>,
2020-07-13 17:26:19 +00:00
pub likes: Vec<UserLike>,
2020-07-13 17:38:51 +00:00
pub survey_responses: Vec<SurveyResponse>,
2020-07-14 05:54:40 +00:00
pub all_conversation_messages: Vec<ConversationMessage>,
2020-07-14 06:07:55 +00:00
pub conversations: Vec<Conversation>,
pub conversation_messages: HashMap<u64, Vec<ConversationMessage>>,
2020-07-14 06:30:24 +00:00
pub friends_list: Vec<Friend>,
pub groups: Vec<GroupID>,
2020-07-14 07:10:10 +00:00
}
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)
}
2020-07-13 17:12:39 +00:00
}