From 6163f6bac3988b62b9bb356e1f98f954e5cb5d2d Mon Sep 17 00:00:00 2001 From: Mathias Mogensen Date: Wed, 10 Jul 2024 13:00:32 +0200 Subject: [PATCH] fix: find all view ids to be filtered --- .../flowy-search/src/event_handler.rs | 4 +- .../flowy-search/src/services/manager.rs | 44 ++++++++++++++++--- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/frontend/rust-lib/flowy-search/src/event_handler.rs b/frontend/rust-lib/flowy-search/src/event_handler.rs index de611a078f6ff..cdaecbb4876b8 100644 --- a/frontend/rust-lib/flowy-search/src/event_handler.rs +++ b/frontend/rust-lib/flowy-search/src/event_handler.rs @@ -21,7 +21,9 @@ pub(crate) async fn search_handler( ) -> Result<(), FlowyError> { let query = data.into_inner(); let manager = upgrade_manager(manager)?; - manager.perform_search(query.search, query.filter, query.channel); + manager + .perform_search(query.search, query.filter, query.channel) + .await; Ok(()) } diff --git a/frontend/rust-lib/flowy-search/src/services/manager.rs b/frontend/rust-lib/flowy-search/src/services/manager.rs index d05e0a6f693db..f54babdc50041 100644 --- a/frontend/rust-lib/flowy-search/src/services/manager.rs +++ b/frontend/rust-lib/flowy-search/src/services/manager.rs @@ -4,7 +4,7 @@ use std::sync::Arc; use super::notifier::{SearchNotifier, SearchResultChanged, SearchResultReceiverRunner}; use crate::entities::{SearchFilterPB, SearchResultNotificationPB, SearchResultPB}; use flowy_error::FlowyResult; -use flowy_folder::manager::FolderManager; +use flowy_folder::{entities::ViewPB, manager::FolderManager}; use lib_dispatch::prelude::af_spawn; use lib_infra::async_trait::async_trait; use tokio::sync::broadcast; @@ -63,7 +63,42 @@ impl SearchManager { self.handlers.get(&search_type) } - pub fn perform_search( + async fn get_view_ids_should_be_filtered(&self, filtered_ids: Vec) -> Vec { + let mut filtered_ids = filtered_ids.clone(); + + let view_futures = filtered_ids + .iter() + .map(|id| self.folder_manager.get_view_pb(id)) + .collect::>(); + + let view_results = futures::future::join_all(view_futures).await; + + let views = view_results + .into_iter() + .filter(|view| view.is_ok()) + .map(|view| view.unwrap()) + .collect::>(); + + for view in views { + let view_ids = self.get_view_ids_recursively(view); + filtered_ids.append(&mut view_ids.clone()); + } + + filtered_ids + } + + fn get_view_ids_recursively(&self, view: ViewPB) -> Vec { + let mut view_ids = vec![]; + view_ids.push(view.id.clone()); + + for child in view.child_views.iter() { + view_ids.append(&mut self.get_view_ids_recursively(child.clone())); + } + + view_ids + } + + pub async fn perform_search( &self, query: String, filter: Option, @@ -86,9 +121,9 @@ impl SearchManager { return; }, }; - let private_views = self.folder_manager.get_view_ids_should_be_filtered(folder); - tracing::warn!("Private views: {:?}", private_views); + let parent_view_ids = self.folder_manager.get_view_ids_should_be_filtered(folder); + let private_views = self.get_view_ids_should_be_filtered(parent_view_ids).await; let max: usize = self.handlers.len(); let handlers = self.handlers.clone(); for (_, handler) in handlers { @@ -100,7 +135,6 @@ impl SearchManager { af_spawn(async move { let res = handler.perform_search(q.clone(), f).await; - let items = res.unwrap_or_default(); // Filter out any items which ID exists in private_views