Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2363,6 +2363,87 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { //nolint:gocyclo
}
return m, nil

case tui.EditLabelsMsg:
// Provider validation: only allow for Gmail accounts
account := m.config.GetAccountByID(msg.AccountID)
if account == nil || !tui.IsGmailAccount(account) {
return m, m.showErrorCmd("Gmail labels are only available for Gmail accounts")
}
// Recipient validation: the account's email must be in To/Cc/Bcc
fetchEmail := account.FetchEmail
if fetchEmail == "" {
fetchEmail = account.Email
}
if !tui.IsRecipientOfEmail(msg.Email, fetchEmail, account) && !account.CatchAll {
return m, m.showErrorCmd("Cannot modify labels: your email address is not a recipient of this message")
}
// Switch back to folder inbox and forward the message to activate the overlay
tui.ClearKittyGraphics()
if m.folderInbox != nil {
m.current = m.folderInbox
return m.folderInbox.Update(msg)
}
return m, nil

case tui.GmailLabelModifiedMsg:
account := m.config.GetAccountByID(msg.AccountID)
if account == nil {
return m, m.showErrorCmd("Account not found")
}
if !tui.IsGmailAccount(account) {
return m, m.showErrorCmd("Gmail labels are only available for Gmail accounts")
}
folder := msg.Folder
if folder == "" {
folder = m.folderName()
}
// Update the in-memory store immediately for instant UI feedback
if msg.Add {
m.store.AddGmailLabel(msg.UID, msg.AccountID, msg.Label)
} else {
m.store.RemoveGmailLabel(msg.UID, msg.AccountID, msg.Label)
}
// Refresh the inbox display
if m.folderInbox != nil {
m.folderInbox.GetInbox().SetEmails(m.store.CloneFolder(folder), m.config.Accounts)
}
// Send the command to the server
return m, func() tea.Msg {
var err error
if msg.Add {
err = m.service.AddGmailLabel(msg.AccountID, folder, msg.UID, msg.Label)
} else {
err = m.service.RemoveGmailLabel(msg.AccountID, folder, msg.UID, msg.Label)
}
return tui.GmailLabelResultMsg{
UID: msg.UID,
AccountID: msg.AccountID,
Label: msg.Label,
Add: msg.Add,
Err: err,
}
}

case tui.GmailLabelResultMsg:
if msg.Err != nil {
// Revert the local store on failure
account := m.config.GetAccountByID(msg.AccountID)
if account != nil {
if msg.Add {
m.store.RemoveGmailLabel(msg.UID, msg.AccountID, msg.Label)
} else {
m.store.AddGmailLabel(msg.UID, msg.AccountID, msg.Label)
}
folder := m.folderName()
if m.folderInbox != nil {
m.folderInbox.GetInbox().SetEmails(m.store.CloneFolder(folder), m.config.Accounts)
}
}
log.Printf("Gmail label %s failed: %v", map[bool]string{true: "add", false: "remove"}[msg.Add], msg.Err)
return m, m.showErrorCmd(msg.Err.Error())
}
return m, nil

case tui.DownloadAttachmentMsg:
m.previousModel = m.current
m.current = tui.NewStatus(fmt.Sprintf("Downloading %s...", msg.Filename))
Expand Down
1 change: 1 addition & 0 deletions backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ type Email struct {
References []string
Attachments []Attachment
AccountID string
Labels []string // Gmail X-GM-LABELS (empty for non-Gmail providers)
}

// Attachment holds data for an email attachment.
Expand Down
1 change: 1 addition & 0 deletions backend/imap/imap.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ func toBackendEmails(emails []fetcher.Email) []backend.Email {
References: e.References,
Attachments: toBackendAttachments(e.Attachments),
AccountID: e.AccountID,
Labels: e.Labels,
}
}
return result
Expand Down
1 change: 1 addition & 0 deletions config/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type CachedEmail struct {
References []string `json:"references,omitempty"`
AccountID string `json:"account_id"`
IsRead bool `json:"is_read"`
Labels []string `json:"labels,omitempty"`
}

