fix: IMAP sync - use imap_uid, add sync queue with retry, restore deleted on sync, upload sent mails

- imap_delete_mail: use stored imap_uid instead of Message-ID search, raise on failure
- imap_move_mail: same imap_uid fix, raise on failure
- New MailSyncQueue model + migration 0007 for pending IMAP operations
- delete_mail route: queues failed IMAP delete for retry
- move_mail route: queues failed IMAP move for retry
- process_sync_queue: retries pending delete/move operations in auto-sync loop
- Auto-sync loop: processes sync queue before pulling new mails
- Restore soft-deleted mails if they still exist on IMAP server during sync
- Upload sent mails to IMAP Sent folder via APPEND after SMTP send
- Plugin version bumped to 1.2.0
This commit is contained in:
Agent Zero
2026-07-20 11:31:17 +02:00
parent 2a33b5706a
commit c24a86bc90
5 changed files with 210 additions and 45 deletions
+29 -1
View File
@@ -3,7 +3,7 @@
from __future__ import annotations
import uuid
from datetime import datetime
from datetime import UTC, datetime
from sqlalchemy import (
Boolean,
@@ -11,6 +11,7 @@ from sqlalchemy import (
ForeignKey,
Index,
Integer,
JSON,
String,
Text,
UniqueConstraint,
@@ -415,3 +416,30 @@ class ContactPgpKey(Base, TenantMixin):
contact_id: Mapped[uuid.UUID] = mapped_column(PGUUID(as_uuid=True), nullable=False)
public_key_armored: Mapped[str] = mapped_column(Text, nullable=False)
key_id: Mapped[str] = mapped_column(String(255), nullable=False, default="")
class MailSyncQueue(Base, TenantMixin):
"""Queue for pending IMAP operations that need retry."""
__tablename__ = "mail_sync_queue"
__table_args__ = (
Index("ix_mail_sync_queue_status", "status"),
Index("ix_mail_sync_queue_mail", "mail_id"),
)
id: Mapped[uuid.UUID] = mapped_column(
PGUUID(as_uuid=True), primary_key=True, default=uuid.uuid4
)
mail_id: Mapped[uuid.UUID] = mapped_column(PGUUID(as_uuid=True), nullable=False)
operation: Mapped[str] = mapped_column(String(50), nullable=False)
payload: Mapped[dict] = mapped_column(JSON, nullable=False, default=dict)
status: Mapped[str] = mapped_column(String(20), nullable=False, default="pending")
attempts: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
max_attempts: Mapped[int] = mapped_column(Integer, nullable=False, default=3)
last_error: Mapped[str | None] = mapped_column(Text, nullable=True)
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), nullable=False, default=lambda: datetime.now(UTC)
)
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), nullable=False, default=lambda: datetime.now(UTC)
)