implements settings, conversation dbus encoding
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
use anyhow::Result;
|
||||
use clap::Subcommand;
|
||||
use dbus::blocking::{Connection, Proxy};
|
||||
use crate::printers::{ConversationPrinter, MessagePrinter};
|
||||
|
||||
const DBUS_NAME: &str = "net.buzzert.kordophonecd";
|
||||
const DBUS_PATH: &str = "/net/buzzert/kordophonecd/daemon";
|
||||
@@ -11,6 +12,7 @@ mod dbus_interface {
|
||||
}
|
||||
|
||||
use dbus_interface::NetBuzzertKordophoneRepository as KordophoneRepository;
|
||||
use dbus_interface::NetBuzzertKordophoneSettings as KordophoneSettings;
|
||||
|
||||
#[derive(Subcommand)]
|
||||
pub enum Commands {
|
||||
@@ -22,6 +24,33 @@ pub enum Commands {
|
||||
|
||||
/// Prints the server Kordophone version.
|
||||
Version,
|
||||
|
||||
/// Configuration options
|
||||
Config {
|
||||
#[command(subcommand)]
|
||||
command: ConfigCommands,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Subcommand)]
|
||||
pub enum ConfigCommands {
|
||||
/// Prints the current settings.
|
||||
Print,
|
||||
|
||||
/// Sets the server URL.
|
||||
SetServerUrl {
|
||||
url: String,
|
||||
},
|
||||
|
||||
/// Sets the username.
|
||||
SetUsername {
|
||||
username: String,
|
||||
},
|
||||
|
||||
/// Sets the credential item.
|
||||
SetCredentialItem {
|
||||
item: String,
|
||||
},
|
||||
}
|
||||
|
||||
impl Commands {
|
||||
@@ -31,6 +60,7 @@ impl Commands {
|
||||
Commands::Version => client.print_version().await,
|
||||
Commands::Conversations => client.print_conversations().await,
|
||||
Commands::Sync => client.sync_conversations().await,
|
||||
Commands::Config { command } => client.config(command).await,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -58,13 +88,53 @@ impl DaemonCli {
|
||||
|
||||
pub async fn print_conversations(&mut self) -> Result<()> {
|
||||
let conversations = KordophoneRepository::get_conversations(&self.proxy())?;
|
||||
println!("Conversations: {:?}", conversations);
|
||||
println!("Number of conversations: {}", conversations.len());
|
||||
|
||||
for conversation in conversations {
|
||||
println!("{}", ConversationPrinter::new(&conversation.into()));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn sync_conversations(&mut self) -> Result<()> {
|
||||
let success = KordophoneRepository::sync_all_conversations(&self.proxy())?;
|
||||
println!("Initiated sync");
|
||||
KordophoneRepository::sync_all_conversations(&self.proxy())
|
||||
.map_err(|e| anyhow::anyhow!("Failed to sync conversations: {}", e))
|
||||
}
|
||||
|
||||
pub async fn config(&mut self, cmd: ConfigCommands) -> Result<()> {
|
||||
match cmd {
|
||||
ConfigCommands::Print => self.print_settings().await,
|
||||
ConfigCommands::SetServerUrl { url } => self.set_server_url(url).await,
|
||||
ConfigCommands::SetUsername { username } => self.set_username(username).await,
|
||||
ConfigCommands::SetCredentialItem { item } => self.set_credential_item(item).await,
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn print_settings(&mut self) -> Result<()> {
|
||||
let server_url = KordophoneSettings::server_url(&self.proxy())?;
|
||||
let username = KordophoneSettings::username(&self.proxy())?;
|
||||
let credential_item = KordophoneSettings::credential_item(&self.proxy())?;
|
||||
|
||||
println!("Server URL: {}", server_url);
|
||||
println!("Username: {}", username);
|
||||
println!("Credential Item: {}", credential_item);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn set_server_url(&mut self, url: String) -> Result<()> {
|
||||
KordophoneSettings::set_server_url(&self.proxy(), url)
|
||||
.map_err(|e| anyhow::anyhow!("Failed to set server URL: {}", e))
|
||||
}
|
||||
|
||||
pub async fn set_username(&mut self, username: String) -> Result<()> {
|
||||
KordophoneSettings::set_username(&self.proxy(), username)
|
||||
.map_err(|e| anyhow::anyhow!("Failed to set username: {}", e))
|
||||
}
|
||||
|
||||
pub async fn set_credential_item(&mut self, item: String) -> Result<()> {
|
||||
KordophoneSettings::set_credential_item(&self.proxy(), item.into())
|
||||
.map_err(|e| anyhow::anyhow!("Failed to set credential item: {}", e))
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
use std::fmt::Display;
|
||||
use time::OffsetDateTime;
|
||||
use pretty::RcDoc;
|
||||
use dbus::arg::{self, RefArg};
|
||||
|
||||
pub struct PrintableConversation {
|
||||
pub guid: String,
|
||||
@@ -37,6 +38,25 @@ impl From<kordophone_db::models::Conversation> for PrintableConversation {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<arg::PropMap> for PrintableConversation {
|
||||
fn from(value: arg::PropMap) -> Self {
|
||||
Self {
|
||||
guid: value.get("guid").unwrap().as_str().unwrap().to_string(),
|
||||
date: OffsetDateTime::from_unix_timestamp(value.get("date").unwrap().as_i64().unwrap()).unwrap(),
|
||||
unread_count: value.get("unread_count").unwrap().as_i64().unwrap().try_into().unwrap(),
|
||||
last_message_preview: value.get("last_message_preview").unwrap().as_str().map(|s| s.to_string()),
|
||||
participants: value.get("participants")
|
||||
.unwrap()
|
||||
.0
|
||||
.as_iter()
|
||||
.unwrap()
|
||||
.map(|s| s.as_str().unwrap().to_string())
|
||||
.collect(),
|
||||
display_name: value.get("display_name").unwrap().as_str().map(|s| s.to_string()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct PrintableMessage {
|
||||
pub guid: String,
|
||||
pub date: OffsetDateTime,
|
||||
|
||||
Reference in New Issue
Block a user