Skip to content

Commit

Permalink
chore: enable cloud search (AppFlowy-IO#5624)
Browse files Browse the repository at this point in the history
* chore: enable cloud search

* fix: filter received msgs by query
  • Loading branch information
Xazin authored Jun 25, 2024
1 parent a7f40b2 commit e4132ed
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:appflowy/plugins/trash/application/trash_service.dart';
import 'package:appflowy/workspace/application/command_palette/search_listener.dart';
import 'package:appflowy/workspace/application/command_palette/search_service.dart';
import 'package:appflowy_backend/protobuf/flowy-folder/trash.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-search/notification.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-search/result.pb.dart';
import 'package:bloc/bloc.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
Expand Down Expand Up @@ -40,6 +41,7 @@ class CommandPaletteBloc
Future<void> close() {
_trashListener.close();
_searchListener.stop();
_debounceOnChanged?.cancel();
return super.close();
}

Expand Down Expand Up @@ -75,13 +77,17 @@ class CommandPaletteBloc
emit(state.copyWith(query: null, isLoading: false, results: []));
}
},
resultsChanged: (results, max) {
resultsChanged: (results) {
if (state.query != _oldQuery) {
emit(state.copyWith(results: []));
emit(state.copyWith(results: [], isLoading: true));
_oldQuery = state.query;
_messagesReceived = 0;
}

if (state.query != results.query) {
return;
}

_messagesReceived++;

final searchResults = _filterDuplicates(results.items);
Expand All @@ -90,7 +96,7 @@ class CommandPaletteBloc
emit(
state.copyWith(
results: searchResults,
isLoading: _messagesReceived != max,
isLoading: _messagesReceived != results.sends.toInt(),
),
);
},
Expand Down Expand Up @@ -155,7 +161,7 @@ class CommandPaletteBloc
void _performSearch(String value) =>
add(CommandPaletteEvent.performSearch(search: value));

void _onResultsChanged(RepeatedSearchResultPB results) =>
void _onResultsChanged(SearchResultNotificationPB results) =>
add(CommandPaletteEvent.resultsChanged(results: results));
}

Expand All @@ -168,8 +174,7 @@ class CommandPaletteEvent with _$CommandPaletteEvent {
_PerformSearch;

const factory CommandPaletteEvent.resultsChanged({
required RepeatedSearchResultPB results,
@Default(1) int max,
required SearchResultNotificationPB results,
}) = _ResultsChanged;

const factory CommandPaletteEvent.trashChanged({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import 'dart:typed_data';
import 'package:appflowy/core/notification/search_notification.dart';
import 'package:appflowy_backend/log.dart';
import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-search/notification.pbenum.dart';
import 'package:appflowy_backend/protobuf/flowy-search/result.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-search/notification.pb.dart';
import 'package:appflowy_result/appflowy_result.dart';
import 'package:flowy_infra/notifier.dart';

Expand All @@ -23,14 +22,15 @@ class SearchListener {
///
final String? channel;

PublishNotifier<RepeatedSearchResultPB>? _updateNotifier = PublishNotifier();
PublishNotifier<RepeatedSearchResultPB>? _updateDidCloseNotifier =
PublishNotifier<SearchResultNotificationPB>? _updateNotifier =
PublishNotifier();
PublishNotifier<SearchResultNotificationPB>? _updateDidCloseNotifier =
PublishNotifier();
SearchNotificationListener? _listener;

void start({
void Function(RepeatedSearchResultPB)? onResultsChanged,
void Function(RepeatedSearchResultPB)? onResultsClosed,
void Function(SearchResultNotificationPB)? onResultsChanged,
void Function(SearchResultNotificationPB)? onResultsClosed,
}) {
if (onResultsChanged != null) {
_updateNotifier?.addPublishListener(onResultsChanged);
Expand All @@ -55,7 +55,7 @@ class SearchListener {
case SearchNotification.DidUpdateResults:
result.fold(
(payload) => _updateNotifier?.value =
RepeatedSearchResultPB.fromBuffer(payload),
SearchResultNotificationPB.fromBuffer(payload),
(err) => Log.error(err),
);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ class CommandPaletteModal extends StatelessWidget {
),
),
],
if (state.results.isNotEmpty) ...[
if (state.results.isNotEmpty &&
(state.query?.isNotEmpty ?? false)) ...[
const Divider(height: 0),
Flexible(
child: SearchResultsList(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,25 @@ class _SearchResultTileState extends State<SearchResultTile> {
SizedBox(width: 24, child: icon),
const HSpace(6),
],
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (widget.isTrashed) ...[
Flexible(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (widget.isTrashed) ...[
FlowyText(
LocaleKeys.commandPalette_fromTrashHint.tr(),
color: AFThemeExtension.of(context)
.textColor
.withAlpha(175),
fontSize: 10,
),
],
FlowyText(
LocaleKeys.commandPalette_fromTrashHint.tr(),
color: AFThemeExtension.of(context)
.textColor
.withAlpha(175),
fontSize: 10,
widget.result.data,
overflow: TextOverflow.ellipsis,
),
],
FlowyText(widget.result.data),
],
),
),
],
),
Expand Down Expand Up @@ -142,6 +147,7 @@ class _DocumentPreview extends StatelessWidget {
color: Theme.of(context).hintColor,
fontSize: 12,
maxLines: 3,
overflow: TextOverflow.ellipsis,
),
);
}
Expand Down
10 changes: 5 additions & 5 deletions frontend/rust-lib/flowy-core/src/deps_resolve/search_deps.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use flowy_folder::manager::FolderManager;
use flowy_search::document::handler::DocumentSearchHandler;
use flowy_search::folder::handler::FolderSearchHandler;
use flowy_search::folder::indexer::FolderIndexManagerImpl;
use flowy_search::services::manager::SearchManager;
Expand All @@ -9,12 +10,11 @@ pub struct SearchDepsResolver();
impl SearchDepsResolver {
pub async fn resolve(
folder_indexer: Arc<FolderIndexManagerImpl>,
_cloud_service: Arc<dyn SearchCloudService>,
_folder_manager: Arc<FolderManager>,
cloud_service: Arc<dyn SearchCloudService>,
folder_manager: Arc<FolderManager>,
) -> Arc<SearchManager> {
let folder_handler = Arc::new(FolderSearchHandler::new(folder_indexer));
// TODO(Mathias): Enable when Cloud Search is ready
// let document_handler = Arc::new(DocumentSearchHandler::new(cloud_service, folder_manager));
Arc::new(SearchManager::new(vec![folder_handler]))
let document_handler = Arc::new(DocumentSearchHandler::new(cloud_service, folder_manager));
Arc::new(SearchManager::new(vec![folder_handler, document_handler]))
}
}
3 changes: 3 additions & 0 deletions frontend/rust-lib/flowy-search/src/entities/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ pub struct SearchResultNotificationPB {

#[pb(index = 3, one_of)]
pub channel: Option<String>,

#[pb(index = 4)]
pub query: String,
}

#[derive(ProtoBuf_Enum, Debug, Default)]
Expand Down
3 changes: 2 additions & 1 deletion frontend/rust-lib/flowy-search/src/services/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,15 @@ impl SearchManager {
let notifier = self.notifier.clone();

af_spawn(async move {
let res = handler.perform_search(q, f).await;
let res = handler.perform_search(q.clone(), f).await;

let items = res.unwrap_or_default();

let notification = SearchResultNotificationPB {
items,
sends: max as u64,
channel: ch,
query: q,
};

let _ = notifier.send(SearchResultChanged::SearchResultUpdate(notification));
Expand Down

0 comments on commit e4132ed

Please sign in to comment.