user_service.dart 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import 'dart:async';
  2. import 'package:appflowy_backend/protobuf/flowy-user/protobuf.dart';
  3. import 'package:dartz/dartz.dart';
  4. import 'package:appflowy_backend/dispatch/dispatch.dart';
  5. import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
  6. import 'package:appflowy_backend/protobuf/flowy-folder2/workspace.pb.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. }) {
  25. final payload = UpdateUserProfilePayloadPB.create()..id = userId;
  26. if (name != null) {
  27. payload.name = name;
  28. }
  29. if (password != null) {
  30. payload.password = password;
  31. }
  32. if (email != null) {
  33. payload.email = email;
  34. }
  35. if (iconUrl != null) {
  36. payload.iconUrl = iconUrl;
  37. }
  38. if (openAIKey != null) {
  39. payload.openaiKey = openAIKey;
  40. }
  41. return UserEventUpdateUserProfile(payload).send();
  42. }
  43. Future<Either<Unit, FlowyError>> deleteWorkspace({
  44. required String workspaceId,
  45. }) {
  46. throw UnimplementedError();
  47. }
  48. Future<Either<Unit, FlowyError>> signOut() {
  49. return UserEventSignOut().send();
  50. }
  51. Future<Either<Unit, FlowyError>> initUser() async {
  52. return UserEventInitUser().send();
  53. }
  54. Future<Either<List<WorkspacePB>, FlowyError>> getWorkspaces() {
  55. final request = WorkspaceIdPB.create();
  56. return FolderEventReadAllWorkspaces(request).send().then((result) {
  57. return result.fold(
  58. (workspaces) => left(workspaces.items),
  59. (error) => right(error),
  60. );
  61. });
  62. }
  63. Future<Either<WorkspacePB, FlowyError>> openWorkspace(String workspaceId) {
  64. final request = WorkspaceIdPB.create()..value = workspaceId;
  65. return FolderEventOpenWorkspace(request).send().then((result) {
  66. return result.fold(
  67. (workspace) => left(workspace),
  68. (error) => right(error),
  69. );
  70. });
  71. }
  72. Future<Either<WorkspacePB, FlowyError>> createWorkspace(
  73. String name,
  74. String desc,
  75. ) {
  76. final request = CreateWorkspacePayloadPB.create()
  77. ..name = name
  78. ..desc = desc;
  79. return FolderEventCreateWorkspace(request).send().then((result) {
  80. return result.fold(
  81. (workspace) => left(workspace),
  82. (error) => right(error),
  83. );
  84. });
  85. }
  86. }