user_service.dart 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. static Future<Either<Unit, FlowyError>> signOut() {
  49. return UserEventSignOut().send();
  50. }
  51. Future<Either<Unit, FlowyError>> initUser() async {
  52. return UserEventInitUser().send();
  53. }
  54. static Future<Either<List<HistoricalUserPB>, FlowyError>>
  55. loadHistoricalUsers() async {
  56. return UserEventGetHistoricalUsers().send().then(
  57. (result) {
  58. return result.fold(
  59. (historicalUsers) => left(historicalUsers.items),
  60. (error) => right(error),
  61. );
  62. },
  63. );
  64. }
  65. static Future<Either<Unit, FlowyError>> openHistoricalUser(
  66. HistoricalUserPB user,
  67. ) async {
  68. return UserEventOpenHistoricalUser(user).send();
  69. }
  70. Future<Either<List<WorkspacePB>, FlowyError>> getWorkspaces() {
  71. final request = WorkspaceIdPB.create();
  72. return FolderEventReadAllWorkspaces(request).send().then((result) {
  73. return result.fold(
  74. (workspaces) => left(workspaces.items),
  75. (error) => right(error),
  76. );
  77. });
  78. }
  79. Future<Either<WorkspacePB, FlowyError>> openWorkspace(String workspaceId) {
  80. final request = WorkspaceIdPB.create()..value = workspaceId;
  81. return FolderEventOpenWorkspace(request).send().then((result) {
  82. return result.fold(
  83. (workspace) => left(workspace),
  84. (error) => right(error),
  85. );
  86. });
  87. }
  88. Future<Either<WorkspacePB, FlowyError>> createWorkspace(
  89. String name,
  90. String desc,
  91. ) {
  92. final request = CreateWorkspacePayloadPB.create()
  93. ..name = name
  94. ..desc = desc;
  95. return FolderEventCreateWorkspace(request).send().then((result) {
  96. return result.fold(
  97. (workspace) => left(workspace),
  98. (error) => right(error),
  99. );
  100. });
  101. }
  102. }