use std::path::Path;
use anyhow::Result;
use config::Config;
use store::Store;
use transport::iroh::Endpoint;
use ipc::BrokerRequest;
use crate::commands::{broker, conn};
pub(crate) async fn run(
cfg: &Config,
endpoint: Option<&Endpoint>,
device: Option<&str>,
number: String,
message: String,
store: &Store,
config_path: Option<&Path>,
) -> Result<String> {
if endpoint.is_none() {
return match broker::call(cfg, device, config_path, BrokerRequest::Send { number, message })
.await?
{
ipc::BrokerResponse::Text(s) => Ok(s),
ipc::BrokerResponse::Failed(reason) => Err(anyhow::anyhow!("{reason}")),
ipc::BrokerResponse::Error(e) => Err(anyhow::anyhow!("{e}")),
other => Err(anyhow::anyhow!("unexpected broker response: {other:?}")),
};
}
let now = session::util::now_ms();
let mut client = conn::connect_map(cfg, endpoint, device).await?;
let result = session::outbox::send_sms(&mut client, store, &number, &message, now).await;
if let Err(e) = client.disconnect().await {
tracing::warn!("MAP disconnect failed: {e}");
}
result
}
pub(crate) async fn run_live(
cfg: &Config,
endpoint: Option<&Endpoint>,
device: Option<&str>,
number: String,
message: String,
config_path: Option<&Path>,
) -> Result<String> {
if endpoint.is_none() {
return match broker::call(
cfg,
device,
config_path,
BrokerRequest::SendLive { number, message },
)
.await?
{
ipc::BrokerResponse::Text(s) => Ok(s),
ipc::BrokerResponse::Failed(reason) => Err(anyhow::anyhow!("{reason}")),
ipc::BrokerResponse::Error(e) => Err(anyhow::anyhow!("{e}")),
other => Err(anyhow::anyhow!("unexpected broker response: {other:?}")),
};
}
let mut client = conn::connect_map(cfg, endpoint, device).await?;
let result = session::outbox::push_sms(&mut client, &number, &message).await;
if let Err(e) = client.disconnect().await {
tracing::warn!("MAP disconnect failed: {e}");
}
result
}