Skip to content

Commit

Permalink
server: Add SearchItems implementation
Browse files Browse the repository at this point in the history
This change add Secret Service method SearchItems and Secret Collection
method SearchItems implementation.

Signed-off-by: Dhanuka Warusadura <[email protected]>
  • Loading branch information
warusadura authored and bilelmoussaoui committed Nov 15, 2024
1 parent e6452d3 commit f097dc5
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
40 changes: 38 additions & 2 deletions server/src/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,30 @@ impl Collection {
#[zbus(out_args("results"))]
pub async fn search_items(
&self,
_attributes: HashMap<String, String>,
attributes: HashMap<String, String>,
) -> Result<Vec<OwnedObjectPath>, ServiceError> {
todo!()
let results = self
.search_inner_items(&attributes)
.await
.iter()
.map(|item| item.path().clone())
.collect::<Vec<_>>();

if results.is_empty() {
tracing::debug!(
"Items with attributes {:?} does not exist in collection: {}.",
attributes,
self.path
);
} else {
tracing::debug!(
"Items with attributes {:?} found in collection: {}.",
attributes,
self.path
);
}

Ok(results)
}

#[zbus(out_args("item", "prompt"))]
Expand Down Expand Up @@ -157,6 +178,21 @@ impl Collection {
self.alias.lock().await.clone()
}

pub async fn search_inner_items(
&self,
attributes: &HashMap<String, String>,
) -> Vec<item::Item> {
let mut items = Vec::new();

for item in self.items.lock().await.iter() {
if item.attributes().await == *attributes {
items.push(item.clone());
}
}

items
}

pub async fn dispatch_items(&self) -> Result<(), Error> {
let keyring_items = self.keyring.items().await;
let mut items = self.items.lock().await;
Expand Down
28 changes: 26 additions & 2 deletions server/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,33 @@ impl Service {
#[zbus(out_args("unlocked", "locked"))]
pub async fn search_items(
&self,
_attributes: HashMap<&str, &str>,
attributes: HashMap<String, String>,
) -> Result<(Vec<OwnedObjectPath>, Vec<OwnedObjectPath>), ServiceError> {
todo!()
let mut unlocked = Vec::new();
let mut locked = Vec::new();
let collections = self.collections.lock().await;

for collection in collections.iter() {
let items = collection.search_inner_items(&attributes).await;
for item in items {
if item.is_locked().await {
locked.push(item.path().clone());
} else {
unlocked.push(item.path().clone());
}
}
}

if unlocked.is_empty() && locked.is_empty() {
tracing::debug!(
"Items with attributes {:?} does not exist in any collection.",
attributes
);
} else {
tracing::debug!("Items with attributes {:?} found.", attributes);
}

Ok((unlocked, locked))
}

#[zbus(out_args("unlocked", "prompt"))]
Expand Down

0 comments on commit f097dc5

Please sign in to comment.