Gmail label sync + no-dup backfill, live label/UI updates, account-delete cleanup, Sync-SPAM toggle, sidebar logo

Gmail sync (SPEC: Gmail sync):
- Provider fetches X-GM-LABELS when X-GM-EXT-1 is advertised; parses custom labels
  (system labels dropped, mUTF-7 decoded) onto RemoteMessage.
- Engine syncs only system folders + All Mail for Gmail, skips every label folder,
  and dedupes the All Mail pass by Message-ID — kills the INBOX/All-Mail duplication.
- Custom labels ride the message.new event; the labels plugin subscribes and maps
  them to chips as "<name> [Gmail]" (per-message + thread aggregate + known set),
  so labels fill in gradually with the sync window. Non-ASCII labels decode right.

Labels self-healing (message-driven reconcile):
- Per-message keys are the source of truth; t: aggregates and _labels are derived.
  Reconcile drops labels of deleted messages, regroups survivors by their current
  thread (fixing JWZ thread merges), rewrites changed t: keys, and rebuilds _labels
  from live messages — keeping user labels, dropping orphaned Gmail ones.
- New SDK reads: sdk.threads.existing / sdk.messages.threads_of. Sidebar refreshes
  live via a plugin.labels.changed WS event on new labels.

Account deletion: prune threads left empty after the cascade (they were user-scoped,
so only messages cascaded) + de-list the account from surviving cross-account threads.

Sync SPAM: opt-in per-account sync_spam (migration 0015, default off) — the engine
skips the Junk folder in backfill and poll unless enabled; wizard checkbox added.

Robustness / UX:
- WebSocket auto-reconnects with backoff and resyncs on (re)connect, and account
  add/delete invalidates threads/labels — no more hard refresh for live updates.
- Backfill banner + sidebar ring also require done < total, so a completed account
  can't leave a stuck "X of X".
- Scrub lone UTF-16 surrogates from parsed headers/bodies so bad messages no longer
  crash the backfill on Postgres text/JSONB.
- Ultimo signet logo in the Mail/Calendar/Contacts sidebar header (and at the top of
  the collapsed rail), via the shared plugin-ui.css + MailLayout.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01U72FZiF1W1pb8XxyWBTREA
This commit is contained in:
2026-08-05 18:04:49 +02:00
co-authored by Claude Opus 4.8
parent e5a87b37b1
commit 4c200ab94a
38 changed files with 863 additions and 106 deletions
+47 -1
View File
@@ -22,7 +22,7 @@ from sqlalchemy import select
from app.db import session_scope
from app.events import get_event_bus
from app.indexer import blobstore
from app.models import Message
from app.models import Message, Thread
class PluginMessages:
@@ -44,6 +44,29 @@ class PluginMessages:
return None
return message_from_bytes(raw).get(name)
async def threads_of(
self, user_id: uuid.UUID, message_ids: Iterable[str]
) -> dict[str, str | None]:
"""Map still-existing message ids to their current thread id (None if
unthreaded). Deleted messages are omitted, so a plugin can prune stale
per-message references and re-home labels onto the live thread after a
JWZ thread merge."""
ids: list[uuid.UUID] = []
for mid in message_ids:
try:
ids.append(uuid.UUID(str(mid)))
except (ValueError, AttributeError, TypeError):
continue
if not ids:
return {}
async with session_scope() as session:
rows = await session.execute(
select(Message.id, Message.thread_id).where(
Message.user_id == user_id, Message.id.in_(ids)
)
)
return {str(mid): (str(tid) if tid else None) for mid, tid in rows}
async def envelope(self, user_id: uuid.UUID, message_id: uuid.UUID) -> dict[str, Any] | None:
async with session_scope() as session:
msg = await session.scalar(
@@ -66,6 +89,29 @@ class PluginMessages:
}
class PluginThreads:
"""Read-only, user-scoped thread queries (e.g. self-healing stale references)."""
async def existing(self, user_id: uuid.UUID, thread_ids: Iterable[str]) -> set[str]:
"""Return the subset of ``thread_ids`` that still exist for the user.
Lets a plugin prune references to threads deleted out from under it (e.g. an
account removal drops its threads, orphaning label aggregates keyed by id)."""
ids: list[uuid.UUID] = []
for tid in thread_ids:
try:
ids.append(uuid.UUID(str(tid)))
except (ValueError, AttributeError, TypeError):
continue
if not ids:
return set()
async with session_scope() as session:
rows = await session.scalars(
select(Thread.id).where(Thread.user_id == user_id, Thread.id.in_(ids))
)
return {str(r) for r in rows}
class PluginActions:
"""User-scoped actions a plugin can take on a message."""