client: implements event/updates websocket
This commit is contained in:
@@ -11,6 +11,8 @@ clap = { version = "4.5.20", features = ["derive"] }
|
||||
dbus = "0.9.7"
|
||||
dbus-tree = "0.9.2"
|
||||
dotenv = "0.15.0"
|
||||
env_logger = "0.11.8"
|
||||
futures-util = "0.3.31"
|
||||
kordophone = { path = "../kordophone" }
|
||||
kordophone-db = { path = "../kordophone-db" }
|
||||
log = "0.4.22"
|
||||
|
||||
@@ -2,10 +2,14 @@ use kordophone::APIInterface;
|
||||
use kordophone::api::http_client::HTTPAPIClient;
|
||||
use kordophone::api::http_client::Credentials;
|
||||
use kordophone::api::InMemoryAuthenticationStore;
|
||||
use kordophone::api::event_socket::EventSocket;
|
||||
|
||||
use anyhow::Result;
|
||||
use clap::Subcommand;
|
||||
use crate::printers::{ConversationPrinter, MessagePrinter};
|
||||
use kordophone::model::event::Event;
|
||||
|
||||
use futures_util::StreamExt;
|
||||
|
||||
pub fn make_api_client_from_env() -> HTTPAPIClient<InMemoryAuthenticationStore> {
|
||||
dotenv::dotenv().ok();
|
||||
@@ -37,6 +41,12 @@ pub enum Commands {
|
||||
|
||||
/// Prints the server Kordophone version.
|
||||
Version,
|
||||
|
||||
/// Prints all events from the server.
|
||||
Events,
|
||||
|
||||
/// Prints all raw updates from the server.
|
||||
RawUpdates,
|
||||
}
|
||||
|
||||
impl Commands {
|
||||
@@ -46,6 +56,8 @@ impl Commands {
|
||||
Commands::Version => client.print_version().await,
|
||||
Commands::Conversations => client.print_conversations().await,
|
||||
Commands::Messages { conversation_id } => client.print_messages(conversation_id).await,
|
||||
Commands::RawUpdates => client.print_raw_updates().await,
|
||||
Commands::Events => client.print_events().await,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -82,6 +94,35 @@ impl ClientCli {
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn print_events(&mut self) -> Result<()> {
|
||||
let socket = self.api.open_event_socket().await?;
|
||||
|
||||
let mut stream = socket.events().await;
|
||||
while let Some(Ok(event)) = stream.next().await {
|
||||
match event {
|
||||
Event::ConversationChanged(conversation) => {
|
||||
println!("Conversation changed: {}", conversation.guid);
|
||||
}
|
||||
Event::MessageReceived(conversation, message) => {
|
||||
println!("Message received: msg: {} conversation: {}", message.guid, conversation.guid);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn print_raw_updates(&mut self) -> Result<()> {
|
||||
let socket = self.api.open_event_socket().await?;
|
||||
|
||||
println!("Listening for raw updates...");
|
||||
let mut stream = socket.raw_updates().await;
|
||||
while let Some(update) = stream.next().await {
|
||||
println!("Got update: {:?}", update);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ mod daemon;
|
||||
|
||||
use anyhow::Result;
|
||||
use clap::{Parser, Subcommand};
|
||||
use log::LevelFilter;
|
||||
|
||||
/// A command line interface for the Kordophone library and daemon
|
||||
#[derive(Parser)]
|
||||
@@ -43,8 +44,22 @@ async fn run_command(command: Commands) -> Result<()> {
|
||||
}
|
||||
}
|
||||
|
||||
fn initialize_logging() {
|
||||
// Weird: is this the best way to do this?
|
||||
let log_level = std::env::var("RUST_LOG")
|
||||
.map(|s| s.parse::<LevelFilter>().unwrap_or(LevelFilter::Info))
|
||||
.unwrap_or(LevelFilter::Info);
|
||||
|
||||
env_logger::Builder::from_default_env()
|
||||
.format_timestamp_secs()
|
||||
.filter_level(log_level)
|
||||
.init();
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
initialize_logging();
|
||||
|
||||
let cli = Cli::parse();
|
||||
|
||||
run_command(cli.command).await
|
||||
|
||||
Reference in New Issue
Block a user