From 4f74b8940a1b0ff8775adc0b8e6bbf46de2bdf86 Mon Sep 17 00:00:00 2001 From: Mike Dilger Date: Mon, 2 Jan 2023 06:15:38 +1300 Subject: [PATCH] Offline mode --- src/fetcher.rs | 4 ++++ src/overlord/mod.rs | 12 ++++++++---- src/settings.rs | 43 +++++++++++++++++++++++++++---------------- src/ui/settings.rs | 9 +++++++++ 4 files changed, 48 insertions(+), 20 deletions(-) diff --git a/src/fetcher.rs b/src/fetcher.rs index 7e8b9c6c..c0fa2074 100644 --- a/src/fetcher.rs +++ b/src/fetcher.rs @@ -102,6 +102,10 @@ impl Fetcher { } } + if GLOBALS.settings.blocking_read().offline { + return Ok(None); + } + // We can't fetch as we are not async and we don't want to block the caller. // So we save this request as pending, and ask the syncer to sync us. self.pending.write().unwrap().insert(url); diff --git a/src/overlord/mod.rs b/src/overlord/mod.rs index a8485318..a733a8e2 100644 --- a/src/overlord/mod.rs +++ b/src/overlord/mod.rs @@ -163,7 +163,7 @@ impl Overlord { } // Pick Relays and start Minions - { + if !GLOBALS.settings.read().await.offline { let pubkeys: Vec = GLOBALS .people .read() @@ -239,10 +239,10 @@ impl Overlord { } info!("Listening on {} relays", relay_count); - } - // Get desired events from relays - self.get_missing_events().await?; + // Get desired events from relays + self.get_missing_events().await?; + } 'mainloop: loop { match self.loop_handler().await { @@ -262,6 +262,10 @@ impl Overlord { } async fn start_minion(&mut self, url: String) -> Result<(), Error> { + if GLOBALS.settings.read().await.offline { + return Ok(()); + } + let url = Url::new(&url); if !url.is_valid_relay_url() { return Err(Error::InvalidUrl(url.inner().to_owned())); diff --git a/src/settings.rs b/src/settings.rs index 3656201d..6f660404 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -14,6 +14,7 @@ pub const DEFAULT_MAX_RELAYS: u8 = 15; pub const DEFAULT_MAX_FPS: u32 = 30; pub const DEFAULT_FEED_RECOMPUTE_INTERVAL_MS: u32 = 2000; pub const DEFAULT_POW: u8 = 0; +pub const DEFAULT_OFFLINE: bool = false; #[derive(Clone, Debug, Deserialize, Serialize)] pub struct Settings { @@ -29,6 +30,7 @@ pub struct Settings { pub max_fps: u32, pub feed_recompute_interval_ms: u32, pub pow: u8, + pub offline: bool, } impl Default for Settings { @@ -46,6 +48,7 @@ impl Default for Settings { max_fps: DEFAULT_MAX_FPS, feed_recompute_interval_ms: DEFAULT_FEED_RECOMPUTE_INTERVAL_MS, pow: DEFAULT_POW, + offline: DEFAULT_OFFLINE, } } } @@ -102,6 +105,7 @@ impl Settings { .unwrap_or(DEFAULT_FEED_RECOMPUTE_INTERVAL_MS) } "pow" => settings.pow = row.1.parse::().unwrap_or(DEFAULT_POW), + "offline" => settings.offline = numstr_to_bool(row.1), _ => {} } } @@ -113,33 +117,40 @@ impl Settings { let maybe_db = GLOBALS.db.lock().await; let db = maybe_db.as_ref().unwrap(); + let bool_to_numstr = |b: bool| -> &str { + if b { + "1" + } else { + "0" + } + }; + let mut stmt = db.prepare( "REPLACE INTO settings (key, value) VALUES \ - ('feed_chunk', ?),('overlap', ?),\ - ('view_posts_referred_to', ?),('view_posts_referring_to', ?),\ - ('view_threaded', ?),('num_relays_per_person', ?),\ - ('max_relays', ?),('max_fps', ?),('feed_recompute_interval_ms', ?),\ - ('pow', ?)", + ('feed_chunk', ?),\ + ('overlap', ?),\ + ('view_posts_referred_to', ?),\ + ('view_posts_referring_to', ?),\ + ('view_threaded', ?),\ + ('num_relays_per_person', ?),\ + ('max_relays', ?),\ + ('max_fps', ?),\ + ('feed_recompute_interval_ms', ?),\ + ('pow', ?),\ + ('offline', ?)", )?; stmt.execute(( self.feed_chunk, self.overlap, - if self.view_posts_referred_to { - "1" - } else { - "0" - }, - if self.view_posts_referring_to { - "1" - } else { - "0" - }, - if self.view_threaded { "1" } else { "0" }, + bool_to_numstr(self.view_posts_referred_to), + bool_to_numstr(self.view_posts_referring_to), + bool_to_numstr(self.view_threaded), self.num_relays_per_person, self.max_relays, self.max_fps, self.feed_recompute_interval_ms, self.pow, + bool_to_numstr(self.offline), ))?; // Save private key identity diff --git a/src/ui/settings.rs b/src/ui/settings.rs index b6013934..49c506e7 100644 --- a/src/ui/settings.rs +++ b/src/ui/settings.rs @@ -102,6 +102,15 @@ pub(super) fn update( ui.separator(); ui.add_space(12.0); + ui.heading("Network"); + + ui.checkbox(&mut app.settings.offline, "Offline Mode") + .on_hover_text("If selected, no network requests will be issued. Takes effect on restart."); + + ui.add_space(12.0); + ui.separator(); + ui.add_space(12.0); + ui.heading("User Interface"); ui.horizontal(|ui| {