daemon: copy audit, cleanup
This commit is contained in:
@@ -20,7 +20,6 @@ use async_trait::async_trait;
|
|||||||
use kordophone_db::{
|
use kordophone_db::{
|
||||||
database::{Database, DatabaseAccess},
|
database::{Database, DatabaseAccess},
|
||||||
models::Conversation,
|
models::Conversation,
|
||||||
repository::Repository,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
use kordophone::model::JwtToken;
|
use kordophone::model::JwtToken;
|
||||||
@@ -113,10 +112,10 @@ impl Daemon {
|
|||||||
},
|
},
|
||||||
|
|
||||||
Event::SyncAllConversations(reply) => {
|
Event::SyncAllConversations(reply) => {
|
||||||
let db_clone = self.database.clone();
|
let mut db_clone = self.database.clone();
|
||||||
let signal_sender = self.signal_sender.clone();
|
let signal_sender = self.signal_sender.clone();
|
||||||
self.runtime.spawn(async move {
|
self.runtime.spawn(async move {
|
||||||
let result = Self::sync_all_conversations_impl(db_clone, signal_sender).await;
|
let result = Self::sync_all_conversations_impl(&mut db_clone, &signal_sender).await;
|
||||||
if let Err(e) = result {
|
if let Err(e) = result {
|
||||||
log::error!("Error handling sync event: {}", e);
|
log::error!("Error handling sync event: {}", e);
|
||||||
}
|
}
|
||||||
@@ -142,7 +141,7 @@ impl Daemon {
|
|||||||
},
|
},
|
||||||
|
|
||||||
Event::UpdateSettings(settings, reply) => {
|
Event::UpdateSettings(settings, reply) => {
|
||||||
self.update_settings(settings).await
|
self.update_settings(&settings).await
|
||||||
.unwrap_or_else(|e| {
|
.unwrap_or_else(|e| {
|
||||||
log::error!("Failed to update settings: {}", e);
|
log::error!("Failed to update settings: {}", e);
|
||||||
});
|
});
|
||||||
@@ -161,10 +160,10 @@ impl Daemon {
|
|||||||
self.database.lock().await.with_repository(|r| r.all_conversations().unwrap()).await
|
self.database.lock().await.with_repository(|r| r.all_conversations().unwrap()).await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn sync_all_conversations_impl(mut database: Arc<Mutex<Database>>, signal_sender: Sender<Signal>) -> Result<()> {
|
async fn sync_all_conversations_impl(database: &mut Arc<Mutex<Database>>, signal_sender: &Sender<Signal>) -> Result<()> {
|
||||||
log::info!(target: target::SYNC, "Starting conversation sync");
|
log::info!(target: target::SYNC, "Starting conversation sync");
|
||||||
|
|
||||||
let mut client = Self::get_client_impl(database.clone()).await?;
|
let mut client = Self::get_client_impl(database).await?;
|
||||||
|
|
||||||
// Fetch conversations from server
|
// Fetch conversations from server
|
||||||
let fetched_conversations = client.get_conversations().await?;
|
let fetched_conversations = client.get_conversations().await?;
|
||||||
@@ -181,14 +180,14 @@ impl Daemon {
|
|||||||
database.with_repository(|r| r.insert_conversation(conversation)).await?;
|
database.with_repository(|r| r.insert_conversation(conversation)).await?;
|
||||||
|
|
||||||
// Fetch and sync messages for this conversation
|
// Fetch and sync messages for this conversation
|
||||||
log::info!(target: target::SYNC, "Fetching messages for conversation {}", conversation_id);
|
log::debug!(target: target::SYNC, "Fetching messages for conversation {}", conversation_id);
|
||||||
let messages = client.get_messages(&conversation_id).await?;
|
let messages = client.get_messages(&conversation_id).await?;
|
||||||
let db_messages: Vec<kordophone_db::models::Message> = messages.into_iter()
|
let db_messages: Vec<kordophone_db::models::Message> = messages.into_iter()
|
||||||
.map(|m| kordophone_db::models::Message::from(m))
|
.map(|m| kordophone_db::models::Message::from(m))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
// Insert each message
|
// Insert each message
|
||||||
log::info!(target: target::SYNC, "Inserting {} messages for conversation {}", db_messages.len(), conversation_id);
|
log::debug!(target: target::SYNC, "Inserting {} messages for conversation {}", db_messages.len(), conversation_id);
|
||||||
database.with_repository(|r| r.insert_messages(&conversation_id, db_messages)).await?;
|
database.with_repository(|r| r.insert_messages(&conversation_id, db_messages)).await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -207,15 +206,15 @@ impl Daemon {
|
|||||||
Ok(settings)
|
Ok(settings)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn update_settings(&mut self, settings: Settings) -> Result<()> {
|
async fn update_settings(&mut self, settings: &Settings) -> Result<()> {
|
||||||
self.database.with_settings(|s| settings.save(s)).await
|
self.database.with_settings(|s| settings.save(s)).await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_client(&mut self) -> Result<HTTPAPIClient<DatabaseTokenStore>> {
|
async fn get_client(&mut self) -> Result<HTTPAPIClient<DatabaseTokenStore>> {
|
||||||
Self::get_client_impl(self.database.clone()).await
|
Self::get_client_impl(&mut self.database).await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_client_impl(mut database: Arc<Mutex<Database>>) -> Result<HTTPAPIClient<DatabaseTokenStore>> {
|
async fn get_client_impl(database: &mut Arc<Mutex<Database>>) -> Result<HTTPAPIClient<DatabaseTokenStore>> {
|
||||||
let settings = database.with_settings(|s|
|
let settings = database.with_settings(|s|
|
||||||
Settings::from_db(s)
|
Settings::from_db(s)
|
||||||
).await?;
|
).await?;
|
||||||
|
|||||||
Reference in New Issue
Block a user