TF-1810 Add GetNewReceiveEmailFromNotificationInteractor to get new receive email from notification

(cherry picked from commit df7a84a932f8496d92e5eed8ccd66c0b8709ae61)
This commit is contained in:
dab246
2023-05-10 16:31:00 +07:00
committed by Dat Vu
parent 41ed9a230d
commit 7e0f338fa0
5 changed files with 110 additions and 1 deletions
@@ -8,12 +8,14 @@ import 'package:jmap_dart_client/jmap/core/session/session.dart';
import 'package:jmap_dart_client/jmap/core/user_name.dart';
import 'package:jmap_dart_client/jmap/mail/email/email.dart';
import 'package:jmap_dart_client/jmap/mail/email/keyword_identifier.dart';
import 'package:model/email/email_property.dart';
import 'package:model/extensions/list_email_extension.dart';
import 'package:model/extensions/mailbox_extension.dart';
import 'package:model/mailbox/presentation_mailbox.dart';
import 'package:tmail_ui_user/features/mailbox/data/datasource/mailbox_datasource.dart';
import 'package:tmail_ui_user/features/push_notification/data/datasource/fcm_datasource.dart';
import 'package:tmail_ui_user/features/push_notification/data/extensions/fcm_subscription_extensions.dart';
import 'package:tmail_ui_user/features/push_notification/domain/exceptions/fcm_exception.dart';
import 'package:tmail_ui_user/features/push_notification/domain/model/fcm_subscription.dart';
import 'package:tmail_ui_user/features/push_notification/domain/model/register_new_token_request.dart';
import 'package:tmail_ui_user/features/push_notification/domain/repository/fcm_repository.dart';
@@ -184,4 +186,34 @@ class FCMRepositoryImpl extends FCMRepository {
return [];
}
}
@override
Future<List<EmailId>> getNewReceiveEmailFromNotification(Session session, AccountId accountId, jmap.State currentState) async {
EmailChangeResponse? emailChangeResponse;
bool hasMoreChanges = true;
jmap.State? sinceState = currentState;
while (hasMoreChanges && sinceState != null) {
final changesResponse = await _threadDataSource.getChanges(
session,
accountId,
sinceState,
propertiesCreated: Properties({EmailProperty.id}));
hasMoreChanges = changesResponse.hasMoreChanges;
sinceState = changesResponse.newStateChanges;
if (emailChangeResponse != null) {
emailChangeResponse.union(changesResponse);
} else {
emailChangeResponse = changesResponse;
}
}
if (emailChangeResponse?.created?.isNotEmpty == true) {
return emailChangeResponse!.created!.listEmailIds;
} else {
throw NotFoundNewReceiveEmailException();
}
}
}
@@ -10,4 +10,8 @@ class NotFoundFirebaseSubscriptionException implements Exception {}
class NotFoundSubscriptionException implements Exception {}
class NotFoundEmailStateException implements Exception {}
class NotFoundEmailStateException implements Exception {}
class NotFoundNewReceiveEmailException implements Exception {}
class EmailStateNoChangeException implements Exception {}
@@ -49,4 +49,6 @@ abstract class FCMRepository {
Properties? propertiesUpdated
}
);
Future<List<EmailId>> getNewReceiveEmailFromNotification(Session session, AccountId accountId, jmap.State currentState);
}
@@ -0,0 +1,28 @@
import 'package:core/presentation/state/failure.dart';
import 'package:core/presentation/state/success.dart';
import 'package:jmap_dart_client/jmap/account_id.dart';
import 'package:jmap_dart_client/jmap/core/user_name.dart';
import 'package:jmap_dart_client/jmap/mail/email/email.dart';
class GetNewReceiveEmailFromNotificationLoading extends UIState {}
class GetNewReceiveEmailFromNotificationSuccess extends UIState {
final List<EmailId> emailIds;
final AccountId accountId;
final UserName userName;
GetNewReceiveEmailFromNotificationSuccess(this.accountId, this.userName, this.emailIds);
@override
List<Object> get props => [accountId, userName, emailIds];
}
class GetNewReceiveEmailFromNotificationFailure extends FeatureFailure {
GetNewReceiveEmailFromNotificationFailure(exception) : super(exception: exception);
@override
List<Object> get props => [exception];
}
@@ -0,0 +1,43 @@
import 'package:core/presentation/state/failure.dart';
import 'package:core/presentation/state/success.dart';
import 'package:dartz/dartz.dart';
import 'package:jmap_dart_client/jmap/account_id.dart';
import 'package:jmap_dart_client/jmap/core/session/session.dart';
import 'package:jmap_dart_client/jmap/core/state.dart' as jmap;
import 'package:jmap_dart_client/jmap/core/user_name.dart';
import 'package:tmail_ui_user/features/email/domain/repository/email_repository.dart';
import 'package:tmail_ui_user/features/push_notification/domain/exceptions/fcm_exception.dart';
import 'package:tmail_ui_user/features/push_notification/domain/repository/fcm_repository.dart';
import 'package:tmail_ui_user/features/push_notification/domain/state/get_new_receive_email_from_notification_state.dart';
class GetNewReceiveEmailFromNotificationInteractor {
final FCMRepository _fcmRepository;
final EmailRepository _emailRepository;
GetNewReceiveEmailFromNotificationInteractor(this._fcmRepository, this._emailRepository);
Stream<Either<Failure, Success>> execute(
Session session,
AccountId accountId,
UserName userName,
jmap.State newState
) async* {
try {
yield Right<Failure, Success>(GetNewReceiveEmailFromNotificationLoading());
final currentState = await _emailRepository.getEmailState(session, accountId);
if (currentState != null && currentState != newState) {
final listEmailIds = await _fcmRepository.getNewReceiveEmailFromNotification(
session,
accountId,
currentState);
yield Right<Failure, Success>(GetNewReceiveEmailFromNotificationSuccess(accountId, userName, listEmailIds));
} else {
yield Left<Failure, Success>(GetNewReceiveEmailFromNotificationFailure(EmailStateNoChangeException()));
}
} catch (e) {
yield Left<Failure, Success>(GetNewReceiveEmailFromNotificationFailure(e));
}
}
}