fix: deleted mails showing in inbox — set deleted_at on move to Trash, show in Trash folder

- delete_mail: set deleted_at when moving to Trash (not just folder_id)
- empty_folder: same — set deleted_at when moving to Trash
- list_mails: show soft-deleted mails when viewing Trash folder
- This prevents sync from moving deleted mails back to inbox
- Trash folder shows deleted_at mails, all other folders hide them
This commit is contained in:
Agent Zero
2026-07-20 13:26:39 +02:00
parent b68accb9a1
commit 11c2a0c9af
+24 -2
View File
@@ -669,6 +669,7 @@ async def empty_folder(
else:
# Move to Trash folder (keep visible in Trash)
mail.folder_id = trash_folder.id
mail.deleted_at = now
try:
await mail_services.imap_delete_mail(db, mail.id, tenant_id, permanent=False)
except Exception as exc:
@@ -1648,7 +1649,11 @@ async def delete_mail(
await db.flush()
elif trash_folder:
# Mail is NOT in Trash → move to Trash folder
# Set deleted_at so the mail disappears from the source folder
# and the sync doesn't move it back. list_mails shows deleted_at
# mails when viewing the Trash folder.
mail.folder_id = trash_folder.id
mail.deleted_at = datetime.now(UTC)
await db.flush()
try:
await mail_services.imap_delete_mail(db, m_id, tenant_id, permanent=False)
@@ -1757,11 +1762,28 @@ async def list_mails(
if folder_id:
f_id = _parse_uuid(folder_id, "folder_id")
stmt = stmt.where(Mail.folder_id == f_id)
# Check if this folder is a Trash folder — if so, show soft-deleted mails too
folder = (
await db.execute(
select(MailFolder).where(
and_(MailFolder.id == f_id, MailFolder.tenant_id == tenant_id)
)
)
).scalar_one_or_none()
is_trash = folder and (
'trash' in (folder.imap_name or '').lower()
or 'papierkorb' in (folder.name or '').lower()
or 'trash' in (folder.name or '').lower()
)
if not is_trash:
# Exclude soft-deleted mails in non-Trash folders
stmt = stmt.where(Mail.deleted_at.is_(None))
else:
# No folder specified — exclude soft-deleted mails
stmt = stmt.where(Mail.deleted_at.is_(None))
if account_id:
a_id = _parse_uuid(account_id, "account_id")
stmt = stmt.where(Mail.account_id == a_id)
# Exclude soft-deleted mails
stmt = stmt.where(Mail.deleted_at.is_(None))
total = (await db.execute(select(func.count()).select_from(stmt.subquery()))).scalar()
# Dynamic sorting
sort_columns = {