From 34c878c7ec6f41d3710a5487b643f9a41e024775 Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 24 Dec 2022 10:10:21 +0100 Subject: [PATCH] Defer daily rules --- .../email/BoundaryCallbackMessages.java | 2 +- app/src/main/java/eu/faircode/email/Core.java | 33 ++++++++++++------- .../main/java/eu/faircode/email/DaoRule.java | 3 +- .../eu/faircode/email/FragmentFolders.java | 2 +- .../eu/faircode/email/WorkerDailyRules.java | 21 ++++++------ 5 files changed, 35 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/BoundaryCallbackMessages.java b/app/src/main/java/eu/faircode/email/BoundaryCallbackMessages.java index 391315396d..81dd588dcb 100644 --- a/app/src/main/java/eu/faircode/email/BoundaryCallbackMessages.java +++ b/app/src/main/java/eu/faircode/email/BoundaryCallbackMessages.java @@ -604,7 +604,7 @@ public class BoundaryCallbackMessages extends PagedList.BoundaryCallback rules = db.rule().getEnabledRules(browsable.id); + List rules = db.rule().getEnabledRules(browsable.id, false); int found = 0; while (state.index >= 0 && found < pageSize && !state.destroyed) { diff --git a/app/src/main/java/eu/faircode/email/Core.java b/app/src/main/java/eu/faircode/email/Core.java index cc8e342c79..62b8321919 100644 --- a/app/src/main/java/eu/faircode/email/Core.java +++ b/app/src/main/java/eu/faircode/email/Core.java @@ -1774,7 +1774,7 @@ class Core { SyncStats stats = new SyncStats(); boolean download = db.folder().getFolderDownload(folder.id); - List rules = db.rule().getEnabledRules(folder.id); + List rules = db.rule().getEnabledRules(folder.id, false); FetchProfile fp = new FetchProfile(); fp.add(UIDFolder.FetchProfileItem.UID); // To check if message exists @@ -2996,18 +2996,28 @@ class Core { } private static void onRule(Context context, JSONArray jargs, EntityMessage message) throws JSONException, MessagingException { - // Download message body + // Deferred rule (download headers, body, etc) DB db = DB.getInstance(context); long id = jargs.getLong(0); - EntityRule rule = db.rule().getRule(id); - if (rule == null) - throw new IllegalArgumentException("Rule not found id=" + id); + if (id < 0) { + List rules = db.rule().getEnabledRules(message.folder, true); + for (EntityRule rule : rules) + if (rule.matches(context, message, null, null)) { + rule.execute(context, message); + if (rule.stop) + break; + } + } else { + EntityRule rule = db.rule().getRule(id); + if (rule == null) + throw new IllegalArgumentException("Rule not found id=" + id); - if (!message.content) - throw new IllegalArgumentException("Message without content id=" + rule.id + ":" + rule.name); + if (!message.content) + throw new IllegalArgumentException("Message without content id=" + rule.id + ":" + rule.name); - rule.execute(context, message); + rule.execute(context, message); + } } private static void onDownload(Context context, JSONArray jargs, EntityAccount account, EntityFolder folder, EntityMessage message, IMAPStore istore, IMAPFolder ifolder, State state) throws MessagingException, IOException, JSONException { @@ -3046,7 +3056,7 @@ class Core { return; } - List rules = db.rule().getEnabledRules(folder.id); + List rules = db.rule().getEnabledRules(folder.id, false); try { db.folder().setFolderSyncState(folder.id, "syncing"); @@ -3939,7 +3949,7 @@ class Core { Log.i(folder.name + " delete local uid=" + uid + " count=" + count); } - List rules = db.rule().getEnabledRules(folder.id); + List rules = db.rule().getEnabledRules(folder.id, false); fp.add(FetchProfile.Item.ENVELOPE); //fp.add(FetchProfile.Item.FLAGS); @@ -5004,8 +5014,7 @@ class Core { try { boolean executed = false; for (EntityRule rule : rules) - if (!rule.daily && - rule.matches(context, message, headers, html)) { + if (rule.matches(context, message, headers, html)) { rule.execute(context, message); executed = true; if (rule.stop) diff --git a/app/src/main/java/eu/faircode/email/DaoRule.java b/app/src/main/java/eu/faircode/email/DaoRule.java index 3114b30c31..7dd2eabe45 100644 --- a/app/src/main/java/eu/faircode/email/DaoRule.java +++ b/app/src/main/java/eu/faircode/email/DaoRule.java @@ -37,8 +37,9 @@ public interface DaoRule { @Query("SELECT * FROM rule" + " WHERE folder = :folder" + " AND enabled" + + " AND (:daily IS NULL OR daily = :daily)" + " ORDER BY `order`, name COLLATE NOCASE") - List getEnabledRules(long folder); + List getEnabledRules(long folder, Boolean daily); @Query("SELECT rule.*, folder.account, folder.name AS folderName, account.name AS accountName FROM rule" + " JOIN folder ON folder.id = rule.folder" + diff --git a/app/src/main/java/eu/faircode/email/FragmentFolders.java b/app/src/main/java/eu/faircode/email/FragmentFolders.java index 31c8c5b2b0..24d41adb94 100644 --- a/app/src/main/java/eu/faircode/email/FragmentFolders.java +++ b/app/src/main/java/eu/faircode/email/FragmentFolders.java @@ -1157,7 +1157,7 @@ public class FragmentFolders extends FragmentBase { DB db = DB.getInstance(context); - List rules = db.rule().getEnabledRules(fid); + List rules = db.rule().getEnabledRules(fid, false); if (rules == null) return 0; EntityLog.log(context, "Executing rules count=" + rules.size()); diff --git a/app/src/main/java/eu/faircode/email/WorkerDailyRules.java b/app/src/main/java/eu/faircode/email/WorkerDailyRules.java index 12fff925e4..fa88e1bffe 100644 --- a/app/src/main/java/eu/faircode/email/WorkerDailyRules.java +++ b/app/src/main/java/eu/faircode/email/WorkerDailyRules.java @@ -56,16 +56,10 @@ public class WorkerDailyRules extends Worker { for (EntityAccount account : accounts) { List folders = db.folder().getFolders(account.id, false, false); for (EntityFolder folder : folders) { - List rules = db.rule().getEnabledRules(folder.id); - - int daily = 0; - for (EntityRule rule : rules) - if (rule.daily) - daily++; - if (daily == 0) + List rules = db.rule().getEnabledRules(folder.id, true); + if (rules.size() == 0) continue; - int count = 0; List mids = db.message().getMessageIdsByFolder(folder.id); for (long mid : mids) @@ -75,22 +69,27 @@ public class WorkerDailyRules extends Worker { continue; count++; + boolean defer = false; boolean needsHeaders = EntityRule.needsHeaders(message, rules); boolean needsBody = EntityRule.needsBody(message, rules); if (needsHeaders && message.headers == null) { + defer = true; EntityOperation.queue(context, message, EntityOperation.HEADERS); - continue; } if (needsBody && !message.content) { + defer = true; EntityOperation.queue(context, message, EntityOperation.BODY); + } + + if (defer) { + EntityOperation.queue(context, message, EntityOperation.RULE, -1L); continue; } for (EntityRule rule : rules) - if (rule.daily && - rule.matches(context, message, null, null)) { + if (rule.matches(context, message, null, null)) { rule.execute(context, message); if (rule.stop) break;