mirror of
https://github.com/mikedilger/gossip.git
synced 2024-09-19 19:46:50 +00:00
Strip out dead setting handling code, we use the Setting object now
This commit is contained in:
parent
8e95ccab00
commit
b1995ead86
@ -1,142 +1,7 @@
|
|||||||
use crate::error::Error;
|
|
||||||
use crate::globals::GLOBALS;
|
|
||||||
use rusqlite::ToSql;
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use tokio::task::spawn_blocking;
|
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
pub struct DbSetting {
|
pub struct DbSetting {
|
||||||
pub key: String,
|
pub key: String,
|
||||||
pub value: String,
|
pub value: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DbSetting {
|
|
||||||
pub async fn fetch(criteria: Option<&str>) -> Result<Vec<DbSetting>, Error> {
|
|
||||||
let sql = "SELECT key, value FROM settings".to_owned();
|
|
||||||
let sql = match criteria {
|
|
||||||
None => sql,
|
|
||||||
Some(crit) => format!("{} WHERE {}", sql, crit),
|
|
||||||
};
|
|
||||||
|
|
||||||
let output: Result<Vec<DbSetting>, Error> = spawn_blocking(move || {
|
|
||||||
let maybe_db = GLOBALS.db.blocking_lock();
|
|
||||||
let db = maybe_db.as_ref().unwrap();
|
|
||||||
|
|
||||||
let mut stmt = db.prepare(&sql)?;
|
|
||||||
let rows = stmt.query_map([], |row| {
|
|
||||||
Ok(DbSetting {
|
|
||||||
key: row.get(0)?,
|
|
||||||
value: row.get(1)?,
|
|
||||||
})
|
|
||||||
})?;
|
|
||||||
|
|
||||||
let mut output: Vec<DbSetting> = Vec::new();
|
|
||||||
for row in rows {
|
|
||||||
output.push(row?);
|
|
||||||
}
|
|
||||||
Ok(output)
|
|
||||||
})
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
output
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn fetch_setting(key: &str) -> Result<Option<String>, Error> {
|
|
||||||
let db_settings = DbSetting::fetch(Some(&format!("key='{}'", key))).await?;
|
|
||||||
|
|
||||||
if db_settings.is_empty() {
|
|
||||||
Ok(None)
|
|
||||||
} else {
|
|
||||||
Ok(Some(db_settings[0].value.clone()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
pub async fn fetch_setting_or_default(key: &str, default: &str) -> Result<String, Error> {
|
|
||||||
let db_settings = DbSetting::fetch(Some(&format!("key='{}'", key))).await?;
|
|
||||||
|
|
||||||
if db_settings.is_empty() {
|
|
||||||
Ok(default.to_string())
|
|
||||||
} else {
|
|
||||||
Ok(db_settings[0].value.clone())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
pub async fn fetch_setting_u64_or_default(key: &str, default: u64) -> Result<u64, Error> {
|
|
||||||
let db_settings = DbSetting::fetch(Some(&format!("key='{}'", key))).await?;
|
|
||||||
|
|
||||||
if db_settings.is_empty() {
|
|
||||||
Ok(default)
|
|
||||||
} else {
|
|
||||||
Ok(db_settings[0].value.parse::<u64>().unwrap_or(default))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
pub async fn set(setting: DbSetting) -> Result<(), Error> {
|
|
||||||
let sql = "INSERT OR REPLACE INTO settings (key, value) VALUES (?1, ?2)";
|
|
||||||
|
|
||||||
spawn_blocking(move || {
|
|
||||||
let maybe_db = GLOBALS.db.blocking_lock();
|
|
||||||
let db = maybe_db.as_ref().unwrap();
|
|
||||||
|
|
||||||
let mut stmt = db.prepare(sql)?;
|
|
||||||
stmt.execute((&setting.key, &setting.value))?;
|
|
||||||
Ok::<(), Error>(())
|
|
||||||
})
|
|
||||||
.await??;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
pub async fn insert(setting: DbSetting) -> Result<(), Error> {
|
|
||||||
let sql = "INSERT OR IGNORE INTO settings (key, value) \
|
|
||||||
VALUES (?1, ?2)";
|
|
||||||
|
|
||||||
spawn_blocking(move || {
|
|
||||||
let maybe_db = GLOBALS.db.blocking_lock();
|
|
||||||
let db = maybe_db.as_ref().unwrap();
|
|
||||||
|
|
||||||
let mut stmt = db.prepare(sql)?;
|
|
||||||
stmt.execute((&setting.key, &setting.value))?;
|
|
||||||
Ok::<(), Error>(())
|
|
||||||
})
|
|
||||||
.await??;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
pub async fn update<T: ToSql + Send + 'static>(key: String, value: T) -> Result<(), Error> {
|
|
||||||
let sql = "UPDATE settings SET value=? WHERE key=?";
|
|
||||||
|
|
||||||
spawn_blocking(move || {
|
|
||||||
let maybe_db = GLOBALS.db.blocking_lock();
|
|
||||||
let db = maybe_db.as_ref().unwrap();
|
|
||||||
|
|
||||||
let mut stmt = db.prepare(sql)?;
|
|
||||||
stmt.execute((&value, &key))?;
|
|
||||||
Ok::<(), Error>(())
|
|
||||||
})
|
|
||||||
.await??;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
pub async fn delete(criteria: &str) -> Result<(), Error> {
|
|
||||||
let sql = format!("DELETE FROM settings WHERE {}", criteria);
|
|
||||||
|
|
||||||
spawn_blocking(move || {
|
|
||||||
let maybe_db = GLOBALS.db.blocking_lock();
|
|
||||||
let db = maybe_db.as_ref().unwrap();
|
|
||||||
db.execute(&sql, [])?;
|
|
||||||
Ok::<(), Error>(())
|
|
||||||
})
|
|
||||||
.await??;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -49,9 +49,6 @@ pub struct Globals {
|
|||||||
/// All nostr relay records we have
|
/// All nostr relay records we have
|
||||||
pub relays: Mutex<HashMap<Url, DbRelay>>,
|
pub relays: Mutex<HashMap<Url, DbRelay>>,
|
||||||
|
|
||||||
/// Whether or not we have a saved private key and need the password to unlock it
|
|
||||||
pub need_password: AtomicBool,
|
|
||||||
|
|
||||||
/// Whether or not we are shutting down. For the UI (minions will be signaled and
|
/// Whether or not we are shutting down. For the UI (minions will be signaled and
|
||||||
/// waited for by the overlord)
|
/// waited for by the overlord)
|
||||||
pub shutting_down: AtomicBool,
|
pub shutting_down: AtomicBool,
|
||||||
@ -80,7 +77,6 @@ lazy_static! {
|
|||||||
desired_events: Mutex::new(HashMap::new()),
|
desired_events: Mutex::new(HashMap::new()),
|
||||||
people: Mutex::new(HashMap::new()),
|
people: Mutex::new(HashMap::new()),
|
||||||
relays: Mutex::new(HashMap::new()),
|
relays: Mutex::new(HashMap::new()),
|
||||||
need_password: AtomicBool::new(false),
|
|
||||||
shutting_down: AtomicBool::new(false),
|
shutting_down: AtomicBool::new(false),
|
||||||
settings: Mutex::new(Settings::default()),
|
settings: Mutex::new(Settings::default()),
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@ mod minion;
|
|||||||
mod relay_picker;
|
mod relay_picker;
|
||||||
|
|
||||||
use crate::comms::BusMessage;
|
use crate::comms::BusMessage;
|
||||||
use crate::db::{DbEvent, DbPerson, DbPersonRelay, DbRelay, DbSetting};
|
use crate::db::{DbEvent, DbPerson, DbPersonRelay, DbRelay};
|
||||||
use crate::error::Error;
|
use crate::error::Error;
|
||||||
use crate::globals::{Globals, GLOBALS};
|
use crate::globals::{Globals, GLOBALS};
|
||||||
use crate::settings::Settings;
|
use crate::settings::Settings;
|
||||||
@ -80,19 +80,6 @@ impl Overlord {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn run_inner(&mut self) -> Result<(), Error> {
|
pub async fn run_inner(&mut self) -> Result<(), Error> {
|
||||||
// Check for a private key
|
|
||||||
if DbSetting::fetch_setting("user_private_key")
|
|
||||||
.await?
|
|
||||||
.is_some()
|
|
||||||
{
|
|
||||||
// We don't bother loading the value just yet because we don't have
|
|
||||||
// the password.
|
|
||||||
info!("Saved private key found. Will need a password to unlock.");
|
|
||||||
GLOBALS
|
|
||||||
.need_password
|
|
||||||
.store(true, std::sync::atomic::Ordering::Relaxed);
|
|
||||||
}
|
|
||||||
|
|
||||||
// FIXME - if this needs doing, it should be done dynamically as
|
// FIXME - if this needs doing, it should be done dynamically as
|
||||||
// new people are encountered, not batch-style on startup.
|
// new people are encountered, not batch-style on startup.
|
||||||
// Create a person record for every person seen, possibly autofollow
|
// Create a person record for every person seen, possibly autofollow
|
||||||
|
Loading…
Reference in New Issue
Block a user