From 16c202734ca04931d3e2846585e4bd1de3521d24 Mon Sep 17 00:00:00 2001 From: James Magahern Date: Mon, 20 Jan 2025 22:23:18 -0800 Subject: [PATCH] kpcli: db: add support for printing messages table --- kpcli/src/db/mod.rs | 35 ++++++++++++++++++++++++++++++++++- kpcli/src/printers.rs | 11 +++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/kpcli/src/db/mod.rs b/kpcli/src/db/mod.rs index 471f6a2..eed8002 100644 --- a/kpcli/src/db/mod.rs +++ b/kpcli/src/db/mod.rs @@ -4,7 +4,7 @@ use kordophone::APIInterface; use std::{env, path::PathBuf}; use kordophone_db::ChatDatabase; -use crate::{client, printers::ConversationPrinter}; +use crate::{client, printers::{ConversationPrinter, MessagePrinter}}; #[derive(Subcommand)] pub enum Commands { @@ -13,6 +13,12 @@ pub enum Commands { #[clap(subcommand)] command: ConversationCommands }, + + /// For dealing with the table of cached messages. + Messages { + #[clap(subcommand)] + command: MessageCommands + }, } #[derive(Subcommand)] @@ -24,6 +30,14 @@ pub enum ConversationCommands { Sync, } +#[derive(Subcommand)] +pub enum MessageCommands { + /// Prints all messages in a conversation. + List { + conversation_id: String + }, +} + impl Commands { pub async fn run(cmd: Commands) -> Result<()> { let mut db = DbClient::new()?; @@ -32,6 +46,9 @@ impl Commands { ConversationCommands::List => db.print_conversations(), ConversationCommands::Sync => db.sync_with_client().await, }, + Commands::Messages { command: cmd } => match cmd { + MessageCommands::List { conversation_id } => db.print_messages(&conversation_id).await, + }, } } } @@ -67,6 +84,14 @@ impl DbClient { Ok(()) } + pub async fn print_messages(&mut self, conversation_id: &str) -> Result<()> { + let messages = self.db.get_messages_for_conversation(conversation_id)?; + for message in messages { + println!("{}", MessagePrinter::new(&message.into())); + } + Ok(()) + } + pub async fn sync_with_client(&mut self) -> Result<()> { let mut client = client::make_api_client_from_env(); let fetched_conversations = client.get_conversations().await?; @@ -75,7 +100,15 @@ impl DbClient { .collect(); for conversation in db_conversations { + let conversation_id = conversation.guid.clone(); self.db.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)?; + } } Ok(()) diff --git a/kpcli/src/printers.rs b/kpcli/src/printers.rs index b00da37..81930d1 100644 --- a/kpcli/src/printers.rs +++ b/kpcli/src/printers.rs @@ -55,6 +55,17 @@ impl From for PrintableMessage { } } +impl From for PrintableMessage { + fn from(value: kordophone_db::models::Message) -> Self { + Self { + guid: value.id, + date: OffsetDateTime::from_unix_timestamp(value.date.and_utc().timestamp()).unwrap(), + sender: value.sender.display_name(), + text: value.text, + } + } +} + pub struct ConversationPrinter<'a> { doc: RcDoc<'a, PrintableConversation> }