From ea3f1ef6f0104328c4ce0afb986fa747ffea9094 Mon Sep 17 00:00:00 2001 From: M66B Date: Fri, 13 Nov 2020 14:06:51 +0100 Subject: [PATCH] Delete old messages on deleting browsed messages --- .../java/eu/faircode/email/DaoMessage.java | 4 +-- .../eu/faircode/email/FragmentFolders.java | 27 ++++++++++++++++--- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/DaoMessage.java b/app/src/main/java/eu/faircode/email/DaoMessage.java index 8cb241f0fb..be473e2dfc 100644 --- a/app/src/main/java/eu/faircode/email/DaoMessage.java +++ b/app/src/main/java/eu/faircode/email/DaoMessage.java @@ -764,9 +764,9 @@ public interface DaoMessage { @Query("DELETE FROM message" + " WHERE folder = :folder" + - " AND ui_browsed" + + " AND (ui_browsed OR received < :before)" + " AND NOT uid IS NULL") - int deleteBrowsedMessages(long folder); + int deleteBrowsedMessages(long folder, long before); @Query("DELETE FROM message" + " WHERE folder = :folder" + diff --git a/app/src/main/java/eu/faircode/email/FragmentFolders.java b/app/src/main/java/eu/faircode/email/FragmentFolders.java index 7bf1c37a94..053e160711 100644 --- a/app/src/main/java/eu/faircode/email/FragmentFolders.java +++ b/app/src/main/java/eu/faircode/email/FragmentFolders.java @@ -58,6 +58,7 @@ import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.google.android.material.snackbar.Snackbar; import java.text.NumberFormat; +import java.util.Calendar; import java.util.Collections; import java.util.List; @@ -678,9 +679,29 @@ public class FragmentFolders extends FragmentBase { try { db.beginTransaction(); - if (browsed) - db.message().deleteBrowsedMessages(fid); - else { + if (browsed) { + EntityFolder folder = db.folder().getFolder(fid); + if (folder == null) + return null; + + int keep_days = folder.keep_days; + if (keep_days == folder.sync_days && + keep_days != Integer.MAX_VALUE) + keep_days++; + + Calendar cal_keep = Calendar.getInstance(); + cal_keep.add(Calendar.DAY_OF_MONTH, -keep_days); + cal_keep.set(Calendar.HOUR_OF_DAY, 0); + cal_keep.set(Calendar.MINUTE, 0); + cal_keep.set(Calendar.SECOND, 0); + cal_keep.set(Calendar.MILLISECOND, 0); + + long keep_time = cal_keep.getTimeInMillis(); + if (keep_time < 0) + keep_time = 0; + + db.message().deleteBrowsedMessages(fid, keep_time); + } else { db.message().deleteLocalMessages(fid); db.folder().setFolderKeywords(fid, DB.Converters.fromStringArray(null)); }