Count executed rules

pull/159/head
M66B 5 years ago
parent 6bd3817fc2
commit 5817a280f6

@ -250,10 +250,9 @@ public class AdapterRule extends RecyclerView.Adapter<AdapterRule.ViewHolder> {
EntityMessage message = db.message().getMessage(mid); EntityMessage message = db.message().getMessage(mid);
if (rule.matches(context, message, null)) { if (rule.matches(context, message, null))
rule.execute(context, message); if (rule.execute(context, message))
applied++; applied++;
}
db.setTransactionSuccessful(); db.setTransactionSuccessful();
} finally { } finally {

@ -196,49 +196,40 @@ public class EntityRule {
return matched; return matched;
} }
void execute(Context context, EntityMessage message) throws IOException { boolean execute(Context context, EntityMessage message) throws JSONException, IOException {
try { JSONObject jaction = new JSONObject(action);
JSONObject jaction = new JSONObject(action); int type = jaction.getInt("type");
int type = jaction.getInt("type"); Log.i("Executing rule=" + type + ":" + name + " message=" + message.id);
Log.i("Executing rule=" + type + ":" + name + " message=" + message.id);
switch (type) {
switch (type) { case TYPE_SEEN:
case TYPE_SEEN: return onActionSeen(context, message, true);
onActionSeen(context, message, true); case TYPE_UNSEEN:
break; return onActionSeen(context, message, false);
case TYPE_UNSEEN: case TYPE_SNOOZE:
onActionSeen(context, message, false); return onActionSnooze(context, message, jaction);
break; case TYPE_FLAG:
case TYPE_SNOOZE: return onActionFlag(context, message, jaction);
onActionSnooze(context, message, jaction); case TYPE_MOVE:
break; return onActionMove(context, message, jaction);
case TYPE_FLAG: case TYPE_COPY:
onActionFlag(context, message, jaction); return onActionCopy(context, message, jaction);
break; case TYPE_ANSWER:
case TYPE_MOVE: return onActionAnswer(context, message, jaction);
onActionMove(context, message, jaction); case TYPE_AUTOMATION:
break; return onActionAutomation(context, message, jaction);
case TYPE_COPY: default:
onActionCopy(context, message, jaction); throw new IllegalArgumentException("Unknown rule type=" + type);
break;
case TYPE_ANSWER:
onActionAnswer(context, message, jaction);
break;
case TYPE_AUTOMATION:
onActionAutomation(context, message, jaction);
break;
}
} catch (JSONException ex) {
Log.e(ex);
} }
} }
private void onActionSeen(Context context, EntityMessage message, boolean seen) { private boolean onActionSeen(Context context, EntityMessage message, boolean seen) {
EntityOperation.queue(context, message, EntityOperation.SEEN, seen); EntityOperation.queue(context, message, EntityOperation.SEEN, seen);
message.seen = seen; message.seen = seen;
return true;
} }
private void onActionMove(Context context, EntityMessage message, JSONObject jargs) throws JSONException { private boolean onActionMove(Context context, EntityMessage message, JSONObject jargs) throws JSONException {
long target = jargs.getLong("target"); long target = jargs.getLong("target");
boolean seen = jargs.optBoolean("seen"); boolean seen = jargs.optBoolean("seen");
boolean thread = jargs.optBoolean("thread"); boolean thread = jargs.optBoolean("thread");
@ -255,9 +246,11 @@ public class EntityRule {
if (seen) if (seen)
message.seen = true; message.seen = true;
return true;
} }
private void onActionCopy(Context context, EntityMessage message, JSONObject jargs) throws JSONException { private boolean onActionCopy(Context context, EntityMessage message, JSONObject jargs) throws JSONException {
long target = jargs.getLong("target"); long target = jargs.getLong("target");
DB db = DB.getInstance(context); DB db = DB.getInstance(context);
@ -266,9 +259,10 @@ public class EntityRule {
throw new IllegalArgumentException("Rule copy to folder not found"); throw new IllegalArgumentException("Rule copy to folder not found");
EntityOperation.queue(context, message, EntityOperation.COPY, target, false); EntityOperation.queue(context, message, EntityOperation.COPY, target, false);
return true;
} }
private void onActionAnswer(Context context, EntityMessage message, JSONObject jargs) throws JSONException, IOException { private boolean onActionAnswer(Context context, EntityMessage message, JSONObject jargs) throws JSONException, IOException {
long iid = jargs.getLong("identity"); long iid = jargs.getLong("identity");
long aid = jargs.getLong("answer"); long aid = jargs.getLong("answer");
boolean cc = (jargs.has("cc") && jargs.getBoolean("cc")); boolean cc = (jargs.has("cc") && jargs.getBoolean("cc"));
@ -311,9 +305,10 @@ public class EntityRule {
null); null);
EntityOperation.queue(context, reply, EntityOperation.SEND); EntityOperation.queue(context, reply, EntityOperation.SEND);
return true;
} }
private void onActionAutomation(Context context, EntityMessage message, JSONObject jargs) throws JSONException { private boolean onActionAutomation(Context context, EntityMessage message, JSONObject jargs) {
String sender = (message.from == null || message.from.length == 0 String sender = (message.from == null || message.from.length == 0
? null : ((InternetAddress) message.from[0]).getAddress()); ? null : ((InternetAddress) message.from[0]).getAddress());
@ -323,30 +318,30 @@ public class EntityRule {
automation.putExtra(EXTRA_SUBJECT, message.subject); automation.putExtra(EXTRA_SUBJECT, message.subject);
Log.i("Sending " + automation); Log.i("Sending " + automation);
try { context.sendBroadcast(automation);
context.sendBroadcast(automation); return true;
} catch (Throwable ex) {
Log.e(ex);
}
} }
private void onActionSnooze(Context context, EntityMessage message, JSONObject jargs) throws JSONException { private boolean onActionSnooze(Context context, EntityMessage message, JSONObject jargs) throws JSONException {
int duration = jargs.getInt("duration"); int duration = jargs.getInt("duration");
long wakeup = message.received + duration * 3600 * 1000L; long wakeup = message.received + duration * 3600 * 1000L;
if (wakeup < new Date().getTime()) if (wakeup < new Date().getTime())
return; return false;
DB db = DB.getInstance(context); DB db = DB.getInstance(context);
db.message().setMessageSnoozed(message.id, wakeup); db.message().setMessageSnoozed(message.id, wakeup);
EntityMessage.snooze(context, message.id, wakeup); EntityMessage.snooze(context, message.id, wakeup);
onActionSeen(context, message, true); onActionSeen(context, message, true);
return true;
} }
private void onActionFlag(Context context, EntityMessage message, JSONObject jargs) throws JSONException { private boolean onActionFlag(Context context, EntityMessage message, JSONObject jargs) throws JSONException {
Integer color = (jargs.has("color") && !jargs.isNull("color") Integer color = (jargs.has("color") && !jargs.isNull("color")
? jargs.getInt("color") : null); ? jargs.getInt("color") : null);
EntityOperation.queue(context, message, EntityOperation.FLAG, true, color); EntityOperation.queue(context, message, EntityOperation.FLAG, true, color);
return true;
} }
@Override @Override

Loading…
Cancel
Save