Two-way draft sync, signatures, sent/bcc fixes, and mail-list UI
Backend - Sent copies: persist locally on send + periodic multi-folder CONDSTORE poll (Sent/Archive/Trash/Junk) so they thread and show without an IDLE-only INBOX. - Bcc: transmitted copy omits Bcc, Sent copy keeps it; store/display bcc_addr. - Drafts: mirror local drafts to the IMAP Drafts folder (append/replace/expunge); inbound sync of foreign drafts (un-threaded) with adopt-to-edit + dedup by UID. - Per-user storage quota (5 GiB default) and manual "sync all folders" action. - Account editing (display/color/server/password) and per-account HTML signature. - Account view scoped to that account's inbox; new migrations 0006–0009. Frontend - Mail-list pagination, folder filters (All/Unread/Pinned), sort toggle, refresh. - Aligned + widened mail-list margins; reading-pane bars fixed height. - Rich-text composer: color palette, highlight, image; attachments; expand/pop-out. - Signatures: auto-insert for new mail, replace on From change, reply insert. - Restore/Delete actions per folder; drafts view merges local + remote, date-sorted. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
+122
-2
@@ -16,8 +16,14 @@ from app.config import get_settings
|
||||
from app.db import get_session
|
||||
from app.identity.deps import CurrentUser, require_csrf
|
||||
from app.indexer import blobstore
|
||||
from app.models import ComposeAttachment, Draft
|
||||
from app.outbox.schemas import DraftIn, DraftOut, SendRequest, SendResponse
|
||||
from app.models import BodyPart, ComposeAttachment, Draft, Mailbox, Message
|
||||
from app.outbox.schemas import (
|
||||
DraftIn,
|
||||
DraftOut,
|
||||
RemoteDraftOut,
|
||||
SendRequest,
|
||||
SendResponse,
|
||||
)
|
||||
from app.outbox.service import DraftError, cancel_outbox, create_outbox, upsert_draft
|
||||
from app.queue import get_arq_pool
|
||||
|
||||
@@ -102,6 +108,104 @@ async def list_drafts(user: CurrentUser, db: DbSession) -> list[DraftOut]:
|
||||
return [_draft_out(d) for d in drafts]
|
||||
|
||||
|
||||
def _fmt_addr(addr: dict[str, str]) -> str:
|
||||
email = addr.get("email", "")
|
||||
name = addr.get("name", "")
|
||||
return f"{name} <{email}>" if name else email
|
||||
|
||||
|
||||
async def _own_draft_uids(db: AsyncSession, user_id: uuid.UUID) -> set[tuple[uuid.UUID, int]]:
|
||||
"""(account_id, imap_uid) of drafts we mirrored — excluded from the remote list."""
|
||||
rows = await db.execute(
|
||||
select(Draft.account_id, Draft.imap_uid).where(
|
||||
Draft.user_id == user_id, Draft.imap_uid.isnot(None)
|
||||
)
|
||||
)
|
||||
return {(r.account_id, r.imap_uid) for r in rows}
|
||||
|
||||
|
||||
@router.get("/drafts/remote", response_model=list[RemoteDraftOut])
|
||||
async def list_remote_drafts(user: CurrentUser, db: DbSession) -> list[RemoteDraftOut]:
|
||||
"""Drafts sitting in the IMAP Drafts folder that we haven't already adopted."""
|
||||
own = await _own_draft_uids(db, user.id)
|
||||
messages = await db.scalars(
|
||||
select(Message)
|
||||
.join(Mailbox, Mailbox.id == Message.mailbox_id)
|
||||
.where(Message.user_id == user.id, Mailbox.special_use == "drafts")
|
||||
.order_by(Message.internal_date.desc())
|
||||
)
|
||||
out: list[RemoteDraftOut] = []
|
||||
for m in messages:
|
||||
if (m.account_id, m.uid) in own:
|
||||
continue
|
||||
out.append(
|
||||
RemoteDraftOut(
|
||||
message_id=m.id,
|
||||
account_id=m.account_id,
|
||||
imap_uid=m.uid,
|
||||
subject=m.subject,
|
||||
to=[_fmt_addr(a) for a in m.to_addr],
|
||||
cc=[_fmt_addr(a) for a in m.cc_addr],
|
||||
bcc=[_fmt_addr(a) for a in m.bcc_addr],
|
||||
snippet=m.snippet,
|
||||
updated_at=(m.date_hdr or m.internal_date).isoformat(),
|
||||
)
|
||||
)
|
||||
return out
|
||||
|
||||
|
||||
@router.post("/drafts/adopt/{message_id}", response_model=DraftOut)
|
||||
async def adopt_remote_draft(
|
||||
message_id: uuid.UUID, user: CurrentUser, db: DbSession
|
||||
) -> DraftOut:
|
||||
"""Turn a Drafts-folder message into an editable local draft (linked by UID)."""
|
||||
msg = await db.scalar(
|
||||
select(Message)
|
||||
.join(Mailbox, Mailbox.id == Message.mailbox_id)
|
||||
.where(
|
||||
Message.id == message_id,
|
||||
Message.user_id == user.id,
|
||||
Mailbox.special_use == "drafts",
|
||||
)
|
||||
)
|
||||
if msg is None:
|
||||
raise_api_error(404, "not_found", "draft message not found")
|
||||
|
||||
# Idempotent: if already adopted (a local draft links this UID), return it.
|
||||
existing = await db.scalar(
|
||||
select(Draft).where(
|
||||
Draft.user_id == user.id,
|
||||
Draft.account_id == msg.account_id,
|
||||
Draft.imap_uid == msg.uid,
|
||||
)
|
||||
)
|
||||
if existing is not None:
|
||||
return _draft_out(existing)
|
||||
|
||||
parts = list(await db.scalars(select(BodyPart).where(BodyPart.message_id == msg.id)))
|
||||
html = next((p.content for p in parts if p.kind == "html_sanitized"), "")
|
||||
text = next((p.content for p in parts if p.kind == "text"), "")
|
||||
payload = {
|
||||
"to": [_fmt_addr(a) for a in msg.to_addr],
|
||||
"cc": [_fmt_addr(a) for a in msg.cc_addr],
|
||||
"bcc": [_fmt_addr(a) for a in msg.bcc_addr],
|
||||
"subject": msg.subject,
|
||||
"html": html,
|
||||
"text": text,
|
||||
"attachment_ids": [],
|
||||
}
|
||||
draft = Draft(
|
||||
user_id=user.id,
|
||||
account_id=msg.account_id,
|
||||
kind="new",
|
||||
payload=payload,
|
||||
imap_uid=msg.uid,
|
||||
)
|
||||
db.add(draft)
|
||||
await db.commit()
|
||||
return _draft_out(draft)
|
||||
|
||||
|
||||
@router.post("/drafts", response_model=DraftOut)
|
||||
async def create_draft(body: DraftIn, user: CurrentUser, db: DbSession) -> DraftOut:
|
||||
try:
|
||||
@@ -125,13 +229,29 @@ async def update_draft(
|
||||
return _draft_out(draft)
|
||||
|
||||
|
||||
@router.post("/drafts/{draft_id}/publish")
|
||||
async def publish_draft(draft_id: uuid.UUID, user: CurrentUser, db: DbSession) -> dict[str, str]:
|
||||
"""Mirror the draft to the account's IMAP Drafts folder (continue on any client)."""
|
||||
draft = await db.scalar(select(Draft).where(Draft.id == draft_id, Draft.user_id == user.id))
|
||||
if draft is None:
|
||||
raise_api_error(404, "not_found", "draft not found")
|
||||
pool = await get_arq_pool()
|
||||
await pool.enqueue_job("push_draft_to_imap_job", str(draft_id))
|
||||
return {"status": "queued"}
|
||||
|
||||
|
||||
@router.delete("/drafts/{draft_id}")
|
||||
async def delete_draft(draft_id: uuid.UUID, user: CurrentUser, db: DbSession) -> dict[str, str]:
|
||||
draft = await db.scalar(select(Draft).where(Draft.id == draft_id, Draft.user_id == user.id))
|
||||
if draft is None:
|
||||
raise_api_error(404, "not_found", "draft not found")
|
||||
account_id, imap_uid = draft.account_id, draft.imap_uid
|
||||
await db.delete(draft)
|
||||
await db.commit()
|
||||
# Remove the mirrored copy from the server Drafts folder too.
|
||||
if imap_uid:
|
||||
pool = await get_arq_pool()
|
||||
await pool.enqueue_job("remove_imap_draft_job", str(account_id), imap_uid)
|
||||
return {"status": "deleted"}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user