i_user_impl.dart 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import 'dart:typed_data';
  2. import 'package:app_flowy/workspace/infrastructure/repos/helper.dart';
  3. import 'package:dartz/dartz.dart';
  4. import 'package:app_flowy/workspace/domain/i_user.dart';
  5. import 'package:app_flowy/workspace/infrastructure/repos/user_repo.dart';
  6. import 'package:flowy_sdk/protobuf/flowy-dart-notify/protobuf.dart';
  7. import 'package:flowy_sdk/protobuf/flowy-user/errors.pb.dart' as user_error;
  8. import 'package:flowy_sdk/protobuf/flowy-user/observable.pb.dart' as user;
  9. import 'package:flowy_sdk/protobuf/flowy-workspace/errors.pb.dart';
  10. import 'package:flowy_sdk/protobuf/flowy-workspace/observable.pb.dart';
  11. export 'package:app_flowy/workspace/domain/i_user.dart';
  12. export 'package:app_flowy/workspace/infrastructure/repos/user_repo.dart';
  13. import 'package:flowy_sdk/rust_stream.dart';
  14. import 'dart:async';
  15. class IUserImpl extends IUser {
  16. UserRepo repo;
  17. IUserImpl({
  18. required this.repo,
  19. });
  20. @override
  21. Future<Either<Unit, WorkspaceError>> deleteWorkspace(String workspaceId) {
  22. return repo.deleteWorkspace(workspaceId: workspaceId);
  23. }
  24. @override
  25. Future<Either<UserProfile, UserError>> fetchUserProfile(String userId) {
  26. return repo.fetchUserProfile(userId: userId);
  27. }
  28. @override
  29. Future<Either<Unit, UserError>> signOut() {
  30. return repo.signOut();
  31. }
  32. @override
  33. UserProfile get user => repo.user;
  34. @override
  35. Future<Either<List<Workspace>, WorkspaceError>> fetchWorkspaces() {
  36. return repo.getWorkspaces();
  37. }
  38. @override
  39. Future<Either<Unit, UserError>> initUser() {
  40. return repo.initUser();
  41. }
  42. }
  43. class IUserListenerImpl extends IUserListener {
  44. StreamSubscription<SubscribeObject>? _subscription;
  45. WorkspacesUpdatedCallback? _workspacesUpdated;
  46. AuthChangedCallback? _authChanged;
  47. UserProfileUpdateCallback? _profileUpdated;
  48. late WorkspaceNotificationParser _workspaceParser;
  49. late UserNotificationParser _userParser;
  50. late UserProfile _user;
  51. IUserListenerImpl({
  52. required UserProfile user,
  53. }) {
  54. _user = user;
  55. }
  56. @override
  57. void start() {
  58. _workspaceParser = WorkspaceNotificationParser(id: _user.token, callback: _NotificationCallback);
  59. _userParser = UserNotificationParser(id: _user.token, callback: _UserNotificationCallback);
  60. _subscription = RustStreamReceiver.listen((observable) {
  61. _workspaceParser.parse(observable);
  62. _userParser.parse(observable);
  63. });
  64. }
  65. @override
  66. Future<void> stop() async {
  67. await _subscription?.cancel();
  68. }
  69. @override
  70. void setAuthCallback(AuthChangedCallback authCallback) {
  71. _authChanged = authCallback;
  72. }
  73. @override
  74. void setProfileCallback(UserProfileUpdateCallback profileCallback) {
  75. _profileUpdated = profileCallback;
  76. }
  77. @override
  78. void setWorkspacesCallback(WorkspacesUpdatedCallback workspacesCallback) {
  79. _workspacesUpdated = workspacesCallback;
  80. }
  81. void _NotificationCallback(WorkspaceNotification ty, Either<Uint8List, WorkspaceError> result) {
  82. switch (ty) {
  83. case WorkspaceNotification.UserCreateWorkspace:
  84. case WorkspaceNotification.UserDeleteWorkspace:
  85. case WorkspaceNotification.WorkspaceListUpdated:
  86. if (_workspacesUpdated != null) {
  87. result.fold(
  88. (payload) {
  89. final workspaces = RepeatedWorkspace.fromBuffer(payload);
  90. _workspacesUpdated!(left(workspaces.items));
  91. },
  92. (error) => _workspacesUpdated!(right(error)),
  93. );
  94. }
  95. break;
  96. case WorkspaceNotification.UserUnauthorized:
  97. if (_authChanged != null) {
  98. result.fold(
  99. (_) {},
  100. (error) => {_authChanged!(right(UserError.create()..code = user_error.ErrorCode.UserUnauthorized))},
  101. );
  102. }
  103. break;
  104. default:
  105. break;
  106. }
  107. }
  108. void _UserNotificationCallback(user.UserNotification ty, Either<Uint8List, UserError> result) {
  109. switch (ty) {
  110. case user.UserNotification.UserUnauthorized:
  111. if (_profileUpdated != null) {
  112. result.fold(
  113. (payload) {
  114. final userProfile = UserProfile.fromBuffer(payload);
  115. _profileUpdated!(left(userProfile));
  116. },
  117. (error) => _profileUpdated!(right(error)),
  118. );
  119. }
  120. break;
  121. default:
  122. break;
  123. }
  124. }
  125. }