TF-4128 Separate download handling into independent class to decouple from views on web

This commit is contained in:
dab246
2025-10-28 16:44:40 +07:00
committed by Dat H. Pham
parent 832f8e1391
commit be8eaf6258
37 changed files with 955 additions and 1095 deletions
@@ -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));
}
}
}
@@ -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(),