delegate default search providers init to the db api

This commit is contained in:
yggverse 2025-01-30 16:46:41 +02:00
parent b8a8fb49de
commit 9945002efa
2 changed files with 57 additions and 50 deletions

View file

@ -19,17 +19,11 @@ impl Search {
/// Create new `Self` /// Create new `Self`
pub fn build(connection: &Rc<RwLock<Connection>>, profile_id: &Rc<i64>) -> Result<Self, Error> { pub fn build(connection: &Rc<RwLock<Connection>>, profile_id: &Rc<i64>) -> Result<Self, Error> {
// Init children components match Database::init(connection, profile_id) {
let database = Database::init(connection, profile_id); Ok(database) => {
// Init fast search index
let memory = Memory::init(); let memory = Memory::init();
match database.records() {
Ok(records) => {
// Init default search providers list on database empty
if records.is_empty() {
restore_defaults(&database)?
}
// Build initial index // Build initial index
index(&database, &memory)?; index(&database, &memory)?;
@ -46,15 +40,16 @@ impl Search {
/// * requires valid [Uri](https://docs.gtk.org/glib/struct.Uri.html) /// * requires valid [Uri](https://docs.gtk.org/glib/struct.Uri.html)
pub fn add(&self, query: &Uri, is_default: bool) -> Result<(), Error> { pub fn add(&self, query: &Uri, is_default: bool) -> Result<(), Error> {
match self.database.add(query.to_string(), is_default) { 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)), Err(e) => Err(Error::Database(e)),
} }
} }
/// Add new search provider record /// Add new search provider record
/// * requires valid [Uri](https://docs.gtk.org/glib/struct.Uri.html) /// * requires valid [Uri](https://docs.gtk.org/glib/struct.Uri.html)
pub fn set_default(&self, profile_search_id: i64) -> Result<(), Error> { pub fn set_default(&self, profile_search_id: i64) -> Result<(), Error> {
match self.database.set_default(profile_search_id) { 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)), Err(e) => Err(Error::Database(e)),
} }
} }
@ -67,15 +62,7 @@ impl Search {
/// Delete record from `database` and `memory` index /// Delete record from `database` and `memory` index
pub fn delete(&self, id: i64) -> Result<(), Error> { pub fn delete(&self, id: i64) -> Result<(), Error> {
match self.database.delete(id) { match self.database.delete(id) {
Ok(_) => match self.database.records() { Ok(_) => index(&self.database, &self.memory),
Ok(records) => {
if records.is_empty() {
restore_defaults(&self.database)?
}
Ok(index(&self.database, &self.memory)?)
}
Err(e) => Err(Error::Database(e)),
},
Err(e) => Err(Error::Database(e)), Err(e) => Err(Error::Database(e)),
} }
} }
@ -116,16 +103,3 @@ fn index(database: &Database, memory: &Memory) -> Result<(), Error> {
} }
Ok(()) 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(())
}

View file

@ -18,11 +18,21 @@ impl Database {
// Constructors // Constructors
/// Create new `Self` /// Create new `Self`
pub fn init(connection: &Rc<RwLock<Connection>>, profile_id: &Rc<i64>) -> Self { pub fn init(connection: &Rc<RwLock<Connection>>, profile_id: &Rc<i64>) -> Result<Self, Error> {
Self { 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(), connection: connection.clone(),
profile_id: profile_id.clone(), profile_id: profile_id.clone(),
} })
} }
// Getters // Getters
@ -67,14 +77,31 @@ impl Database {
let tx = writable.transaction()?; let tx = writable.transaction()?;
// Delete record by ID // Delete record by ID
match delete(&tx, id) { delete(&tx, id)?;
Ok(_) => match tx.commit() {
Ok(_) => Ok(()), let records = select(&tx, *self.profile_id)?;
Err(e) => Err(e),
}, // Restore defaults if DB becomes empty
Err(e) => Err(e), 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 /// Delete record from database
pub fn set_default(&self, id: i64) -> Result<(), Error> { pub fn set_default(&self, id: i64) -> Result<(), Error> {
@ -86,13 +113,8 @@ impl Database {
reset(&tx, *self.profile_id, false)?; reset(&tx, *self.profile_id, false)?;
// Delete record by ID // Delete record by ID
match set_default(&tx, *self.profile_id, id, true) { set_default(&tx, *self.profile_id, id, true)?;
Ok(_) => match tx.commit() { tx.commit()
Ok(_) => Ok(()),
Err(e) => Err(e),
},
Err(e) => Err(e),
}
} }
} }
@ -181,3 +203,14 @@ fn set_default(
fn last_insert_id(tx: &Transaction) -> i64 { fn last_insert_id(tx: &Transaction) -> i64 {
tx.last_insert_rowid() 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(())
}