import 'dart:async'; import 'package:appflowy/env/cloud_env.dart'; import 'package:appflowy/startup/startup.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/workspace.pb.dart'; import 'package:appflowy_backend/protobuf/flowy-user/protobuf.dart'; import 'package:appflowy_result/appflowy_result.dart'; import 'package:fixnum/fixnum.dart'; abstract class IUserBackendService { Future> cancelSubscription(String workspaceId); Future> createSubscription( String workspaceId, SubscriptionPlanPB plan, ); } class UserBackendService implements IUserBackendService { UserBackendService({required this.userId}); final Int64 userId; static Future> getCurrentUserProfile() async { final result = await UserEventGetUserProfile().send(); return result; } Future> updateUserProfile({ String? name, String? password, String? email, String? iconUrl, String? openAIKey, String? stabilityAiKey, }) { final payload = UpdateUserProfilePayloadPB.create()..id = userId; if (name != null) { payload.name = name; } if (password != null) { payload.password = password; } if (email != null) { payload.email = email; } if (iconUrl != null) { payload.iconUrl = iconUrl; } if (openAIKey != null) { payload.openaiKey = openAIKey; } if (stabilityAiKey != null) { payload.stabilityAiKey = stabilityAiKey; } return UserEventUpdateUserProfile(payload).send(); } Future> deleteWorkspace({ required String workspaceId, }) { throw UnimplementedError(); } static Future> signInWithMagicLink( String email, String redirectTo, ) async { final payload = MagicLinkSignInPB(email: email, redirectTo: redirectTo); return UserEventMagicLinkSignIn(payload).send(); } static Future> signOut() { return UserEventSignOut().send(); } Future> initUser() async { return UserEventInitUser().send(); } static Future> getAnonUser() async { return UserEventGetAnonUser().send(); } static Future> openAnonUser() async { return UserEventOpenAnonUser().send(); } Future, FlowyError>> getWorkspaces() { return UserEventGetAllWorkspace().send().then((value) { return value.fold( (workspaces) => FlowyResult.success(workspaces.items), (error) => FlowyResult.failure(error), ); }); } Future> openWorkspace(String workspaceId) { final payload = UserWorkspaceIdPB.create()..workspaceId = workspaceId; return UserEventOpenWorkspace(payload).send(); } Future> getCurrentWorkspace() { return FolderEventReadCurrentWorkspace().send().then((result) { return result.fold( (workspace) => FlowyResult.success(workspace), (error) => FlowyResult.failure(error), ); }); } Future> createWorkspace( String name, String desc, ) { final request = CreateWorkspacePayloadPB.create() ..name = name ..desc = desc; return FolderEventCreateFolderWorkspace(request).send().then((result) { return result.fold( (workspace) => FlowyResult.success(workspace), (error) => FlowyResult.failure(error), ); }); } Future> createUserWorkspace( String name, ) { final request = CreateWorkspacePB.create()..name = name; return UserEventCreateWorkspace(request).send(); } Future> deleteWorkspaceById( String workspaceId, ) { final request = UserWorkspaceIdPB.create()..workspaceId = workspaceId; return UserEventDeleteWorkspace(request).send(); } Future> renameWorkspace( String workspaceId, String name, ) { final request = RenameWorkspacePB() ..workspaceId = workspaceId ..newName = name; return UserEventRenameWorkspace(request).send(); } Future> updateWorkspaceIcon( String workspaceId, String icon, ) { final request = ChangeWorkspaceIconPB() ..workspaceId = workspaceId ..newIcon = icon; return UserEventChangeWorkspaceIcon(request).send(); } Future> getWorkspaceMembers( String workspaceId, ) async { final data = QueryWorkspacePB()..workspaceId = workspaceId; return UserEventGetWorkspaceMembers(data).send(); } Future> addWorkspaceMember( String workspaceId, String email, ) async { final data = AddWorkspaceMemberPB() ..workspaceId = workspaceId ..email = email; return UserEventAddWorkspaceMember(data).send(); } Future> inviteWorkspaceMember( String workspaceId, String email, { AFRolePB? role, }) async { final data = WorkspaceMemberInvitationPB() ..workspaceId = workspaceId ..inviteeEmail = email; if (role != null) { data.role = role; } return UserEventInviteWorkspaceMember(data).send(); } Future> removeWorkspaceMember( String workspaceId, String email, ) async { final data = RemoveWorkspaceMemberPB() ..workspaceId = workspaceId ..email = email; return UserEventRemoveWorkspaceMember(data).send(); } Future> updateWorkspaceMember( String workspaceId, String email, AFRolePB role, ) async { final data = UpdateWorkspaceMemberPB() ..workspaceId = workspaceId ..email = email ..role = role; return UserEventUpdateWorkspaceMember(data).send(); } Future> leaveWorkspace( String workspaceId, ) async { final data = UserWorkspaceIdPB.create()..workspaceId = workspaceId; return UserEventLeaveWorkspace(data).send(); } static Future> getWorkspaceSubscriptions() { return UserEventGetWorkspaceSubscriptions().send(); } Future> getWorkspaceMember() async { final data = WorkspaceMemberIdPB.create()..uid = userId; return UserEventGetMemberInfo(data).send(); } @override Future> createSubscription( String workspaceId, SubscriptionPlanPB plan, ) { final request = SubscribeWorkspacePB() ..workspaceId = workspaceId ..recurringInterval = RecurringIntervalPB.Year ..workspaceSubscriptionPlan = plan ..successUrl = '${getIt().appflowyCloudConfig.base_url}/web/payment-success'; return UserEventSubscribeWorkspace(request).send(); } @override Future> cancelSubscription( String workspaceId, ) { final request = UserWorkspaceIdPB()..workspaceId = workspaceId; return UserEventCancelWorkspaceSubscription(request).send(); } }