daemon: implement solution for background sync
This commit is contained in:
@@ -3,7 +3,10 @@ use clap::Subcommand;
|
||||
use kordophone::APIInterface;
|
||||
use std::{env, path::PathBuf};
|
||||
|
||||
use kordophone_db::database::Database;
|
||||
use kordophone_db::{
|
||||
database::{Database, DatabaseAccess},
|
||||
models::{Conversation, Message},
|
||||
};
|
||||
use crate::{client, printers::{ConversationPrinter, MessagePrinter}};
|
||||
|
||||
#[derive(Subcommand)]
|
||||
@@ -72,16 +75,16 @@ impl Commands {
|
||||
let mut db = DbClient::new()?;
|
||||
match cmd {
|
||||
Commands::Conversations { command: cmd } => match cmd {
|
||||
ConversationCommands::List => db.print_conversations(),
|
||||
ConversationCommands::List => db.print_conversations().await,
|
||||
ConversationCommands::Sync => db.sync_with_client().await,
|
||||
},
|
||||
Commands::Messages { command: cmd } => match cmd {
|
||||
MessageCommands::List { conversation_id } => db.print_messages(&conversation_id).await,
|
||||
},
|
||||
Commands::Settings { command: cmd } => match cmd {
|
||||
SettingsCommands::Get { key } => db.get_setting(key),
|
||||
SettingsCommands::Put { key, value } => db.put_setting(key, value),
|
||||
SettingsCommands::Delete { key } => db.delete_setting(key),
|
||||
SettingsCommands::Get { key } => db.get_setting(key).await,
|
||||
SettingsCommands::Put { key, value } => db.put_setting(key, value).await,
|
||||
SettingsCommands::Delete { key } => db.delete_setting(key).await,
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -107,10 +110,10 @@ impl DbClient {
|
||||
Ok( Self { database: db })
|
||||
}
|
||||
|
||||
pub fn print_conversations(&mut self) -> Result<()> {
|
||||
pub async fn print_conversations(&mut self) -> Result<()> {
|
||||
let all_conversations = self.database.with_repository(|repository| {
|
||||
repository.all_conversations()
|
||||
})?;
|
||||
}).await?;
|
||||
|
||||
println!("{} Conversations: ", all_conversations.len());
|
||||
for conversation in all_conversations {
|
||||
@@ -123,7 +126,7 @@ impl DbClient {
|
||||
pub async fn print_messages(&mut self, conversation_id: &str) -> Result<()> {
|
||||
let messages = self.database.with_repository(|repository| {
|
||||
repository.get_messages_for_conversation(conversation_id)
|
||||
})?;
|
||||
}).await?;
|
||||
|
||||
for message in messages {
|
||||
println!("{}", MessagePrinter::new(&message.into()));
|
||||
@@ -145,7 +148,7 @@ impl DbClient {
|
||||
// Insert the conversation
|
||||
self.database.with_repository(|repository| {
|
||||
repository.insert_conversation(conversation)
|
||||
})?;
|
||||
}).await?;
|
||||
|
||||
// Fetch and sync messages for this conversation
|
||||
let messages = client.get_messages(&conversation_id).await?;
|
||||
@@ -160,13 +163,13 @@ impl DbClient {
|
||||
}
|
||||
|
||||
Ok(())
|
||||
})?;
|
||||
}).await?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get_setting(&mut self, key: Option<String>) -> Result<()> {
|
||||
pub async fn get_setting(&mut self, key: Option<String>) -> Result<()> {
|
||||
self.database.with_settings(|settings| {
|
||||
match key {
|
||||
Some(key) => {
|
||||
@@ -196,23 +199,23 @@ impl DbClient {
|
||||
}
|
||||
|
||||
Ok(())
|
||||
})
|
||||
}).await
|
||||
}
|
||||
|
||||
pub fn put_setting(&mut self, key: String, value: String) -> Result<()> {
|
||||
pub async fn put_setting(&mut self, key: String, value: String) -> Result<()> {
|
||||
self.database.with_settings(|settings| {
|
||||
settings.put(&key, &value)?;
|
||||
Ok(())
|
||||
})
|
||||
}).await
|
||||
}
|
||||
|
||||
pub fn delete_setting(&mut self, key: String) -> Result<()> {
|
||||
pub async fn delete_setting(&mut self, key: String) -> Result<()> {
|
||||
self.database.with_settings(|settings| {
|
||||
let count = settings.del(&key)?;
|
||||
if count == 0 {
|
||||
println!("Setting '{}' not found", key);
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
}).await
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user