// EmailCache stores cached emails for all accounts.
Expand Down
6 changes: 4 additions & 2 deletions config/default_keybinds.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"filter": "f",
"open": "enter",
"next_tab": "l",
"prev_tab": "h"
"prev_tab": "h",
"edit_labels": "L"
},
"email": {
"reply": "r",
Expand All @@ -30,7 +31,8 @@
"rsvp_tentative": "3",
"focus_attachments": "tab",
"apply_patch": "p",
"send_patch": "P"
"send_patch": "P",
"edit_labels": "L"
},
"composer": {
"external_editor": "ctrl+e",
Expand Down
4 changes: 4 additions & 0 deletions config/keybinds.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type InboxKeys struct {
Open string `json:"open"`
NextTab string `json:"next_tab"`
PrevTab string `json:"prev_tab"`
EditLabels string `json:"edit_labels"`
}

type EmailKeys struct {
Expand All @@ -60,6 +61,7 @@ type EmailKeys struct {
FocusAttachments string `json:"focus_attachments"`
ApplyPatch string `json:"apply_patch"`
SendPatch string `json:"send_patch"`
EditLabels string `json:"edit_labels"`
}

type ComposerKeys struct {
Expand Down Expand Up @@ -130,6 +132,7 @@ func ValidateKeybinds(kb KeybindsConfig) []string {
"open": kb.Inbox.Open,
"next_tab": kb.Inbox.NextTab,
"prev_tab": kb.Inbox.PrevTab,
"edit_labels": kb.Inbox.EditLabels,
},
"email": {
"reply": kb.Email.Reply,
Expand All @@ -143,6 +146,7 @@ func ValidateKeybinds(kb KeybindsConfig) []string {
"focus_attachments": kb.Email.FocusAttachments,
"apply_patch": kb.Email.ApplyPatch,
"send_patch": kb.Email.SendPatch,
"edit_labels": kb.Email.EditLabels,
},
"composer": {
"undo_send": kb.Composer.UndoSend,
Expand Down
2 changes: 2 additions & 0 deletions daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ func (d *Daemon) registerHandlers() {
d.server.Handle(daemonrpc.MethodUnsubscribe, d.handleUnsubscribe)
d.server.Handle(daemonrpc.MethodQueueEmail, d.handleQueueEmail)
d.server.Handle(daemonrpc.MethodCancelEmail, d.handleCancelEmail)
d.server.Handle(daemonrpc.MethodAddGmailLabel, d.handleAddGmailLabel)
d.server.Handle(daemonrpc.MethodRemoveGmailLabel, d.handleRemoveGmailLabel)
}

// Run starts the daemon: creates providers, starts the socket listener,
Expand Down
49 changes: 49 additions & 0 deletions daemon/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/floatpane/matcha/daemonrpc"
"github.com/floatpane/matcha/fetcher"
"github.com/google/uuid"
)

Expand Down Expand Up @@ -382,3 +383,51 @@ func (d *Daemon) handleCancelEmail(_ context.Context, _ *daemonrpc.Conn, params
log.Printf("daemon: cancelled email %s", args.JobID)
return true, nil
}

func (d *Daemon) handleAddGmailLabel(ctx context.Context, _ *daemonrpc.Conn, params json.RawMessage) (any, error) {
args, err := decodeParams[daemonrpc.AddGmailLabelParams](params)
if err != nil {
return nil, parseError(err)
}

d.mu.RLock()
account := d.config.GetAccountByID(args.AccountID)
d.mu.RUnlock()

if account == nil {
return nil, fmt.Errorf("no account for %s", args.AccountID)
}

ctx, cancel := context.WithTimeout(ctx, mutateTimeout)
defer cancel()
_ = ctx

if err := fetcher.AddGmailLabel(account, args.Folder, args.UID, args.Label); err != nil {
return nil, err
}
return true, nil
}

func (d *Daemon) handleRemoveGmailLabel(ctx context.Context, _ *daemonrpc.Conn, params json.RawMessage) (any, error) {
args, err := decodeParams[daemonrpc.RemoveGmailLabelParams](params)
if err != nil {
return nil, parseError(err)
}

d.mu.RLock()
account := d.config.GetAccountByID(args.AccountID)
d.mu.RUnlock()

if account == nil {
return nil, fmt.Errorf("no account for %s", args.AccountID)
}

ctx, cancel := context.WithTimeout(ctx, mutateTimeout)
defer cancel()
_ = ctx

if err := fetcher.RemoveGmailLabel(account, args.Folder, args.UID, args.Label); err != nil {
return nil, err
}
return true, nil
}
40 changes: 40 additions & 0 deletions daemonclient/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ type Service interface {
MoveEmails(accountID string, uids []uint32, src, dst string) error
MarkRead(accountID, folder string, uids []uint32) error
MarkUnread(accountID, folder string, uids []uint32) error
AddGmailLabel(accountID, folder string, uid uint32, label string) error
RemoveGmailLabel(accountID, folder string, uid uint32, label string) error
QueueEmail(accountID string, to, cc, bcc []string, subject, body, htmlBody string, images map[string][]byte, attachments map[string][]byte, inReplyTo string, references []string, signSMIME, encryptSMIME, signPGP, encryptPGP bool, delaySeconds int, prebuiltRaw []byte) (string, error)
CancelEmail(jobID string) error
FetchFolders(accountID string) ([]backend.Folder, error)
Expand Down Expand Up @@ -287,6 +289,28 @@ func (s *daemonService) MarkUnread(accountID, folder string, uids []uint32) erro
})
}

func (s *daemonService) AddGmailLabel(accountID, folder string, uid uint32, label string) error {
return s.call(func(c *Client) error {
return c.Call(daemonrpc.MethodAddGmailLabel, daemonrpc.AddGmailLabelParams{
AccountID: accountID,
Folder: folder,
UID: uid,
Label: label,
}, nil)
})
}

func (s *daemonService) RemoveGmailLabel(accountID, folder string, uid uint32, label string) error {
return s.call(func(c *Client) error {
return c.Call(daemonrpc.MethodRemoveGmailLabel, daemonrpc.RemoveGmailLabelParams{
AccountID: accountID,
Folder: folder,
UID: uid,
Label: label,
}, nil)
})
}

func (s *daemonService) QueueEmail(accountID string, to, cc, bcc []string, subject, body, htmlBody string, images map[string][]byte, attachments map[string][]byte, inReplyTo string, references []string, signSMIME, encryptSMIME, signPGP, encryptPGP bool, delaySeconds int, prebuiltRaw []byte) (string, error) {
var result daemonrpc.QueueEmailResult
err := s.call(func(c *Client) error {
Expand Down Expand Up @@ -495,6 +519,22 @@ func (s *directService) MarkUnread(accountID, folder string, uids []uint32) erro
return nil
}

func (s *directService) AddGmailLabel(accountID, folder string, uid uint32, label string) error {
acct := s.cfg.GetAccountByID(accountID)
if acct == nil {
return fmt.Errorf("no account for %s", accountID)
}
return fetcher.AddGmailLabel(acct, folder, uid, label)
}

func (s *directService) RemoveGmailLabel(accountID, folder string, uid uint32, label string) error {
acct := s.cfg.GetAccountByID(accountID)
if acct == nil {
return fmt.Errorf("no account for %s", accountID)
}
return fetcher.RemoveGmailLabel(acct, folder, uid, label)
}

func (s *directService) FetchFolders(accountID string) ([]backend.Folder, error) {
p, err := s.getProvider(accountID)
if err != nil {
Expand Down
58 changes: 37 additions & 21 deletions daemonrpc/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,29 @@ const (

// RPC method names.
const (
MethodPing = "Ping"
MethodGetStatus = "GetStatus"
MethodGetAccounts = "GetAccounts"
MethodReloadConfig = "ReloadConfig"
MethodFetchEmails = "FetchEmails"
MethodFetchEmailBody = "FetchEmailBody"
MethodSendEmail = "SendEmail"
MethodDeleteEmails = "DeleteEmails"
MethodArchiveEmails = "ArchiveEmails"
MethodMoveEmails = "MoveEmails"
MethodMarkRead = "MarkRead"
MethodFetchFolders = "FetchFolders"
MethodRefreshFolder = "RefreshFolder"
MethodSubscribe = "Subscribe"
MethodUnsubscribe = "Unsubscribe"
MethodSendRSVP = "SendRSVP"
MethodGetCachedEmails = "GetCachedEmails"
MethodGetCachedBody = "GetCachedBody"
MethodExportContacts = "ExportContacts"
MethodQueueEmail = "QueueEmail"
MethodCancelEmail = "CancelEmail"
MethodPing = "Ping"
MethodGetStatus = "GetStatus"
MethodGetAccounts = "GetAccounts"
MethodReloadConfig = "ReloadConfig"
MethodFetchEmails = "FetchEmails"
MethodFetchEmailBody = "FetchEmailBody"
MethodSendEmail = "SendEmail"
MethodDeleteEmails = "DeleteEmails"
MethodArchiveEmails = "ArchiveEmails"
MethodMoveEmails = "MoveEmails"
MethodMarkRead = "MarkRead"
MethodFetchFolders = "FetchFolders"
MethodRefreshFolder = "RefreshFolder"
MethodSubscribe = "Subscribe"
MethodUnsubscribe = "Unsubscribe"
MethodSendRSVP = "SendRSVP"
MethodGetCachedEmails = "GetCachedEmails"
MethodGetCachedBody = "GetCachedBody"
MethodExportContacts = "ExportContacts"
MethodQueueEmail = "QueueEmail"
MethodCancelEmail = "CancelEmail"
MethodAddGmailLabel = "AddGmailLabel"
MethodRemoveGmailLabel = "RemoveGmailLabel"
)

// Event type names.
Expand Down Expand Up @@ -209,6 +211,20 @@ type ExportContactsParams struct {
Format string `json:"format"` // "json" or "csv"
}

type AddGmailLabelParams struct {
AccountID string `json:"account_id"`
Folder string `json:"folder"`
UID uint32 `json:"uid"`
Label string `json:"label"`
}

type RemoveGmailLabelParams struct {
AccountID string `json:"account_id"`
Folder string `json:"folder"`
UID uint32 `json:"uid"`
Label string `json:"label"`
}

// Event data types.

type NewMailEvent struct {
Expand Down
1 change: 1 addition & 0 deletions fetcher/dispatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func backendEmailsToFetcher(in []backend.Email) []Email {
References: e.References,
Attachments: backendAttachmentsToFetcher(e.Attachments),
AccountID: e.AccountID,
Labels: e.Labels,
}
}
return out
Expand Down
Loading
Loading