@@ -9,6 +9,7 @@ import android.content.Context
99import com.flowcrypt.email.R
1010import com.flowcrypt.email.api.email.FoldersManager
1111import com.flowcrypt.email.api.email.gmail.GmailApiHelper.Companion.LABEL_DRAFT
12+ import com.flowcrypt.email.api.email.gmail.GmailApiHelper.Companion.LABEL_SPAM
1213import com.flowcrypt.email.api.email.gmail.GmailApiHelper.Companion.LABEL_TRASH
1314import com.flowcrypt.email.api.email.gmail.GmailApiHelper.Companion.labelsToImapFlags
1415import com.flowcrypt.email.api.email.gmail.api.GmaiAPIMimeMessage
@@ -128,10 +129,15 @@ object GmailHistoryHandler {
128129 val toBeDeletedLocally =
129130 gmailThreadInfoList.filter { ! it.labels.contains(localFolder.fullName) }.toSet()
130131
132+ val threadIdsOfForeverDeleted =
133+ threadIdsToBeDeleted - gmailThreadInfoList.map { it.id }.toSet()
134+
131135 roomDatabase.msgDao().deleteByUIDsSuspend(
132136 account = accountEntity.email,
133137 label = localFolder.fullName,
134- msgsUID = toBeDeletedLocally.map { Long .MAX_VALUE - it.id.toLongRadix16 })
138+ msgsUID = toBeDeletedLocally.map { Long .MAX_VALUE - it.id.toLongRadix16 }
139+ + threadIdsOfForeverDeleted.map { Long .MAX_VALUE - it.toLongRadix16 }
140+ )
135141
136142 // need to update the left threads
137143 val toBeUpdated = gmailThreadInfoList - toBeDeletedLocally
@@ -388,21 +394,34 @@ object GmailHistoryHandler {
388394 historyMessageLabelIds.joinToString(LABEL_IDS_SEPARATOR )
389395 )
390396
391- if (localFolder.fullName in (historyLabelRemoved.labelIds ? : emptyList())) {
392- newCandidatesMap.remove(historyLabelRemoved.message.uid)
393- updateCandidates.remove(historyLabelRemoved.message.uid)
394- deleteCandidates[historyLabelRemoved.message.uid] = historyLabelRemoved.message.threadId
395- continue
396- }
397-
398- if (LABEL_TRASH in (historyLabelRemoved.labelIds ? : emptyList())) {
399- val message = historyLabelRemoved.message
400- if (localFolder.fullName in historyMessageLabelIds) {
401- deleteCandidates.remove(message.uid)
402- updateCandidates.remove(message.uid)
403- newCandidatesMap[message.uid] = message
397+ if (localFolder.isAll) {
398+ if (
399+ historyLabelRemoved.labelIds?.contains(LABEL_TRASH ) == true ||
400+ historyLabelRemoved.labelIds?.contains(LABEL_SPAM ) == true
401+ ) {
402+ deleteCandidates.remove(historyLabelRemoved.message.uid)
403+ updateCandidates.remove(historyLabelRemoved.message.uid)
404+ newCandidatesMap[historyLabelRemoved.message.uid] = historyLabelRemoved.message
404405 continue
405406 }
407+ } else {
408+ if (localFolder.fullName in (historyLabelRemoved.labelIds ? : emptyList())) {
409+ newCandidatesMap.remove(historyLabelRemoved.message.uid)
410+ updateCandidates.remove(historyLabelRemoved.message.uid)
411+ deleteCandidates[historyLabelRemoved.message.uid] =
412+ historyLabelRemoved.message.threadId
413+ continue
414+ }
415+
416+ if (LABEL_TRASH in (historyLabelRemoved.labelIds ? : emptyList())) {
417+ val message = historyLabelRemoved.message
418+ if (localFolder.fullName in historyMessageLabelIds) {
419+ deleteCandidates.remove(message.uid)
420+ updateCandidates.remove(message.uid)
421+ newCandidatesMap[message.uid] = message
422+ continue
423+ }
424+ }
406425 }
407426
408427 val existedFlags = labelsToImapFlags(historyMessageLabelIds)
@@ -417,18 +436,31 @@ object GmailHistoryHandler {
417436 historyLabelAdded.message.threadId,
418437 historyLabelAdded.message.labelIds.joinToString(LABEL_IDS_SEPARATOR )
419438 )
420- if (localFolder.fullName in (historyLabelAdded.labelIds ? : emptyList())) {
421- deleteCandidates.remove(historyLabelAdded.message.uid)
422- updateCandidates.remove(historyLabelAdded.message.uid)
423- newCandidatesMap[historyLabelAdded.message.uid] = historyLabelAdded.message
424- continue
425- }
426439
427- if ((historyLabelAdded.labelIds ? : emptyList()).contains(LABEL_TRASH )) {
428- newCandidatesMap.remove(historyLabelAdded.message.uid)
429- updateCandidates.remove(historyLabelAdded.message.uid)
430- deleteCandidates[historyLabelAdded.message.uid] = historyLabelAdded.message.threadId
431- continue
440+ if (localFolder.isAll) {
441+ if (
442+ historyLabelAdded.labelIds?.contains(LABEL_TRASH ) == true ||
443+ historyLabelAdded.labelIds?.contains(LABEL_SPAM ) == true
444+ ) {
445+ newCandidatesMap.remove(historyLabelAdded.message.uid)
446+ updateCandidates.remove(historyLabelAdded.message.uid)
447+ deleteCandidates[historyLabelAdded.message.uid] = historyLabelAdded.message.threadId
448+ continue
449+ }
450+ } else {
451+ if (localFolder.fullName in (historyLabelAdded.labelIds ? : emptyList())) {
452+ deleteCandidates.remove(historyLabelAdded.message.uid)
453+ updateCandidates.remove(historyLabelAdded.message.uid)
454+ newCandidatesMap[historyLabelAdded.message.uid] = historyLabelAdded.message
455+ continue
456+ }
457+
458+ if ((historyLabelAdded.labelIds ? : emptyList()).contains(LABEL_TRASH )) {
459+ newCandidatesMap.remove(historyLabelAdded.message.uid)
460+ updateCandidates.remove(historyLabelAdded.message.uid)
461+ deleteCandidates[historyLabelAdded.message.uid] = historyLabelAdded.message.threadId
462+ continue
463+ }
432464 }
433465
434466 val existedFlags = labelsToImapFlags(historyLabelAdded.message.labelIds ? : emptyList())
@@ -437,6 +469,12 @@ object GmailHistoryHandler {
437469 }
438470 }
439471 }
472+
473+ deleteCandidates.keys.forEach {
474+ // we remove updates here as updating labels doesn't make sense for deleted items
475+ labelsToBeUpdatedMap.remove(it)
476+ }
477+
440478 action.invoke(
441479 deleteCandidates,
442480 newCandidatesMap,
0 commit comments