From 94ebe3b61c39613c8b4c72720c4f937b59952b01 Mon Sep 17 00:00:00 2001 From: Jonas Winkler <17569239+jonaswinkler@users.noreply.github.com> Date: Mon, 20 Feb 2023 11:46:46 +0100 Subject: [PATCH] implement better mail actions --- src/paperless_mail/mail.py | 32 ++++++++++++++++++++++++-------- src/paperless_mail/tasks.py | 16 ++-------------- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/src/paperless_mail/mail.py b/src/paperless_mail/mail.py index b49d3c3b5..acf1654d5 100644 --- a/src/paperless_mail/mail.py +++ b/src/paperless_mail/mail.py @@ -9,7 +9,7 @@ from typing import Dict import magic import pathvalidate -from celery import chord +from celery import chord, shared_task from django.conf import settings from django.db import DatabaseError from documents.loggers import LoggingMixin @@ -26,7 +26,6 @@ from imap_tools import NOT from imap_tools.mailbox import MailBoxTls from paperless_mail.models import MailAccount from paperless_mail.models import MailRule -from paperless_mail.tasks import apply_mail_action # Apple Mail sets multiple IMAP KEYWORD and the general "\Flagged" FLAG # imaplib => conn.fetch(b"", "FLAGS") @@ -145,6 +144,27 @@ class TagMailAction(BaseMailAction): raise MailError("No keyword specified.") +@shared_task +def apply_mail_action( + result: str, + rule_id: int, + message_uid: str, +): + rule = MailRule.objects.get(pk=rule_id) + account = MailAccount.objects.get(pk=rule.account.pk) + + action = get_rule_action(rule) + + with get_mailbox( + server=account.imap_server, + port=account.imap_port, + security=account.imap_security, + ) as M: + M.login(username=account.username, password=account.password) + M.folder.set(rule.folder) + action.post_consume(M, message_uid, rule.action_parameter) + + def get_rule_action(rule) -> BaseMailAction: if rule.action == MailRule.MailAction.FLAG: return FlagMailAction() @@ -531,10 +551,8 @@ class MailAccountHandler(LoggingMixin): ) mail_action_task = apply_mail_action.s( - M=M, - action=get_rule_action(rule), + rule_id=rule.pk, message_uid=message.uid, - parameter=rule.action_parameter, ) chord(header=consume_tasks, body=mail_action_task).delay() @@ -597,10 +615,8 @@ class MailAccountHandler(LoggingMixin): ) mail_action_task = apply_mail_action.s( - M=M, - action=get_rule_action(rule), + rule_id=rule.pk, message_uid=message.uid, - parameter=rule.action_parameter, ) (consume_task | mail_action_task).delay() diff --git a/src/paperless_mail/tasks.py b/src/paperless_mail/tasks.py index 9716f13a8..ab013a41e 100644 --- a/src/paperless_mail/tasks.py +++ b/src/paperless_mail/tasks.py @@ -1,8 +1,7 @@ import logging from celery import shared_task -from imap_tools import MailBox -from paperless_mail.mail import BaseMailAction + from paperless_mail.mail import MailAccountHandler from paperless_mail.mail import MailError from paperless_mail.models import MailAccount @@ -10,23 +9,12 @@ from paperless_mail.models import MailAccount logger = logging.getLogger("paperless.mail.tasks") -@shared_task -def apply_mail_action( - result: str, - M: MailBox, - action: BaseMailAction, - message_uid: str, - parameter: str, -): - action.post_consume(M, message_uid, parameter) - - @shared_task def process_mail_accounts(): total_new_documents = 0 for account in MailAccount.objects.all(): try: - total_new_documents += MailAccountHandler().handl2e_mail_account(account) + total_new_documents += MailAccountHandler().handle_mail_account(account) except MailError: logger.exception(f"Error while processing mail account {account}")