Skip to content

Commit

Permalink
fix: filter the trash ids and other private view ids
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasXu0 committed Apr 1, 2024
1 parent ab1ebca commit 6d28a36
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,43 +31,34 @@ class UserWorkspaceBloc extends Bloc<UserWorkspaceEvent, UserWorkspaceState> {
final isCollabWorkspaceOn =
userProfile.authenticator != AuthenticatorPB.Local &&
FeatureFlag.collaborativeWorkspace.isOn;
final currentWorkspace = result?.$1;
if (currentWorkspace != null && result?.$3 == true) {
await _userService.openWorkspace(currentWorkspace.workspaceId);
final currentWorkspace = result.$1;
if (currentWorkspace != null && result.$3 == true) {
final result = await _userService
.openWorkspace(currentWorkspace.workspaceId);
result.onSuccess((s) async {
await getIt<KeyValueStorage>().set(
KVKeys.lastOpenedWorkspaceId,
currentWorkspace.workspaceId,
);
});
}
emit(
state.copyWith(
currentWorkspace: currentWorkspace,
workspaces: result?.$2 ?? [],
workspaces: result.$2,
isCollabWorkspaceOn: isCollabWorkspaceOn,
actionResult: null,
),
);
},
fetchWorkspaces: () async {
final result = await _fetchWorkspaces();
if (result != null) {
emit(
state.copyWith(
currentWorkspace: result.$1,
workspaces: result.$2,
),
);
} else {
emit(
state.copyWith(
actionResult: UserWorkspaceActionResult(
actionType: UserWorkspaceActionType.none,
result: FlowyResult.failure(
FlowyError(
code: ErrorCode.Internal,
msg: LocaleKeys.workspace_fetchWorkspacesFailed.tr(),
),
),
),
),
);
}
emit(
state.copyWith(
currentWorkspace: result.$1,
workspaces: result.$2,
),
);
},
createWorkspace: (name) async {
final result = await _userService.createUserWorkspace(name);
Expand Down Expand Up @@ -256,34 +247,35 @@ class UserWorkspaceBloc extends Bloc<UserWorkspaceEvent, UserWorkspaceState> {

Future<
(
UserWorkspacePB currentWorkspace,
UserWorkspacePB? currentWorkspace,
List<UserWorkspacePB> workspaces,
bool shouldOpenWorkspace,
)?> _fetchWorkspaces() async {
)> _fetchWorkspaces() async {
try {
final lastOpenedWorkspaceId = await getIt<KeyValueStorage>().get(
KVKeys.lastOpenedWorkspaceId,
);
final currentWorkspace =
await _userService.getCurrentWorkspace().getOrThrow();
final workspaces = await _userService.getWorkspaces().getOrThrow();
UserWorkspacePB currentWorkspaceInList =
workspaces.firstWhere((e) => e.workspaceId == currentWorkspace.id);
UserWorkspacePB? currentWorkspaceInList = workspaces
.firstWhereOrNull((e) => e.workspaceId == currentWorkspace.id);
if (lastOpenedWorkspaceId != null) {
final lastOpenedWorkspace = workspaces
.firstWhereOrNull((e) => e.workspaceId == lastOpenedWorkspaceId);
if (lastOpenedWorkspace != null) {
currentWorkspaceInList = lastOpenedWorkspace;
}
}
currentWorkspaceInList ??= workspaces.first;
return (
currentWorkspaceInList,
workspaces,
lastOpenedWorkspaceId != currentWorkspace.id
);
} catch (e) {
Log.error('fetch workspace error: $e');
return null;
return (null, <UserWorkspacePB>[], false);
}
}
}
Expand Down
40 changes: 20 additions & 20 deletions frontend/rust-lib/flowy-folder/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1144,7 +1144,7 @@ impl FolderManager {
let view_ids_should_be_filtered = self.get_view_ids_should_be_filtered(folder);
views
.into_iter()
.filter(|view| view_ids_should_be_filtered.contains(&view.id))
.filter(|view| !view_ids_should_be_filtered.contains(&view.id))
.collect()
})
}
Expand All @@ -1160,35 +1160,20 @@ impl FolderManager {
.collect::<Vec<String>>();
let mut all_trash_ids = trash_ids.clone();
for trash_id in trash_ids {
all_trash_ids.extend(self.get_all_child_view_ids(folder, &trash_id));
all_trash_ids.extend(get_all_child_view_ids(folder, &trash_id));
}
all_trash_ids
}

/// Get all the child views belong to the view id, including the child views of the child views.
fn get_all_child_view_ids(&self, folder: &Folder, view_id: &str) -> Vec<String> {
let child_view_ids = folder
.views
.get_views_belong_to(view_id)
.into_iter()
.map(|view| view.id.clone())
.collect::<Vec<String>>();
let mut all_child_view_ids = child_view_ids.clone();
for child_view_id in child_view_ids {
all_child_view_ids.extend(self.get_all_child_view_ids(folder, &child_view_id));
}
all_child_view_ids
}

/// Filter the views that are in the trash and belong to the other private sections.
fn get_view_ids_should_be_filtered(&self, folder: &Folder) -> Vec<String> {
let trash_ids = self.get_all_trash_ids(folder);
let other_private_view_ids = self.get_other_private_view_ids(&folder);
let other_private_view_ids = self.get_other_private_view_ids(folder);
[trash_ids, other_private_view_ids].concat()
}

fn get_other_private_view_ids(&self, folder: &Folder) -> Vec<String> {
let private_view_ids = folder
let my_private_view_ids = folder
.get_my_private_sections()
.into_iter()
.map(|view| view.id)
Expand All @@ -1202,7 +1187,7 @@ impl FolderManager {

all_private_view_ids
.into_iter()
.filter(|id| private_view_ids.contains(id))
.filter(|id| !my_private_view_ids.contains(id))
.collect()
}
}
Expand Down Expand Up @@ -1242,6 +1227,21 @@ pub(crate) fn get_workspace_public_view_pbs(_workspace_id: &str, folder: &Folder
.collect()
}

/// Get all the child views belong to the view id, including the child views of the child views.
fn get_all_child_view_ids(folder: &Folder, view_id: &str) -> Vec<String> {
let child_view_ids = folder
.views
.get_views_belong_to(view_id)
.into_iter()
.map(|view| view.id.clone())
.collect::<Vec<String>>();
let mut all_child_view_ids = child_view_ids.clone();
for child_view_id in child_view_ids {
all_child_view_ids.extend(get_all_child_view_ids(folder, &child_view_id));
}
all_child_view_ids
}

/// Get the current private views of the user.
pub(crate) fn get_workspace_private_view_pbs(_workspace_id: &str, folder: &Folder) -> Vec<ViewPB> {
// get the trash ids
Expand Down

0 comments on commit 6d28a36

Please sign in to comment.