diff options
author | ARaspiK <araspik@protonmail.com> | 2020-07-05 14:29:52 +0000 |
---|---|---|
committer | Reto Brunner <reto@labrat.space> | 2020-07-08 09:13:03 +0200 |
commit | 0535f6333f2f5d13469fc315a65c53ff8a5e83f3 (patch) | |
tree | fa3adf96b16d4a0296c04b6a17f5433c03fbc8de /worker/notmuch/message.go | |
parent | fda3f43e7c5e5a175a01dd3e5b8637b1ecb30c51 (diff) | |
download | aerc-0535f6333f2f5d13469fc315a65c53ff8a5e83f3.tar.gz |
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.
Diffstat (limited to 'worker/notmuch/message.go')
-rw-r--r-- | worker/notmuch/message.go | 90 |
1 files changed, 44 insertions, 46 deletions
diff --git a/worker/notmuch/message.go b/worker/notmuch/message.go index 65295073..3d3ed0c3 100644 --- a/worker/notmuch/message.go +++ b/worker/notmuch/message.go @@ -64,70 +64,68 @@ func (m *Message) NewBodyPartReader(requestedParts []int) (io.Reader, error) { return lib.FetchEntityPartReader(msg, requestedParts) } -// MarkAnswered either adds or removes the "replied" tag from the message. -func (m *Message) MarkAnswered(answered bool) error { - haveReplied := false +// SetOneFlag adds or removes a single flag from the message. +// Notmuch doesn't support all the flags, and for those this errors. +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() if err != nil { return err } for _, t := range tags { - if t == "replied" { - haveReplied = true + if t == tag { + oldState = true break } } - if haveReplied == answered { - // we already have the desired state - return nil + if flag == models.SeenFlag { + oldState = !oldState } - if haveReplied { - err := m.RemoveTag("replied") - if err != nil { - return err - } + // Skip if flag already in correct state. + if oldState == enable { return nil } - err = m.AddTag("replied") - if err != nil { - return err + if !enable { + if flag == models.SeenFlag { + return m.AddTag("unread") + } else { + return m.RemoveTag(tag) + } + } else { + if flag == models.SeenFlag { + return m.RemoveTag("unread") + } else { + return m.AddTag(tag) + } } - return nil +} + +// MarkAnswered either adds or removes the "replied" tag from the message. +func (m *Message) MarkAnswered(answered bool) error { + return m.SetOneFlag(models.AnsweredFlag, answered) } // MarkRead either adds or removes the maildir.FlagSeen flag from the message. func (m *Message) MarkRead(seen bool) error { - haveUnread := false - 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 + return m.SetOneFlag(models.SeenFlag, seen) } // tags returns the notmuch tags of a message |