Add additional flagging functionality
More mail flags can now be set, unset, and toggled, not just the read/seen flag. This functionality is implemented with a new `:flag` and `:unflag` command, which are extensions to the matching `:read` and `:unread` commands, adding support for different flags. In fact, the `read`/`unread` commands are now recognized aliases to `flag`/`unflag`. The new commands are also well documented in aerc(1). The change mostly extends the previous read/unread setting functionality by adding a selection for the flag to change.
This commit is contained in:
parent
fda3f43e7c
commit
0535f6333f
11 changed files with 247 additions and 189 deletions
|
@ -1,9 +1,9 @@
|
||||||
package msg
|
package msg
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"git.sr.ht/~sircmpwn/getopt"
|
"git.sr.ht/~sircmpwn/getopt"
|
||||||
|
|
||||||
|
@ -13,35 +13,105 @@ import (
|
||||||
"git.sr.ht/~sircmpwn/aerc/worker/types"
|
"git.sr.ht/~sircmpwn/aerc/worker/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Read struct{}
|
type FlagMsg struct{}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
register(Read{})
|
register(FlagMsg{})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (Read) Aliases() []string {
|
func (FlagMsg) Aliases() []string {
|
||||||
return []string{"read", "unread"}
|
return []string{"flag", "unflag", "read", "unread"}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (Read) Complete(aerc *widgets.Aerc, args []string) []string {
|
func (FlagMsg) Complete(aerc *widgets.Aerc, args []string) []string {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (Read) Execute(aerc *widgets.Aerc, args []string) error {
|
// If this was called as 'flag' or 'unflag', without the toggle (-t)
|
||||||
opts, optind, err := getopt.Getopts(args, "t")
|
// option, then it will flag the corresponding messages with the given
|
||||||
|
// flag. If the toggle option was given, it will individually toggle
|
||||||
|
// the given flag for the corresponding messages.
|
||||||
|
//
|
||||||
|
// If this was called as 'read' or 'unread', it has the same effect as
|
||||||
|
// 'flag' or 'unflag', respectively, but the 'Seen' flag is affected.
|
||||||
|
func (FlagMsg) Execute(aerc *widgets.Aerc, args []string) error {
|
||||||
|
|
||||||
|
// The flag to change
|
||||||
|
var flag models.Flag
|
||||||
|
// User-readable name of the flag to change
|
||||||
|
var flagName string
|
||||||
|
// Whether to toggle the flag (true) or to enable/disable it (false)
|
||||||
|
var toggle bool
|
||||||
|
// Whether to enable (true) or disable (false) the flag
|
||||||
|
enable := (args[0] == "read" || args[0] == "flag")
|
||||||
|
// User-readable name for the action being performed
|
||||||
|
var actionName string
|
||||||
|
// Getopt option string, varies by command name
|
||||||
|
var getoptString string
|
||||||
|
// Help message to provide on parsing failure
|
||||||
|
var helpMessage string
|
||||||
|
// Used during parsing to prevent choosing a flag muliple times
|
||||||
|
// A default flag will be used if this is false
|
||||||
|
flagChosen := false
|
||||||
|
|
||||||
|
if args[0] == "read" || args[0] == "unread" {
|
||||||
|
flag = models.SeenFlag
|
||||||
|
flagName = "read"
|
||||||
|
getoptString = "t"
|
||||||
|
helpMessage = "Usage: " + args[0] + " [-t]"
|
||||||
|
} else { // 'flag' / 'unflag'
|
||||||
|
flag = models.FlaggedFlag
|
||||||
|
flagName = "flagged"
|
||||||
|
getoptString = "tax:"
|
||||||
|
helpMessage = "Usage: " + args[0] + " [-t] [-a | -x <flag>]"
|
||||||
|
}
|
||||||
|
|
||||||
|
opts, optind, err := getopt.Getopts(args, getoptString)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if optind != len(args) {
|
|
||||||
return errors.New("Usage: " + args[0] + " [-t]")
|
|
||||||
}
|
|
||||||
var toggle bool
|
|
||||||
|
|
||||||
for _, opt := range opts {
|
for _, opt := range opts {
|
||||||
switch opt.Option {
|
switch opt.Option {
|
||||||
case 't':
|
case 't':
|
||||||
toggle = true
|
toggle = true
|
||||||
|
case 'a':
|
||||||
|
if flagChosen {
|
||||||
|
return fmt.Errorf("Cannot choose a flag multiple times! " + helpMessage)
|
||||||
}
|
}
|
||||||
|
flag = models.AnsweredFlag
|
||||||
|
flagName = "answered"
|
||||||
|
flagChosen = true
|
||||||
|
case 'x':
|
||||||
|
if flagChosen {
|
||||||
|
return fmt.Errorf("Cannot choose a flag multiple times! " + helpMessage)
|
||||||
|
}
|
||||||
|
// TODO: Support all flags?
|
||||||
|
switch opt.Value {
|
||||||
|
case "Seen":
|
||||||
|
flag = models.SeenFlag
|
||||||
|
flagName = "seen"
|
||||||
|
case "Answered":
|
||||||
|
flag = models.AnsweredFlag
|
||||||
|
flagName = "answered"
|
||||||
|
case "Flagged":
|
||||||
|
flag = models.FlaggedFlag
|
||||||
|
flagName = "flagged"
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("Unknown / Prohibited flag \"%v\"", opt.Value)
|
||||||
|
}
|
||||||
|
flagChosen = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if toggle {
|
||||||
|
actionName = "Toggling"
|
||||||
|
} else if enable {
|
||||||
|
actionName = "Setting"
|
||||||
|
} else {
|
||||||
|
actionName = "Unsetting"
|
||||||
|
}
|
||||||
|
if optind != len(args) {
|
||||||
|
// Any non-option arguments: Error
|
||||||
|
return fmt.Errorf(helpMessage)
|
||||||
}
|
}
|
||||||
|
|
||||||
h := newHelper(aerc)
|
h := newHelper(aerc)
|
||||||
|
@ -50,57 +120,68 @@ func (Read) Execute(aerc *widgets.Aerc, args []string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UIDs of messages to enable or disable the flag for.
|
||||||
|
var toEnable []uint32
|
||||||
|
var toDisable []uint32
|
||||||
|
|
||||||
if toggle {
|
if toggle {
|
||||||
// ignore command given, simply toggle all the read states
|
// If toggling, split messages into those that need to
|
||||||
return submitToggle(aerc, store, h)
|
// be enabled / disabled.
|
||||||
|
msgs, err := h.messages()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
for _, m := range msgs {
|
||||||
|
var enabled bool
|
||||||
|
for _, mFlag := range m.Flags {
|
||||||
|
if mFlag == flag {
|
||||||
|
enabled = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if enabled {
|
||||||
|
toDisable = append(toDisable, m.Uid)
|
||||||
|
} else {
|
||||||
|
toEnable = append(toEnable, m.Uid)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
msgUids, err := h.markedOrSelectedUids()
|
msgUids, err := h.markedOrSelectedUids()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
switch args[0] {
|
if enable {
|
||||||
case "read":
|
toEnable = msgUids
|
||||||
submitReadChange(aerc, store, msgUids, true)
|
} else {
|
||||||
case "unread":
|
toDisable = msgUids
|
||||||
submitReadChange(aerc, store, msgUids, false)
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
success := true
|
||||||
|
|
||||||
|
if len(toEnable) != 0 {
|
||||||
|
submitFlagChange(aerc, store, toEnable, flag, true, &wg, &success)
|
||||||
|
}
|
||||||
|
if len(toDisable) != 0 {
|
||||||
|
submitFlagChange(aerc, store, toDisable, flag, false, &wg, &success)
|
||||||
|
}
|
||||||
|
|
||||||
|
// We need to do flagging in the background, else we block the main thread
|
||||||
|
go func() {
|
||||||
|
wg.Wait()
|
||||||
|
if success {
|
||||||
|
aerc.PushStatus(actionName + " flag '" + flagName + "' successful", 10*time.Second)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func splitMessages(msgs []*models.MessageInfo) (read []uint32, unread []uint32) {
|
func submitFlagChange(aerc *widgets.Aerc, store *lib.MessageStore,
|
||||||
for _, m := range msgs {
|
uids []uint32, flag models.Flag, newState bool,
|
||||||
var seen bool
|
wg *sync.WaitGroup, success *bool) {
|
||||||
for _, flag := range m.Flags {
|
store.Flag(uids, flag, newState, func(msg types.WorkerMessage) {
|
||||||
if flag == models.SeenFlag {
|
|
||||||
seen = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if seen {
|
|
||||||
read = append(read, m.Uid)
|
|
||||||
} else {
|
|
||||||
unread = append(unread, m.Uid)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return read, unread
|
|
||||||
}
|
|
||||||
|
|
||||||
func submitReadChange(aerc *widgets.Aerc, store *lib.MessageStore,
|
|
||||||
uids []uint32, newState bool) {
|
|
||||||
store.Read(uids, newState, func(msg types.WorkerMessage) {
|
|
||||||
switch msg := msg.(type) {
|
|
||||||
case *types.Done:
|
|
||||||
aerc.PushStatus(msg_success, 10*time.Second)
|
|
||||||
case *types.Error:
|
|
||||||
aerc.PushError(" " + msg.Error.Error())
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func submitReadChangeWg(aerc *widgets.Aerc, store *lib.MessageStore,
|
|
||||||
uids []uint32, newState bool, wg *sync.WaitGroup, success *bool) {
|
|
||||||
store.Read(uids, newState, func(msg types.WorkerMessage) {
|
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
switch msg := msg.(type) {
|
switch msg := msg.(type) {
|
||||||
case *types.Done:
|
case *types.Done:
|
||||||
|
@ -112,35 +193,3 @@ func submitReadChangeWg(aerc *widgets.Aerc, store *lib.MessageStore,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func submitToggle(aerc *widgets.Aerc, store *lib.MessageStore, h *helper) error {
|
|
||||||
msgs, err := h.messages()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
read, unread := splitMessages(msgs)
|
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
|
||||||
success := true
|
|
||||||
|
|
||||||
if len(read) != 0 {
|
|
||||||
newState := false
|
|
||||||
submitReadChangeWg(aerc, store, read, newState, &wg, &success)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(unread) != 0 {
|
|
||||||
newState := true
|
|
||||||
submitReadChangeWg(aerc, store, unread, newState, &wg, &success)
|
|
||||||
}
|
|
||||||
// we need to do that in the background, else we block the main thread
|
|
||||||
go func() {
|
|
||||||
wg.Wait()
|
|
||||||
if success {
|
|
||||||
aerc.PushStatus(msg_success, 10*time.Second)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
return nil
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
const msg_success = "read state set successfully"
|
|
||||||
|
|
|
@ -136,16 +136,39 @@ message list, the message in the message viewer, etc).
|
||||||
|
|
||||||
*-T*: Use the specified template file for creating the initial message body
|
*-T*: Use the specified template file for creating the initial message body
|
||||||
|
|
||||||
*read*
|
*read* [-t]
|
||||||
Marks the marked or selected messages as read.
|
Marks the marked or selected messages as read.
|
||||||
|
|
||||||
*-t*: Toggle the messages between read and unread.
|
*-t*: Toggle the messages between read and unread.
|
||||||
|
|
||||||
*unread*
|
*unread* [-t]
|
||||||
Marks the marked or selected messages as unread.
|
Marks the marked or selected messages as unread.
|
||||||
|
|
||||||
*-t*: Toggle the messages between read and unread.
|
*-t*: Toggle the messages between read and unread.
|
||||||
|
|
||||||
|
*flag* [-t] [-a | -x <flag>]
|
||||||
|
Sets (enables) a certain flag on the marked or selected messages.
|
||||||
|
|
||||||
|
*-t*: Toggle the flag instead of setting (enabling) it.
|
||||||
|
|
||||||
|
*-a*: Mark message as answered/unanswered.
|
||||||
|
|
||||||
|
*-x <flag>*: Mark message with specific flag.
|
||||||
|
|
||||||
|
The available flags are (adapted from RFC 3501, section 2.3.2):
|
||||||
|
|
||||||
|
Seen
|
||||||
|
Message has been read
|
||||||
|
|
||||||
|
Answered
|
||||||
|
Message has been answered
|
||||||
|
|
||||||
|
Flagged
|
||||||
|
Message is flagged for urgent/special attention
|
||||||
|
|
||||||
|
*unflag* [-t] <flag>
|
||||||
|
Operates exactly like *flag*, defaulting to unsetting (disabling) flags.
|
||||||
|
|
||||||
*modify-labels* <[+-]label>...
|
*modify-labels* <[+-]label>...
|
||||||
Modify message labels (e.g. notmuch tags). Labels prefixed with a '+' are
|
Modify message labels (e.g. notmuch tags). Labels prefixed with a '+' are
|
||||||
added, those prefixed with a '-' removed. As a convenience, labels without
|
added, those prefixed with a '-' removed. As a convenience, labels without
|
||||||
|
|
|
@ -94,7 +94,7 @@ func NewMessageStoreView(messageInfo *models.MessageInfo,
|
||||||
} else {
|
} else {
|
||||||
cb(msv, nil)
|
cb(msv, nil)
|
||||||
}
|
}
|
||||||
store.Read([]uint32{messageInfo.Uid}, true, nil)
|
store.Flag([]uint32{messageInfo.Uid}, models.SeenFlag, true, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (msv *MessageStoreView) MessageInfo() *models.MessageInfo {
|
func (msv *MessageStoreView) MessageInfo() *models.MessageInfo {
|
||||||
|
|
|
@ -333,11 +333,12 @@ func (store *MessageStore) Move(uids []uint32, dest string, createDest bool,
|
||||||
store.update()
|
store.update()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (store *MessageStore) Read(uids []uint32, read bool,
|
func (store *MessageStore) Flag(uids []uint32, flag models.Flag,
|
||||||
cb func(msg types.WorkerMessage)) {
|
enable bool, cb func(msg types.WorkerMessage)) {
|
||||||
|
|
||||||
store.worker.PostAction(&types.ReadMessages{
|
store.worker.PostAction(&types.FlagMessages{
|
||||||
Read: read,
|
Enable: enable,
|
||||||
|
Flag: flag,
|
||||||
Uids: uids,
|
Uids: uids,
|
||||||
}, cb)
|
}, cb)
|
||||||
}
|
}
|
||||||
|
|
|
@ -76,12 +76,11 @@ func (imapw *IMAPWorker) handleAnsweredMessages(msg *types.AnsweredMessages) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (imapw *IMAPWorker) handleReadMessages(msg *types.ReadMessages) {
|
func (imapw *IMAPWorker) handleFlagMessages(msg *types.FlagMessages) {
|
||||||
|
flags := []interface{}{flagToImap[msg.Flag]}
|
||||||
item := imap.FormatFlagsOp(imap.AddFlags, true)
|
item := imap.FormatFlagsOp(imap.AddFlags, true)
|
||||||
flags := []interface{}{imap.SeenFlag}
|
if !msg.Enable {
|
||||||
if !msg.Read {
|
|
||||||
item = imap.FormatFlagsOp(imap.RemoveFlags, true)
|
item = imap.FormatFlagsOp(imap.RemoveFlags, true)
|
||||||
flags = []interface{}{imap.SeenFlag}
|
|
||||||
}
|
}
|
||||||
uids := toSeqSet(msg.Uids)
|
uids := toSeqSet(msg.Uids)
|
||||||
emitErr := func(err error) {
|
emitErr := func(err error) {
|
||||||
|
@ -99,7 +98,7 @@ func (imapw *IMAPWorker) handleReadMessages(msg *types.ReadMessages) {
|
||||||
}, func(_msg types.WorkerMessage) {
|
}, func(_msg types.WorkerMessage) {
|
||||||
switch m := _msg.(type) {
|
switch m := _msg.(type) {
|
||||||
case *types.Error:
|
case *types.Error:
|
||||||
err := fmt.Errorf("handleReadMessages: %v", m.Error)
|
err := fmt.Errorf("handleFlagMessages: %v", m.Error)
|
||||||
imapw.worker.Logger.Printf("could not fetch headers: %s", err)
|
imapw.worker.Logger.Printf("could not fetch headers: %s", err)
|
||||||
emitErr(err)
|
emitErr(err)
|
||||||
case *types.Done:
|
case *types.Done:
|
||||||
|
|
|
@ -173,8 +173,8 @@ func (w *IMAPWorker) handleMessage(msg types.WorkerMessage) error {
|
||||||
w.handleFetchFullMessages(msg)
|
w.handleFetchFullMessages(msg)
|
||||||
case *types.DeleteMessages:
|
case *types.DeleteMessages:
|
||||||
w.handleDeleteMessages(msg)
|
w.handleDeleteMessages(msg)
|
||||||
case *types.ReadMessages:
|
case *types.FlagMessages:
|
||||||
w.handleReadMessages(msg)
|
w.handleFlagMessages(msg)
|
||||||
case *types.AnsweredMessages:
|
case *types.AnsweredMessages:
|
||||||
w.handleAnsweredMessages(msg)
|
w.handleAnsweredMessages(msg)
|
||||||
case *types.CopyMessages:
|
case *types.CopyMessages:
|
||||||
|
|
|
@ -53,43 +53,29 @@ func (m Message) SetFlags(flags []maildir.Flag) error {
|
||||||
return m.dir.SetFlags(m.key, flags)
|
return m.dir.SetFlags(m.key, flags)
|
||||||
}
|
}
|
||||||
|
|
||||||
// MarkReplied either adds or removes the maildir.FlagReplied flag from the
|
// SetOneFlag enables or disables a single message flag on the message.
|
||||||
// message.
|
func (m Message) SetOneFlag(flag maildir.Flag, enable bool) error {
|
||||||
func (m Message) MarkReplied(answered bool) error {
|
|
||||||
flags, err := m.Flags()
|
flags, err := m.Flags()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not read previous flags: %v", err)
|
return fmt.Errorf("could not read previous flags: %v", err)
|
||||||
}
|
}
|
||||||
if answered {
|
if enable {
|
||||||
flags = append(flags, maildir.FlagReplied)
|
flags = append(flags, flag)
|
||||||
return m.SetFlags(flags)
|
return m.SetFlags(flags)
|
||||||
}
|
}
|
||||||
var newFlags []maildir.Flag
|
var newFlags []maildir.Flag
|
||||||
for _, flag := range flags {
|
for _, oldFlag := range flags {
|
||||||
if flag != maildir.FlagReplied {
|
if oldFlag != flag {
|
||||||
newFlags = append(newFlags, flag)
|
newFlags = append(newFlags, oldFlag)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return m.SetFlags(newFlags)
|
return m.SetFlags(newFlags)
|
||||||
}
|
}
|
||||||
|
|
||||||
// MarkRead either adds or removes the maildir.FlagSeen flag from the message.
|
// MarkReplied either adds or removes the maildir.FlagReplied flag from the
|
||||||
func (m Message) MarkRead(seen bool) error {
|
// message.
|
||||||
flags, err := m.Flags()
|
func (m Message) MarkReplied(answered bool) error {
|
||||||
if err != nil {
|
return m.SetOneFlag(maildir.FlagReplied, answered)
|
||||||
return fmt.Errorf("could not read previous flags: %v", err)
|
|
||||||
}
|
|
||||||
if seen {
|
|
||||||
flags = append(flags, maildir.FlagSeen)
|
|
||||||
return m.SetFlags(flags)
|
|
||||||
}
|
|
||||||
var newFlags []maildir.Flag
|
|
||||||
for _, flag := range flags {
|
|
||||||
if flag != maildir.FlagSeen {
|
|
||||||
newFlags = append(newFlags, flag)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return m.SetFlags(newFlags)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove deletes the email immediately.
|
// Remove deletes the email immediately.
|
||||||
|
|
|
@ -193,8 +193,8 @@ func (w *Worker) handleMessage(msg types.WorkerMessage) error {
|
||||||
return w.handleFetchFullMessages(msg)
|
return w.handleFetchFullMessages(msg)
|
||||||
case *types.DeleteMessages:
|
case *types.DeleteMessages:
|
||||||
return w.handleDeleteMessages(msg)
|
return w.handleDeleteMessages(msg)
|
||||||
case *types.ReadMessages:
|
case *types.FlagMessages:
|
||||||
return w.handleReadMessages(msg)
|
return w.handleFlagMessages(msg)
|
||||||
case *types.AnsweredMessages:
|
case *types.AnsweredMessages:
|
||||||
return w.handleAnsweredMessages(msg)
|
return w.handleAnsweredMessages(msg)
|
||||||
case *types.CopyMessages:
|
case *types.CopyMessages:
|
||||||
|
@ -473,7 +473,7 @@ func (w *Worker) handleAnsweredMessages(msg *types.AnsweredMessages) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *Worker) handleReadMessages(msg *types.ReadMessages) error {
|
func (w *Worker) handleFlagMessages(msg *types.FlagMessages) error {
|
||||||
for _, uid := range msg.Uids {
|
for _, uid := range msg.Uids {
|
||||||
m, err := w.c.Message(*w.selected, uid)
|
m, err := w.c.Message(*w.selected, uid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -481,8 +481,9 @@ func (w *Worker) handleReadMessages(msg *types.ReadMessages) error {
|
||||||
w.err(msg, err)
|
w.err(msg, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if err := m.MarkRead(msg.Read); err != nil {
|
flag := flagToMaildir[msg.Flag]
|
||||||
w.worker.Logger.Printf("could not mark message as read: %v", err)
|
if err := m.SetOneFlag(flag, msg.Enable); err != nil {
|
||||||
|
w.worker.Logger.Printf("could change flag %v to %v on message: %v", flag, msg.Enable, err)
|
||||||
w.err(msg, err)
|
w.err(msg, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
|
@ -64,70 +64,68 @@ func (m *Message) NewBodyPartReader(requestedParts []int) (io.Reader, error) {
|
||||||
return lib.FetchEntityPartReader(msg, requestedParts)
|
return lib.FetchEntityPartReader(msg, requestedParts)
|
||||||
}
|
}
|
||||||
|
|
||||||
// MarkAnswered either adds or removes the "replied" tag from the message.
|
// SetOneFlag adds or removes a single flag from the message.
|
||||||
func (m *Message) MarkAnswered(answered bool) error {
|
// Notmuch doesn't support all the flags, and for those this errors.
|
||||||
haveReplied := false
|
func (m *Message) SetOneFlag(flag models.Flag, enable bool) error {
|
||||||
|
// Translate the flag into a notmuch tag, ignoring no-op flags.
|
||||||
|
var tag string
|
||||||
|
switch flag {
|
||||||
|
case models.SeenFlag:
|
||||||
|
// Note: Inverted properly later
|
||||||
|
tag = "unread"
|
||||||
|
case models.AnsweredFlag:
|
||||||
|
tag = "replied"
|
||||||
|
case models.FlaggedFlag:
|
||||||
|
tag = "flagged"
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("Notmuch doesn't support flag %v", flag)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the current state of the flag.
|
||||||
|
// Note that notmuch handles models.SeenFlag in an inverted sense.
|
||||||
|
oldState := false
|
||||||
tags, err := m.Tags()
|
tags, err := m.Tags()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for _, t := range tags {
|
for _, t := range tags {
|
||||||
if t == "replied" {
|
if t == tag {
|
||||||
haveReplied = true
|
oldState = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if haveReplied == answered {
|
if flag == models.SeenFlag {
|
||||||
// we already have the desired state
|
oldState = !oldState
|
||||||
|
}
|
||||||
|
|
||||||
|
// Skip if flag already in correct state.
|
||||||
|
if oldState == enable {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if haveReplied {
|
if !enable {
|
||||||
err := m.RemoveTag("replied")
|
if flag == models.SeenFlag {
|
||||||
if err != nil {
|
return m.AddTag("unread")
|
||||||
return err
|
} else {
|
||||||
|
return m.RemoveTag(tag)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if flag == models.SeenFlag {
|
||||||
|
return m.RemoveTag("unread")
|
||||||
|
} else {
|
||||||
|
return m.AddTag(tag)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err = m.AddTag("replied")
|
// MarkAnswered either adds or removes the "replied" tag from the message.
|
||||||
if err != nil {
|
func (m *Message) MarkAnswered(answered bool) error {
|
||||||
return err
|
return m.SetOneFlag(models.AnsweredFlag, answered)
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MarkRead either adds or removes the maildir.FlagSeen flag from the message.
|
// MarkRead either adds or removes the maildir.FlagSeen flag from the message.
|
||||||
func (m *Message) MarkRead(seen bool) error {
|
func (m *Message) MarkRead(seen bool) error {
|
||||||
haveUnread := false
|
return m.SetOneFlag(models.SeenFlag, seen)
|
||||||
tags, err := m.Tags()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
for _, t := range tags {
|
|
||||||
if t == "unread" {
|
|
||||||
haveUnread = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (haveUnread && !seen) || (!haveUnread && seen) {
|
|
||||||
// we already have the desired state
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if haveUnread {
|
|
||||||
err := m.RemoveTag("unread")
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
err = m.AddTag("unread")
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// tags returns the notmuch tags of a message
|
// tags returns the notmuch tags of a message
|
||||||
|
|
|
@ -113,8 +113,8 @@ func (w *worker) handleMessage(msg types.WorkerMessage) error {
|
||||||
return w.handleFetchMessageBodyPart(msg)
|
return w.handleFetchMessageBodyPart(msg)
|
||||||
case *types.FetchFullMessages:
|
case *types.FetchFullMessages:
|
||||||
return w.handleFetchFullMessages(msg)
|
return w.handleFetchFullMessages(msg)
|
||||||
case *types.ReadMessages:
|
case *types.FlagMessages:
|
||||||
return w.handleReadMessages(msg)
|
return w.handleFlagMessages(msg)
|
||||||
case *types.AnsweredMessages:
|
case *types.AnsweredMessages:
|
||||||
return w.handleAnsweredMessages(msg)
|
return w.handleAnsweredMessages(msg)
|
||||||
case *types.SearchDirectory:
|
case *types.SearchDirectory:
|
||||||
|
@ -392,7 +392,7 @@ func (w *worker) handleAnsweredMessages(msg *types.AnsweredMessages) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *worker) handleReadMessages(msg *types.ReadMessages) error {
|
func (w *worker) handleFlagMessages(msg *types.FlagMessages) error {
|
||||||
for _, uid := range msg.Uids {
|
for _, uid := range msg.Uids {
|
||||||
m, err := w.msgFromUid(uid)
|
m, err := w.msgFromUid(uid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -400,8 +400,8 @@ func (w *worker) handleReadMessages(msg *types.ReadMessages) error {
|
||||||
w.err(msg, err)
|
w.err(msg, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if err := m.MarkRead(msg.Read); err != nil {
|
if err := m.SetOneFlag(msg.Flag, msg.Enable); err != nil {
|
||||||
w.w.Logger.Printf("could not mark message as read: %v", err)
|
w.w.Logger.Printf("could not set flag %v as %v for message: %v", msg.Flag, msg.Enable, err)
|
||||||
w.err(msg, err)
|
w.err(msg, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
|
@ -113,10 +113,11 @@ type DeleteMessages struct {
|
||||||
Uids []uint32
|
Uids []uint32
|
||||||
}
|
}
|
||||||
|
|
||||||
// Marks messages as read or unread
|
// Flag messages with different mail types
|
||||||
type ReadMessages struct {
|
type FlagMessages struct {
|
||||||
Message
|
Message
|
||||||
Read bool
|
Enable bool
|
||||||
|
Flag models.Flag
|
||||||
Uids []uint32
|
Uids []uint32
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue