Clear the reading panel when its last message leaves

"Mark as Spam" moved the message but left it on screen. Two causes, both fixed
generally rather than for this one action.

`move_job` published message.changed without a thread_id, so the WS handler
invalidated the message list but never the thread the user was reading — the
panel had no reason to refetch. It now names the thread.

And a thread whose last visible message has gone should not linger. MailLayout
closes the panel when the open thread comes back empty, whoever emptied it —
core action or plugin.

`move_to_special` now also publishes the move immediately rather than waiting
for the IMAP job, since a plugin acting on the user's behalf has no client-side
handle to update optimistically the way the core endpoint's caller does.

That exposed an ordering bug: the optimistic event reaches the passive-learning
hook, which would re-teach the lesson /learn had just taught. /learn now records
`learned` before moving, so the hook's dedup stands down — and records it with
an upsert, because provider-filed spam has no verdict row of ours to update.

One real mistake on the way: the event first reported the destination folder as
the source, which would have made every move look like a no-op to anything
reading the pair.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018SqJYt7jD7oUEkoY68sBZo
This commit is contained in:
2026-09-17 19:47:42 +02:00
co-authored by Claude Opus 5
parent c76b186b10
commit 8e3252679a
6 changed files with 186 additions and 15 deletions
+24
View File
@@ -254,10 +254,34 @@ class PluginActions:
if dst is None or dst.id == msg.mailbox_id:
return False
dst_id, src_id = dst.id, msg.mailbox_id
# The SOURCE folder's special-use, not the destination parameter:
# consumers of message.changed read the pair as a transition, and
# reporting them equal would erase the move.
src_special = await session.scalar(
select(Mailbox.special_use).where(Mailbox.id == src_id)
)
thread_id, dst_special = msg.thread_id, dst.special_use
msg.mailbox_id = dst_id # optimistic, as the core endpoint does
await session.commit()
await enqueue_job("move_job", str(message_id), str(dst_id), str(src_id))
# Announce the optimistic state now rather than waiting for the IMAP move
# to finish. The core's own move endpoint gets this for free — its caller
# updates the UI from the response — but a plugin acting on the user's
# behalf has no client-side handle, so without this the message lingers on
# screen until the job lands. move_job publishes again when it does.
await get_event_bus().publish(
"message.changed",
{
"user_id": str(user_id),
"message_id": str(message_id),
"thread_id": str(thread_id) if thread_id else None,
"mailbox_id": str(dst_id),
"previous_mailbox_id": str(src_id),
"special_use": dst_special,
"previous_special_use": src_special,
},
)
return True
async def trash(self, user_id: uuid.UUID, message_id: uuid.UUID) -> bool:
+5
View File
@@ -130,6 +130,10 @@ async def move_job(
# transition, not the destination. "Now in Spam" and "moved out of Spam"
# are different facts, and only the pair distinguishes them.
src_special, dst_special = src.special_use, dst.special_use
# The open reading panel is keyed by thread: without this the UI
# invalidates the message list but never refetches the thread the user is
# looking at, so a moved message stays on screen.
thread_id = message.thread_id
provider = build_provider(account)
try:
@@ -152,6 +156,7 @@ async def move_job(
{
"user_id": str(user_id),
"message_id": str(mid),
"thread_id": str(thread_id) if thread_id else None,
"mailbox_id": str(dst_id),
"previous_mailbox_id": src_mailbox_id,
"special_use": dst_special,