user_service.dart 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. var 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(AuthTypePB authType) {
  49. final payload = SignOutPB()..authType = authType;
  50. return UserEventSignOut(payload).send();
  51. }
  52. Future<Either<Unit, FlowyError>> initUser() async {
  53. return UserEventInitUser().send();
  54. }
  55. Future<Either<List<WorkspacePB>, FlowyError>> getWorkspaces() {
  56. final request = WorkspaceIdPB.create();
  57. return FolderEventReadAllWorkspaces(request).send().then((result) {
  58. return result.fold(
  59. (workspaces) => left(workspaces.items),
  60. (error) => right(error),
  61. );
  62. });
  63. }
  64. Future<Either<WorkspacePB, FlowyError>> openWorkspace(String workspaceId) {
  65. final request = WorkspaceIdPB.create()..value = workspaceId;
  66. return FolderEventOpenWorkspace(request).send().then((result) {
  67. return result.fold(
  68. (workspace) => left(workspace),
  69. (error) => right(error),
  70. );
  71. });
  72. }
  73. Future<Either<WorkspacePB, FlowyError>> createWorkspace(
  74. String name,
  75. String desc,
  76. ) {
  77. final request = CreateWorkspacePayloadPB.create()
  78. ..name = name
  79. ..desc = desc;
  80. return FolderEventCreateWorkspace(request).send().then((result) {
  81. return result.fold(
  82. (workspace) => left(workspace),
  83. (error) => right(error),
  84. );
  85. });
  86. }
  87. }