daemon: add support for getting messages from db
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
use anyhow::Result;
|
||||
use clap::Subcommand;
|
||||
use dbus::blocking::{Connection, Proxy};
|
||||
use prettytable::table;
|
||||
use crate::printers::{ConversationPrinter, MessagePrinter};
|
||||
use std::future;
|
||||
|
||||
const DBUS_NAME: &str = "net.buzzert.kordophonecd";
|
||||
const DBUS_PATH: &str = "/net/buzzert/kordophonecd/daemon";
|
||||
@@ -34,6 +34,12 @@ pub enum Commands {
|
||||
|
||||
/// Waits for signals from the daemon.
|
||||
Signals,
|
||||
|
||||
/// Prints the messages for a conversation.
|
||||
Messages {
|
||||
conversation_id: String,
|
||||
last_message_id: Option<String>,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Subcommand)]
|
||||
@@ -66,6 +72,7 @@ impl Commands {
|
||||
Commands::Sync => client.sync_conversations().await,
|
||||
Commands::Config { command } => client.config(command).await,
|
||||
Commands::Signals => client.wait_for_signals().await,
|
||||
Commands::Messages { conversation_id, last_message_id } => client.print_messages(conversation_id, last_message_id).await,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -107,6 +114,17 @@ impl DaemonCli {
|
||||
.map_err(|e| anyhow::anyhow!("Failed to sync conversations: {}", e))
|
||||
}
|
||||
|
||||
pub async fn print_messages(&mut self, conversation_id: String, last_message_id: Option<String>) -> Result<()> {
|
||||
let messages = KordophoneRepository::get_messages(&self.proxy(), &conversation_id, &last_message_id.unwrap_or_default())?;
|
||||
println!("Number of messages: {}", messages.len());
|
||||
|
||||
for message in messages {
|
||||
println!("{}", MessagePrinter::new(&message.into()));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn wait_for_signals(&mut self) -> Result<()> {
|
||||
use dbus::Message;
|
||||
mod dbus_signals {
|
||||
@@ -136,13 +154,17 @@ impl DaemonCli {
|
||||
}
|
||||
|
||||
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())?;
|
||||
let server_url = KordophoneSettings::server_url(&self.proxy()).unwrap_or_default();
|
||||
let username = KordophoneSettings::username(&self.proxy()).unwrap_or_default();
|
||||
let credential_item = KordophoneSettings::credential_item(&self.proxy()).unwrap_or_default();
|
||||
|
||||
let table = table!(
|
||||
[ b->"Server URL", &server_url ],
|
||||
[ b->"Username", &username ],
|
||||
[ b->"Credential Item", &credential_item ]
|
||||
);
|
||||
table.printstd();
|
||||
|
||||
println!("Server URL: {}", server_url);
|
||||
println!("Username: {}", username);
|
||||
println!("Credential Item: {}", credential_item);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -86,6 +86,17 @@ impl From<kordophone_db::models::Message> for PrintableMessage {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<arg::PropMap> for PrintableMessage {
|
||||
fn from(value: arg::PropMap) -> Self {
|
||||
Self {
|
||||
guid: value.get("id").unwrap().as_str().unwrap().to_string(),
|
||||
date: OffsetDateTime::from_unix_timestamp(value.get("date").unwrap().as_i64().unwrap()).unwrap(),
|
||||
sender: value.get("sender").unwrap().as_str().unwrap().to_string(),
|
||||
text: value.get("text").unwrap().as_str().unwrap().to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ConversationPrinter<'a> {
|
||||
doc: RcDoc<'a, PrintableConversation>
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user