| 
									
										
										
										
											2022-02-28 23:37:41 -05:00
										 |  |  | import 'dart:async'; | 
					
						
							| 
									
										
										
										
											2022-07-03 16:52:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-04-04 08:41:16 +08:00
										 |  |  | import 'package:appflowy_backend/protobuf/flowy-folder2/workspace.pb.dart'; | 
					
						
							| 
									
										
										
										
											2023-10-09 23:14:24 +08:00
										 |  |  | import 'package:appflowy_backend/protobuf/flowy-user/protobuf.dart'; | 
					
						
							|  |  |  | import 'package:dartz/dartz.dart'; | 
					
						
							| 
									
										
										
										
											2023-04-04 08:41:16 +08:00
										 |  |  | import 'package:fixnum/fixnum.dart'; | 
					
						
							| 
									
										
										
										
											2022-02-28 23:37:41 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-26 16:27:17 +08:00
										 |  |  | class UserBackendService { | 
					
						
							|  |  |  |   UserBackendService({ | 
					
						
							| 
									
										
										
										
											2022-07-03 16:52:06 +08:00
										 |  |  |     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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-16 14:58:24 +08:00
										 |  |  |   static Future<Either<FlowyError, UserProfilePB>> | 
					
						
							|  |  |  |       getCurrentUserProfile() async { | 
					
						
							| 
									
										
										
										
											2023-09-21 12:41:52 +08:00
										 |  |  |     final result = await UserEventGetUserProfile().send(); | 
					
						
							| 
									
										
										
										
											2023-05-16 14:58:24 +08:00
										 |  |  |     return result.swap(); | 
					
						
							| 
									
										
										
										
											2022-02-28 23:37:41 -05:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-03 16:52:06 +08:00
										 |  |  |   Future<Either<Unit, FlowyError>> updateUserProfile({ | 
					
						
							|  |  |  |     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(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-10 15:10:42 +08:00
										 |  |  |   Future<Either<Unit, FlowyError>> deleteWorkspace({ | 
					
						
							|  |  |  |     required String workspaceId, | 
					
						
							|  |  |  |   }) { | 
					
						
							| 
									
										
										
										
											2022-02-28 23:37:41 -05:00
										 |  |  |     throw UnimplementedError(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-17 23:46:39 +08:00
										 |  |  |   static Future<Either<Unit, FlowyError>> signOut() { | 
					
						
							| 
									
										
										
										
											2023-07-14 13:37:13 +08:00
										 |  |  |     return UserEventSignOut().send(); | 
					
						
							| 
									
										
										
										
											2022-02-28 23:37:41 -05:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Future<Either<Unit, FlowyError>> initUser() async { | 
					
						
							|  |  |  |     return UserEventInitUser().send(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-09 10:13:49 +08:00
										 |  |  |   static Future<Either<List<HistoricalUserPB>, FlowyError>> | 
					
						
							| 
									
										
										
										
											2023-08-07 22:24:04 +08:00
										 |  |  |       loadHistoricalUsers() async { | 
					
						
							|  |  |  |     return UserEventGetHistoricalUsers().send().then( | 
					
						
							|  |  |  |       (result) { | 
					
						
							|  |  |  |         return result.fold( | 
					
						
							|  |  |  |           (historicalUsers) => left(historicalUsers.items), | 
					
						
							|  |  |  |           (error) => right(error), | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-09 10:13:49 +08:00
										 |  |  |   static Future<Either<Unit, FlowyError>> openHistoricalUser( | 
					
						
							| 
									
										
										
										
											2023-08-07 22:24:04 +08:00
										 |  |  |     HistoricalUserPB user, | 
					
						
							|  |  |  |   ) async { | 
					
						
							|  |  |  |     return UserEventOpenHistoricalUser(user).send(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-19 14:11:29 +08:00
										 |  |  |   Future<Either<List<WorkspacePB>, FlowyError>> getWorkspaces() { | 
					
						
							| 
									
										
										
										
											2023-11-01 11:45:35 +08:00
										 |  |  |     // final request = WorkspaceIdPB.create();
 | 
					
						
							|  |  |  |     // return FolderEventReadAllWorkspaces(request).send().then((result) {
 | 
					
						
							|  |  |  |     //   return result.fold(
 | 
					
						
							|  |  |  |     //     (workspaces) => left(workspaces.items),
 | 
					
						
							|  |  |  |     //     (error) => right(error),
 | 
					
						
							|  |  |  |     //   );
 | 
					
						
							|  |  |  |     // });
 | 
					
						
							|  |  |  |     return Future.value(left([])); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2022-02-28 23:37:41 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-05 14:00:24 +08:00
										 |  |  |   Future<Either<Unit, FlowyError>> openWorkspace(String workspaceId) { | 
					
						
							|  |  |  |     final payload = UserWorkspaceIdPB.create()..workspaceId = workspaceId; | 
					
						
							|  |  |  |     return UserEventOpenWorkspace(payload).send(); | 
					
						
							| 
									
										
										
										
											2022-02-28 23:37:41 -05:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-01 11:45:35 +08:00
										 |  |  |   Future<Either<WorkspacePB, FlowyError>> getCurrentWorkspace() { | 
					
						
							|  |  |  |     return FolderEventReadCurrentWorkspace().send().then((result) { | 
					
						
							| 
									
										
										
										
											2022-02-28 23:37:41 -05:00
										 |  |  |       return result.fold( | 
					
						
							|  |  |  |         (workspace) => left(workspace), | 
					
						
							|  |  |  |         (error) => right(error), | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-08 22:19:05 +08:00
										 |  |  |   Future<Either<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; | 
					
						
							|  |  |  |     return FolderEventCreateWorkspace(request).send().then((result) { | 
					
						
							|  |  |  |       return result.fold( | 
					
						
							|  |  |  |         (workspace) => left(workspace), | 
					
						
							|  |  |  |         (error) => right(error), | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } |