user_service.dart 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import 'dart:async';
  2. import 'package:appflowy_backend/dispatch/dispatch.dart';
  3. import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
  4. import 'package:appflowy_backend/protobuf/flowy-folder2/workspace.pb.dart';
  5. import 'package:appflowy_backend/protobuf/flowy-user/protobuf.dart';
  6. import 'package:dartz/dartz.dart';
  7. import 'package:fixnum/fixnum.dart';
  8. class UserBackendService {
  9. UserBackendService({
  10. required this.userId,
  11. });
  12. final Int64 userId;
  13. static Future<Either<FlowyError, UserProfilePB>>
  14. getCurrentUserProfile() async {
  15. final result = await UserEventGetUserProfile().send();
  16. return result.swap();
  17. }
  18. Future<Either<Unit, FlowyError>> updateUserProfile({
  19. String? name,
  20. String? password,
  21. String? email,
  22. String? iconUrl,
  23. String? openAIKey,
  24. String? stabilityAiKey,
  25. }) {
  26. final payload = UpdateUserProfilePayloadPB.create()..id = userId;
  27. if (name != null) {
  28. payload.name = name;
  29. }
  30. if (password != null) {
  31. payload.password = password;
  32. }
  33. if (email != null) {
  34. payload.email = email;
  35. }
  36. if (iconUrl != null) {
  37. payload.iconUrl = iconUrl;
  38. }
  39. if (openAIKey != null) {
  40. payload.openaiKey = openAIKey;
  41. }
  42. if (stabilityAiKey != null) {
  43. payload.stabilityAiKey = stabilityAiKey;
  44. }
  45. return UserEventUpdateUserProfile(payload).send();
  46. }
  47. Future<Either<Unit, FlowyError>> deleteWorkspace({
  48. required String workspaceId,
  49. }) {
  50. throw UnimplementedError();
  51. }
  52. static Future<Either<Unit, FlowyError>> signOut() {
  53. return UserEventSignOut().send();
  54. }
  55. Future<Either<Unit, FlowyError>> initUser() async {
  56. return UserEventInitUser().send();
  57. }
  58. static Future<Either<List<HistoricalUserPB>, FlowyError>>
  59. loadHistoricalUsers() async {
  60. return UserEventGetHistoricalUsers().send().then(
  61. (result) {
  62. return result.fold(
  63. (historicalUsers) => left(historicalUsers.items),
  64. (error) => right(error),
  65. );
  66. },
  67. );
  68. }
  69. static Future<Either<Unit, FlowyError>> openHistoricalUser(
  70. HistoricalUserPB user,
  71. ) async {
  72. return UserEventOpenHistoricalUser(user).send();
  73. }
  74. Future<Either<List<WorkspacePB>, FlowyError>> getWorkspaces() {
  75. final request = WorkspaceIdPB.create();
  76. return FolderEventReadAllWorkspaces(request).send().then((result) {
  77. return result.fold(
  78. (workspaces) => left(workspaces.items),
  79. (error) => right(error),
  80. );
  81. });
  82. }
  83. Future<Either<WorkspacePB, FlowyError>> openWorkspace(String workspaceId) {
  84. final request = WorkspaceIdPB.create()..value = workspaceId;
  85. return FolderEventOpenWorkspace(request).send().then((result) {
  86. return result.fold(
  87. (workspace) => left(workspace),
  88. (error) => right(error),
  89. );
  90. });
  91. }
  92. Future<Either<WorkspacePB, FlowyError>> createWorkspace(
  93. String name,
  94. String desc,
  95. ) {
  96. final request = CreateWorkspacePayloadPB.create()
  97. ..name = name
  98. ..desc = desc;
  99. return FolderEventCreateWorkspace(request).send().then((result) {
  100. return result.fold(
  101. (workspace) => left(workspace),
  102. (error) => right(error),
  103. );
  104. });
  105. }
  106. }