Skip to content

Commit a758ffc

Browse files
committed
remove unused code, minor refactorings
1 parent 76e3ad9 commit a758ffc

File tree

6 files changed

+61
-61
lines changed

6 files changed

+61
-61
lines changed

ntfy-daemon/src/http_client.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ impl RequestInfo {
3434
#[async_trait]
3535
trait LightHttpClient: Send + Sync {
3636
fn get(&self, url: &str) -> RequestBuilder;
37+
fn post(&self, url: &str) -> RequestBuilder;
3738
async fn execute(&self, request: Request) -> Result<Response>;
3839
}
3940

@@ -43,6 +44,10 @@ impl LightHttpClient for Client {
4344
self.get(url)
4445
}
4546

47+
fn post(&self, url: &str) -> RequestBuilder {
48+
self.post(url)
49+
}
50+
4651
async fn execute(&self, request: Request) -> Result<Response> {
4752
Ok(self.execute(request).await?)
4853
}
@@ -77,6 +82,10 @@ impl HttpClient {
7782
self.client.get(url)
7883
}
7984

85+
pub fn post(&self, url: &str) -> RequestBuilder {
86+
self.client.post(url)
87+
}
88+
8089
pub async fn execute(&self, request: Request) -> Result<Response> {
8190
self.request_tracker
8291
.push(RequestInfo::from_request(&request))
@@ -183,6 +192,10 @@ impl LightHttpClient for NullableClient {
183192
Client::new().get(url)
184193
}
185194

195+
fn post(&self, url: &str) -> RequestBuilder {
196+
Client::new().post(url)
197+
}
198+
186199
async fn execute(&self, request: Request) -> Result<Response> {
187200
time::sleep(Duration::from_millis(1)).await;
188201
let url = request.url().to_string();

ntfy-daemon/src/lib.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@ use http_client::HttpClient;
1919
#[derive(Clone)]
2020
pub struct SharedEnv {
2121
db: message_repo::Db,
22-
proxy: Arc<dyn models::NotificationProxy>,
23-
http: reqwest::Client,
24-
nullable_http: HttpClient,
25-
network: Arc<dyn models::NetworkMonitorProxy>,
22+
notifier: Arc<dyn models::NotificationProxy>,
23+
http_client: HttpClient,
24+
network_monitor: Arc<dyn models::NetworkMonitorProxy>,
2625
credentials: credentials::Credentials,
2726
}
2827

ntfy-daemon/src/listener.rs

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -114,35 +114,6 @@ pub struct ListenerActor {
114114
}
115115

116116
impl ListenerActor {
117-
pub fn new(config: ListenerConfig) -> ListenerHandle {
118-
let (event_tx, event_rx) = async_channel::bounded(64);
119-
let (commands_tx, commands_rx) = mpsc::channel(1);
120-
121-
let config_clone = config.clone();
122-
123-
// use a new local set to isolate panics
124-
let local_set = LocalSet::new();
125-
local_set.spawn_local(async move {
126-
let this = Self {
127-
event_tx,
128-
commands_rx: Some(commands_rx),
129-
config: config_clone,
130-
state: ConnectionState::Unitialized,
131-
};
132-
133-
this.run_loop().await;
134-
});
135-
spawn_local(local_set);
136-
137-
ListenerHandle {
138-
events: event_rx,
139-
config,
140-
commands: commands_tx,
141-
listener_actor: Arc::new(RwLock::new(None)),
142-
join_handle: Arc::new(None),
143-
}
144-
}
145-
146117
pub async fn run_loop(mut self) {
147118
let mut commands_rx = self.commands_rx.take().unwrap();
148119
loop {
@@ -280,6 +251,35 @@ pub struct ListenerHandle {
280251
}
281252

282253
impl ListenerHandle {
254+
pub fn new(config: ListenerConfig) -> ListenerHandle {
255+
let (event_tx, event_rx) = async_channel::bounded(64);
256+
let (commands_tx, commands_rx) = mpsc::channel(1);
257+
258+
let config_clone = config.clone();
259+
260+
// use a new local set to isolate panics
261+
let local_set = LocalSet::new();
262+
local_set.spawn_local(async move {
263+
let this = ListenerActor {
264+
event_tx,
265+
commands_rx: Some(commands_rx),
266+
config: config_clone,
267+
state: ConnectionState::Unitialized,
268+
};
269+
270+
this.run_loop().await;
271+
});
272+
spawn_local(local_set);
273+
274+
Self {
275+
events: event_rx,
276+
config,
277+
commands: commands_tx,
278+
listener_actor: Arc::new(RwLock::new(None)),
279+
join_handle: Arc::new(None),
280+
}
281+
}
282+
283283
// the response will be sent as an event in self.events
284284
pub async fn request_state(&self) -> ConnectionState {
285285
let (tx, rx) = oneshot::channel();
@@ -337,7 +337,7 @@ mod tests {
337337
since: 0,
338338
};
339339

340-
let mut listener = ListenerActor::new(config.clone());
340+
let mut listener = ListenerHandle::new(config.clone());
341341
let items: Vec<_> = listener.events.take(3).collect().await;
342342

343343
dbg!(&items);
@@ -383,7 +383,7 @@ mod tests {
383383
since: 0,
384384
};
385385

386-
let mut listener = ListenerActor::new(config.clone());
386+
let mut listener = ListenerHandle::new(config.clone());
387387
let items: Vec<_> = listener.events.take(3).collect().await;
388388

389389
dbg!(&items);
@@ -414,7 +414,7 @@ mod tests {
414414
since: 0,
415415
};
416416

417-
let mut listener = ListenerActor::new(config.clone());
417+
let mut listener = ListenerHandle::new(config.clone());
418418

419419
// assert_event_matches!(listener, ListenerEvent::Connected { .. },);
420420
});

ntfy-daemon/src/ntfy.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,8 @@ impl NtfyActor {
249249
) -> impl Future<Output = anyhow::Result<SubscriptionHandle>> {
250250
let server = sub.server.clone();
251251
let topic = sub.topic.clone();
252-
let listener = ListenerActor::new(ListenerConfig {
253-
http_client: self.env.nullable_http.clone(),
252+
let listener = ListenerHandle::new(ListenerConfig {
253+
http_client: self.env.http_client.clone(),
254254
credentials: self.env.credentials.clone(),
255255
endpoint: server.clone(),
256256
topic: topic.clone(),
@@ -378,7 +378,6 @@ impl NtfyHandle {
378378
}
379379

380380
pub fn start(
381-
socket_path: std::path::PathBuf,
382381
dbpath: &str,
383382
notification_proxy: Arc<dyn models::NotificationProxy>,
384383
network_proxy: Arc<dyn models::NetworkMonitorProxy>,
@@ -400,10 +399,9 @@ pub fn start(
400399

401400
let env = SharedEnv {
402401
db: Db::connect(&dbpath).unwrap(),
403-
proxy: notification_proxy,
404-
http: build_client().unwrap(),
405-
nullable_http: HttpClient::new(build_client().unwrap()),
406-
network: network_proxy,
402+
notifier: notification_proxy,
403+
http_client: HttpClient::new(build_client().unwrap()),
404+
network_monitor: network_proxy,
407405
credentials,
408406
};
409407

@@ -452,9 +450,8 @@ mod tests {
452450
let notification_proxy = Arc::new(NullNotifier::new());
453451
let network_proxy = Arc::new(NullNetworkMonitor::new());
454452
let dbpath = ":memory:";
455-
let socket_path = std::path::PathBuf::from("/tmp/ntfy.sock");
456453

457-
let handle = start(socket_path, dbpath, notification_proxy, network_proxy).unwrap();
454+
let handle = start(dbpath, notification_proxy, network_proxy).unwrap();
458455

459456
let rt = tokio::runtime::Builder::new_current_thread()
460457
.enable_all()

ntfy-daemon/src/subscription.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ impl SubscriptionActor {
182182
async fn publish(&self, msg: String) -> anyhow::Result<()> {
183183
let server = &self.model.server;
184184
let creds = self.env.credentials.get(server);
185-
let mut req = self.env.http.post(server);
185+
let mut req = self.env.http_client.post(server);
186186
if let Some(creds) = creds {
187187
req = req.basic_auth(creds.username, Some(creds.password));
188188
}
@@ -212,7 +212,7 @@ impl SubscriptionActor {
212212
if !already_stored {
213213
// Show notification. If this fails, panic
214214
if !{ self.model.muted } {
215-
let notifier = self.env.proxy.clone();
215+
let notifier = self.env.notifier.clone();
216216

217217
let title = { msg.notification_title(&self.model) };
218218

src/application.rs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ mod imp {
3030
#[derive(Default)]
3131
pub struct NotifyApplication {
3232
pub window: RefCell<WeakRef<NotifyWindow>>,
33-
pub socket_path: RefCell<PathBuf>,
3433
pub hold_guard: OnceCell<gio::ApplicationHoldGuard>,
3534
pub ntfy: OnceCell<NtfyHandle>,
3635
}
@@ -59,8 +58,6 @@ mod imp {
5958
// Set icons for shell
6059
gtk::Window::set_default_icon_name(APP_ID);
6160

62-
let socket_path = glib::user_data_dir().join("com.ranfdev.Notify.socket");
63-
self.socket_path.replace(socket_path);
6461
app.setup_css();
6562
app.setup_gactions();
6663
app.setup_accels();
@@ -72,7 +69,7 @@ mod imp {
7269
let app = self.obj();
7370

7471
if self.hold_guard.get().is_none() {
75-
app.ensure_rpc_running(&self.socket_path.borrow());
72+
app.ensure_rpc_running();
7673
}
7774

7875
glib::MainContext::default().spawn_local(async move {
@@ -109,7 +106,7 @@ impl NotifyApplication {
109106
return;
110107
}
111108
}
112-
self.build_window(&self.imp().socket_path.borrow());
109+
self.build_window();
113110
self.main_window().present();
114111
}
115112

@@ -254,7 +251,7 @@ impl NotifyApplication {
254251
Ok(())
255252
}
256253

257-
fn ensure_rpc_running(&self, socket_path: &Path) {
254+
fn ensure_rpc_running(&self) {
258255
let dbpath = glib::user_data_dir().join("com.ranfdev.Notify.sqlite");
259256
info!(database_path = %dbpath.display());
260257

@@ -318,13 +315,7 @@ impl NotifyApplication {
318315
}
319316
}
320317
let proxies = std::sync::Arc::new(Proxies { notification: s });
321-
let ntfy = ntfy_daemon::start(
322-
socket_path.to_owned(),
323-
dbpath.to_str().unwrap(),
324-
proxies.clone(),
325-
proxies,
326-
)
327-
.unwrap();
318+
let ntfy = ntfy_daemon::start(dbpath.to_str().unwrap(), proxies.clone(), proxies).unwrap();
328319
self.imp()
329320
.ntfy
330321
.set(ntfy)
@@ -333,7 +324,7 @@ impl NotifyApplication {
333324
self.imp().hold_guard.set(self.hold()).unwrap();
334325
}
335326

336-
fn build_window(&self, socket_path: &Path) {
327+
fn build_window(&self) {
337328
let ntfy = self.imp().ntfy.get().unwrap();
338329

339330
let window = NotifyWindow::new(self, ntfy.clone());

0 commit comments

Comments
 (0)