Repository: paperless-ngx/paperless-ngx

Issue:
[BUG] mail consumption fails if action is delete and no criteria are given
### Description

A mail consumption rule containing no criteria and has the action "delete" fails with this messages:
```
today at 22:30:00[2023-04-24 22:30:00,569] [ERROR] [paperless_mail] Rule XXX.YYY: Error while processing rule: AND expects params
today at 22:30:00Traceback (most recent call last):
today at 22:30:00  File "/usr/src/paperless/src/paperless_mail/mail.py", line 290, in handle_mail_account
today at 22:30:00    total_processed_files += self.handle_mail_rule(
today at 22:30:00  File "/usr/src/paperless/src/paperless_mail/mail.py", line 357, in handle_mail_rule
today at 22:30:00    criterias_imap = AND(**criterias)
today at 22:30:00  File "/usr/local/lib/python3.9/site-packages/imap_tools/query.py", line 97, in __init__
today at 22:30:00    raise ValueError('{} expects params'.format(self.__class__.__name__))
today at 22:30:00ValueError: AND expects params
```

When switching to action "Tag" it is successful even with empty criteria. On the other hand, "Delete" action gives no error if any criteria is given.

See this screenshot with removed Name and Account information:
![grafik](https://user-images.githubusercontent.com/33538320/234111880-17b00d7f-c4cd-4bc9-b94d-d9e42836f2e8.png)

### Possible Cause
Looking at the code of [`make_criterias`](https://github.com/paperless-ngx/paperless-ngx/blob/dev/src/paperless_mail/mail.py#L364) in `mail.py` and the missing `get_criteria` of  [`DeleteMailAction`](https://github.com/paperless-ngx/paperless-ngx/blob/dev/src/paperless_mail/mail.py#L100), it seems like in this special situation no criteria at all is given to [`LogicOperator()`](imap_tools/query.py) in [`imap_tools/query.py`](https://github.com/ikvk/imap_tools/blob/master/imap_tools/query.py) and thus resulting in the [`ValueError`](https://github.com/ikvk/imap_tools/blob/master/imap_tools/query.py#L97) from above.

This might also be the case for [`MoveMailAction`](https://github.com/paperless-ngx/paperless-ngx/blob/dev/src/paperless_mail/mail.py#L121) as `get_criteria` is missing there also.

### Steps to reproduce

1. Create mail-account
2. Create mail rule for this account
3. Leave fields empty for "from", "to", "subject" and "body" and set "maximum_age" to 0
4. Set "delete" as action
5. Save
6. Wait for the mail task cron to see the error message
7. Add any criteria or change action to "tag" will create no error

### Webserver logs

```bash
today at 22:30:00[2023-04-24 22:30:00,569] [ERROR] [paperless_mail] Rule XXX.YYY: Error while processing rule: AND expects params
today at 22:30:00Traceback (most recent call last):
today at 22:30:00  File "/usr/src/paperless/src/paperless_mail/mail.py", line 290, in handle_mail_account
today at 22:30:00    total_processed_files += self.handle_mail_rule(
today at 22:30:00  File "/usr/src/paperless/src/paperless_mail/mail.py", line 357, in handle_mail_rule
today at 22:30:00    criterias_imap = AND(**criterias)
today at 22:30:00  File "/usr/local/lib/python3.9/site-packages/imap_tools/query.py", line 97, in __init__
today at 22:30:00    raise ValueError('{} expects params'.format(self.__class__.__name__))
today at 22:30:00ValueError: AND expects params
```


### Browser logs

_No response_

### Paperless-ngx version

1.13.0

### Host OS

x86_64 on QNAP 

### Installation method

Docker - official image

### Browser

Firefox

### Configuration changes

_No response_

### Other

_No response_


Discussion:
One idea would be to check wether `rule_query` or `criterias` are empty before combining them via `AND` in [line 384](https://github.com/paperless-ngx/paperless-ngx/blob/dev/src/paperless_mail/mail.py#L384) and [line 386](https://github.com/paperless-ngx/paperless-ngx/blob/dev/src/paperless_mail/mail.py#L386).

Resolving patch:
diff --git a/src/paperless_mail/mail.py b/src/paperless_mail/mail.py
--- a/src/paperless_mail/mail.py
+++ b/src/paperless_mail/mail.py
@@ -381,7 +381,8 @@ def make_criterias(rule):
 
     rule_query = get_rule_action(rule).get_criteria()
     if isinstance(rule_query, dict):
-        return AND(**rule_query, **criterias)
+        if len(rule_query) or len(criterias):
+            return AND(**rule_query, **criterias)
     else:
         return AND(rule_query, **criterias)
 


Test patch:
diff --git a/src/paperless_mail/tests/test_mail.py b/src/paperless_mail/tests/test_mail.py
--- a/src/paperless_mail/tests/test_mail.py
+++ b/src/paperless_mail/tests/test_mail.py
@@ -612,6 +612,29 @@ def test_handle_mail_account_delete(self):
 
         self.assertEqual(len(self.bogus_mailbox.messages), 1)
 
+    def test_handle_mail_account_delete_no_filters(self):
+
+        account = MailAccount.objects.create(
+            name="test",
+            imap_server="",
+            username="admin",
+            password="secret",
+        )
+
+        _ = MailRule.objects.create(
+            name="testrule",
+            account=account,
+            action=MailRule.MailAction.DELETE,
+            maximum_age=0,
+        )
+
+        self.assertEqual(len(self.bogus_mailbox.messages), 3)
+
+        self.mail_account_handler.handle_mail_account(account)
+        self.apply_mail_actions()
+
+        self.assertEqual(len(self.bogus_mailbox.messages), 0)
+
     def test_handle_mail_account_flag(self):
         account = MailAccount.objects.create(
             name="test",