Phase 4: triage actions (delete, move, mark; bulk select) (AUD-70)

Bulk select in the triage list, with delete/move/mark and a recoverable
delete (FR-6, FR-9).

- migration v4: messages.folder + messages.mailbox; folder/category views
- internal/mailops: app folder -> Gmail label moves over IMAP (IPv4), locate
  by X-GM-MSGID, create labels lazily, move/save/reply/restore, flag marks
- mailfetch: CREATE, UID MOVE (COPY+EXPUNGE fallback), STORE flags, X-GM-MSGID search
- server: POST /actions/bulk with CSRF, UI delete confirmation, second
  confirmation for messages classified Spam (never auto-deleted), per-message
  and summary action-log rows (FR-13)
- inbox: select-all toolbar, checkboxes, folder nav, Restore on non-inbox rows

Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
2026-10-11 21:44:52 +11:00
parent 735934dab2
commit 8c0416750e
18 changed files with 2023 additions and 71 deletions

View File

@@ -0,0 +1,114 @@
// IMAP write operations for triage actions (Phase 4, FR-6/FR-9).
//
// The read path selects INBOX and fetches messages. The write path adds the
// operations a move needs: create the destination label, locate a message in
// the mailbox it currently lives in (Gmail gives each label its own UID, so the
// stable X-GM-MSGID is used), move it, and set or clear flags.
//
// Deletes are never hard-deletes: a "delete" is a move to a recoverable label.
package mailfetch
import (
"errors"
"strconv"
"strings"
)
// ErrMailboxExists is returned by CreateMailbox only when the server reports
// something other than "already exists".
var ErrMailboxExists = errors.New("mailbox already exists")
// CreateMailbox creates an IMAP mailbox (a Gmail label). An already-existing
// mailbox is not an error: triage creates "Delete" and "Saved" lazily.
func (c *Client) CreateMailbox(name string) error {
err := c.exec("CREATE "+imapQuote(name), nil)
if err == nil {
return nil
}
var se *ServerError
if errors.As(err, &se) {
upper := strings.ToUpper(se.Response)
if strings.Contains(upper, "ALREADYEXISTS") ||
strings.Contains(upper, "ALREADY EXISTS") ||
strings.Contains(upper, "MAILBOX EXISTS") {
return nil
}
}
return err
}
// Move moves a message to dest using the IMAP MOVE extension (RFC 6851, which
// Gmail supports). If the server rejects MOVE, it falls back to COPY followed
// by a source-side delete so the destination copy remains recoverable.
func (c *Client) Move(uid uint32, dest string) error {
id := strconv.FormatUint(uint64(uid), 10)
if err := c.exec("UID MOVE "+id+" "+imapQuote(dest), nil); err == nil {
return nil
} else {
var se *ServerError
if !errors.As(err, &se) {
return err
}
}
if err := c.exec("UID COPY "+id+" "+imapQuote(dest), nil); err != nil {
return err
}
if err := c.exec("UID STORE "+id+" +FLAGS (\\Deleted)", nil); err != nil {
return err
}
return c.Expunge(uid)
}
// Expunge removes a \Deleted message from the selected mailbox. UIDPLUS
// (RFC 4315) keeps the expunge scoped to one UID; the plain EXPUNGE fallback is
// only used by servers that do not advertise it.
func (c *Client) Expunge(uid uint32) error {
id := strconv.FormatUint(uint64(uid), 10)
if err := c.exec("UID EXPUNGE "+id, nil); err == nil {
return nil
} else {
var se *ServerError
if !errors.As(err, &se) {
return err
}
}
return c.exec("EXPUNGE", nil)
}
// StoreFlags adds (op "+") or removes (op "-") message flags.
func (c *Client) StoreFlags(uid uint32, op string, flag string) error {
if op != "+" && op != "-" {
return errors.New("flag operation must be + or -")
}
id := strconv.FormatUint(uint64(uid), 10)
return c.exec("UID STORE "+id+" "+op+"FLAGS ("+flag+")", nil)
}
// SearchGmailID returns the UIDs of the messages with the given Gmail message
// id (X-GM-MSGID) in the currently selected mailbox. Gmail assigns a different
// UID per label, so this is how a message is located after a move.
func (c *Client) SearchGmailID(gmailID string) ([]uint32, error) {
gmailID = strings.TrimSpace(gmailID)
if gmailID == "" {
return nil, errors.New("empty x-gm-msgid")
}
for _, r := range gmailID {
if r < '0' || r > '9' {
return nil, errors.New("x-gm-msgid must be numeric")
}
}
var uids []uint32
err := c.exec("UID SEARCH X-GM-MSGID "+gmailID, func(resp *response) error {
if !strings.HasPrefix(strings.ToUpper(resp.line), "* SEARCH") {
return nil
}
fields := strings.Fields(resp.line)
for _, f := range fields[2:] {
if n, err := strconv.ParseUint(f, 10, 32); err == nil {
uids = append(uids, uint32(n))
}
}
return nil
})
return uids, err
}

