TF-1710 Add empty spam folder interactor
(cherry picked from commit f0d22cd8925185607b5dea74f2cf40052980d19c)
This commit is contained in:
@@ -50,7 +50,7 @@ abstract class ThreadDataSource {
|
||||
|
||||
Future<void> update(AccountId accountId, UserName userName, {List<Email>? updated, List<Email>? created, List<EmailId>? destroyed});
|
||||
|
||||
Future<List<EmailId>> emptyTrashFolder(
|
||||
Future<List<EmailId>> emptyMailboxFolder(
|
||||
Session session,
|
||||
AccountId accountId,
|
||||
MailboxId mailboxId,
|
||||
|
||||
@@ -91,7 +91,7 @@ class LocalThreadDataSourceImpl extends ThreadDataSource {
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<EmailId>> emptyTrashFolder(
|
||||
Future<List<EmailId>> emptyMailboxFolder(
|
||||
Session session,
|
||||
AccountId accountId,
|
||||
MailboxId mailboxId,
|
||||
|
||||
@@ -85,14 +85,14 @@ class ThreadDataSourceImpl extends ThreadDataSource {
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<EmailId>> emptyTrashFolder(
|
||||
Future<List<EmailId>> emptyMailboxFolder(
|
||||
Session session,
|
||||
AccountId accountId,
|
||||
MailboxId mailboxId,
|
||||
Future<void> Function(List<EmailId>? newDestroyed) updateDestroyedEmailCache
|
||||
) {
|
||||
return Future.sync(() async {
|
||||
return await _threadIsolateWorker.emptyTrashFolder(
|
||||
return await _threadIsolateWorker.emptyMailboxFolder(
|
||||
session,
|
||||
accountId,
|
||||
mailboxId,
|
||||
|
||||
+5
-5
@@ -5,19 +5,19 @@ import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox.dart';
|
||||
import 'package:tmail_ui_user/features/email/data/network/email_api.dart';
|
||||
import 'package:tmail_ui_user/features/thread/data/network/thread_api.dart';
|
||||
|
||||
class EmptyTrashFolderArguments with EquatableMixin {
|
||||
class EmptyMailboxFolderArguments with EquatableMixin {
|
||||
final Session session;
|
||||
final AccountId accountId;
|
||||
final MailboxId trashMailboxId;
|
||||
final MailboxId mailboxId;
|
||||
final ThreadAPI threadAPI;
|
||||
final EmailAPI emailAPI;
|
||||
|
||||
EmptyTrashFolderArguments(
|
||||
EmptyMailboxFolderArguments(
|
||||
this.session,
|
||||
this.threadAPI,
|
||||
this.emailAPI,
|
||||
this.accountId,
|
||||
this.trashMailboxId,
|
||||
this.mailboxId,
|
||||
);
|
||||
|
||||
@override
|
||||
@@ -26,6 +26,6 @@ class EmptyTrashFolderArguments with EquatableMixin {
|
||||
accountId,
|
||||
emailAPI,
|
||||
threadAPI,
|
||||
trashMailboxId
|
||||
mailboxId
|
||||
];
|
||||
}
|
||||
@@ -14,8 +14,9 @@ import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox.dart';
|
||||
import 'package:model/email/email_property.dart';
|
||||
import 'package:model/extensions/list_email_extension.dart';
|
||||
import 'package:tmail_ui_user/features/email/data/network/email_api.dart';
|
||||
import 'package:tmail_ui_user/features/thread/data/model/empty_trash_folder_arguments.dart';
|
||||
import 'package:tmail_ui_user/features/thread/data/model/empty_mailbox_folder_arguments.dart';
|
||||
import 'package:tmail_ui_user/features/thread/data/network/thread_api.dart';
|
||||
import 'package:tmail_ui_user/features/thread/domain/exceptions/thread_exceptions.dart';
|
||||
import 'package:worker_manager/worker_manager.dart';
|
||||
|
||||
class ThreadIsolateWorker {
|
||||
@@ -25,29 +26,35 @@ class ThreadIsolateWorker {
|
||||
|
||||
ThreadIsolateWorker(this._threadAPI, this._emailAPI, this._isolateExecutor);
|
||||
|
||||
Future<List<EmailId>> emptyTrashFolder(
|
||||
Future<List<EmailId>> emptyMailboxFolder(
|
||||
Session session,
|
||||
AccountId accountId,
|
||||
MailboxId mailboxId,
|
||||
Future<void> Function(List<EmailId>? newDestroyed) updateDestroyedEmailCache,
|
||||
) async {
|
||||
if (PlatformInfo.isWeb) {
|
||||
return _emptyTrashFolderOnWeb(session, accountId, mailboxId, updateDestroyedEmailCache);
|
||||
return _emptyMailboxFolderOnWeb(session, accountId, mailboxId, updateDestroyedEmailCache);
|
||||
} else {
|
||||
final result = await _isolateExecutor.execute(
|
||||
arg1: EmptyTrashFolderArguments(session, _threadAPI, _emailAPI, accountId, mailboxId),
|
||||
fun1: _emptyTrashFolderAction,
|
||||
notification: (value) {
|
||||
if (value is List<EmailId>) {
|
||||
updateDestroyedEmailCache.call(value);
|
||||
log('ThreadIsolateWorker::emptyTrashFolder(): onUpdateProgress: PERCENT ${value.length}');
|
||||
}
|
||||
});
|
||||
return result;
|
||||
arg1: EmptyMailboxFolderArguments(session, _threadAPI, _emailAPI, accountId, mailboxId),
|
||||
fun1: _emptyMailboxFolderAction,
|
||||
notification: (value) {
|
||||
if (value is List<EmailId>) {
|
||||
updateDestroyedEmailCache.call(value);
|
||||
log('ThreadIsolateWorker::emptyMailboxFolder(): onUpdateProgress: PERCENT ${value.length}');
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
if (result.isEmpty) {
|
||||
throw NotFoundEmailsDeletedException();
|
||||
} else {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static Future<List<EmailId>> _emptyTrashFolderAction(EmptyTrashFolderArguments args, TypeSendPort sendPort) async {
|
||||
static Future<List<EmailId>> _emptyMailboxFolderAction(EmptyMailboxFolderArguments args, TypeSendPort sendPort) async {
|
||||
List<EmailId> emailListCompleted = List.empty(growable: true);
|
||||
try {
|
||||
var hasEmails = true;
|
||||
@@ -60,7 +67,7 @@ class ThreadIsolateWorker {
|
||||
sort: <Comparator>{}..add(
|
||||
EmailComparator(EmailComparatorProperty.receivedAt)
|
||||
..setIsAscending(false)),
|
||||
filter: EmailFilterCondition(inMailbox: args.trashMailboxId, before: lastEmail?.receivedAt),
|
||||
filter: EmailFilterCondition(inMailbox: args.mailboxId, before: lastEmail?.receivedAt),
|
||||
properties: Properties({EmailProperty.id}));
|
||||
|
||||
var newEmailList = emailsResponse.emailList ?? <Email>[];
|
||||
@@ -68,7 +75,7 @@ class ThreadIsolateWorker {
|
||||
newEmailList = newEmailList.where((email) => email.id != lastEmail!.id).toList();
|
||||
}
|
||||
|
||||
log('ThreadIsolateWorker::_emptyTrashFolderAction(): ${newEmailList.length}');
|
||||
log('ThreadIsolateWorker::_emptyMailboxFolderAction(): ${newEmailList.length}');
|
||||
|
||||
if (newEmailList.isNotEmpty) {
|
||||
lastEmail = newEmailList.last;
|
||||
@@ -86,16 +93,16 @@ class ThreadIsolateWorker {
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
log('ThreadIsolateWorker::_emptyTrashFolderAction(): ERROR: $e');
|
||||
log('ThreadIsolateWorker::_emptyMailboxFolderAction(): ERROR: $e');
|
||||
}
|
||||
log('ThreadIsolateWorker::_emptyTrashFolderAction(): TOTAL_REMOVE: ${emailListCompleted.length}');
|
||||
log('ThreadIsolateWorker::_emptyMailboxFolderAction(): TOTAL_REMOVE: ${emailListCompleted.length}');
|
||||
return emailListCompleted;
|
||||
}
|
||||
|
||||
Future<List<EmailId>> _emptyTrashFolderOnWeb(
|
||||
Future<List<EmailId>> _emptyMailboxFolderOnWeb(
|
||||
Session session,
|
||||
AccountId accountId,
|
||||
MailboxId trashMailboxId,
|
||||
MailboxId mailboxId,
|
||||
Future<void> Function(List<EmailId> newDestroyed) updateDestroyedEmailCache,
|
||||
) async {
|
||||
List<EmailId> emailListCompleted = List.empty(growable: true);
|
||||
@@ -110,7 +117,7 @@ class ThreadIsolateWorker {
|
||||
sort: <Comparator>{}..add(
|
||||
EmailComparator(EmailComparatorProperty.receivedAt)
|
||||
..setIsAscending(false)),
|
||||
filter: EmailFilterCondition(inMailbox: trashMailboxId, before: lastEmail?.receivedAt),
|
||||
filter: EmailFilterCondition(inMailbox: mailboxId, before: lastEmail?.receivedAt),
|
||||
properties: Properties({EmailProperty.id}));
|
||||
|
||||
var newEmailList = emailsResponse.emailList ?? <Email>[];
|
||||
@@ -118,7 +125,7 @@ class ThreadIsolateWorker {
|
||||
newEmailList = newEmailList.where((email) => email.id != lastEmail!.id).toList();
|
||||
}
|
||||
|
||||
log('ThreadIsolateWorker::_emptyTrashFolderOnWeb(): ${newEmailList.length}');
|
||||
log('ThreadIsolateWorker::_emptyMailboxFolderOnWeb(): ${newEmailList.length}');
|
||||
|
||||
if (newEmailList.isNotEmpty) {
|
||||
lastEmail = newEmailList.last;
|
||||
@@ -135,9 +142,9 @@ class ThreadIsolateWorker {
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
log('ThreadIsolateWorker::_emptyTrashFolderOnWeb(): ERROR: $e');
|
||||
log('ThreadIsolateWorker::_emptyMailboxFolderOnWeb(): ERROR: $e');
|
||||
}
|
||||
log('ThreadIsolateWorker::_emptyTrashFolderOnWeb(): TOTAL_REMOVE: ${emailListCompleted.length}');
|
||||
log('ThreadIsolateWorker::_emptyMailboxFolderOnWeb(): TOTAL_REMOVE: ${emailListCompleted.length}');
|
||||
return emailListCompleted;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
|
||||
import 'package:core/core.dart';
|
||||
import 'package:core/data/model/source_type/data_source_type.dart';
|
||||
import 'package:core/utils/app_logger.dart';
|
||||
import 'package:dartz/dartz.dart' as dartz;
|
||||
import 'package:jmap_dart_client/jmap/account_id.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/filter/filter.dart';
|
||||
@@ -300,7 +301,7 @@ class ThreadRepositoryImpl extends ThreadRepository {
|
||||
|
||||
@override
|
||||
Future<List<EmailId>> emptyTrashFolder(Session session, AccountId accountId, MailboxId trashMailboxId) async {
|
||||
return mapDataSource[DataSourceType.network]!.emptyTrashFolder(
|
||||
return mapDataSource[DataSourceType.network]!.emptyMailboxFolder(
|
||||
session,
|
||||
accountId,
|
||||
trashMailboxId,
|
||||
@@ -377,4 +378,20 @@ class ThreadRepositoryImpl extends ThreadRepository {
|
||||
) {
|
||||
return mapDataSource[DataSourceType.network]!.getEmailById(session, accountId, emailId, properties: properties);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<EmailId>> emptySpamFolder(Session session, AccountId accountId, MailboxId spamMailboxId) {
|
||||
return mapDataSource[DataSourceType.network]!.emptyMailboxFolder(
|
||||
session,
|
||||
accountId,
|
||||
spamMailboxId,
|
||||
(listEmailIdDeleted) async {
|
||||
await _updateEmailCache(
|
||||
accountId,
|
||||
session.username,
|
||||
newDestroyed: listEmailIdDeleted
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
|
||||
class NotFoundEmailsDeletedException implements Exception {}
|
||||
@@ -64,4 +64,10 @@ abstract class ThreadRepository {
|
||||
EmailId emailId,
|
||||
{Properties? properties}
|
||||
);
|
||||
|
||||
Future<List<EmailId>> emptySpamFolder(
|
||||
Session session,
|
||||
AccountId accountId,
|
||||
MailboxId spamMailboxId,
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import 'package:core/presentation/state/failure.dart';
|
||||
import 'package:core/presentation/state/success.dart';
|
||||
import 'package:jmap_dart_client/jmap/mail/email/email.dart';
|
||||
import 'package:tmail_ui_user/features/base/state/ui_action_state.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/state.dart' as jmap;
|
||||
|
||||
class EmptySpamFolderLoading extends LoadingState {}
|
||||
|
||||
class EmptySpamFolderSuccess extends UIActionState {
|
||||
|
||||
final List<EmailId> emailIds;
|
||||
|
||||
EmptySpamFolderSuccess(
|
||||
this.emailIds, {
|
||||
jmap.State? currentEmailState,
|
||||
jmap.State? currentMailboxState,
|
||||
}) : super(currentEmailState, currentMailboxState);
|
||||
|
||||
@override
|
||||
List<Object?> get props => [emailIds, ...super.props];
|
||||
}
|
||||
|
||||
class EmptySpamFolderFailure extends FeatureFailure {
|
||||
|
||||
EmptySpamFolderFailure(dynamic exception) : super(exception: exception);
|
||||
}
|
||||
@@ -1,13 +1,23 @@
|
||||
import 'package:core/presentation/state/failure.dart';
|
||||
import 'package:core/presentation/state/success.dart';
|
||||
import 'package:jmap_dart_client/jmap/mail/email/email.dart';
|
||||
import 'package:tmail_ui_user/features/base/state/ui_action_state.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/state.dart' as jmap;
|
||||
|
||||
class EmptyTrashFolderLoading extends LoadingState {}
|
||||
|
||||
class EmptyTrashFolderSuccess extends UIActionState {
|
||||
|
||||
EmptyTrashFolderSuccess({
|
||||
jmap.State? currentEmailState,
|
||||
jmap.State? currentMailboxState,
|
||||
}) : super(currentEmailState, currentMailboxState);
|
||||
final List<EmailId> emailIds;
|
||||
|
||||
EmptyTrashFolderSuccess(
|
||||
this.emailIds, {
|
||||
jmap.State? currentEmailState,
|
||||
jmap.State? currentMailboxState,
|
||||
}) : super(currentEmailState, currentMailboxState);
|
||||
|
||||
@override
|
||||
List<Object?> get props => [emailIds, ...super.props];
|
||||
}
|
||||
|
||||
class EmptyTrashFolderFailure extends FeatureFailure {
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
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/mail/mailbox/mailbox.dart';
|
||||
import 'package:tmail_ui_user/features/email/domain/repository/email_repository.dart';
|
||||
import 'package:tmail_ui_user/features/mailbox/domain/repository/mailbox_repository.dart';
|
||||
import 'package:tmail_ui_user/features/thread/domain/repository/thread_repository.dart';
|
||||
import 'package:tmail_ui_user/features/thread/domain/state/empty_spam_folder_state.dart';
|
||||
|
||||
class EmptySpamFolderInteractor {
|
||||
final ThreadRepository threadRepository;
|
||||
final MailboxRepository _mailboxRepository;
|
||||
final EmailRepository _emailRepository;
|
||||
|
||||
EmptySpamFolderInteractor(
|
||||
this.threadRepository,
|
||||
this._mailboxRepository,
|
||||
this._emailRepository
|
||||
);
|
||||
|
||||
Stream<Either<Failure, Success>> execute(Session session, AccountId accountId, MailboxId spamMailboxId) async* {
|
||||
try {
|
||||
yield Right<Failure, Success>(EmptySpamFolderLoading());
|
||||
|
||||
final listState = await Future.wait([
|
||||
_mailboxRepository.getMailboxState(session, accountId),
|
||||
_emailRepository.getEmailState(session, accountId),
|
||||
], eagerError: true);
|
||||
|
||||
final currentMailboxState = listState.first;
|
||||
final currentEmailState = listState.last;
|
||||
|
||||
final emailIdDeleted = await threadRepository.emptySpamFolder(session, accountId, spamMailboxId);
|
||||
yield Right<Failure, Success>(EmptySpamFolderSuccess(
|
||||
emailIdDeleted,
|
||||
currentMailboxState: currentMailboxState,
|
||||
currentEmailState: currentEmailState,
|
||||
));
|
||||
} catch (e) {
|
||||
yield Left<Failure, Success>(EmptySpamFolderFailure(e));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -21,7 +21,7 @@ class EmptyTrashFolderInteractor {
|
||||
|
||||
Stream<Either<Failure, Success>> execute(Session session, AccountId accountId, MailboxId trashMailboxId) async* {
|
||||
try {
|
||||
yield Right<Failure, Success>(LoadingState());
|
||||
yield Right<Failure, Success>(EmptyTrashFolderLoading());
|
||||
|
||||
final listState = await Future.wait([
|
||||
_mailboxRepository.getMailboxState(session, accountId),
|
||||
@@ -31,15 +31,12 @@ class EmptyTrashFolderInteractor {
|
||||
final currentMailboxState = listState.first;
|
||||
final currentEmailState = listState.last;
|
||||
|
||||
final result = await threadRepository.emptyTrashFolder(session, accountId, trashMailboxId);
|
||||
if (result.isNotEmpty) {
|
||||
yield Right<Failure, Success>(EmptyTrashFolderSuccess(
|
||||
currentMailboxState: currentMailboxState,
|
||||
currentEmailState: currentEmailState,
|
||||
));
|
||||
} else {
|
||||
yield Left<Failure, Success>(EmptyTrashFolderFailure(null));
|
||||
}
|
||||
final emailIdDeleted = await threadRepository.emptyTrashFolder(session, accountId, trashMailboxId);
|
||||
yield Right<Failure, Success>(EmptyTrashFolderSuccess(
|
||||
emailIdDeleted,
|
||||
currentMailboxState: currentMailboxState,
|
||||
currentEmailState: currentEmailState,
|
||||
));
|
||||
} catch (e) {
|
||||
yield Left<Failure, Success>(EmptyTrashFolderFailure(e));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user