Defer daily rules

pull/210/head
M66B 2 years ago
parent 64609c67e9
commit 34c878c7ec

@ -604,7 +604,7 @@ public class BoundaryCallbackMessages extends PagedList.BoundaryCallback<TupleMe
throw ex; throw ex;
} }
List<EntityRule> rules = db.rule().getEnabledRules(browsable.id); List<EntityRule> rules = db.rule().getEnabledRules(browsable.id, false);
int found = 0; int found = 0;
while (state.index >= 0 && found < pageSize && !state.destroyed) { while (state.index >= 0 && found < pageSize && !state.destroyed) {

@ -1774,7 +1774,7 @@ class Core {
SyncStats stats = new SyncStats(); SyncStats stats = new SyncStats();
boolean download = db.folder().getFolderDownload(folder.id); boolean download = db.folder().getFolderDownload(folder.id);
List<EntityRule> rules = db.rule().getEnabledRules(folder.id); List<EntityRule> rules = db.rule().getEnabledRules(folder.id, false);
FetchProfile fp = new FetchProfile(); FetchProfile fp = new FetchProfile();
fp.add(UIDFolder.FetchProfileItem.UID); // To check if message exists 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 { 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); DB db = DB.getInstance(context);
long id = jargs.getLong(0); long id = jargs.getLong(0);
EntityRule rule = db.rule().getRule(id); if (id < 0) {
if (rule == null) List<EntityRule> rules = db.rule().getEnabledRules(message.folder, true);
throw new IllegalArgumentException("Rule not found id=" + id); 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) if (!message.content)
throw new IllegalArgumentException("Message without content id=" + rule.id + ":" + rule.name); 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 { 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; return;
} }
List<EntityRule> rules = db.rule().getEnabledRules(folder.id); List<EntityRule> rules = db.rule().getEnabledRules(folder.id, false);
try { try {
db.folder().setFolderSyncState(folder.id, "syncing"); db.folder().setFolderSyncState(folder.id, "syncing");
@ -3939,7 +3949,7 @@ class Core {
Log.i(folder.name + " delete local uid=" + uid + " count=" + count); Log.i(folder.name + " delete local uid=" + uid + " count=" + count);
} }
List<EntityRule> rules = db.rule().getEnabledRules(folder.id); List<EntityRule> rules = db.rule().getEnabledRules(folder.id, false);
fp.add(FetchProfile.Item.ENVELOPE); fp.add(FetchProfile.Item.ENVELOPE);
//fp.add(FetchProfile.Item.FLAGS); //fp.add(FetchProfile.Item.FLAGS);
@ -5004,8 +5014,7 @@ class Core {
try { try {
boolean executed = false; boolean executed = false;
for (EntityRule rule : rules) for (EntityRule rule : rules)
if (!rule.daily && if (rule.matches(context, message, headers, html)) {
rule.matches(context, message, headers, html)) {
rule.execute(context, message); rule.execute(context, message);
executed = true; executed = true;
if (rule.stop) if (rule.stop)

@ -37,8 +37,9 @@ public interface DaoRule {
@Query("SELECT * FROM rule" + @Query("SELECT * FROM rule" +
" WHERE folder = :folder" + " WHERE folder = :folder" +
" AND enabled" + " AND enabled" +
" AND (:daily IS NULL OR daily = :daily)" +
" ORDER BY `order`, name COLLATE NOCASE") " ORDER BY `order`, name COLLATE NOCASE")
List<EntityRule> getEnabledRules(long folder); List<EntityRule> getEnabledRules(long folder, Boolean daily);
@Query("SELECT rule.*, folder.account, folder.name AS folderName, account.name AS accountName FROM rule" + @Query("SELECT rule.*, folder.account, folder.name AS folderName, account.name AS accountName FROM rule" +
" JOIN folder ON folder.id = rule.folder" + " JOIN folder ON folder.id = rule.folder" +

@ -1157,7 +1157,7 @@ public class FragmentFolders extends FragmentBase {
DB db = DB.getInstance(context); DB db = DB.getInstance(context);
List<EntityRule> rules = db.rule().getEnabledRules(fid); List<EntityRule> rules = db.rule().getEnabledRules(fid, false);
if (rules == null) if (rules == null)
return 0; return 0;
EntityLog.log(context, "Executing rules count=" + rules.size()); EntityLog.log(context, "Executing rules count=" + rules.size());

@ -56,16 +56,10 @@ public class WorkerDailyRules extends Worker {
for (EntityAccount account : accounts) { for (EntityAccount account : accounts) {
List<EntityFolder> folders = db.folder().getFolders(account.id, false, false); List<EntityFolder> folders = db.folder().getFolders(account.id, false, false);
for (EntityFolder folder : folders) { for (EntityFolder folder : folders) {
List<EntityRule> rules = db.rule().getEnabledRules(folder.id); List<EntityRule> rules = db.rule().getEnabledRules(folder.id, true);
if (rules.size() == 0)
int daily = 0;
for (EntityRule rule : rules)
if (rule.daily)
daily++;
if (daily == 0)
continue; continue;
int count = 0; int count = 0;
List<Long> mids = db.message().getMessageIdsByFolder(folder.id); List<Long> mids = db.message().getMessageIdsByFolder(folder.id);
for (long mid : mids) for (long mid : mids)
@ -75,22 +69,27 @@ public class WorkerDailyRules extends Worker {
continue; continue;
count++; count++;
boolean defer = false;
boolean needsHeaders = EntityRule.needsHeaders(message, rules); boolean needsHeaders = EntityRule.needsHeaders(message, rules);
boolean needsBody = EntityRule.needsBody(message, rules); boolean needsBody = EntityRule.needsBody(message, rules);
if (needsHeaders && message.headers == null) { if (needsHeaders && message.headers == null) {
defer = true;
EntityOperation.queue(context, message, EntityOperation.HEADERS); EntityOperation.queue(context, message, EntityOperation.HEADERS);
continue;
} }
if (needsBody && !message.content) { if (needsBody && !message.content) {
defer = true;
EntityOperation.queue(context, message, EntityOperation.BODY); EntityOperation.queue(context, message, EntityOperation.BODY);
}
if (defer) {
EntityOperation.queue(context, message, EntityOperation.RULE, -1L);
continue; continue;
} }
for (EntityRule rule : rules) for (EntityRule rule : rules)
if (rule.daily && if (rule.matches(context, message, null, null)) {
rule.matches(context, message, null, null)) {
rule.execute(context, message); rule.execute(context, message);
if (rule.stop) if (rule.stop)
break; break;

Loading…
Cancel
Save