Skip to content

fix(email-links): right Mail account from the sidebar; links follow moved mail and go when it is deleted - #11598

Draft
rubenvdlinde wants to merge 6 commits into
mainfrom
fix/mail-links-account-and-cleanup
Draft

rubenvdlinde wants to merge 6 commits into
mainfrom
fix/mail-links-account-and-cleanup

Conversation

@rubenvdlinde

Copy link
Copy Markdown
Contributor

What was wrong

  • The Mail sidebar linked every mail with account id 1, so a mail from a second Mail account was linked to the wrong account and its links did not show.
  • When Mail removed a message, its links stayed behind and pointed at nothing.

What changes

  • Account: the sidebar takes the account from Mail (message, then mailbox, then account) instead of a constant 1.
  • Moved mail (also to the Trash): Mail deletes its cached row on a move and stores the moved copy only on its next sync. The links are kept and wait; after the sync they move to the stored copy with the same Message-ID in the same account.
  • Deleted from the Trash: the links move to another stored copy of the same mail if there is one, otherwise they are removed. A link that would duplicate one the object already has is removed.
  • The object's mail relation (_mail, which the sidebar's "Connected to" reads) moves and is removed together with the link. If that write is refused (no user during a background sync), it is logged and the link still moves.
  • A new MailMessageLinksListener hears Mail's BeforeMessageDeletedEvent, MessageDeletedEvent and SynchronizationEvent, after Mail's own listeners, and is registered with the other integration listeners. The per-account queries live in a new EmailLinkAccountQueries, so EmailLinkMapper keeps its public surface. Both are mapped to the Integrations component.

How it was tested

  • Unit tests for the account queries, the reconciler (keep, wait, repoint, remove on delete from the Trash, duplicate, refused relation write), the listener (including Mail's __call getters such as getTrashMailboxId()) and the wiring.
  • Full unit suite in 12 shards: 69,241 tests; the only failures are the existing PdfTextReplacerTest ones (no PDF code touched).
  • Jest: the mail-sidebar specs, 236 tests.
  • phpcs, phpmd, phpstan and psalm clean on the changed files.

How it was tested on a live instance

Nextcloud 34.0.6 with Mail 5.13.5 and PostgreSQL 17, one user with two Mail accounts (1 and 2), an object whose schema allows mail links:

Step Result
Link a mail of account 2 from the sidebar, on a URL without the account in it stored with account 2 (before: 1)
Move it to Archive, Archive syncs link and sidebar connection follow the Archive copy
Delete it to Trash, Trash syncs link follows the Trash copy
Delete it from Trash link removed, the object lists no mails
Delete from Trash while the same mail is still in Inbox link moves to the Inbox copy

The live run found two problems the unit tests had missed, both fixed with a test: Mail fires no "new messages" event on a mailbox's first sync (the listener now uses SynchronizationEvent), and getTrashMailboxId() is a __call getter, so a method_exists check never found the Trash.

Notes

🤖 Generated with Claude Code

Mail 5's path URLs (/apps/mail/box/{mailbox}/thread/{message}) do not
name the account, so the observer assumed account 1 for every thread
and read a mailbox id as the account id for /box/{mailbox}. Any user
whose Mail account is not id 1, or who has several accounts, got
links and lookups made against the wrong account, or a 403 from the
ownership check.

The observer now asks Mail which mailbox the open message is in and
which account owns that mailbox, and publishes account and message id
together, so a tab never loads a message under a missing or stale
account. When Mail cannot answer, the account stays empty and the
tabs do nothing rather than act on a guess.

Assisted-by: ClaudeCode:claude-opus-5-5
Nothing listened to Mail's delete events, so a link kept pointing at a
Mail message id after the message was deleted. The object then listed
mail nobody could open, and the dead id could never be cleaned up.

Mail's MessageDeletedEvent carries the account, the mailbox and the
IMAP uid, not the database id, and it also fires on a move (including
to the trash), where the message row keeps its id. So the listener does
not trust the event to name the gone message: it reconciles the
account's links against the rows Mail still stores. A link whose row
survives is left alone. A link whose row is gone moves to another
stored copy of the same mail (same RFC Message-ID) in the same account,
because the link is about the mail, not one copy of it; copies in other
accounts are not used, since every read of a link checks the ownership
of its own account. Without a copy, or when the object already links
that copy, the link is removed.

The listener is registered by event name, so an instance without Mail
boots as before, and a failure is logged without stopping Mail's delete.

Assisted-by: ClaudeCode:claude-opus-5-5
Mail drops the cached row of a moved message as well as a deleted one, and
stores the moved copy only on its next sync, so the account reconcile
removed the links of every mail moved to another folder or to the trash.

- MailMessageDeletedListener becomes MailMessageLinksListener and also
  hears BeforeMessageDeletedEvent and NewMessagesSynchronized. A delete
  from the account's trash releases that one message: its links move to
  another stored copy or are removed. Any other event only repoints, so a
  moved mail's links wait for the copy and move to it once Mail stores it.
- The listener runs at priority -1, after Mail's own cache listener, and
  is registered by IntegrationWiring next to the calendar link listeners,
  in the Integrations area with the reconciler.
- The per-account queries move from EmailLinkMapper to
  EmailLinkAccountQueries, so the mapper keeps its public surface.

Assisted-by: ClaudeCode:claude-opus-5-5
…mail relation too

Found on a live Nextcloud with Mail 5.13:

- NewMessagesSynchronized fires only on a partial sync, so a mailbox opened
  for the first time never repointed its links. SynchronizationEvent fires
  after every sync of the account and replaces it.
- Mail's MailAccount answers getTrashMailboxId() through __call, so the
  method_exists() check never saw the trash and an expunge removed nothing.
  The listener checks is_callable() instead.
- The Mail sidebar reads the object's `_mail` relation (`{account}/{message}`),
  not the email link row. A repointed or removed link now moves or drops
  that relation as well; a refused write (no user in a background sync) is
  logged and the link still moves.

Assisted-by: ClaudeCode:claude-opus-5-5
@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Quality Report — ConductionNL/openregister @ 020ffc3

Check PHP Vue Security License Tests
lint ❌
phpcs ❌
phpmd ❌
psalm ❌
phpstan ❌
phpmetrics ❌
eslint ❌
stylelint ❌
build ✅
check-specs ❌
test-l10n ❌
test-l10n-parity ❌
test-l10n-browser-en ✅
format ❌
check-schema-l10n ❌
test-newman-route-ratchet ❌
composer ❌ ❌
npm ❌ ✅ 671/671
app:check-code ⏭️
info.xml ✅
REUSE ✅
lockfile sync ✅
PHPUnit 🚨 NO VERDICT — enabled but never ran
Newman 🚨 NO VERDICT — enabled but never ran
Playwright 🚨 NO VERDICT — enabled but never ran
Hydra gates ⏭️

Quality workflow — 2026-12-02 12:51 UTC

Download the full PDF report from the workflow artifacts.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant