From a758ffcd6efcce2de701014ef9ceb4bb0d91a7b8 Mon Sep 17 00:00:00 2001 From: ranfdev Date: Thu, 21 Nov 2024 10:53:22 +0100 Subject: [PATCH] remove unused code, minor refactorings --- ntfy-daemon/src/http_client.rs | 13 +++++++ ntfy-daemon/src/lib.rs | 7 ++-- ntfy-daemon/src/listener.rs | 64 ++++++++++++++++----------------- ntfy-daemon/src/ntfy.rs | 15 ++++---- ntfy-daemon/src/subscription.rs | 4 +-- src/application.rs | 19 +++------- 6 files changed, 61 insertions(+), 61 deletions(-) diff --git a/ntfy-daemon/src/http_client.rs b/ntfy-daemon/src/http_client.rs index 2cd8c97..cd58ce8 100644 --- a/ntfy-daemon/src/http_client.rs +++ b/ntfy-daemon/src/http_client.rs @@ -34,6 +34,7 @@ impl RequestInfo { #[async_trait] trait LightHttpClient: Send + Sync { fn get(&self, url: &str) -> RequestBuilder; + fn post(&self, url: &str) -> RequestBuilder; async fn execute(&self, request: Request) -> Result; } @@ -43,6 +44,10 @@ impl LightHttpClient for Client { self.get(url) } + fn post(&self, url: &str) -> RequestBuilder { + self.post(url) + } + async fn execute(&self, request: Request) -> Result { Ok(self.execute(request).await?) } @@ -77,6 +82,10 @@ impl HttpClient { self.client.get(url) } + pub fn post(&self, url: &str) -> RequestBuilder { + self.client.post(url) + } + pub async fn execute(&self, request: Request) -> Result { self.request_tracker .push(RequestInfo::from_request(&request)) @@ -183,6 +192,10 @@ impl LightHttpClient for NullableClient { Client::new().get(url) } + fn post(&self, url: &str) -> RequestBuilder { + Client::new().post(url) + } + async fn execute(&self, request: Request) -> Result { time::sleep(Duration::from_millis(1)).await; let url = request.url().to_string(); diff --git a/ntfy-daemon/src/lib.rs b/ntfy-daemon/src/lib.rs index 7e4c83f..e1e834e 100644 --- a/ntfy-daemon/src/lib.rs +++ b/ntfy-daemon/src/lib.rs @@ -19,10 +19,9 @@ use http_client::HttpClient; #[derive(Clone)] pub struct SharedEnv { db: message_repo::Db, - proxy: Arc, - http: reqwest::Client, - nullable_http: HttpClient, - network: Arc, + notifier: Arc, + http_client: HttpClient, + network_monitor: Arc, credentials: credentials::Credentials, } diff --git a/ntfy-daemon/src/listener.rs b/ntfy-daemon/src/listener.rs index fa15fd7..68a37e6 100644 --- a/ntfy-daemon/src/listener.rs +++ b/ntfy-daemon/src/listener.rs @@ -114,35 +114,6 @@ pub struct ListenerActor { } impl ListenerActor { - pub fn new(config: ListenerConfig) -> ListenerHandle { - let (event_tx, event_rx) = async_channel::bounded(64); - let (commands_tx, commands_rx) = mpsc::channel(1); - - let config_clone = config.clone(); - - // use a new local set to isolate panics - let local_set = LocalSet::new(); - local_set.spawn_local(async move { - let this = Self { - event_tx, - commands_rx: Some(commands_rx), - config: config_clone, - state: ConnectionState::Unitialized, - }; - - this.run_loop().await; - }); - spawn_local(local_set); - - ListenerHandle { - events: event_rx, - config, - commands: commands_tx, - listener_actor: Arc::new(RwLock::new(None)), - join_handle: Arc::new(None), - } - } - pub async fn run_loop(mut self) { let mut commands_rx = self.commands_rx.take().unwrap(); loop { @@ -280,6 +251,35 @@ pub struct ListenerHandle { } impl ListenerHandle { + pub fn new(config: ListenerConfig) -> ListenerHandle { + let (event_tx, event_rx) = async_channel::bounded(64); + let (commands_tx, commands_rx) = mpsc::channel(1); + + let config_clone = config.clone(); + + // use a new local set to isolate panics + let local_set = LocalSet::new(); + local_set.spawn_local(async move { + let this = ListenerActor { + event_tx, + commands_rx: Some(commands_rx), + config: config_clone, + state: ConnectionState::Unitialized, + }; + + this.run_loop().await; + }); + spawn_local(local_set); + + Self { + events: event_rx, + config, + commands: commands_tx, + listener_actor: Arc::new(RwLock::new(None)), + join_handle: Arc::new(None), + } + } + // the response will be sent as an event in self.events pub async fn request_state(&self) -> ConnectionState { let (tx, rx) = oneshot::channel(); @@ -337,7 +337,7 @@ mod tests { since: 0, }; - let mut listener = ListenerActor::new(config.clone()); + let mut listener = ListenerHandle::new(config.clone()); let items: Vec<_> = listener.events.take(3).collect().await; dbg!(&items); @@ -383,7 +383,7 @@ mod tests { since: 0, }; - let mut listener = ListenerActor::new(config.clone()); + let mut listener = ListenerHandle::new(config.clone()); let items: Vec<_> = listener.events.take(3).collect().await; dbg!(&items); @@ -414,7 +414,7 @@ mod tests { since: 0, }; - let mut listener = ListenerActor::new(config.clone()); + let mut listener = ListenerHandle::new(config.clone()); // assert_event_matches!(listener, ListenerEvent::Connected { .. },); }); diff --git a/ntfy-daemon/src/ntfy.rs b/ntfy-daemon/src/ntfy.rs index d3518ce..e2f9307 100644 --- a/ntfy-daemon/src/ntfy.rs +++ b/ntfy-daemon/src/ntfy.rs @@ -249,8 +249,8 @@ impl NtfyActor { ) -> impl Future> { let server = sub.server.clone(); let topic = sub.topic.clone(); - let listener = ListenerActor::new(ListenerConfig { - http_client: self.env.nullable_http.clone(), + let listener = ListenerHandle::new(ListenerConfig { + http_client: self.env.http_client.clone(), credentials: self.env.credentials.clone(), endpoint: server.clone(), topic: topic.clone(), @@ -378,7 +378,6 @@ impl NtfyHandle { } pub fn start( - socket_path: std::path::PathBuf, dbpath: &str, notification_proxy: Arc, network_proxy: Arc, @@ -400,10 +399,9 @@ pub fn start( let env = SharedEnv { db: Db::connect(&dbpath).unwrap(), - proxy: notification_proxy, - http: build_client().unwrap(), - nullable_http: HttpClient::new(build_client().unwrap()), - network: network_proxy, + notifier: notification_proxy, + http_client: HttpClient::new(build_client().unwrap()), + network_monitor: network_proxy, credentials, }; @@ -452,9 +450,8 @@ mod tests { let notification_proxy = Arc::new(NullNotifier::new()); let network_proxy = Arc::new(NullNetworkMonitor::new()); let dbpath = ":memory:"; - let socket_path = std::path::PathBuf::from("/tmp/ntfy.sock"); - let handle = start(socket_path, dbpath, notification_proxy, network_proxy).unwrap(); + let handle = start(dbpath, notification_proxy, network_proxy).unwrap(); let rt = tokio::runtime::Builder::new_current_thread() .enable_all() diff --git a/ntfy-daemon/src/subscription.rs b/ntfy-daemon/src/subscription.rs index 0243429..286e0d3 100644 --- a/ntfy-daemon/src/subscription.rs +++ b/ntfy-daemon/src/subscription.rs @@ -182,7 +182,7 @@ impl SubscriptionActor { async fn publish(&self, msg: String) -> anyhow::Result<()> { let server = &self.model.server; let creds = self.env.credentials.get(server); - let mut req = self.env.http.post(server); + let mut req = self.env.http_client.post(server); if let Some(creds) = creds { req = req.basic_auth(creds.username, Some(creds.password)); } @@ -212,7 +212,7 @@ impl SubscriptionActor { if !already_stored { // Show notification. If this fails, panic if !{ self.model.muted } { - let notifier = self.env.proxy.clone(); + let notifier = self.env.notifier.clone(); let title = { msg.notification_title(&self.model) }; diff --git a/src/application.rs b/src/application.rs index 0252fe6..1eb346f 100644 --- a/src/application.rs +++ b/src/application.rs @@ -30,7 +30,6 @@ mod imp { #[derive(Default)] pub struct NotifyApplication { pub window: RefCell>, - pub socket_path: RefCell, pub hold_guard: OnceCell, pub ntfy: OnceCell, } @@ -59,8 +58,6 @@ mod imp { // Set icons for shell gtk::Window::set_default_icon_name(APP_ID); - let socket_path = glib::user_data_dir().join("com.ranfdev.Notify.socket"); - self.socket_path.replace(socket_path); app.setup_css(); app.setup_gactions(); app.setup_accels(); @@ -72,7 +69,7 @@ mod imp { let app = self.obj(); if self.hold_guard.get().is_none() { - app.ensure_rpc_running(&self.socket_path.borrow()); + app.ensure_rpc_running(); } glib::MainContext::default().spawn_local(async move { @@ -109,7 +106,7 @@ impl NotifyApplication { return; } } - self.build_window(&self.imp().socket_path.borrow()); + self.build_window(); self.main_window().present(); } @@ -254,7 +251,7 @@ impl NotifyApplication { Ok(()) } - fn ensure_rpc_running(&self, socket_path: &Path) { + fn ensure_rpc_running(&self) { let dbpath = glib::user_data_dir().join("com.ranfdev.Notify.sqlite"); info!(database_path = %dbpath.display()); @@ -318,13 +315,7 @@ impl NotifyApplication { } } let proxies = std::sync::Arc::new(Proxies { notification: s }); - let ntfy = ntfy_daemon::start( - socket_path.to_owned(), - dbpath.to_str().unwrap(), - proxies.clone(), - proxies, - ) - .unwrap(); + let ntfy = ntfy_daemon::start(dbpath.to_str().unwrap(), proxies.clone(), proxies).unwrap(); self.imp() .ntfy .set(ntfy) @@ -333,7 +324,7 @@ impl NotifyApplication { self.imp().hold_guard.set(self.hold()).unwrap(); } - fn build_window(&self, socket_path: &Path) { + fn build_window(&self) { let ntfy = self.imp().ntfy.get().unwrap(); let window = NotifyWindow::new(self, ntfy.clone());