kpcli: db: add support for printing messages table
This commit is contained in:
@@ -4,7 +4,7 @@ use kordophone::APIInterface;
|
|||||||
use std::{env, path::PathBuf};
|
use std::{env, path::PathBuf};
|
||||||
|
|
||||||
use kordophone_db::ChatDatabase;
|
use kordophone_db::ChatDatabase;
|
||||||
use crate::{client, printers::ConversationPrinter};
|
use crate::{client, printers::{ConversationPrinter, MessagePrinter}};
|
||||||
|
|
||||||
#[derive(Subcommand)]
|
#[derive(Subcommand)]
|
||||||
pub enum Commands {
|
pub enum Commands {
|
||||||
@@ -13,6 +13,12 @@ pub enum Commands {
|
|||||||
#[clap(subcommand)]
|
#[clap(subcommand)]
|
||||||
command: ConversationCommands
|
command: ConversationCommands
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/// For dealing with the table of cached messages.
|
||||||
|
Messages {
|
||||||
|
#[clap(subcommand)]
|
||||||
|
command: MessageCommands
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Subcommand)]
|
#[derive(Subcommand)]
|
||||||
@@ -24,6 +30,14 @@ pub enum ConversationCommands {
|
|||||||
Sync,
|
Sync,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Subcommand)]
|
||||||
|
pub enum MessageCommands {
|
||||||
|
/// Prints all messages in a conversation.
|
||||||
|
List {
|
||||||
|
conversation_id: String
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
impl Commands {
|
impl Commands {
|
||||||
pub async fn run(cmd: Commands) -> Result<()> {
|
pub async fn run(cmd: Commands) -> Result<()> {
|
||||||
let mut db = DbClient::new()?;
|
let mut db = DbClient::new()?;
|
||||||
@@ -32,6 +46,9 @@ impl Commands {
|
|||||||
ConversationCommands::List => db.print_conversations(),
|
ConversationCommands::List => db.print_conversations(),
|
||||||
ConversationCommands::Sync => db.sync_with_client().await,
|
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(())
|
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<()> {
|
pub async fn sync_with_client(&mut self) -> Result<()> {
|
||||||
let mut client = client::make_api_client_from_env();
|
let mut client = client::make_api_client_from_env();
|
||||||
let fetched_conversations = client.get_conversations().await?;
|
let fetched_conversations = client.get_conversations().await?;
|
||||||
@@ -75,7 +100,15 @@ impl DbClient {
|
|||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
for conversation in db_conversations {
|
for conversation in db_conversations {
|
||||||
|
let conversation_id = conversation.guid.clone();
|
||||||
self.db.insert_conversation(conversation)?;
|
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(())
|
Ok(())
|
||||||
|
|||||||
@@ -55,6 +55,17 @@ impl From<kordophone::model::Message> for PrintableMessage {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<kordophone_db::models::Message> 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> {
|
pub struct ConversationPrinter<'a> {
|
||||||
doc: RcDoc<'a, PrintableConversation>
|
doc: RcDoc<'a, PrintableConversation>
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user