From 9945002efa97b8f5d6b3cf9ebada13b074034a6e Mon Sep 17 00:00:00 2001 From: yggverse Date: Thu, 30 Jan 2025 16:46:41 +0200 Subject: [PATCH] delegate default search providers init to the db api --- src/profile/search.rs | 42 +++++----------------- src/profile/search/database.rs | 65 +++++++++++++++++++++++++--------- 2 files changed, 57 insertions(+), 50 deletions(-) diff --git a/src/profile/search.rs b/src/profile/search.rs index dc56b44c..db892335 100644 --- a/src/profile/search.rs +++ b/src/profile/search.rs @@ -19,16 +19,10 @@ impl Search { /// Create new `Self` pub fn build(connection: &Rc>, profile_id: &Rc) -> Result { - // Init children components - let database = Database::init(connection, profile_id); - let memory = Memory::init(); - - match database.records() { - Ok(records) => { - // Init default search providers list on database empty - if records.is_empty() { - restore_defaults(&database)? - } + match Database::init(connection, profile_id) { + Ok(database) => { + // Init fast search index + let memory = Memory::init(); // Build initial index index(&database, &memory)?; @@ -46,15 +40,16 @@ impl Search { /// * requires valid [Uri](https://docs.gtk.org/glib/struct.Uri.html) pub fn add(&self, query: &Uri, is_default: bool) -> Result<(), Error> { match self.database.add(query.to_string(), is_default) { - Ok(_) => Ok(index(&self.database, &self.memory)?), + Ok(_) => index(&self.database, &self.memory), Err(e) => Err(Error::Database(e)), } } + /// Add new search provider record /// * requires valid [Uri](https://docs.gtk.org/glib/struct.Uri.html) pub fn set_default(&self, profile_search_id: i64) -> Result<(), Error> { match self.database.set_default(profile_search_id) { - Ok(_) => Ok(index(&self.database, &self.memory)?), + Ok(_) => index(&self.database, &self.memory), Err(e) => Err(Error::Database(e)), } } @@ -67,15 +62,7 @@ impl Search { /// Delete record from `database` and `memory` index pub fn delete(&self, id: i64) -> Result<(), Error> { match self.database.delete(id) { - Ok(_) => match self.database.records() { - Ok(records) => { - if records.is_empty() { - restore_defaults(&self.database)? - } - Ok(index(&self.database, &self.memory)?) - } - Err(e) => Err(Error::Database(e)), - }, + Ok(_) => index(&self.database, &self.memory), Err(e) => Err(Error::Database(e)), } } @@ -116,16 +103,3 @@ fn index(database: &Database, memory: &Memory) -> Result<(), Error> { } Ok(()) } - -/// Create default search providers list for given profile -fn restore_defaults(database: &Database) -> Result<(), Error> { - for (provider, is_default) in &[ - ("gemini://kennedy.gemi.dev/search", true), - ("gemini://tlgs.one/search/search", false), - ] { - if let Err(e) = database.add(provider.to_string(), *is_default) { - return Err(Error::Database(e)); - } - } - Ok(()) -} diff --git a/src/profile/search/database.rs b/src/profile/search/database.rs index f5083cf6..3d0e2245 100644 --- a/src/profile/search/database.rs +++ b/src/profile/search/database.rs @@ -18,11 +18,21 @@ impl Database { // Constructors /// Create new `Self` - pub fn init(connection: &Rc>, profile_id: &Rc) -> Self { - Self { + pub fn init(connection: &Rc>, profile_id: &Rc) -> Result { + let mut writable = connection.write().unwrap(); // @TODO handle + let tx = writable.transaction()?; + + let records = select(&tx, **profile_id)?; + + if records.is_empty() { + add_defaults(&tx, **profile_id)?; + tx.commit()?; + } + + Ok(Self { connection: connection.clone(), profile_id: profile_id.clone(), - } + }) } // Getters @@ -67,13 +77,30 @@ impl Database { let tx = writable.transaction()?; // Delete record by ID - match delete(&tx, id) { - Ok(_) => match tx.commit() { - Ok(_) => Ok(()), - Err(e) => Err(e), - }, - Err(e) => Err(e), + delete(&tx, id)?; + + let records = select(&tx, *self.profile_id)?; + + // Restore defaults if DB becomes empty + if records.is_empty() { + add_defaults(&tx, *self.profile_id)?; + } else { + // At least one provider should be selected as default + let mut has_default = false; + for record in &records { + if record.is_default { + has_default = true; + break; + } + } + // Select first + if !has_default { + set_default(&tx, *self.profile_id, records[0].id, true)?; + } } + + // Done + tx.commit() } /// Delete record from database @@ -86,13 +113,8 @@ impl Database { reset(&tx, *self.profile_id, false)?; // Delete record by ID - match set_default(&tx, *self.profile_id, id, true) { - Ok(_) => match tx.commit() { - Ok(_) => Ok(()), - Err(e) => Err(e), - }, - Err(e) => Err(e), - } + set_default(&tx, *self.profile_id, id, true)?; + tx.commit() } } @@ -181,3 +203,14 @@ fn set_default( fn last_insert_id(tx: &Transaction) -> i64 { tx.last_insert_rowid() } + +/// Init default search providers list for given profile +fn add_defaults(tx: &Transaction, profile_id: i64) -> Result<(), Error> { + for (provider, is_default) in &[ + ("gemini://kennedy.gemi.dev/search", true), + ("gemini://tlgs.one/search/search", false), + ] { + insert(tx, profile_id, provider.to_string(), *is_default)?; + } + Ok(()) +}