reorg: split repo / database so settings can use db connection as well
This commit is contained in:
@@ -3,7 +3,7 @@ use clap::Subcommand;
|
||||
use dbus::blocking::{Connection, Proxy};
|
||||
|
||||
const DBUS_NAME: &str = "net.buzzert.kordophonecd";
|
||||
const DBUS_PATH: &str = "/net/buzzert/kordophone/Server";
|
||||
const DBUS_PATH: &str = "/net/buzzert/kordophonecd";
|
||||
|
||||
mod dbus_interface {
|
||||
#![allow(unused)]
|
||||
|
||||
@@ -3,7 +3,7 @@ use clap::Subcommand;
|
||||
use kordophone::APIInterface;
|
||||
use std::{env, path::PathBuf};
|
||||
|
||||
use kordophone_db::ChatDatabase;
|
||||
use kordophone_db::{database::Database, repository::Repository, settings};
|
||||
use crate::{client, printers::{ConversationPrinter, MessagePrinter}};
|
||||
|
||||
#[derive(Subcommand)]
|
||||
@@ -19,6 +19,12 @@ pub enum Commands {
|
||||
#[clap(subcommand)]
|
||||
command: MessageCommands
|
||||
},
|
||||
|
||||
/// For managing settings in the database.
|
||||
Settings {
|
||||
#[clap(subcommand)]
|
||||
command: SettingsCommands
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Subcommand)]
|
||||
@@ -38,6 +44,29 @@ pub enum MessageCommands {
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Subcommand)]
|
||||
pub enum SettingsCommands {
|
||||
/// Lists all settings or gets a specific setting.
|
||||
Get {
|
||||
/// The key to get. If not provided, all settings will be listed.
|
||||
key: Option<String>
|
||||
},
|
||||
|
||||
/// Sets a setting value.
|
||||
Put {
|
||||
/// The key to set.
|
||||
key: String,
|
||||
/// The value to set.
|
||||
value: String,
|
||||
},
|
||||
|
||||
/// Deletes a setting.
|
||||
Delete {
|
||||
/// The key to delete.
|
||||
key: String,
|
||||
},
|
||||
}
|
||||
|
||||
impl Commands {
|
||||
pub async fn run(cmd: Commands) -> Result<()> {
|
||||
let mut db = DbClient::new()?;
|
||||
@@ -49,12 +78,17 @@ impl Commands {
|
||||
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),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct DbClient {
|
||||
db: ChatDatabase
|
||||
database: Database
|
||||
}
|
||||
|
||||
impl DbClient {
|
||||
@@ -69,12 +103,12 @@ impl DbClient {
|
||||
|
||||
println!("kpcli: Using temporary db at {}", path_str);
|
||||
|
||||
let db = ChatDatabase::new(path_str)?;
|
||||
Ok( Self { db })
|
||||
let db = Database::new(path_str)?;
|
||||
Ok( Self { database: db })
|
||||
}
|
||||
|
||||
pub fn print_conversations(&mut self) -> Result<()> {
|
||||
let all_conversations = self.db.all_conversations()?;
|
||||
let all_conversations = self.database.get_repository().all_conversations()?;
|
||||
|
||||
println!("{} Conversations: ", all_conversations.len());
|
||||
for conversation in all_conversations {
|
||||
@@ -85,7 +119,7 @@ impl DbClient {
|
||||
}
|
||||
|
||||
pub async fn print_messages(&mut self, conversation_id: &str) -> Result<()> {
|
||||
let messages = self.db.get_messages_for_conversation(conversation_id)?;
|
||||
let messages = self.database.get_repository().get_messages_for_conversation(conversation_id)?;
|
||||
for message in messages {
|
||||
println!("{}", MessagePrinter::new(&message.into()));
|
||||
}
|
||||
@@ -99,18 +133,70 @@ impl DbClient {
|
||||
.map(|c| kordophone_db::models::Conversation::from(c))
|
||||
.collect();
|
||||
|
||||
let mut repository = self.database.get_repository();
|
||||
for conversation in db_conversations {
|
||||
let conversation_id = conversation.guid.clone();
|
||||
self.db.insert_conversation(conversation)?;
|
||||
repository.insert_conversation(conversation)?;
|
||||
|
||||
// Fetch and sync messages for this conversation
|
||||
let messages = client.get_messages(&conversation_id).await?;
|
||||
for message in messages {
|
||||
let db_message = kordophone_db::models::Message::from(message);
|
||||
self.db.insert_message(&conversation_id, db_message)?;
|
||||
let db_messages: Vec<kordophone_db::models::Message> = messages.into_iter()
|
||||
.map(|m| kordophone_db::models::Message::from(m))
|
||||
.collect();
|
||||
|
||||
for message in db_messages {
|
||||
repository.insert_message(&conversation_id, message)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get_setting(&mut self, key: Option<String>) -> Result<()> {
|
||||
let mut settings = self.database.get_settings();
|
||||
|
||||
match key {
|
||||
Some(key) => {
|
||||
// Get a specific setting
|
||||
let value: Option<String> = settings.get(&key)?;
|
||||
match value {
|
||||
Some(v) => println!("{} = {}", key, v),
|
||||
None => println!("Setting '{}' not found", key),
|
||||
}
|
||||
},
|
||||
None => {
|
||||
// List all settings
|
||||
let keys = settings.list_keys()?;
|
||||
if keys.is_empty() {
|
||||
println!("No settings found");
|
||||
} else {
|
||||
println!("Settings:");
|
||||
for key in keys {
|
||||
let value: Option<String> = settings.get(&key)?;
|
||||
match value {
|
||||
Some(v) => println!(" {} = {}", key, v),
|
||||
None => println!(" {} = <error reading value>", key),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn put_setting(&mut self, key: String, value: String) -> Result<()> {
|
||||
let mut settings = self.database.get_settings();
|
||||
settings.put(&key, &value)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn delete_setting(&mut self, key: String) -> Result<()> {
|
||||
let mut settings = self.database.get_settings();
|
||||
let count = settings.del(&key)?;
|
||||
if count == 0 {
|
||||
println!("Setting '{}' not found", key);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user