fix(mail): refactor duplicated inbox logic and fix clear race condition (#435)
This commit is contained in:
@@ -2,6 +2,7 @@ package cmd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -11,6 +12,23 @@ import (
|
|||||||
"github.com/steveyegge/gastown/internal/style"
|
"github.com/steveyegge/gastown/internal/style"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// getMailbox returns the mailbox for the given address.
|
||||||
|
func getMailbox(address string) (*mail.Mailbox, error) {
|
||||||
|
// All mail uses town beads (two-level architecture)
|
||||||
|
workDir, err := findMailWorkDir()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("not in a Gas Town workspace: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get mailbox
|
||||||
|
router := mail.NewRouter(workDir)
|
||||||
|
mailbox, err := router.GetMailbox(address)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("getting mailbox: %w", err)
|
||||||
|
}
|
||||||
|
return mailbox, nil
|
||||||
|
}
|
||||||
|
|
||||||
func runMailInbox(cmd *cobra.Command, args []string) error {
|
func runMailInbox(cmd *cobra.Command, args []string) error {
|
||||||
// Determine which inbox to check (priority: --identity flag, positional arg, auto-detect)
|
// Determine which inbox to check (priority: --identity flag, positional arg, auto-detect)
|
||||||
address := ""
|
address := ""
|
||||||
@@ -22,17 +40,9 @@ func runMailInbox(cmd *cobra.Command, args []string) error {
|
|||||||
address = detectSender()
|
address = detectSender()
|
||||||
}
|
}
|
||||||
|
|
||||||
// All mail uses town beads (two-level architecture)
|
mailbox, err := getMailbox(address)
|
||||||
workDir, err := findMailWorkDir()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("not in a Gas Town workspace: %w", err)
|
return err
|
||||||
}
|
|
||||||
|
|
||||||
// Get mailbox
|
|
||||||
router := mail.NewRouter(workDir)
|
|
||||||
mailbox, err := router.GetMailbox(address)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("getting mailbox: %w", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get messages
|
// Get messages
|
||||||
@@ -93,22 +103,17 @@ func runMailInbox(cmd *cobra.Command, args []string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func runMailRead(cmd *cobra.Command, args []string) error {
|
func runMailRead(cmd *cobra.Command, args []string) error {
|
||||||
|
if len(args) == 0 {
|
||||||
|
return errors.New("msgID argument required")
|
||||||
|
}
|
||||||
msgID := args[0]
|
msgID := args[0]
|
||||||
|
|
||||||
// Determine which inbox
|
// Determine which inbox
|
||||||
address := detectSender()
|
address := detectSender()
|
||||||
|
|
||||||
// All mail uses town beads (two-level architecture)
|
mailbox, err := getMailbox(address)
|
||||||
workDir, err := findMailWorkDir()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("not in a Gas Town workspace: %w", err)
|
return err
|
||||||
}
|
|
||||||
|
|
||||||
// Get mailbox and message
|
|
||||||
router := mail.NewRouter(workDir)
|
|
||||||
mailbox, err := router.GetMailbox(address)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("getting mailbox: %w", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
msg, err := mailbox.Get(msgID)
|
msg, err := mailbox.Get(msgID)
|
||||||
@@ -164,15 +169,7 @@ func runMailPeek(cmd *cobra.Command, args []string) error {
|
|||||||
// Determine which inbox
|
// Determine which inbox
|
||||||
address := detectSender()
|
address := detectSender()
|
||||||
|
|
||||||
// All mail uses town beads (two-level architecture)
|
mailbox, err := getMailbox(address)
|
||||||
workDir, err := findMailWorkDir()
|
|
||||||
if err != nil {
|
|
||||||
return NewSilentExit(1) // Silent exit - no workspace
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get mailbox
|
|
||||||
router := mail.NewRouter(workDir)
|
|
||||||
mailbox, err := router.GetMailbox(address)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return NewSilentExit(1) // Silent exit - can't access mailbox
|
return NewSilentExit(1) // Silent exit - can't access mailbox
|
||||||
}
|
}
|
||||||
@@ -220,22 +217,17 @@ func runMailPeek(cmd *cobra.Command, args []string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func runMailDelete(cmd *cobra.Command, args []string) error {
|
func runMailDelete(cmd *cobra.Command, args []string) error {
|
||||||
|
if len(args) == 0 {
|
||||||
|
return errors.New("msgID argument required")
|
||||||
|
}
|
||||||
msgID := args[0]
|
msgID := args[0]
|
||||||
|
|
||||||
// Determine which inbox
|
// Determine which inbox
|
||||||
address := detectSender()
|
address := detectSender()
|
||||||
|
|
||||||
// All mail uses town beads (two-level architecture)
|
mailbox, err := getMailbox(address)
|
||||||
workDir, err := findMailWorkDir()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("not in a Gas Town workspace: %w", err)
|
return err
|
||||||
}
|
|
||||||
|
|
||||||
// Get mailbox
|
|
||||||
router := mail.NewRouter(workDir)
|
|
||||||
mailbox, err := router.GetMailbox(address)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("getting mailbox: %w", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := mailbox.Delete(msgID); err != nil {
|
if err := mailbox.Delete(msgID); err != nil {
|
||||||
@@ -250,17 +242,9 @@ func runMailArchive(cmd *cobra.Command, args []string) error {
|
|||||||
// Determine which inbox
|
// Determine which inbox
|
||||||
address := detectSender()
|
address := detectSender()
|
||||||
|
|
||||||
// All mail uses town beads (two-level architecture)
|
mailbox, err := getMailbox(address)
|
||||||
workDir, err := findMailWorkDir()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("not in a Gas Town workspace: %w", err)
|
return err
|
||||||
}
|
|
||||||
|
|
||||||
// Get mailbox
|
|
||||||
router := mail.NewRouter(workDir)
|
|
||||||
mailbox, err := router.GetMailbox(address)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("getting mailbox: %w", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Archive all specified messages
|
// Archive all specified messages
|
||||||
@@ -296,17 +280,9 @@ func runMailMarkRead(cmd *cobra.Command, args []string) error {
|
|||||||
// Determine which inbox
|
// Determine which inbox
|
||||||
address := detectSender()
|
address := detectSender()
|
||||||
|
|
||||||
// All mail uses town beads (two-level architecture)
|
mailbox, err := getMailbox(address)
|
||||||
workDir, err := findMailWorkDir()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("not in a Gas Town workspace: %w", err)
|
return err
|
||||||
}
|
|
||||||
|
|
||||||
// Get mailbox
|
|
||||||
router := mail.NewRouter(workDir)
|
|
||||||
mailbox, err := router.GetMailbox(address)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("getting mailbox: %w", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mark all specified messages as read
|
// Mark all specified messages as read
|
||||||
@@ -342,17 +318,9 @@ func runMailMarkUnread(cmd *cobra.Command, args []string) error {
|
|||||||
// Determine which inbox
|
// Determine which inbox
|
||||||
address := detectSender()
|
address := detectSender()
|
||||||
|
|
||||||
// All mail uses town beads (two-level architecture)
|
mailbox, err := getMailbox(address)
|
||||||
workDir, err := findMailWorkDir()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("not in a Gas Town workspace: %w", err)
|
return err
|
||||||
}
|
|
||||||
|
|
||||||
// Get mailbox
|
|
||||||
router := mail.NewRouter(workDir)
|
|
||||||
mailbox, err := router.GetMailbox(address)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("getting mailbox: %w", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mark all specified messages as unread
|
// Mark all specified messages as unread
|
||||||
@@ -393,17 +361,9 @@ func runMailClear(cmd *cobra.Command, args []string) error {
|
|||||||
address = detectSender()
|
address = detectSender()
|
||||||
}
|
}
|
||||||
|
|
||||||
// All mail uses town beads (two-level architecture)
|
mailbox, err := getMailbox(address)
|
||||||
workDir, err := findMailWorkDir()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("not in a Gas Town workspace: %w", err)
|
return err
|
||||||
}
|
|
||||||
|
|
||||||
// Get mailbox
|
|
||||||
router := mail.NewRouter(workDir)
|
|
||||||
mailbox, err := router.GetMailbox(address)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("getting mailbox: %w", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// List all messages
|
// List all messages
|
||||||
@@ -422,6 +382,10 @@ func runMailClear(cmd *cobra.Command, args []string) error {
|
|||||||
var errors []string
|
var errors []string
|
||||||
for _, msg := range messages {
|
for _, msg := range messages {
|
||||||
if err := mailbox.Delete(msg.ID); err != nil {
|
if err := mailbox.Delete(msg.ID); err != nil {
|
||||||
|
// If file is already gone (race condition), ignore it and count as success
|
||||||
|
if os.IsNotExist(err) || strings.Contains(err.Error(), "no such file") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
errors = append(errors, fmt.Sprintf("%s: %v", msg.ID, err))
|
errors = append(errors, fmt.Sprintf("%s: %v", msg.ID, err))
|
||||||
} else {
|
} else {
|
||||||
deleted++
|
deleted++
|
||||||
|
|||||||
Reference in New Issue
Block a user