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,16 +19,10 @@ impl Search {
/// Create new `Self`
pub fn build(connection: &Rc<RwLock<Connection>>, profile_id: &Rc<i64>) -> Result<Self, Error> {
// 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(())
}

View file

@ -18,11 +18,21 @@ impl Database {
// Constructors
/// Create new `Self`
pub fn init(connection: &Rc<RwLock<Connection>>, profile_id: &Rc<i64>) -> Self {
Self {
pub fn init(connection: &Rc<RwLock<Connection>>, profile_id: &Rc<i64>) -> Result<Self, Error> {
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(())
}