From 700f3740f9eb7dba30f0faffc504e1223105bdf5 Mon Sep 17 00:00:00 2001 From: M66B Date: Mon, 29 Apr 2024 13:31:38 +0200 Subject: [PATCH] Expressions: added attachment count function --- .../java/eu/faircode/email/EntityRule.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/app/src/main/java/eu/faircode/email/EntityRule.java b/app/src/main/java/eu/faircode/email/EntityRule.java index 4cb6c14c80..a727b53180 100644 --- a/app/src/main/java/eu/faircode/email/EntityRule.java +++ b/app/src/main/java/eu/faircode/email/EntityRule.java @@ -764,6 +764,30 @@ public class EntityRule { } } + public static class AttachmentsFunction extends AbstractFunction { + private final Context context; + private final EntityMessage message; + + AttachmentsFunction(Context context, EntityMessage message) { + this.context = context; + this.message = message; + } + + @Override + public EvaluationValue evaluate( + Expression expression, Token functionToken, EvaluationValue... parameterValues) { + int result = 0; + + if (message != null) { + DB db = DB.getInstance(context); + result = db.attachment().countAttachments(message.id); + } + + Log.i("EXPR attachments()=" + result); + return expression.convertValue(result); + } + } + @InfixOperator(precedence = OPERATOR_PRECEDENCE_COMPARISON) public static class ContainsOperator extends AbstractOperator { private final boolean regex; @@ -846,6 +870,7 @@ public class EntityRule { MessageFunction fMessage = new MessageFunction(message); BlocklistFunction fBlocklist = new BlocklistFunction(context, message, headers); MxFunction fMx = new MxFunction(context, message); + AttachmentsFunction fAttachments = new AttachmentsFunction(context, message); ContainsOperator oContains = new ContainsOperator(false); ContainsOperator oMatches = new ContainsOperator(true); @@ -857,6 +882,7 @@ public class EntityRule { configuration.getFunctionDictionary().addFunction("Blocklist", fBlocklist); configuration.getFunctionDictionary().addFunction("onBlocklist", fBlocklist); configuration.getFunctionDictionary().addFunction("hasMx", fMx); + configuration.getFunctionDictionary().addFunction("attachments", fAttachments); configuration.getOperatorDictionary().addOperator("Contains", oContains); configuration.getOperatorDictionary().addOperator("Matches", oMatches);