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:
@@ -669,6 +669,7 @@ async def empty_folder(
|
|||||||
else:
|
else:
|
||||||
# Move to Trash folder (keep visible in Trash)
|
# Move to Trash folder (keep visible in Trash)
|
||||||
mail.folder_id = trash_folder.id
|
mail.folder_id = trash_folder.id
|
||||||
|
mail.deleted_at = now
|
||||||
try:
|
try:
|
||||||
await mail_services.imap_delete_mail(db, mail.id, tenant_id, permanent=False)
|
await mail_services.imap_delete_mail(db, mail.id, tenant_id, permanent=False)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
@@ -1648,7 +1649,11 @@ async def delete_mail(
|
|||||||
await db.flush()
|
await db.flush()
|
||||||
elif trash_folder:
|
elif trash_folder:
|
||||||
# Mail is NOT in Trash → move to 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.folder_id = trash_folder.id
|
||||||
|
mail.deleted_at = datetime.now(UTC)
|
||||||
await db.flush()
|
await db.flush()
|
||||||
try:
|
try:
|
||||||
await mail_services.imap_delete_mail(db, m_id, tenant_id, permanent=False)
|
await mail_services.imap_delete_mail(db, m_id, tenant_id, permanent=False)
|
||||||
@@ -1757,11 +1762,28 @@ async def list_mails(
|
|||||||
if folder_id:
|
if folder_id:
|
||||||
f_id = _parse_uuid(folder_id, "folder_id")
|
f_id = _parse_uuid(folder_id, "folder_id")
|
||||||
stmt = stmt.where(Mail.folder_id == f_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:
|
if account_id:
|
||||||
a_id = _parse_uuid(account_id, "account_id")
|
a_id = _parse_uuid(account_id, "account_id")
|
||||||
stmt = stmt.where(Mail.account_id == a_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()
|
total = (await db.execute(select(func.count()).select_from(stmt.subquery()))).scalar()
|
||||||
# Dynamic sorting
|
# Dynamic sorting
|
||||||
sort_columns = {
|
sort_columns = {
|
||||||
|
|||||||
Reference in New Issue
Block a user