Bring projects, wikis, and teams together with AI. AppFlowy is the AI collaborative workspace where you achieve more without losing control of your data. The leading open source Notion alternative.
Dart blogconfluence-alternativecontent-managementcontent-servicesdocumentationflutternote-takingnotion-alternativeproject-managementtask-managementteam-collaborationwiki
chore: do not get workspace usage when current user is not owner
Nathan92179fe61cb7
diff --git a/frontend/appflowy_flutter/lib/workspace/application/menu/sidebar_sections_bloc.dart b/frontend/appflowy_flutter/lib/workspace/application/menu/sidebar_sections_bloc.dartindex 5a20b29c09..d6a6a73578 100644--- a/frontend/appflowy_flutter/lib/workspace/application/menu/sidebar_sections_bloc.dart+++ b/frontend/appflowy_flutter/lib/workspace/application/menu/sidebar_sections_bloc.dart@@ -244,7 +244,10 @@ class SidebarSectionsBloc}void _initial(UserProfilePB userProfile, String workspaceId) {- _workspaceService = WorkspaceService(workspaceId: workspaceId);+ _workspaceService = WorkspaceService(+ workspaceId: workspaceId,+ userId: userProfile.id,+ );_listener = WorkspaceSectionsListener(user: userProfile,diff --git a/frontend/appflowy_flutter/lib/workspace/application/settings/billing/settings_billing_bloc.dart b/frontend/appflowy_flutter/lib/workspace/application/settings/billing/settings_billing_bloc.dartindex ab324df87f..df880891e9 100644--- a/frontend/appflowy_flutter/lib/workspace/application/settings/billing/settings_billing_bloc.dart+++ b/frontend/appflowy_flutter/lib/workspace/application/settings/billing/settings_billing_bloc.dart@@ -29,7 +29,7 @@ class SettingsBillingBlocrequired Int64 userId,}) : super(const _Initial()) {_userService = UserBackendService(userId: userId);- _service = WorkspaceService(workspaceId: workspaceId);+ _service = WorkspaceService(workspaceId: workspaceId, userId: userId);_successListenable = getIt<SubscriptionSuccessListenable>();_successListenable.addListener(_onPaymentSuccessful);diff --git a/frontend/appflowy_flutter/lib/workspace/application/settings/plan/settings_plan_bloc.dart b/frontend/appflowy_flutter/lib/workspace/application/settings/plan/settings_plan_bloc.dartindex dcc5156937..26975b00ff 100644--- a/frontend/appflowy_flutter/lib/workspace/application/settings/plan/settings_plan_bloc.dart+++ b/frontend/appflowy_flutter/lib/workspace/application/settings/plan/settings_plan_bloc.dart@@ -23,7 +23,10 @@ class SettingsPlanBloc extends Bloc<SettingsPlanEvent, SettingsPlanState> {required this.workspaceId,required Int64 userId,}) : super(const _Initial()) {- _service = WorkspaceService(workspaceId: workspaceId);+ _service = WorkspaceService(+ workspaceId: workspaceId,+ userId: userId,+ );_userService = UserBackendService(userId: userId);_successListenable = getIt<SubscriptionSuccessListenable>();_successListenable.addListener(_onPaymentSuccessful);@@ -43,7 +46,7 @@ class SettingsPlanBloc extends Bloc<SettingsPlanEvent, SettingsPlanState> {FlowyError? error;final usageResult = snapshots.first.fold(- (s) => s as WorkspaceUsagePB,+ (s) => s as WorkspaceUsagePB?,(f) {error = f;return null;diff --git a/frontend/appflowy_flutter/lib/workspace/application/sidebar/billing/sidebar_plan_bloc.dart b/frontend/appflowy_flutter/lib/workspace/application/sidebar/billing/sidebar_plan_bloc.dartindex 2fae0e4872..56d6ae8cc8 100644--- a/frontend/appflowy_flutter/lib/workspace/application/sidebar/billing/sidebar_plan_bloc.dart+++ b/frontend/appflowy_flutter/lib/workspace/application/sidebar/billing/sidebar_plan_bloc.dart@@ -3,6 +3,7 @@ import 'dart:async';import 'package:appflowy/startup/startup.dart';import 'package:appflowy/workspace/application/settings/file_storage/file_storage_listener.dart';import 'package:appflowy/workspace/application/subscription_success_listenable/subscription_success_listenable.dart';+import 'package:appflowy/workspace/application/workspace/workspace_service.dart';import 'package:appflowy_backend/dispatch/dispatch.dart';import 'package:appflowy_backend/dispatch/error.dart';import 'package:appflowy_backend/log.dart';@@ -182,20 +183,24 @@ class SidebarPlanBloc extends Bloc<SidebarPlanEvent, SidebarPlanState> {);}- void _checkWorkspaceUsage() {- if (state.workspaceId != null) {- final payload = UserWorkspaceIdPB(workspaceId: state.workspaceId!);- UserEventGetWorkspaceUsage(payload).send().then((result) {- result.onSuccess(- (usage) {- if (isClosed) {- return;- }- add(SidebarPlanEvent.updateWorkspaceUsage(usage));- },- );- });+ Future<void> _checkWorkspaceUsage() async {+ if (state.workspaceId == null || state.userProfile == null) {+ return;}++ await WorkspaceService(+ workspaceId: state.workspaceId!,+ userId: state.userProfile!.id,+ ).getWorkspaceUsage().then((result) {+ result.fold(+ (usage) {+ if (!isClosed && usage != null) {+ add(SidebarPlanEvent.updateWorkspaceUsage(usage));+ }+ },+ (error) => Log.error("Failed to get workspace usage: $error"),+ );+ });}}diff --git a/frontend/appflowy_flutter/lib/workspace/application/sidebar/space/space_bloc.dart b/frontend/appflowy_flutter/lib/workspace/application/sidebar/space/space_bloc.dartindex d4c1b4fe16..6d6ce05051 100644--- a/frontend/appflowy_flutter/lib/workspace/application/sidebar/space/space_bloc.dart+++ b/frontend/appflowy_flutter/lib/workspace/application/sidebar/space/space_bloc.dart@@ -486,7 +486,10 @@ class SpaceBloc extends Bloc<SpaceEvent, SpaceState> {}void _initial(UserProfilePB userProfile, String workspaceId) {- _workspaceService = WorkspaceService(workspaceId: workspaceId);+ _workspaceService = WorkspaceService(+ workspaceId: workspaceId,+ userId: userProfile.id,+ );this.userProfile = userProfile;this.workspaceId = workspaceId;diff --git a/frontend/appflowy_flutter/lib/workspace/application/workspace/workspace_service.dart b/frontend/appflowy_flutter/lib/workspace/application/workspace/workspace_service.dartindex b958e5cd30..ae6220994e 100644--- a/frontend/appflowy_flutter/lib/workspace/application/workspace/workspace_service.dart+++ b/frontend/appflowy_flutter/lib/workspace/application/workspace/workspace_service.dart@@ -1,15 +1,18 @@import 'dart:async';+import 'package:appflowy/shared/af_role_pb_extension.dart';import 'package:appflowy_backend/dispatch/dispatch.dart';import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';import 'package:appflowy_backend/protobuf/flowy-folder/protobuf.dart';import 'package:appflowy_backend/protobuf/flowy-user/workspace.pb.dart';import 'package:appflowy_result/appflowy_result.dart';+import 'package:fixnum/fixnum.dart' as fixnum;class WorkspaceService {- WorkspaceService({required this.workspaceId});+ WorkspaceService({required this.workspaceId, required this.userId});final String workspaceId;+ final fixnum.Int64 userId;Future<FlowyResult<ViewPB, FlowyError>> createView({required String name,@@ -82,7 +85,18 @@ class WorkspaceService {return FolderEventMoveView(payload).send();}- Future<FlowyResult<WorkspaceUsagePB, FlowyError>> getWorkspaceUsage() {+ Future<FlowyResult<WorkspaceUsagePB?, FlowyError>> getWorkspaceUsage() async {+ final request = WorkspaceMemberIdPB()..uid = userId;+ final result = await UserEventGetMemberInfo(request).send();+ final isOwner = result.fold(+ (member) => member.role.isOwner,+ (_) => false,+ );++ if (!isOwner) {+ return FlowyResult.success(null);+ }+final payload = UserWorkspaceIdPB(workspaceId: workspaceId);return UserEventGetWorkspaceUsage(payload).send();}diff --git a/frontend/appflowy_flutter/test/util.dart b/frontend/appflowy_flutter/test/util.dartindex c4f2a21c64..3bb774411b 100644--- a/frontend/appflowy_flutter/test/util.dart+++ b/frontend/appflowy_flutter/test/util.dart@@ -69,7 +69,10 @@ class AppFlowyUnitTest {}Future<void> _initialServices() async {- workspaceService = WorkspaceService(workspaceId: currentWorkspace.id);+ workspaceService = WorkspaceService(+ workspaceId: currentWorkspace.id,+ userId: userProfile.id,+ );}Future<ViewPB> createWorkspace() async {diff --git a/frontend/rust-lib/flowy-user/src/event_handler.rs b/frontend/rust-lib/flowy-user/src/event_handler.rsindex 0e42525d04..85b9274df3 100644--- a/frontend/rust-lib/flowy-user/src/event_handler.rs+++ b/frontend/rust-lib/flowy-user/src/event_handler.rs@@ -896,7 +896,10 @@ pub async fn get_workspace_member_info(manager: AFPluginState<Weak<UserManager>>,) -> DataResult<WorkspaceMemberPB, FlowyError> {let manager = upgrade_manager(manager)?;- let member = manager.get_workspace_member_info(param.uid).await?;+ let workspace_id = manager.get_session()?.user_workspace.workspace_id()?;+ let member = manager+ .get_workspace_member_info(param.uid, &workspace_id)+ .await?;data_result_ok(member.into())}diff --git a/frontend/rust-lib/flowy-user/src/user_manager/manager_user_workspace.rs b/frontend/rust-lib/flowy-user/src/user_manager/manager_user_workspace.rsindex 496019cd2e..507916acf3 100644--- a/frontend/rust-lib/flowy-user/src/user_manager/manager_user_workspace.rs+++ b/frontend/rust-lib/flowy-user/src/user_manager/manager_user_workspace.rs@@ -583,8 +583,11 @@ impl UserManager {Ok(UseAISettingPB::from(settings))}- pub async fn get_workspace_member_info(&self, uid: i64) -> FlowyResult<WorkspaceMember> {- let workspace_id = self.get_session()?.user_workspace.workspace_id()?;+ pub async fn get_workspace_member_info(+ &self,+ uid: i64,+ workspace_id: &Uuid,+ ) -> FlowyResult<WorkspaceMember> {let db = self.authenticate_user.get_sqlite_connection(uid)?;// Can opt in using memory cacheif let Ok(member_record) = select_workspace_member(db, &workspace_id.to_string(), uid) {