Fix mark as mailbox, empty trash do not reconnect when get 401 error

Signed-off-by: dab246 <tdvu@linagora.com>
This commit is contained in:
dab246
2025-04-21 15:57:32 +07:00
committed by Dat H. Pham
parent 7a819191cb
commit 3996372dcc
2 changed files with 168 additions and 185 deletions
@@ -90,61 +90,57 @@ class MailboxIsolateWorker {
await HiveCacheConfig.instance.setUp(); await HiveCacheConfig.instance.setUp();
List<EmailId> emailIdsCompleted = List.empty(growable: true); List<EmailId> emailIdsCompleted = List.empty(growable: true);
try { bool mailboxHasEmails = true;
bool mailboxHasEmails = true; UTCDate? lastReceivedDate;
UTCDate? lastReceivedDate; EmailId? lastEmailId;
EmailId? lastEmailId;
while (mailboxHasEmails) { while (mailboxHasEmails) {
final emailResponse = await args.threadAPI final emailResponse = await args.threadAPI
.getAllEmail( .getAllEmail(
args.session,
args.accountId,
limit: UnsignedInt(30),
filter: EmailFilterCondition(
inMailbox: args.mailboxId,
notKeyword: KeyWordIdentifier.emailSeen.value,
before: lastReceivedDate),
sort: <Comparator>{}..add(
EmailComparator(EmailComparatorProperty.receivedAt)
..setIsAscending(false)),
properties: Properties({
EmailProperty.id,
EmailProperty.keywords,
EmailProperty.receivedAt,
}))
.then((response) {
var listEmails = response.emailList;
if (listEmails != null && listEmails.isNotEmpty && lastEmailId != null) {
listEmails = listEmails
.where((email) => email.id != lastEmailId)
.toList();
}
return EmailsResponse(emailList: listEmails, state: response.state);
});
final listEmailUnread = emailResponse.emailList;
log('MailboxIsolateWorker::_handleMarkAsMailboxRead(): listEmailUnread: ${listEmailUnread?.length}');
if (listEmailUnread == null || listEmailUnread.isEmpty) {
mailboxHasEmails = false;
} else {
lastEmailId = listEmailUnread.last.id;
lastReceivedDate = listEmailUnread.last.receivedAt;
final result = await args.emailAPI.markAsRead(
args.session, args.session,
args.accountId, args.accountId,
listEmailUnread.listEmailIds, limit: UnsignedInt(30),
ReadActions.markAsRead); filter: EmailFilterCondition(
inMailbox: args.mailboxId,
notKeyword: KeyWordIdentifier.emailSeen.value,
before: lastReceivedDate),
sort: <Comparator>{}..add(
EmailComparator(EmailComparatorProperty.receivedAt)
..setIsAscending(false)),
properties: Properties({
EmailProperty.id,
EmailProperty.keywords,
EmailProperty.receivedAt,
}))
.then((response) {
var listEmails = response.emailList;
if (listEmails != null && listEmails.isNotEmpty && lastEmailId != null) {
listEmails = listEmails
.where((email) => email.id != lastEmailId)
.toList();
}
return EmailsResponse(emailList: listEmails, state: response.state);
});
final listEmailUnread = emailResponse.emailList;
log('MailboxIsolateWorker::_handleMarkAsMailboxRead(): MARK_READ: ${result.emailIdsSuccess.length}'); log('MailboxIsolateWorker::_handleMarkAsMailboxRead(): listEmailUnread: ${listEmailUnread?.length}');
emailIdsCompleted.addAll(result.emailIdsSuccess);
sendPort.send(emailIdsCompleted); if (listEmailUnread == null || listEmailUnread.isEmpty) {
} mailboxHasEmails = false;
} else {
lastEmailId = listEmailUnread.last.id;
lastReceivedDate = listEmailUnread.last.receivedAt;
final result = await args.emailAPI.markAsRead(
args.session,
args.accountId,
listEmailUnread.listEmailIds,
ReadActions.markAsRead);
log('MailboxIsolateWorker::_handleMarkAsMailboxRead(): MARK_READ: ${result.emailIdsSuccess.length}');
emailIdsCompleted.addAll(result.emailIdsSuccess);
sendPort.send(emailIdsCompleted);
} }
} catch (e) {
log('MailboxIsolateWorker::_handleMarkAsMailboxRead(): ERROR: $e');
} }
log('MailboxIsolateWorker::_handleMarkAsMailboxRead(): TOTAL_READ: ${emailIdsCompleted.length}'); log('MailboxIsolateWorker::_handleMarkAsMailboxRead(): TOTAL_READ: ${emailIdsCompleted.length}');
return emailIdsCompleted; return emailIdsCompleted;
@@ -158,65 +154,61 @@ class MailboxIsolateWorker {
StreamController<Either<Failure, Success>> onProgressController StreamController<Either<Failure, Success>> onProgressController
) async { ) async {
List<EmailId> emailIdsCompleted = List.empty(growable: true); List<EmailId> emailIdsCompleted = List.empty(growable: true);
try { bool mailboxHasEmails = true;
bool mailboxHasEmails = true; UTCDate? lastReceivedDate;
UTCDate? lastReceivedDate; EmailId? lastEmailId;
EmailId? lastEmailId;
while (mailboxHasEmails) { while (mailboxHasEmails) {
final emailResponse = await _threadApi final emailResponse = await _threadApi
.getAllEmail( .getAllEmail(
session,
accountId,
limit: UnsignedInt(30),
filter: EmailFilterCondition(
inMailbox: mailboxId,
notKeyword: KeyWordIdentifier.emailSeen.value,
before: lastReceivedDate),
sort: <Comparator>{}..add(
EmailComparator(EmailComparatorProperty.receivedAt)
..setIsAscending(false)),
properties: Properties({
EmailProperty.id,
EmailProperty.keywords,
EmailProperty.receivedAt,
})
).then((response) {
var listEmails = response.emailList;
if (listEmails != null && listEmails.isNotEmpty && lastEmailId != null) {
listEmails = listEmails
.where((email) => email.id != lastEmailId)
.toList();
}
return EmailsResponse(emailList: listEmails, state: response.state);
});
final listEmailUnread = emailResponse.emailList;
log('MailboxIsolateWorker::_handleMarkAsMailboxReadActionOnWeb(): listEmailUnread: ${listEmailUnread?.length}');
if (listEmailUnread == null || listEmailUnread.isEmpty) {
mailboxHasEmails = false;
} else {
lastEmailId = listEmailUnread.last.id;
lastReceivedDate = listEmailUnread.last.receivedAt;
final result = await _emailApi.markAsRead(
session, session,
accountId, accountId,
listEmailUnread.listEmailIds, limit: UnsignedInt(30),
ReadActions.markAsRead, filter: EmailFilterCondition(
); inMailbox: mailboxId,
log('MailboxIsolateWorker::_handleMarkAsMailboxReadActionOnWeb(): MARK_READ: ${result.emailIdsSuccess.length}'); notKeyword: KeyWordIdentifier.emailSeen.value,
emailIdsCompleted.addAll(result.emailIdsSuccess); before: lastReceivedDate),
sort: <Comparator>{}..add(
EmailComparator(EmailComparatorProperty.receivedAt)
..setIsAscending(false)),
properties: Properties({
EmailProperty.id,
EmailProperty.keywords,
EmailProperty.receivedAt,
})
).then((response) {
var listEmails = response.emailList;
if (listEmails != null && listEmails.isNotEmpty && lastEmailId != null) {
listEmails = listEmails
.where((email) => email.id != lastEmailId)
.toList();
}
return EmailsResponse(emailList: listEmails, state: response.state);
});
final listEmailUnread = emailResponse.emailList;
onProgressController.add(Right(UpdatingMarkAsMailboxReadState( log('MailboxIsolateWorker::_handleMarkAsMailboxReadActionOnWeb(): listEmailUnread: ${listEmailUnread?.length}');
mailboxId: mailboxId,
totalUnread: totalEmailUnread, if (listEmailUnread == null || listEmailUnread.isEmpty) {
countRead: emailIdsCompleted.length))); mailboxHasEmails = false;
} } else {
lastEmailId = listEmailUnread.last.id;
lastReceivedDate = listEmailUnread.last.receivedAt;
final result = await _emailApi.markAsRead(
session,
accountId,
listEmailUnread.listEmailIds,
ReadActions.markAsRead,
);
log('MailboxIsolateWorker::_handleMarkAsMailboxReadActionOnWeb(): MARK_READ: ${result.emailIdsSuccess.length}');
emailIdsCompleted.addAll(result.emailIdsSuccess);
onProgressController.add(Right(UpdatingMarkAsMailboxReadState(
mailboxId: mailboxId,
totalUnread: totalEmailUnread,
countRead: emailIdsCompleted.length)));
} }
} catch (e) {
log('MailboxIsolateWorker::_handleMarkAsMailboxReadActionOnWeb(): ERROR: $e');
} }
log('MailboxIsolateWorker::_handleMarkAsMailboxReadActionOnWeb(): TOTAL_READ: ${emailIdsCompleted.length}'); log('MailboxIsolateWorker::_handleMarkAsMailboxReadActionOnWeb(): TOTAL_READ: ${emailIdsCompleted.length}');
return emailIdsCompleted; return emailIdsCompleted;
@@ -81,56 +81,51 @@ class ThreadIsolateWorker {
EmptyMailboxFolderArguments args, EmptyMailboxFolderArguments args,
TypeSendPort sendPort TypeSendPort sendPort
) async { ) async {
try { final rootIsolateToken = args.isolateToken;
final rootIsolateToken = args.isolateToken; BackgroundIsolateBinaryMessenger.ensureInitialized(rootIsolateToken);
BackgroundIsolateBinaryMessenger.ensureInitialized(rootIsolateToken); await HiveCacheConfig.instance.setUp();
await HiveCacheConfig.instance.setUp();
List<EmailId> emailListCompleted = List.empty(growable: true); List<EmailId> emailListCompleted = List.empty(growable: true);
var hasEmails = true; var hasEmails = true;
Email? lastEmail; Email? lastEmail;
while (hasEmails) { while (hasEmails) {
final emailsResponse = await args.threadAPI.getAllEmail( final emailsResponse = await args.threadAPI.getAllEmail(
args.session,
args.accountId,
sort: <Comparator>{}..add(
EmailComparator(EmailComparatorProperty.receivedAt)
..setIsAscending(false)),
filter: EmailFilterCondition(inMailbox: args.mailboxId, before: lastEmail?.receivedAt),
properties: Properties({
EmailProperty.id,
EmailProperty.receivedAt
}),
);
var newEmailList = emailsResponse.emailList ?? <Email>[];
if (lastEmail != null) {
newEmailList = newEmailList.where((email) => email.id != lastEmail!.id).toList();
}
log('ThreadIsolateWorker::_emptyMailboxFolderAction(): ${newEmailList.length}');
if (newEmailList.isNotEmpty) {
lastEmail = newEmailList.last;
hasEmails = true;
final listEmailIdDeleted = await args.emailAPI.deleteMultipleEmailsPermanently(
args.session, args.session,
args.accountId, args.accountId,
sort: <Comparator>{}..add( newEmailList.listEmailIds);
EmailComparator(EmailComparatorProperty.receivedAt) emailListCompleted.addAll(listEmailIdDeleted.emailIdsSuccess);
..setIsAscending(false)), sendPort.send(emailListCompleted);
filter: EmailFilterCondition(inMailbox: args.mailboxId, before: lastEmail?.receivedAt), } else {
properties: Properties({ hasEmails = false;
EmailProperty.id,
EmailProperty.receivedAt
}),
);
var newEmailList = emailsResponse.emailList ?? <Email>[];
if (lastEmail != null) {
newEmailList = newEmailList.where((email) => email.id != lastEmail!.id).toList();
}
log('ThreadIsolateWorker::_emptyMailboxFolderAction(): ${newEmailList.length}');
if (newEmailList.isNotEmpty) {
lastEmail = newEmailList.last;
hasEmails = true;
final listEmailIdDeleted = await args.emailAPI.deleteMultipleEmailsPermanently(
args.session,
args.accountId,
newEmailList.listEmailIds);
emailListCompleted.addAll(listEmailIdDeleted.emailIdsSuccess);
sendPort.send(emailListCompleted);
} else {
hasEmails = false;
}
} }
log('ThreadIsolateWorker::_emptyMailboxFolderAction(): TOTAL_REMOVE: ${emailListCompleted.length}');
return emailListCompleted;
} catch (e) {
logError('ThreadIsolateWorker::_emptyMailboxFolderAction(): ERROR: $e');
rethrow;
} }
log('ThreadIsolateWorker::_emptyMailboxFolderAction(): TOTAL_REMOVE: ${emailListCompleted.length}');
return emailListCompleted;
} }
Future<List<EmailId>> _emptyMailboxFolderOnWeb( Future<List<EmailId>> _emptyMailboxFolderOnWeb(
@@ -141,49 +136,45 @@ class ThreadIsolateWorker {
StreamController<dartz.Either<Failure, Success>> onProgressController StreamController<dartz.Either<Failure, Success>> onProgressController
) async { ) async {
List<EmailId> emailListCompleted = List.empty(growable: true); List<EmailId> emailListCompleted = List.empty(growable: true);
try { var hasEmails = true;
var hasEmails = true; Email? lastEmail;
Email? lastEmail;
while (hasEmails) { while (hasEmails) {
final emailsResponse = await _threadAPI.getAllEmail( final emailsResponse = await _threadAPI.getAllEmail(
session,
accountId,
sort: <Comparator>{}..add(
EmailComparator(EmailComparatorProperty.receivedAt)
..setIsAscending(false)),
filter: EmailFilterCondition(inMailbox: mailboxId, before: lastEmail?.receivedAt),
properties: Properties({
EmailProperty.id,
EmailProperty.receivedAt
}),
);
var newEmailList = emailsResponse.emailList ?? <Email>[];
if (lastEmail != null) {
newEmailList = newEmailList.where((email) => email.id != lastEmail!.id).toList();
}
log('ThreadIsolateWorker::_emptyMailboxFolderOnWeb(): ${newEmailList.length}');
if (newEmailList.isNotEmpty) {
lastEmail = newEmailList.last;
hasEmails = true;
final listEmailIdDeleted = await _emailAPI.deleteMultipleEmailsPermanently(
session, session,
accountId, accountId,
sort: <Comparator>{}..add( newEmailList.listEmailIds);
EmailComparator(EmailComparatorProperty.receivedAt) emailListCompleted.addAll(listEmailIdDeleted.emailIdsSuccess);
..setIsAscending(false)),
filter: EmailFilterCondition(inMailbox: mailboxId, before: lastEmail?.receivedAt),
properties: Properties({
EmailProperty.id,
EmailProperty.receivedAt
}),
);
var newEmailList = emailsResponse.emailList ?? <Email>[]; onProgressController.add(Right<Failure, Success>(EmptyingFolderState(
if (lastEmail != null) { mailboxId, emailListCompleted.length, totalEmails
newEmailList = newEmailList.where((email) => email.id != lastEmail!.id).toList(); )));
} } else {
hasEmails = false;
log('ThreadIsolateWorker::_emptyMailboxFolderOnWeb(): ${newEmailList.length}');
if (newEmailList.isNotEmpty) {
lastEmail = newEmailList.last;
hasEmails = true;
final listEmailIdDeleted = await _emailAPI.deleteMultipleEmailsPermanently(
session,
accountId,
newEmailList.listEmailIds);
emailListCompleted.addAll(listEmailIdDeleted.emailIdsSuccess);
onProgressController.add(Right<Failure, Success>(EmptyingFolderState(
mailboxId, emailListCompleted.length, totalEmails
)));
} else {
hasEmails = false;
}
} }
} catch (e) {
log('ThreadIsolateWorker::_emptyMailboxFolderOnWeb(): ERROR: $e');
} }
log('ThreadIsolateWorker::_emptyMailboxFolderOnWeb(): TOTAL_REMOVE: ${emailListCompleted.length}'); log('ThreadIsolateWorker::_emptyMailboxFolderOnWeb(): TOTAL_REMOVE: ${emailListCompleted.length}');
return emailListCompleted; return emailListCompleted;