TF-4128 Separate download handling into independent class to decouple from views on web
This commit is contained in:
@@ -1,11 +1,7 @@
|
||||
import 'dart:async';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:core/data/network/download/downloaded_response.dart';
|
||||
import 'package:core/presentation/state/failure.dart';
|
||||
import 'package:core/presentation/state/success.dart';
|
||||
import 'package:core/presentation/utils/html_transformer/transform_configuration.dart';
|
||||
import 'package:dartz/dartz.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:email_recovery/email_recovery/email_recovery_action.dart';
|
||||
import 'package:email_recovery/email_recovery/email_recovery_action_id.dart';
|
||||
@@ -17,7 +13,6 @@ 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/mail/email/email.dart';
|
||||
import 'package:model/account/account_request.dart';
|
||||
import 'package:model/download/download_task_id.dart';
|
||||
import 'package:model/email/attachment.dart';
|
||||
import 'package:model/email/email_content.dart';
|
||||
import 'package:model/email/mark_star_action.dart';
|
||||
@@ -59,13 +54,6 @@ abstract class EmailRepository {
|
||||
ReadActions readActions,
|
||||
);
|
||||
|
||||
Future<List<DownloadTaskId>> downloadAttachments(
|
||||
List<Attachment> attachments,
|
||||
AccountId accountId,
|
||||
String baseDownloadUrl,
|
||||
AccountRequest accountRequest
|
||||
);
|
||||
|
||||
Future<DownloadedResponse> exportAttachment(
|
||||
Attachment attachment,
|
||||
AccountId accountId,
|
||||
@@ -74,16 +62,6 @@ abstract class EmailRepository {
|
||||
CancelToken cancelToken
|
||||
);
|
||||
|
||||
Future<Uint8List> downloadAttachmentForWeb(
|
||||
DownloadTaskId taskId,
|
||||
Attachment attachment,
|
||||
AccountId accountId,
|
||||
String baseDownloadUrl,
|
||||
AccountRequest accountRequest,
|
||||
StreamController<Either<Failure, Success>> onReceiveController,
|
||||
{CancelToken? cancelToken}
|
||||
);
|
||||
|
||||
Future<({
|
||||
List<EmailId> emailIdsSuccess,
|
||||
Map<Id, SetError> mapErrors,
|
||||
@@ -212,16 +190,7 @@ abstract class EmailRepository {
|
||||
String htmlContent,
|
||||
TransformConfiguration configuration);
|
||||
|
||||
Future<void> downloadAllAttachmentsForWeb(
|
||||
AccountId accountId,
|
||||
EmailId emailId,
|
||||
String baseDownloadAllUrl,
|
||||
String outputFileName,
|
||||
AccountRequest accountRequest,
|
||||
DownloadTaskId taskId,
|
||||
StreamController<Either<Failure, Success>> onReceiveController,
|
||||
{CancelToken? cancelToken}
|
||||
);
|
||||
|
||||
|
||||
Future<DownloadedResponse> exportAllAttachments(
|
||||
AccountId accountId,
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
import 'package:core/presentation/state/failure.dart';
|
||||
import 'package:core/presentation/state/success.dart';
|
||||
import 'package:model/download/download_task_id.dart';
|
||||
|
||||
class DownloadAttachmentsSuccess extends UIState {
|
||||
final List<DownloadTaskId> taskIds;
|
||||
|
||||
DownloadAttachmentsSuccess(this.taskIds);
|
||||
|
||||
@override
|
||||
List<Object> get props => [taskIds];
|
||||
}
|
||||
|
||||
class DownloadAttachmentsFailure extends FeatureFailure {
|
||||
|
||||
DownloadAttachmentsFailure(dynamic exception) : super(exception: exception);
|
||||
}
|
||||
+10
-10
@@ -13,21 +13,21 @@ import 'package:model/account/authentication_type.dart';
|
||||
import 'package:model/account/password.dart';
|
||||
import 'package:model/download/download_task_id.dart';
|
||||
import 'package:model/email/attachment.dart';
|
||||
import 'package:tmail_ui_user/features/email/domain/repository/email_repository.dart';
|
||||
import 'package:tmail_ui_user/features/email/domain/state/download_all_attachments_for_web_state.dart';
|
||||
import 'package:tmail_ui_user/features/login/domain/repository/account_repository.dart';
|
||||
import 'package:tmail_ui_user/features/login/domain/repository/authentication_oidc_repository.dart';
|
||||
import 'package:tmail_ui_user/features/login/domain/repository/credential_repository.dart';
|
||||
import 'package:tmail_ui_user/features/mailbox_dashboard/domain/repository/download_repository.dart';
|
||||
|
||||
class DownloadAllAttachmentsForWebInteractor {
|
||||
const DownloadAllAttachmentsForWebInteractor(
|
||||
this._emailRepository,
|
||||
this._downloadRepository,
|
||||
this._accountRepository,
|
||||
this._authenticationOIDCRepository,
|
||||
this.credentialRepository,
|
||||
);
|
||||
|
||||
final EmailRepository _emailRepository;
|
||||
final DownloadRepository _downloadRepository;
|
||||
final AccountRepository _accountRepository;
|
||||
final AuthenticationOIDCRepository _authenticationOIDCRepository;
|
||||
final CredentialRepository credentialRepository;
|
||||
@@ -37,16 +37,16 @@ class DownloadAllAttachmentsForWebInteractor {
|
||||
EmailId emailId,
|
||||
String baseDownloadAllUrl,
|
||||
Attachment attachment,
|
||||
DownloadTaskId taskId,
|
||||
StreamController<Either<Failure, Success>> onReceiveController,
|
||||
{CancelToken? cancelToken}
|
||||
) async* {
|
||||
DownloadTaskId taskId, {
|
||||
StreamController<Either<Failure, Success>>? onReceiveController,
|
||||
CancelToken? cancelToken,
|
||||
}) async* {
|
||||
try {
|
||||
yield Right(StartDownloadAllAttachmentsForWeb(
|
||||
taskId,
|
||||
attachment,
|
||||
));
|
||||
onReceiveController.add(Right(StartDownloadAllAttachmentsForWeb(
|
||||
onReceiveController?.add(Right(StartDownloadAllAttachmentsForWeb(
|
||||
taskId,
|
||||
attachment,
|
||||
cancelToken: cancelToken,
|
||||
@@ -64,14 +64,14 @@ class DownloadAllAttachmentsForWebInteractor {
|
||||
);
|
||||
}
|
||||
|
||||
await _emailRepository.downloadAllAttachmentsForWeb(
|
||||
await _downloadRepository.downloadAllAttachmentsForWeb(
|
||||
accountId,
|
||||
emailId,
|
||||
baseDownloadAllUrl,
|
||||
attachment.name!,
|
||||
accountRequest,
|
||||
taskId,
|
||||
onReceiveController,
|
||||
onReceiveController: onReceiveController,
|
||||
cancelToken: cancelToken
|
||||
);
|
||||
|
||||
|
||||
@@ -11,36 +11,36 @@ import 'package:model/account/authentication_type.dart';
|
||||
import 'package:model/account/password.dart';
|
||||
import 'package:model/download/download_task_id.dart';
|
||||
import 'package:model/email/attachment.dart';
|
||||
import 'package:tmail_ui_user/features/email/domain/repository/email_repository.dart';
|
||||
import 'package:tmail_ui_user/features/email/domain/state/download_attachment_for_web_state.dart';
|
||||
import 'package:tmail_ui_user/features/login/domain/repository/account_repository.dart';
|
||||
import 'package:tmail_ui_user/features/login/domain/repository/authentication_oidc_repository.dart';
|
||||
import 'package:tmail_ui_user/features/login/domain/repository/credential_repository.dart';
|
||||
import 'package:tmail_ui_user/features/mailbox_dashboard/domain/repository/download_repository.dart';
|
||||
|
||||
class DownloadAttachmentForWebInteractor {
|
||||
final EmailRepository emailRepository;
|
||||
final DownloadRepository _downloadRepository;
|
||||
final CredentialRepository credentialRepository;
|
||||
final AccountRepository _accountRepository;
|
||||
final AuthenticationOIDCRepository _authenticationOIDCRepository;
|
||||
|
||||
DownloadAttachmentForWebInteractor(
|
||||
this.emailRepository,
|
||||
this._downloadRepository,
|
||||
this.credentialRepository,
|
||||
this._accountRepository,
|
||||
this._authenticationOIDCRepository);
|
||||
|
||||
Stream<Either<Failure, Success>> execute(
|
||||
DownloadTaskId taskId,
|
||||
Attachment attachment,
|
||||
AccountId accountId,
|
||||
String baseDownloadUrl,
|
||||
StreamController<Either<Failure, Success>> onReceiveController,
|
||||
{CancelToken? cancelToken,
|
||||
bool previewerSupported = false,
|
||||
DownloadTaskId taskId,
|
||||
Attachment attachment,
|
||||
AccountId accountId,
|
||||
String baseDownloadUrl, {
|
||||
StreamController<Either<Failure, Success>>? onReceiveController,
|
||||
CancelToken? cancelToken,
|
||||
bool previewerSupported = false,
|
||||
}) async* {
|
||||
try {
|
||||
yield Right<Failure, Success>(StartDownloadAttachmentForWeb(taskId, attachment, cancelToken, previewerSupported));
|
||||
onReceiveController.add(Right(StartDownloadAttachmentForWeb(taskId, attachment, cancelToken, previewerSupported)));
|
||||
onReceiveController?.add(Right(StartDownloadAttachmentForWeb(taskId, attachment, cancelToken, previewerSupported)));
|
||||
|
||||
final currentAccount = await _accountRepository.getCurrentAccount();
|
||||
AccountRequest? accountRequest;
|
||||
@@ -56,13 +56,13 @@ class DownloadAttachmentForWebInteractor {
|
||||
);
|
||||
}
|
||||
|
||||
final bytesDownloaded = await emailRepository.downloadAttachmentForWeb(
|
||||
final bytesDownloaded = await _downloadRepository.downloadAttachmentForWeb(
|
||||
taskId,
|
||||
attachment,
|
||||
accountId,
|
||||
baseDownloadUrl,
|
||||
accountRequest,
|
||||
onReceiveController,
|
||||
onReceiveController: onReceiveController,
|
||||
cancelToken: cancelToken
|
||||
);
|
||||
|
||||
|
||||
@@ -1,130 +0,0 @@
|
||||
import 'package:core/presentation/state/failure.dart';
|
||||
import 'package:core/presentation/state/success.dart';
|
||||
import 'package:core/utils/app_logger.dart';
|
||||
import 'package:dartz/dartz.dart';
|
||||
import 'package:jmap_dart_client/jmap/account_id.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/user_name.dart';
|
||||
import 'package:model/account/account_request.dart';
|
||||
import 'package:model/account/authentication_type.dart';
|
||||
import 'package:model/account/password.dart';
|
||||
import 'package:model/account/personal_account.dart';
|
||||
import 'package:model/email/attachment.dart';
|
||||
import 'package:model/oidc/token_oidc.dart';
|
||||
import 'package:tmail_ui_user/features/email/domain/repository/email_repository.dart';
|
||||
import 'package:tmail_ui_user/features/email/domain/state/download_attachments_state.dart';
|
||||
import 'package:tmail_ui_user/features/login/data/network/interceptors/authorization_interceptors.dart';
|
||||
import 'package:tmail_ui_user/features/login/domain/exceptions/authentication_exception.dart';
|
||||
import 'package:tmail_ui_user/features/login/domain/extensions/oidc_configuration_extensions.dart';
|
||||
import 'package:tmail_ui_user/features/login/domain/repository/account_repository.dart';
|
||||
import 'package:tmail_ui_user/features/login/domain/repository/authentication_oidc_repository.dart';
|
||||
import 'package:tmail_ui_user/features/login/domain/repository/credential_repository.dart';
|
||||
|
||||
class DownloadAttachmentsInteractor {
|
||||
final EmailRepository emailRepository;
|
||||
final CredentialRepository credentialRepository;
|
||||
final AccountRepository _accountRepository;
|
||||
final AuthenticationOIDCRepository _authenticationOIDCRepository;
|
||||
final AuthorizationInterceptors _authorizationInterceptors;
|
||||
|
||||
DownloadAttachmentsInteractor(
|
||||
this.emailRepository,
|
||||
this.credentialRepository,
|
||||
this._accountRepository,
|
||||
this._authenticationOIDCRepository,
|
||||
this._authorizationInterceptors,
|
||||
);
|
||||
|
||||
Stream<Either<Failure, Success>> execute(
|
||||
List<Attachment> attachments,
|
||||
AccountId accountId,
|
||||
String baseDownloadUrl
|
||||
) async* {
|
||||
try {
|
||||
final currentAccount = await _accountRepository.getCurrentAccount();
|
||||
|
||||
AccountRequest? accountRequest;
|
||||
|
||||
if (currentAccount.authenticationType == AuthenticationType.oidc) {
|
||||
final tokenOidc = await _authenticationOIDCRepository.getStoredTokenOIDC(currentAccount.id);
|
||||
accountRequest = AccountRequest.withOidc(token: tokenOidc);
|
||||
} else {
|
||||
final authenticationInfoCache = await credentialRepository.getAuthenticationInfoStored();
|
||||
accountRequest = AccountRequest.withBasic(
|
||||
userName: UserName(authenticationInfoCache.username),
|
||||
password: Password(authenticationInfoCache.password),
|
||||
);
|
||||
}
|
||||
|
||||
final taskIds = await emailRepository.downloadAttachments(
|
||||
attachments,
|
||||
accountId,
|
||||
baseDownloadUrl,
|
||||
accountRequest
|
||||
);
|
||||
|
||||
yield Right<Failure, Success>(DownloadAttachmentsSuccess(taskIds));
|
||||
} catch (exception) {
|
||||
logError('DownloadAttachmentsInteractor::execute(): $exception');
|
||||
if (exception is DownloadAttachmentHasTokenExpiredException &&
|
||||
exception.refreshToken.isNotEmpty) {
|
||||
yield* _retryDownloadAttachments(
|
||||
accountId,
|
||||
baseDownloadUrl,
|
||||
attachments,
|
||||
exception.refreshToken);
|
||||
} else {
|
||||
yield Left<Failure, Success>(DownloadAttachmentsFailure(exception));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Stream<Either<Failure, Success>> _retryDownloadAttachments(
|
||||
AccountId accountId,
|
||||
String baseDownloadUrl,
|
||||
List<Attachment> attachments,
|
||||
String refreshToken) async* {
|
||||
log('DownloadAttachmentsInteractor::_retryDownloadAttachments(): $refreshToken');
|
||||
try {
|
||||
final accountCurrent = await _accountRepository.getCurrentAccount();
|
||||
final oidcConfig = await _authenticationOIDCRepository.getStoredOidcConfiguration();
|
||||
final newTokenOIDC = await _authenticationOIDCRepository.refreshingTokensOIDC(
|
||||
oidcConfig.clientId,
|
||||
oidcConfig.redirectUrl,
|
||||
oidcConfig.discoveryUrl,
|
||||
oidcConfig.scopes,
|
||||
refreshToken);
|
||||
|
||||
await _accountRepository.deleteCurrentAccount(accountCurrent.id);
|
||||
|
||||
await _authenticationOIDCRepository.persistTokenOIDC(newTokenOIDC);
|
||||
|
||||
await _accountRepository.setCurrentAccount(
|
||||
PersonalAccount(
|
||||
newTokenOIDC.tokenIdHash,
|
||||
AuthenticationType.oidc,
|
||||
isSelected: true,
|
||||
accountId: accountId,
|
||||
apiUrl: accountCurrent.apiUrl,
|
||||
userName: accountCurrent.userName
|
||||
)
|
||||
);
|
||||
|
||||
_authorizationInterceptors.setTokenAndAuthorityOidc(
|
||||
newToken: newTokenOIDC,
|
||||
newConfig: oidcConfig);
|
||||
|
||||
final accountRequest = AccountRequest.withOidc(token: newTokenOIDC);
|
||||
|
||||
final taskIds = await emailRepository.downloadAttachments(
|
||||
attachments,
|
||||
accountId,
|
||||
baseDownloadUrl,
|
||||
accountRequest);
|
||||
|
||||
yield Right<Failure, Success>(DownloadAttachmentsSuccess(taskIds));
|
||||
} catch (e) {
|
||||
logError('RefreshTokenOIDCInteractor::execute(): $e');
|
||||
yield Left<Failure, Success>(DownloadAttachmentsFailure(e));
|
||||
}
|
||||
}
|
||||
}
|
||||
+10
-11
@@ -9,14 +9,19 @@ import 'package:dartz/dartz.dart';
|
||||
import 'package:jmap_dart_client/jmap/account_id.dart';
|
||||
import 'package:model/download/download_task_id.dart';
|
||||
import 'package:model/email/attachment.dart';
|
||||
import 'package:tmail_ui_user/features/email/domain/repository/email_repository.dart';
|
||||
import 'package:tmail_ui_user/features/email/domain/state/download_attachment_for_web_state.dart';
|
||||
import 'package:tmail_ui_user/features/email/domain/state/get_html_content_from_attachment_state.dart';
|
||||
import 'package:tmail_ui_user/features/email/domain/usecases/download_attachment_for_web_interactor.dart';
|
||||
|
||||
class GetHtmlContentFromAttachmentInteractor {
|
||||
GetHtmlContentFromAttachmentInteractor(this._downloadAttachmentForWebInteractor);
|
||||
GetHtmlContentFromAttachmentInteractor(
|
||||
this._downloadAttachmentForWebInteractor,
|
||||
this._emailRepository,
|
||||
);
|
||||
|
||||
final DownloadAttachmentForWebInteractor _downloadAttachmentForWebInteractor;
|
||||
final EmailRepository _emailRepository;
|
||||
|
||||
Stream<Either<Failure, Success>> execute(
|
||||
AccountId accountId,
|
||||
@@ -25,7 +30,6 @@ class GetHtmlContentFromAttachmentInteractor {
|
||||
String baseDownloadUrl,
|
||||
TransformConfiguration transformConfiguration,
|
||||
) async* {
|
||||
final onReceiveController = StreamController<Either<Failure, Success>>();
|
||||
try {
|
||||
yield Right(GettingHtmlContentFromAttachment(attachment: attachment));
|
||||
final downloadState = await _downloadAttachmentForWebInteractor.execute(
|
||||
@@ -33,7 +37,6 @@ class GetHtmlContentFromAttachmentInteractor {
|
||||
attachment,
|
||||
accountId,
|
||||
baseDownloadUrl,
|
||||
onReceiveController,
|
||||
).last;
|
||||
|
||||
Either<Failure, Success>? sanitizeState;
|
||||
@@ -62,7 +65,6 @@ class GetHtmlContentFromAttachmentInteractor {
|
||||
},
|
||||
);
|
||||
|
||||
onReceiveController.close();
|
||||
if (sanitizeState != null) {
|
||||
yield sanitizeState!;
|
||||
} else {
|
||||
@@ -74,7 +76,6 @@ class GetHtmlContentFromAttachmentInteractor {
|
||||
|
||||
} catch (e) {
|
||||
logError('GetHtmlContentFromAttachmentInteractor:exception: $e');
|
||||
onReceiveController.close();
|
||||
yield Left(GetHtmlContentFromAttachmentFailure(
|
||||
exception: e,
|
||||
attachment: attachment,
|
||||
@@ -88,12 +89,10 @@ class GetHtmlContentFromAttachmentInteractor {
|
||||
Attachment attachment,
|
||||
) async {
|
||||
try {
|
||||
final sanitizedHtmlContent = await _downloadAttachmentForWebInteractor
|
||||
.emailRepository
|
||||
.sanitizeHtmlContent(
|
||||
htmlContent,
|
||||
transformConfiguration,
|
||||
);
|
||||
final sanitizedHtmlContent = await _emailRepository.sanitizeHtmlContent(
|
||||
htmlContent,
|
||||
transformConfiguration,
|
||||
);
|
||||
return Right(GetHtmlContentFromAttachmentSuccess(
|
||||
sanitizedHtmlContent: sanitizedHtmlContent,
|
||||
htmlAttachmentTitle: attachment.generateFileName(),
|
||||
|
||||
Reference in New Issue
Block a user