user_service.dart 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import 'dart:async';
  2. import 'package:appflowy_backend/log.dart';
  3. import 'package:appflowy_backend/protobuf/flowy-user/protobuf.dart';
  4. import 'package:dartz/dartz.dart';
  5. import 'package:appflowy_backend/dispatch/dispatch.dart';
  6. import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
  7. import 'package:appflowy_backend/protobuf/flowy-folder2/workspace.pb.dart';
  8. import 'package:fixnum/fixnum.dart';
  9. class UserBackendService {
  10. UserBackendService({
  11. required this.userId,
  12. });
  13. final Int64 userId;
  14. static Future<Either<FlowyError, UserProfilePB>>
  15. getCurrentUserProfile() async {
  16. final result = await UserEventGetUserProfile().send().then((value) {
  17. value.fold((l) => null, (r) => Log.info(r));
  18. return value;
  19. });
  20. return result.swap();
  21. }
  22. Future<Either<Unit, FlowyError>> updateUserProfile({
  23. String? name,
  24. String? password,
  25. String? email,
  26. String? iconUrl,
  27. String? openAIKey,
  28. }) {
  29. final payload = UpdateUserProfilePayloadPB.create()..id = userId;
  30. if (name != null) {
  31. payload.name = name;
  32. }
  33. if (password != null) {
  34. payload.password = password;
  35. }
  36. if (email != null) {
  37. payload.email = email;
  38. }
  39. if (iconUrl != null) {
  40. payload.iconUrl = iconUrl;
  41. }
  42. if (openAIKey != null) {
  43. payload.openaiKey = openAIKey;
  44. }
  45. return UserEventUpdateUserProfile(payload).send();
  46. }
  47. Future<Either<Unit, FlowyError>> deleteWorkspace({
  48. required String workspaceId,
  49. }) {
  50. throw UnimplementedError();
  51. }
  52. 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. }