diff options
author | Jason Cox <me@jasoncarloscox.com> | 2024-01-25 08:25:49 -0500 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2024-01-26 21:36:12 +0100 |
commit | 0aab8ac318f6dee479afba13d09a5d1d1c0baa91 (patch) | |
tree | f33819ffdf9ab21325a6d7b58e5d652c16a927eb /worker/notmuch | |
parent | 936d519a67301bd78a4d737dc47234e3769e639f (diff) | |
download | aerc-0aab8ac318f6dee479afba13d09a5d1d1c0baa91.tar.gz |
notmuch: centralize flag/tag mappings
Mimic other backends that have centralized mappings. Doing so makes
adding support for the draft flag simpler in a subsequent commit.
Signed-off-by: Jason Cox <me@jasoncarloscox.com>
Acked-by: Robin Jarry <robin@jarry.cc>
Diffstat (limited to 'worker/notmuch')
-rw-r--r-- | worker/notmuch/message.go | 30 | ||||
-rw-r--r-- | worker/notmuch/notmuch.go | 24 | ||||
-rw-r--r-- | worker/notmuch/search.go | 15 |
3 files changed, 36 insertions, 33 deletions
diff --git a/worker/notmuch/message.go b/worker/notmuch/message.go index 2beda4d6..849363f1 100644 --- a/worker/notmuch/message.go +++ b/worker/notmuch/message.go @@ -78,21 +78,13 @@ func (m *Message) NewBodyPartReader(requestedParts []int) (io.Reader, error) { // Notmuch doesn't support all the flags, and for those this errors. func (m *Message) SetFlag(flag models.Flags, 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: + tag, ok := flagToTag[flag] + if !ok { 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. + // Note that notmuch handles some flags in an inverted sense oldState := false tags, err := m.Tags() if err != nil { @@ -105,9 +97,7 @@ func (m *Message) SetFlag(flag models.Flags, enable bool) error { } } - if flag == models.SeenFlag { - // Invert the operation since notmuch uses unread tags instead - // of seen tags + if flagToInvert[flag] { enable = !enable } @@ -147,13 +137,11 @@ func (m *Message) ModelFlags() (models.Flags, error) { return 0, err } for _, tag := range tags { - switch tag { - case "replied": - flags |= models.AnsweredFlag - case "flagged": - flags |= models.FlaggedFlag - case "unread": - flags &^= models.SeenFlag + flag := tagToFlag[tag] + if flagToInvert[flag] { + flags &^= flag + } else { + flags |= flag } } return flags, nil diff --git a/worker/notmuch/notmuch.go b/worker/notmuch/notmuch.go new file mode 100644 index 00000000..5610cac0 --- /dev/null +++ b/worker/notmuch/notmuch.go @@ -0,0 +1,24 @@ +//go:build notmuch +// +build notmuch + +package notmuch + +import "git.sr.ht/~rjarry/aerc/models" + +var tagToFlag = map[string]models.Flags{ + "unread": models.SeenFlag, + "replied": models.AnsweredFlag, + "flagged": models.FlaggedFlag, +} + +var flagToTag = map[models.Flags]string{ + models.SeenFlag: "unread", + models.AnsweredFlag: "replied", + models.FlaggedFlag: "flagged", +} + +var flagToInvert = map[models.Flags]bool{ + models.SeenFlag: true, + models.AnsweredFlag: false, + models.FlaggedFlag: false, +} diff --git a/worker/notmuch/search.go b/worker/notmuch/search.go index b3592d41..36d4c2df 100644 --- a/worker/notmuch/search.go +++ b/worker/notmuch/search.go @@ -67,7 +67,7 @@ func translate(crit *types.SearchCriteria) string { } // flags - for _, f := range []models.Flags{models.SeenFlag, models.AnsweredFlag, models.FlaggedFlag} { + for f := range flagToTag { if crit.WithFlags.Has(f) { base.and(getParsedFlag(f, false)) } @@ -100,17 +100,8 @@ func translate(crit *types.SearchCriteria) string { } func getParsedFlag(flag models.Flags, inverse bool) string { - name := "" - switch flag { - case models.AnsweredFlag: - name = "tag:replied" - case models.SeenFlag: - name = "tag:unread" - inverse = !inverse - case models.FlaggedFlag: - name = "tag:flagged" - } - if inverse { + name := "tag:" + flagToTag[flag] + if flagToInvert[flag] { name = "not " + name } return name |