View File

@@ -0,0 +1,96 @@
package mailfetch
import (
"errors"
"strings"
"testing"
)
func TestCreateMailboxIgnoresAlreadyExists(t *testing.T) {
c, _ := newFakeClient("A0001 NO [ALREADYEXISTS] Mailbox already exists\r\n")
if err := c.CreateMailbox("Delete"); err != nil {
t.Fatalf("CreateMailbox() error = %v, want nil for an existing mailbox", err)
}
}
func TestCreateMailboxReturnsOtherError(t *testing.T) {
c, _ := newFakeClient("A0001 NO [CANNOT] permission denied\r\n")
err := c.CreateMailbox("Delete")
if err == nil {
t.Fatal("CreateMailbox() error = nil, want failure")
}
var se *ServerError
if !errors.As(err, &se) {
t.Fatalf("CreateMailbox() error = %T, want *ServerError", err)
}
}
func TestMoveUsesMoveExtension(t *testing.T) {
c, fc := newFakeClient("A0001 OK MOVE completed\r\n")
if err := c.Move(5, "Delete"); err != nil {
t.Fatalf("Move() error = %v", err)
}
if !strings.Contains(fc.w.String(), `UID MOVE 5 "Delete"`) {
t.Errorf("command = %q, want UID MOVE", fc.w.String())
}
}
func TestMoveFallsBackToCopyStoreExpunge(t *testing.T) {
script := "A0001 NO [BAD] MOVE not supported\r\n" +
"A0002 OK COPY completed\r\n" +
"A0003 OK STORE completed\r\n" +
"A0004 OK EXPUNGE completed\r\n"
c, fc := newFakeClient(script)
if err := c.Move(9, "Delete"); err != nil {
t.Fatalf("Move() fallback error = %v", err)
}
got := fc.w.String()
for _, want := range []string{
`UID MOVE 9 "Delete"`,
`UID COPY 9 "Delete"`,
`UID STORE 9 +FLAGS (\Deleted)`,
"UID EXPUNGE 9",
} {
if !strings.Contains(got, want) {
t.Errorf("writes = %q, missing %q", got, want)
}
}
}
func TestStoreFlags(t *testing.T) {
c, fc := newFakeClient("A0001 OK STORE completed\r\n")
if err := c.StoreFlags(4, "+", `\Seen`); err != nil {
t.Fatalf("StoreFlags() error = %v", err)
}
if !strings.Contains(fc.w.String(), `UID STORE 4 +FLAGS (\Seen)`) {
t.Errorf("command = %q", fc.w.String())
}
}
func TestStoreFlagsRejectsBadOp(t *testing.T) {
c, _ := newFakeClient("")
if err := c.StoreFlags(4, "*", `\Seen`); err == nil {
t.Fatal("StoreFlags() error = nil, want failure")
}
}
func TestSearchGmailID(t *testing.T) {
c, fc := newFakeClient("* SEARCH 3 4\r\nA0001 OK SEARCH completed\r\n")
uids, err := c.SearchGmailID("1234000000000000000")
if err != nil {
t.Fatalf("SearchGmailID() error = %v", err)
}
if len(uids) != 2 || uids[0] != 3 || uids[1] != 4 {
t.Fatalf("uids = %v, want [3 4]", uids)
}
if !strings.Contains(fc.w.String(), "UID SEARCH X-GM-MSGID 1234000000000000000") {
t.Errorf("command = %q", fc.w.String())
}
}
func TestSearchGmailIDRejectsNonNumeric(t *testing.T) {
c, _ := newFakeClient("")
if _, err := c.SearchGmailID(`1234") DELETE`); err == nil {
t.Fatal("SearchGmailID() error = nil, want failure for a non-numeric id")
}
}