| 
									
										
										
										
											2022-02-28 23:37:41 -05:00
										 |  |  | import 'dart:async'; | 
					
						
							| 
									
										
										
										
											2022-07-03 16:52:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-12 17:08:55 +02:00
										 |  |  | import 'package:appflowy/env/cloud_env.dart'; | 
					
						
							|  |  |  | import 'package:appflowy/startup/startup.dart'; | 
					
						
							| 
									
										
										
										
											2023-01-08 12:10:53 +08:00
										 |  |  | import 'package:appflowy_backend/dispatch/dispatch.dart'; | 
					
						
							|  |  |  | import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart'; | 
					
						
							| 
									
										
										
										
											2023-12-31 07:29:40 +08:00
										 |  |  | import 'package:appflowy_backend/protobuf/flowy-folder/workspace.pb.dart'; | 
					
						
							| 
									
										
										
										
											2023-10-09 23:14:24 +08:00
										 |  |  | import 'package:appflowy_backend/protobuf/flowy-user/protobuf.dart'; | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  | import 'package:appflowy_result/appflowy_result.dart'; | 
					
						
							| 
									
										
										
										
											2023-04-04 08:41:16 +08:00
										 |  |  | import 'package:fixnum/fixnum.dart'; | 
					
						
							| 
									
										
										
										
											2022-02-28 23:37:41 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-17 14:30:19 +02:00
										 |  |  | abstract class IUserBackendService { | 
					
						
							|  |  |  |   Future<FlowyResult<void, FlowyError>> cancelSubscription(String workspaceId); | 
					
						
							|  |  |  |   Future<FlowyResult<PaymentLinkPB, FlowyError>> createSubscription( | 
					
						
							|  |  |  |     String workspaceId, | 
					
						
							|  |  |  |     SubscriptionPlanPB plan, | 
					
						
							|  |  |  |   ); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class UserBackendService implements IUserBackendService { | 
					
						
							| 
									
										
										
										
											2024-06-12 17:08:55 +02:00
										 |  |  |   UserBackendService({required this.userId}); | 
					
						
							| 
									
										
										
										
											2023-02-16 10:17:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-04 08:41:16 +08:00
										 |  |  |   final Int64 userId; | 
					
						
							| 
									
										
										
										
											2023-02-16 10:17:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |   static Future<FlowyResult<UserProfilePB, FlowyError>> | 
					
						
							| 
									
										
										
										
											2023-05-16 14:58:24 +08:00
										 |  |  |       getCurrentUserProfile() async { | 
					
						
							| 
									
										
										
										
											2023-09-21 12:41:52 +08:00
										 |  |  |     final result = await UserEventGetUserProfile().send(); | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |     return result; | 
					
						
							| 
									
										
										
										
											2022-02-28 23:37:41 -05:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |   Future<FlowyResult<void, FlowyError>> updateUserProfile({ | 
					
						
							| 
									
										
										
										
											2022-07-03 16:52:06 +08:00
										 |  |  |     String? name, | 
					
						
							|  |  |  |     String? password, | 
					
						
							|  |  |  |     String? email, | 
					
						
							| 
									
										
										
										
											2022-08-08 22:19:05 +08:00
										 |  |  |     String? iconUrl, | 
					
						
							| 
									
										
										
										
											2023-02-14 10:04:36 +08:00
										 |  |  |     String? openAIKey, | 
					
						
							| 
									
										
										
										
											2023-10-09 23:14:24 +08:00
										 |  |  |     String? stabilityAiKey, | 
					
						
							| 
									
										
										
										
											2022-07-03 16:52:06 +08:00
										 |  |  |   }) { | 
					
						
							| 
									
										
										
										
											2023-06-07 13:55:37 +05:30
										 |  |  |     final payload = UpdateUserProfilePayloadPB.create()..id = userId; | 
					
						
							| 
									
										
										
										
											2022-07-03 16:52:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (name != null) { | 
					
						
							|  |  |  |       payload.name = name; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (password != null) { | 
					
						
							|  |  |  |       payload.password = password; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (email != null) { | 
					
						
							|  |  |  |       payload.email = email; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-08 22:19:05 +08:00
										 |  |  |     if (iconUrl != null) { | 
					
						
							|  |  |  |       payload.iconUrl = iconUrl; | 
					
						
							| 
									
										
										
										
											2022-08-06 22:31:55 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-14 10:04:36 +08:00
										 |  |  |     if (openAIKey != null) { | 
					
						
							|  |  |  |       payload.openaiKey = openAIKey; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-09 23:14:24 +08:00
										 |  |  |     if (stabilityAiKey != null) { | 
					
						
							|  |  |  |       payload.stabilityAiKey = stabilityAiKey; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-03 16:52:06 +08:00
										 |  |  |     return UserEventUpdateUserProfile(payload).send(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |   Future<FlowyResult<void, FlowyError>> deleteWorkspace({ | 
					
						
							| 
									
										
										
										
											2023-04-10 15:10:42 +08:00
										 |  |  |     required String workspaceId, | 
					
						
							|  |  |  |   }) { | 
					
						
							| 
									
										
										
										
											2022-02-28 23:37:41 -05:00
										 |  |  |     throw UnimplementedError(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-11 16:33:28 +08:00
										 |  |  |   static Future<FlowyResult<UserProfilePB, FlowyError>> signInWithMagicLink( | 
					
						
							|  |  |  |     String email, | 
					
						
							|  |  |  |     String redirectTo, | 
					
						
							|  |  |  |   ) async { | 
					
						
							|  |  |  |     final payload = MagicLinkSignInPB(email: email, redirectTo: redirectTo); | 
					
						
							|  |  |  |     return UserEventMagicLinkSignIn(payload).send(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |   static Future<FlowyResult<void, FlowyError>> signOut() { | 
					
						
							| 
									
										
										
										
											2023-07-14 13:37:13 +08:00
										 |  |  |     return UserEventSignOut().send(); | 
					
						
							| 
									
										
										
										
											2022-02-28 23:37:41 -05:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |   Future<FlowyResult<void, FlowyError>> initUser() async { | 
					
						
							| 
									
										
										
										
											2022-02-28 23:37:41 -05:00
										 |  |  |     return UserEventInitUser().send(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |   static Future<FlowyResult<UserProfilePB, FlowyError>> getAnonUser() async { | 
					
						
							| 
									
										
										
										
											2023-12-21 14:13:21 +08:00
										 |  |  |     return UserEventGetAnonUser().send(); | 
					
						
							| 
									
										
										
										
											2023-08-07 22:24:04 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |   static Future<FlowyResult<void, FlowyError>> openAnonUser() async { | 
					
						
							| 
									
										
										
										
											2023-12-21 14:13:21 +08:00
										 |  |  |     return UserEventOpenAnonUser().send(); | 
					
						
							| 
									
										
										
										
											2023-08-07 22:24:04 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-04 09:43:00 +07:00
										 |  |  |   Future<FlowyResult<List<UserWorkspacePB>, FlowyError>> getWorkspaces() { | 
					
						
							|  |  |  |     return UserEventGetAllWorkspace().send().then((value) { | 
					
						
							|  |  |  |       return value.fold( | 
					
						
							|  |  |  |         (workspaces) => FlowyResult.success(workspaces.items), | 
					
						
							|  |  |  |         (error) => FlowyResult.failure(error), | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2023-11-01 11:45:35 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2022-02-28 23:37:41 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |   Future<FlowyResult<void, FlowyError>> openWorkspace(String workspaceId) { | 
					
						
							| 
									
										
										
										
											2023-11-05 14:00:24 +08:00
										 |  |  |     final payload = UserWorkspaceIdPB.create()..workspaceId = workspaceId; | 
					
						
							|  |  |  |     return UserEventOpenWorkspace(payload).send(); | 
					
						
							| 
									
										
										
										
											2022-02-28 23:37:41 -05:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-27 09:57:06 +08:00
										 |  |  |   static Future<FlowyResult<WorkspacePB, FlowyError>> getCurrentWorkspace() { | 
					
						
							| 
									
										
										
										
											2023-11-01 11:45:35 +08:00
										 |  |  |     return FolderEventReadCurrentWorkspace().send().then((result) { | 
					
						
							| 
									
										
										
										
											2022-02-28 23:37:41 -05:00
										 |  |  |       return result.fold( | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |         (workspace) => FlowyResult.success(workspace), | 
					
						
							|  |  |  |         (error) => FlowyResult.failure(error), | 
					
						
							| 
									
										
										
										
											2022-02-28 23:37:41 -05:00
										 |  |  |       ); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |   Future<FlowyResult<WorkspacePB, FlowyError>> createWorkspace( | 
					
						
							| 
									
										
										
										
											2023-04-10 15:10:42 +08:00
										 |  |  |     String name, | 
					
						
							|  |  |  |     String desc, | 
					
						
							|  |  |  |   ) { | 
					
						
							| 
									
										
										
										
											2022-07-19 14:11:29 +08:00
										 |  |  |     final request = CreateWorkspacePayloadPB.create() | 
					
						
							| 
									
										
										
										
											2022-02-28 23:37:41 -05:00
										 |  |  |       ..name = name | 
					
						
							|  |  |  |       ..desc = desc; | 
					
						
							| 
									
										
										
										
											2024-03-13 15:07:52 +08:00
										 |  |  |     return FolderEventCreateFolderWorkspace(request).send().then((result) { | 
					
						
							| 
									
										
										
										
											2022-02-28 23:37:41 -05:00
										 |  |  |       return result.fold( | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |         (workspace) => FlowyResult.success(workspace), | 
					
						
							|  |  |  |         (error) => FlowyResult.failure(error), | 
					
						
							| 
									
										
										
										
											2022-02-28 23:37:41 -05:00
										 |  |  |       ); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2024-03-04 09:43:00 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   Future<FlowyResult<UserWorkspacePB, FlowyError>> createUserWorkspace( | 
					
						
							|  |  |  |     String name, | 
					
						
							|  |  |  |   ) { | 
					
						
							|  |  |  |     final request = CreateWorkspacePB.create()..name = name; | 
					
						
							|  |  |  |     return UserEventCreateWorkspace(request).send(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Future<FlowyResult<void, FlowyError>> deleteWorkspaceById( | 
					
						
							|  |  |  |     String workspaceId, | 
					
						
							|  |  |  |   ) { | 
					
						
							|  |  |  |     final request = UserWorkspaceIdPB.create()..workspaceId = workspaceId; | 
					
						
							|  |  |  |     return UserEventDeleteWorkspace(request).send(); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2024-03-05 13:51:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   Future<FlowyResult<void, FlowyError>> renameWorkspace( | 
					
						
							|  |  |  |     String workspaceId, | 
					
						
							|  |  |  |     String name, | 
					
						
							|  |  |  |   ) { | 
					
						
							|  |  |  |     final request = RenameWorkspacePB() | 
					
						
							|  |  |  |       ..workspaceId = workspaceId | 
					
						
							|  |  |  |       ..newName = name; | 
					
						
							|  |  |  |     return UserEventRenameWorkspace(request).send(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Future<FlowyResult<void, FlowyError>> updateWorkspaceIcon( | 
					
						
							|  |  |  |     String workspaceId, | 
					
						
							|  |  |  |     String icon, | 
					
						
							|  |  |  |   ) { | 
					
						
							|  |  |  |     final request = ChangeWorkspaceIconPB() | 
					
						
							|  |  |  |       ..workspaceId = workspaceId | 
					
						
							|  |  |  |       ..newIcon = icon; | 
					
						
							|  |  |  |     return UserEventChangeWorkspaceIcon(request).send(); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2024-03-21 11:02:03 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   Future<FlowyResult<RepeatedWorkspaceMemberPB, FlowyError>> | 
					
						
							|  |  |  |       getWorkspaceMembers( | 
					
						
							|  |  |  |     String workspaceId, | 
					
						
							|  |  |  |   ) async { | 
					
						
							|  |  |  |     final data = QueryWorkspacePB()..workspaceId = workspaceId; | 
					
						
							| 
									
										
										
										
											2024-06-17 14:30:19 +02:00
										 |  |  |     return UserEventGetWorkspaceMembers(data).send(); | 
					
						
							| 
									
										
										
										
											2024-03-21 11:02:03 +07:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Future<FlowyResult<void, FlowyError>> addWorkspaceMember( | 
					
						
							|  |  |  |     String workspaceId, | 
					
						
							|  |  |  |     String email, | 
					
						
							|  |  |  |   ) async { | 
					
						
							|  |  |  |     final data = AddWorkspaceMemberPB() | 
					
						
							|  |  |  |       ..workspaceId = workspaceId | 
					
						
							|  |  |  |       ..email = email; | 
					
						
							|  |  |  |     return UserEventAddWorkspaceMember(data).send(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-30 21:38:46 +08:00
										 |  |  |   Future<FlowyResult<void, FlowyError>> 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(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-21 11:02:03 +07:00
										 |  |  |   Future<FlowyResult<void, FlowyError>> removeWorkspaceMember( | 
					
						
							|  |  |  |     String workspaceId, | 
					
						
							|  |  |  |     String email, | 
					
						
							|  |  |  |   ) async { | 
					
						
							|  |  |  |     final data = RemoveWorkspaceMemberPB() | 
					
						
							|  |  |  |       ..workspaceId = workspaceId | 
					
						
							|  |  |  |       ..email = email; | 
					
						
							|  |  |  |     return UserEventRemoveWorkspaceMember(data).send(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Future<FlowyResult<void, FlowyError>> updateWorkspaceMember( | 
					
						
							|  |  |  |     String workspaceId, | 
					
						
							|  |  |  |     String email, | 
					
						
							|  |  |  |     AFRolePB role, | 
					
						
							|  |  |  |   ) async { | 
					
						
							|  |  |  |     final data = UpdateWorkspaceMemberPB() | 
					
						
							|  |  |  |       ..workspaceId = workspaceId | 
					
						
							|  |  |  |       ..email = email | 
					
						
							|  |  |  |       ..role = role; | 
					
						
							|  |  |  |     return UserEventUpdateWorkspaceMember(data).send(); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2024-03-29 19:01:43 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   Future<FlowyResult<void, FlowyError>> leaveWorkspace( | 
					
						
							|  |  |  |     String workspaceId, | 
					
						
							|  |  |  |   ) async { | 
					
						
							|  |  |  |     final data = UserWorkspaceIdPB.create()..workspaceId = workspaceId; | 
					
						
							|  |  |  |     return UserEventLeaveWorkspace(data).send(); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2024-06-12 17:08:55 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   static Future<FlowyResult<RepeatedWorkspaceSubscriptionPB, FlowyError>> | 
					
						
							|  |  |  |       getWorkspaceSubscriptions() { | 
					
						
							|  |  |  |     return UserEventGetWorkspaceSubscriptions().send(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-17 14:30:19 +02:00
										 |  |  |   Future<FlowyResult<WorkspaceMemberPB, FlowyError>> | 
					
						
							|  |  |  |       getWorkspaceMember() async { | 
					
						
							|  |  |  |     final data = WorkspaceMemberIdPB.create()..uid = userId; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return UserEventGetMemberInfo(data).send(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   @override | 
					
						
							|  |  |  |   Future<FlowyResult<PaymentLinkPB, FlowyError>> createSubscription( | 
					
						
							| 
									
										
										
										
											2024-06-12 17:08:55 +02:00
										 |  |  |     String workspaceId, | 
					
						
							|  |  |  |     SubscriptionPlanPB plan, | 
					
						
							|  |  |  |   ) { | 
					
						
							|  |  |  |     final request = SubscribeWorkspacePB() | 
					
						
							|  |  |  |       ..workspaceId = workspaceId | 
					
						
							| 
									
										
										
										
											2024-06-17 14:30:19 +02:00
										 |  |  |       ..recurringInterval = RecurringIntervalPB.Year | 
					
						
							| 
									
										
										
										
											2024-06-12 17:08:55 +02:00
										 |  |  |       ..workspaceSubscriptionPlan = plan | 
					
						
							|  |  |  |       ..successUrl = | 
					
						
							|  |  |  |           '${getIt<AppFlowyCloudSharedEnv>().appflowyCloudConfig.base_url}/web/payment-success'; | 
					
						
							|  |  |  |     return UserEventSubscribeWorkspace(request).send(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-17 14:30:19 +02:00
										 |  |  |   @override | 
					
						
							|  |  |  |   Future<FlowyResult<void, FlowyError>> cancelSubscription( | 
					
						
							| 
									
										
										
										
											2024-06-12 17:08:55 +02:00
										 |  |  |     String workspaceId, | 
					
						
							|  |  |  |   ) { | 
					
						
							|  |  |  |     final request = UserWorkspaceIdPB()..workspaceId = workspaceId; | 
					
						
							|  |  |  |     return UserEventCancelWorkspaceSubscription(request).send(); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2022-02-28 23:37:41 -05:00
										 |  |  | } |