restructure code

This commit is contained in:
Simon Laux
2020-11-26 03:06:36 +01:00
parent 850f7e1174
commit b87ca6e747

View File

@@ -20,9 +20,9 @@
//! ``` //! ```
//! [`SaveMimeHeaders`]: ../config/enum.Config.html#variant.SaveMimeHeaders //! [`SaveMimeHeaders`]: ../config/enum.Config.html#variant.SaveMimeHeaders
use crate::constants::DC_GCM_ADDDAYMARKER;
use crate::chat::*; use crate::chat::*;
use crate::constants::Viewtype; use crate::constants::Viewtype;
use crate::constants::DC_GCM_ADDDAYMARKER;
use crate::contact::*; use crate::contact::*;
use crate::context::Context; use crate::context::Context;
// use crate::error::Error; // use crate::error::Error;
@@ -35,7 +35,6 @@ use std::path::Path;
use zip::write::FileOptions; use zip::write::FileOptions;
use crate::location::Location; use crate::location::Location;
use futures::future::join_all;
use serde::Serialize; use serde::Serialize;
#[derive(Debug)] #[derive(Debug)]
@@ -191,31 +190,36 @@ impl ChatItemJSON {
async fn export_chat_data(context: &Context, chat_id: ChatId) -> ExportChatResult { async fn export_chat_data(context: &Context, chat_id: ChatId) -> ExportChatResult {
let mut blobs = Vec::new(); let mut blobs = Vec::new();
let mut chat_author_ids = Vec::new(); let mut chat_author_ids = Vec::new();
// get all messages // message_ids var is used for writing message info to files
let chat_items = get_chat_msgs(context, chat_id, DC_GCM_ADDDAYMARKER, None).await; let mut message_ids: Vec<MsgId> = Vec::new();
let message_futures = chat_items let mut message_json: Vec<ChatItemJSON> = Vec::new();
.clone()
.into_iter() for item in get_chat_msgs(context, chat_id, DC_GCM_ADDDAYMARKER, None).await {
.map(|chat_item| match chat_item { if let Some(json_item) = match item {
ChatItem::Message { msg_id } => msg_id, ChatItem::Message { msg_id } => match Message::load_from_db(context, msg_id).await {
_ => MsgId::new_unset(), Ok(message) => {
}) let filename = message.get_filename();
.filter(|msg_id| !msg_id.is_unset()) if let Some(file) = filename {
.map(|msg_id| Message::load_from_db(context, msg_id)) // push referenced blobs (attachments)
.collect::<Vec<_>>(); blobs.push(file);
let messages: Vec<std::result::Result<Message, anyhow::Error>> = }
join_all(message_futures).await; message_ids.push(message.id);
// push all referenced blobs and populate contactid list // populate contactid list
for message in &messages { chat_author_ids.push(message.from_id);
if let Ok(msg) = &message { Some(ChatItemJSON::from_message(&message, &context).await)
let filename = msg.get_filename(); }
if let Some(file) = filename { Err(error_message) => Some(ChatItemJSON::MessageError {
// push referenced blobs (attachments) id: msg_id.to_u32(),
blobs.push(file); error: error_message.to_string(),
} }),
chat_author_ids.push(msg.from_id); },
ChatItem::DayMarker { timestamp } => Some(ChatItemJSON::DayMarker { timestamp }),
ChatItem::Marker1 => None,
} {
message_json.push(json_item)
} }
} }
// deduplicate contact list and load the contacts // deduplicate contact list and load the contacts
chat_author_ids.sort(); chat_author_ids.sort();
chat_author_ids.dedup(); chat_author_ids.dedup();
@@ -277,33 +281,6 @@ async fn export_chat_data(context: &Context, chat_id: ChatId) -> ExportChatResul
None => None, None => None,
}; };
let mut message_ids: Vec<MsgId> = Vec::new();
for message in &messages {
if let Ok(msg) = &message {
message_ids.push(msg.id);
}
}
drop(messages);
let mut message_json: Vec<ChatItemJSON> = Vec::new();
for item in chat_items {
let json_item_result = match item {
ChatItem::Message { msg_id } => match Message::load_from_db(context, msg_id).await {
Ok(message) => Some(ChatItemJSON::from_message(&message, &context).await),
Err(error_message) => Some(ChatItemJSON::MessageError {
id: msg_id.to_u32(),
error: error_message.to_string(),
}),
},
ChatItem::DayMarker { timestamp } => Some(ChatItemJSON::DayMarker { timestamp }),
ChatItem::Marker1 => None,
};
if let Some(json_item) = json_item_result {
message_json.push(json_item)
}
}
let chat_json = ChatJSON { let chat_json = ChatJSON {
chat_json_version: 1, chat_json_version: 1,
export_timestamp: time(), export_timestamp: time(),