kordophone: add support for /messages
This commit is contained in:
@@ -5,7 +5,7 @@ use kordophone::api::http_client::Credentials;
|
||||
use dotenv;
|
||||
use anyhow::Result;
|
||||
use clap::Subcommand;
|
||||
use crate::printers::ConversationPrinter;
|
||||
use crate::printers::{ConversationPrinter, MessagePrinter};
|
||||
|
||||
pub fn make_api_client_from_env() -> HTTPAPIClient {
|
||||
dotenv::dotenv().ok();
|
||||
@@ -30,6 +30,11 @@ pub enum Commands {
|
||||
/// Prints all known conversations on the server.
|
||||
Conversations,
|
||||
|
||||
/// Prints all messages in a conversation.
|
||||
Messages {
|
||||
conversation_id: String,
|
||||
},
|
||||
|
||||
/// Prints the server Kordophone version.
|
||||
Version,
|
||||
}
|
||||
@@ -40,6 +45,7 @@ impl Commands {
|
||||
match cmd {
|
||||
Commands::Version => client.print_version().await,
|
||||
Commands::Conversations => client.print_conversations().await,
|
||||
Commands::Messages { conversation_id } => client.print_messages(conversation_id).await,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -68,6 +74,14 @@ impl ClientCli {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn print_messages(&mut self, conversation_id: String) -> Result<()> {
|
||||
let messages = self.api.get_messages(&conversation_id).await?;
|
||||
for message in messages {
|
||||
println!("{}", MessagePrinter::new(&message.into()));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -37,6 +37,24 @@ impl From<kordophone_db::models::Conversation> for PrintableConversation {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct PrintableMessage {
|
||||
pub guid: String,
|
||||
pub date: OffsetDateTime,
|
||||
pub sender: String,
|
||||
pub text: String,
|
||||
}
|
||||
|
||||
impl From<kordophone::model::Message> for PrintableMessage {
|
||||
fn from(value: kordophone::model::Message) -> Self {
|
||||
Self {
|
||||
guid: value.guid,
|
||||
date: value.date,
|
||||
sender: value.sender.unwrap_or("<me>".to_string()),
|
||||
text: value.text,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ConversationPrinter<'a> {
|
||||
doc: RcDoc<'a, PrintableConversation>
|
||||
}
|
||||
@@ -56,6 +74,9 @@ impl<'a> ConversationPrinter<'a> {
|
||||
.append(RcDoc::line())
|
||||
.append("Date: ")
|
||||
.append(conversation.date.to_string())
|
||||
.append(RcDoc::line())
|
||||
.append("Unread Count: ")
|
||||
.append(conversation.unread_count.to_string())
|
||||
.append(RcDoc::line())
|
||||
.append("Participants: ")
|
||||
.append("[")
|
||||
@@ -89,4 +110,36 @@ impl<'a> Display for ConversationPrinter<'a> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
self.doc.render_fmt(180, f)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct MessagePrinter<'a> {
|
||||
doc: RcDoc<'a, PrintableMessage>
|
||||
}
|
||||
|
||||
impl<'a> Display for MessagePrinter<'a> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
self.doc.render_fmt(180, f)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> MessagePrinter<'a> {
|
||||
pub fn new(message: &'a PrintableMessage) -> Self {
|
||||
let doc = RcDoc::text(format!("<Message: \"{}\"", &message.guid))
|
||||
.append(
|
||||
RcDoc::line()
|
||||
.append("Date: ")
|
||||
.append(message.date.to_string())
|
||||
.append(RcDoc::line())
|
||||
.append("Sender: ")
|
||||
.append(&message.sender)
|
||||
.append(RcDoc::line())
|
||||
.append("Body: ")
|
||||
.append(&message.text)
|
||||
.nest(4)
|
||||
)
|
||||
.append(RcDoc::line())
|
||||
.append(">");
|
||||
|
||||
MessagePrinter { doc }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user