Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
gferon committed Nov 14, 2023
1 parent e95bc30 commit dee0425
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 95 deletions.
1 change: 1 addition & 0 deletions presage-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ presage = { path = "../presage" }
presage-store-sled = { path = "../presage-store-sled" }

anyhow = "1.0"
axum = "0.6"
base64 = "0.12"
chrono = { version = "0.4", default-features = false, features = ["serde", "clock"] }
clap = { version = ">=4.2.4", features = ["derive"] }
Expand Down
79 changes: 61 additions & 18 deletions presage-cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use core::fmt;
use std::convert::TryInto;
use std::path::Path;
use std::path::PathBuf;
use std::time::Duration;
use std::time::UNIX_EPOCH;

use anyhow::{anyhow, bail, Context as _};
use axum::extract::Path;
use axum::routing::post;
use axum::Router;
use chrono::Local;
use clap::{ArgGroup, Parser, Subcommand};
use directories::ProjectDirs;
Expand Down Expand Up @@ -119,6 +121,9 @@ enum Cmd {
Receive {
#[clap(long = "notifications", short = 'n')]
notifications: bool,
/// Start a webserver to be able to send messages, useful for testing
#[clap(long)]
webserver: bool,
},
#[clap(about = "List groups")]
ListGroups,
Expand Down Expand Up @@ -181,7 +186,7 @@ fn parse_group_master_key(value: &str) -> anyhow::Result<GroupMasterKeyBytes> {
.map_err(|_| anyhow::format_err!("master key should be 32 bytes long"))
}

#[tokio::main(flavor = "multi_thread")]
#[tokio::main(flavor = "current_thread")]
async fn main() -> anyhow::Result<()> {
env_logger::from_env(
Env::default().default_filter_or(format!("{}=warn", env!("CARGO_PKG_NAME"))),
Expand Down Expand Up @@ -232,11 +237,30 @@ async fn send<C: Store + 'static>(
Ok(())
}

async fn process_incoming_messages<C: Store>(
mut manager: Manager<C, Registered<C>>,
attachments_tmp_dir: &std::path::Path,
notifications: bool,
) -> anyhow::Result<()> {
let messages = manager
.receive_messages(ReceivingMode::Forever)
.await
.context("failed to initialize messages stream")?;

pin_mut!(messages);

while let Some(content) = messages.next().await {
process_incoming_message(&mut manager, attachments_tmp_dir, notifications, &content).await;
}

Ok(())
}

// Note to developers, this is a good example of a function you can use as a source of inspiration
// to process incoming messages.
async fn process_incoming_message<C: Store>(
manager: &mut Manager<C, Registered<C>>,
attachments_tmp_dir: &Path,
attachments_tmp_dir: &std::path::Path,
notifications: bool,
content: &Content,
) {
Expand Down Expand Up @@ -423,29 +447,45 @@ fn print_message<C: Store>(
}

async fn receive<C: Store>(
manager: &mut Manager<C, Registered<C>>,
manager: Manager<C, Registered<C>>,
notifications: bool,
webserver: bool,
) -> anyhow::Result<()> {
let attachments_tmp_dir = Builder::new().prefix("presage-attachments").tempdir()?;
info!(
"attachments will be stored in {}",
attachments_tmp_dir.path().display()
);

let messages = manager
.receive_messages(ReceivingMode::Forever)
.await
.context("failed to initialize messages stream")?;
pin_mut!(messages);
if webserver {
let app = Router::new()
.with_state(manager)
.route("/message", post(web_send_message));

while let Some(content) = messages.next().await {
process_incoming_message(manager, attachments_tmp_dir.path(), notifications, &content)
.await;
// run our app with hyper, listening globally on port 3000
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
let webserver =
axum::Server::bind(&"0.0.0.0:3000".parse().unwrap()).serve(app.into_make_service());

future::join(
webserver,
process_incoming_messages(manager, attachments_tmp_dir.path(), notifications),
)
.await;
} else {
process_incoming_messages(manager, attachments_tmp_dir.path(), notifications).await;
}

Ok(())
}

async fn web_send_message<C: Store>(
manager: Manager<C, Registered<C>>,
Path(recipient): Path<Uuid>,
Path(message): Path<String>,
) {
}

async fn run<C: Store + 'static>(subcommand: Cmd, config_store: C) -> anyhow::Result<()> {
match subcommand {
Cmd::Register {
Expand Down Expand Up @@ -499,7 +539,7 @@ async fn run<C: Store + 'static>(subcommand: Cmd, config_store: C) -> anyhow::Re
.await;

match manager {
(Ok(manager), _) => {
(Ok(mut manager), _) => {
let uuid = manager.whoami().await.unwrap().uuid;
println!("{uuid:?}");
}
Expand All @@ -508,9 +548,12 @@ async fn run<C: Store + 'static>(subcommand: Cmd, config_store: C) -> anyhow::Re
}
}
}
Cmd::Receive { notifications } => {
let mut manager = Manager::load_registered(config_store).await?;
receive(&mut manager, notifications).await?;
Cmd::Receive {
notifications,
webserver,
} => {
let manager = Manager::load_registered(config_store).await?;
receive(manager, notifications, webserver).await?;
}
Cmd::Send { uuid, message } => {
let mut manager = Manager::load_registered(config_store).await?;
Expand Down Expand Up @@ -622,8 +665,8 @@ async fn run<C: Store + 'static>(subcommand: Cmd, config_store: C) -> anyhow::Re
}
}
Cmd::Whoami => {
let manager = Manager::load_registered(config_store).await?;
println!("{:?}", &manager.whoami().await?);
let mut manager = Manager::load_registered(config_store).await?;
println!("{:?}", manager.whoami().await?);
}
Cmd::GetContact { ref uuid } => {
let manager = Manager::load_registered(config_store).await?;
Expand Down
Loading

0 comments on commit dee0425

Please sign in to comment.