From 2bb6182c07ffe01e1192de44c710a206dbcdf79e Mon Sep 17 00:00:00 2001 From: Mike Dilger Date: Sat, 19 Aug 2023 10:22:48 +1200 Subject: [PATCH] Stop using GLOBALS.settings, use storage directly instead --- src/comms.rs | 1 - src/delegation.rs | 6 +++--- src/globals.rs | 5 ----- src/main.rs | 5 ----- src/overlord/minion/mod.rs | 28 ++++++++-------------------- src/overlord/mod.rs | 11 +++-------- src/signer.rs | 6 +++--- src/ui/mod.rs | 2 +- src/ui/settings/mod.rs | 9 ++------- 9 files changed, 20 insertions(+), 53 deletions(-) diff --git a/src/comms.rs b/src/comms.rs index d2e4ddc2..221bb5c3 100644 --- a/src/comms.rs +++ b/src/comms.rs @@ -37,7 +37,6 @@ pub enum ToOverlordMessage { ClearAllUsageOnRelay(RelayUrl), Repost(Id), RankRelay(RelayUrl, u8), - SaveSettings, Search(String), SetActivePerson(PublicKey), AdjustRelayUsageBit(RelayUrl, u64, bool), diff --git a/src/delegation.rs b/src/delegation.rs index b5505362..27b8496e 100644 --- a/src/delegation.rs +++ b/src/delegation.rs @@ -59,9 +59,9 @@ impl Delegation { } pub async fn save_through_settings(&self) -> Result<(), Error> { - GLOBALS.settings.write().delegatee_tag = self.get_delegatee_tag_as_str(); - let settings = GLOBALS.settings.read(); - settings.save()?; + GLOBALS + .storage + .write_setting_delegatee_tag(&self.get_delegatee_tag_as_str(), None)?; Ok(()) } } diff --git a/src/globals.rs b/src/globals.rs index bf0ae495..b58fef4a 100644 --- a/src/globals.rs +++ b/src/globals.rs @@ -6,7 +6,6 @@ use crate::media::Media; use crate::people::{People, Person}; use crate::relay::Relay; use crate::relay_picker_hooks::Hooks; -use crate::settings::Settings; use crate::signer::Signer; use crate::status::StatusQueue; use crate::storage::Storage; @@ -59,9 +58,6 @@ pub struct Globals { /// waited for by the overlord) pub shutting_down: AtomicBool, - /// Settings - pub settings: PRwLock, - /// Signer pub signer: Signer, @@ -144,7 +140,6 @@ lazy_static! { connected_relays: DashMap::new(), relay_picker: Default::default(), shutting_down: AtomicBool::new(false), - settings: PRwLock::new(Settings::default()), signer: Signer::default(), dismissed: RwLock::new(Vec::new()), feed: Feed::new(), diff --git a/src/main.rs b/src/main.rs index df9342c7..dc43c0bb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -52,7 +52,6 @@ mod ui; use crate::comms::ToOverlordMessage; use crate::error::Error; use crate::globals::GLOBALS; -use crate::settings::Settings; use std::ops::DerefMut; use std::{env, thread}; use tracing_subscriber::filter::{EnvFilter, LevelFilter}; @@ -83,10 +82,6 @@ fn main() -> Result<(), Error> { // Initialize storage GLOBALS.storage.init()?; - // Load settings - let settings = Settings::load(); - *GLOBALS.settings.write() = settings; - // We create and enter the runtime on the main thread so that // non-async code can have a runtime context within which to spawn // async tasks. diff --git a/src/overlord/minion/mod.rs b/src/overlord/minion/mod.rs index fbecdea2..5ccef530 100644 --- a/src/overlord/minion/mod.rs +++ b/src/overlord/minion/mod.rs @@ -434,13 +434,8 @@ impl Minion { followed_pubkeys: Vec, ) -> Result<(), Error> { let mut filters: Vec = Vec::new(); - let (overlap, feed_chunk) = { - let settings = GLOBALS.settings.read().clone(); - ( - Duration::from_secs(settings.overlap), - Duration::from_secs(settings.feed_chunk), - ) - }; + let overlap = Duration::from_secs(GLOBALS.storage.read_setting_overlap()); + let feed_chunk = Duration::from_secs(GLOBALS.storage.read_setting_feed_chunk()); tracing::debug!( "Following {} people at {}", @@ -568,13 +563,8 @@ impl Minion { // (and any other relay for the time being until nip65 is in widespread use) async fn subscribe_mentions(&mut self, job_id: u64) -> Result<(), Error> { let mut filters: Vec = Vec::new(); - let (overlap, replies_chunk) = { - let settings = GLOBALS.settings.read().clone(); - ( - Duration::from_secs(settings.overlap), - Duration::from_secs(settings.replies_chunk), - ) - }; + let overlap = Duration::from_secs(GLOBALS.storage.read_setting_overlap()); + let replies_chunk = Duration::from_secs(GLOBALS.storage.read_setting_replies_chunk()); // Compute how far to look back let replies_since = { @@ -697,7 +687,7 @@ impl Minion { ..Default::default() }]; - // let feed_chunk = GLOBALS.settings.read().await.feed_chunk; + // let feed_chunk = GLOBALS.storage.read_setting_feed_chunk(); // Don't do this anymore. It's low value and we can't compute how far back to look // until after we get their 25th oldest post. @@ -811,11 +801,9 @@ impl Minion { // Compute how far to look back let (feed_since, special_since) = { - // Get related settings - let (overlap, feed_chunk) = { - let settings = GLOBALS.settings.read().await.clone(); - (settings.overlap, settings.feed_chunk) - }; + // Get related settings + let overlap = Duration::from_secs(GLOBALS.storage.read_setting_overlap()); + let feed_chunk = Duration::from_secs(GLOBALS.storage.read_setting_feed_chunk()); /* // Find the oldest 'last_fetched' among the 'person_relay' table. diff --git a/src/overlord/mod.rs b/src/overlord/mod.rs index 8a89e50b..50049e9a 100644 --- a/src/overlord/mod.rs +++ b/src/overlord/mod.rs @@ -725,11 +725,6 @@ impl Overlord { ToOverlordMessage::Repost(id) => { self.repost(id).await?; } - ToOverlordMessage::SaveSettings => { - let settings = GLOBALS.settings.read().clone(); - settings.save()?; - tracing::debug!("Settings saved."); - } ToOverlordMessage::Search(text) => { Overlord::search(text).await?; } @@ -756,9 +751,9 @@ impl Overlord { // Update public key from private key let public_key = GLOBALS.signer.public_key().unwrap(); - GLOBALS.settings.write().public_key = Some(public_key); - let settings = GLOBALS.settings.read().clone(); - settings.save()?; + GLOBALS + .storage + .write_setting_public_key(&Some(public_key), None)?; } ToOverlordMessage::UpdateFollowing(merge) => { self.update_following(merge).await?; diff --git a/src/signer.rs b/src/signer.rs index 9bca4ad6..e6925e36 100644 --- a/src/signer.rs +++ b/src/signer.rs @@ -27,9 +27,9 @@ impl Signer { } pub async fn save_through_settings(&self) -> Result<(), Error> { - GLOBALS.settings.write().public_key = *self.public.read(); - let settings = GLOBALS.settings.read().clone(); - settings.save()?; + GLOBALS + .storage + .write_setting_public_key(&self.public.read(), None)?; let epk = self.encrypted.read().clone(); GLOBALS.storage.write_encrypted_private_key(&epk, None)?; diff --git a/src/ui/mod.rs b/src/ui/mod.rs index b0772553..fa977798 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -290,7 +290,7 @@ impl Drop for GossipUi { impl GossipUi { fn new(cctx: &eframe::CreationContext<'_>) -> Self { - let mut settings = GLOBALS.settings.read().clone(); + let mut settings = Settings::load(); if let Some(dpi) = settings.override_dpi { let ppt: f32 = dpi as f32 / 72.0; diff --git a/src/ui/settings/mod.rs b/src/ui/settings/mod.rs index 0219a819..4bd2427a 100644 --- a/src/ui/settings/mod.rs +++ b/src/ui/settings/mod.rs @@ -1,7 +1,5 @@ -use crate::comms::ToOverlordMessage; use crate::settings::Settings; use crate::ui::{GossipUi, SettingsTab}; -use crate::GLOBALS; use eframe::egui; use egui::{Align, Context, Layout, ScrollArea, Ui, Vec2}; @@ -19,7 +17,7 @@ pub(super) fn update(app: &mut GossipUi, ctx: &Context, frame: &mut eframe::Fram let stored_settings = Settings::load(); if stored_settings != app.settings { if ui.button("REVERT CHANGES").clicked() { - app.settings = GLOBALS.settings.read().clone(); + app.settings = Settings::load(); // Fully revert any DPI changes match app.settings.override_dpi { @@ -51,10 +49,7 @@ pub(super) fn update(app: &mut GossipUi, ctx: &Context, frame: &mut eframe::Fram } // Copy local settings to global settings - *GLOBALS.settings.write() = app.settings.clone(); - - // Tell the overlord to save them - let _ = GLOBALS.to_overlord.send(ToOverlordMessage::SaveSettings); + let _ = app.settings.save(); } } });