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();
List<EmailId> emailIdsCompleted = List.empty(growable: true);
try {
bool mailboxHasEmails = true;
UTCDate? lastReceivedDate;
EmailId? lastEmailId;
bool mailboxHasEmails = true;
UTCDate? lastReceivedDate;
EmailId? lastEmailId;
while (mailboxHasEmails) {
final emailResponse = await args.threadAPI
.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(
while (mailboxHasEmails) {
final emailResponse = await args.threadAPI
.getAllEmail(
args.session,
args.accountId,
listEmailUnread.listEmailIds,
ReadActions.markAsRead);
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(): MARK_READ: ${result.emailIdsSuccess.length}');
emailIdsCompleted.addAll(result.emailIdsSuccess);
sendPort.send(emailIdsCompleted);
}
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.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}');
return emailIdsCompleted;
@@ -158,65 +154,61 @@ class MailboxIsolateWorker {
StreamController<Either<Failure, Success>> onProgressController
) async {
List<EmailId> emailIdsCompleted = List.empty(growable: true);
try {
bool mailboxHasEmails = true;
UTCDate? lastReceivedDate;
EmailId? lastEmailId;
bool mailboxHasEmails = true;
UTCDate? lastReceivedDate;
EmailId? lastEmailId;
while (mailboxHasEmails) {
final emailResponse = await _threadApi
.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(
while (mailboxHasEmails) {
final emailResponse = await _threadApi
.getAllEmail(
session,
accountId,
listEmailUnread.listEmailIds,
ReadActions.markAsRead,
);
log('MailboxIsolateWorker::_handleMarkAsMailboxReadActionOnWeb(): MARK_READ: ${result.emailIdsSuccess.length}');
emailIdsCompleted.addAll(result.emailIdsSuccess);
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;
onProgressController.add(Right(UpdatingMarkAsMailboxReadState(
mailboxId: mailboxId,
totalUnread: totalEmailUnread,
countRead: emailIdsCompleted.length)));
}
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,
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}');
return emailIdsCompleted;
@@ -81,56 +81,51 @@ class ThreadIsolateWorker {
EmptyMailboxFolderArguments args,
TypeSendPort sendPort
) async {
try {
final rootIsolateToken = args.isolateToken;
BackgroundIsolateBinaryMessenger.ensureInitialized(rootIsolateToken);
await HiveCacheConfig.instance.setUp();
final rootIsolateToken = args.isolateToken;
BackgroundIsolateBinaryMessenger.ensureInitialized(rootIsolateToken);
await HiveCacheConfig.instance.setUp();
List<EmailId> emailListCompleted = List.empty(growable: true);
List<EmailId> emailListCompleted = List.empty(growable: true);
var hasEmails = true;
Email? lastEmail;
var hasEmails = true;
Email? lastEmail;
while (hasEmails) {
final emailsResponse = await args.threadAPI.getAllEmail(
while (hasEmails) {
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.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.accountId,
newEmailList.listEmailIds);
emailListCompleted.addAll(listEmailIdDeleted.emailIdsSuccess);
sendPort.send(emailListCompleted);
} else {
hasEmails = false;
}
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(
@@ -141,49 +136,45 @@ class ThreadIsolateWorker {
StreamController<dartz.Either<Failure, Success>> onProgressController
) async {
List<EmailId> emailListCompleted = List.empty(growable: true);
try {
var hasEmails = true;
Email? lastEmail;
var hasEmails = true;
Email? lastEmail;
while (hasEmails) {
final emailsResponse = await _threadAPI.getAllEmail(
while (hasEmails) {
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,
accountId,
sort: <Comparator>{}..add(
EmailComparator(EmailComparatorProperty.receivedAt)
..setIsAscending(false)),
filter: EmailFilterCondition(inMailbox: mailboxId, before: lastEmail?.receivedAt),
properties: Properties({
EmailProperty.id,
EmailProperty.receivedAt
}),
);
newEmailList.listEmailIds);
emailListCompleted.addAll(listEmailIdDeleted.emailIdsSuccess);
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,
accountId,
newEmailList.listEmailIds);
emailListCompleted.addAll(listEmailIdDeleted.emailIdsSuccess);
onProgressController.add(Right<Failure, Success>(EmptyingFolderState(
mailboxId, emailListCompleted.length, totalEmails
)));
} else {
hasEmails = false;
}
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}');
return emailListCompleted;