diff options
Diffstat (limited to 'worker/lib')
-rw-r--r-- | worker/lib/maildir.go | 16 | ||||
-rw-r--r-- | worker/lib/parse.go | 2 | ||||
-rw-r--r-- | worker/lib/parse_test.go | 6 | ||||
-rw-r--r-- | worker/lib/search.go | 49 | ||||
-rw-r--r-- | worker/lib/sort.go | 10 |
5 files changed, 31 insertions, 52 deletions
diff --git a/worker/lib/maildir.go b/worker/lib/maildir.go index f3fe9415..43d3b820 100644 --- a/worker/lib/maildir.go +++ b/worker/lib/maildir.go @@ -113,7 +113,7 @@ func StripUIDFromMessageFilename(basename string) string { return uidReg.ReplaceAllString(basename, "") } -var MaildirToFlag = map[maildir.Flag]models.Flag{ +var MaildirToFlag = map[maildir.Flag]models.Flags{ maildir.FlagReplied: models.AnsweredFlag, maildir.FlagSeen: models.SeenFlag, maildir.FlagTrashed: models.DeletedFlag, @@ -122,7 +122,7 @@ var MaildirToFlag = map[maildir.Flag]models.Flag{ // maildir.FlagPassed Flag = 'P' } -var FlagToMaildir = map[models.Flag]maildir.Flag{ +var FlagToMaildir = map[models.Flags]maildir.Flag{ models.AnsweredFlag: maildir.FlagReplied, models.SeenFlag: maildir.FlagSeen, models.DeletedFlag: maildir.FlagTrashed, @@ -131,20 +131,20 @@ var FlagToMaildir = map[models.Flag]maildir.Flag{ // maildir.FlagPassed Flag = 'P' } -func FromMaildirFlags(maildirFlags []maildir.Flag) []models.Flag { - var flags []models.Flag +func FromMaildirFlags(maildirFlags []maildir.Flag) models.Flags { + var flags models.Flags for _, maildirFlag := range maildirFlags { if flag, ok := MaildirToFlag[maildirFlag]; ok { - flags = append(flags, flag) + flags |= flag } } return flags } -func ToMaildirFlags(flags []models.Flag) []maildir.Flag { +func ToMaildirFlags(flags models.Flags) []maildir.Flag { var maildirFlags []maildir.Flag - for _, flag := range flags { - if maildirFlag, ok := FlagToMaildir[flag]; ok { + for flag, maildirFlag := range FlagToMaildir { + if flags.Has(flag) { maildirFlags = append(maildirFlags, maildirFlag) } } diff --git a/worker/lib/parse.go b/worker/lib/parse.go index 67955c8f..89e79684 100644 --- a/worker/lib/parse.go +++ b/worker/lib/parse.go @@ -254,7 +254,7 @@ func parseAddressList(h *mail.Header, key string) ([]*mail.Address, error) { // RawMessage is an interface that describes a raw message type RawMessage interface { NewReader() (io.ReadCloser, error) - ModelFlags() ([]models.Flag, error) + ModelFlags() (models.Flags, error) Labels() ([]string, error) UID() uint32 } diff --git a/worker/lib/parse_test.go b/worker/lib/parse_test.go index 227b121e..4bf95431 100644 --- a/worker/lib/parse_test.go +++ b/worker/lib/parse_test.go @@ -72,9 +72,9 @@ func newMockRawMessageFromPath(p string) *mockRawMessage { func (m *mockRawMessage) NewReader() (io.ReadCloser, error) { return os.Open(m.path) } -func (m *mockRawMessage) ModelFlags() ([]models.Flag, error) { return nil, nil } -func (m *mockRawMessage) Labels() ([]string, error) { return nil, nil } -func (m *mockRawMessage) UID() uint32 { return 0 } +func (m *mockRawMessage) ModelFlags() (models.Flags, error) { return 0, nil } +func (m *mockRawMessage) Labels() ([]string, error) { return nil, nil } +func (m *mockRawMessage) UID() uint32 { return 0 } func die(err error) { if err != nil { diff --git a/worker/lib/search.go b/worker/lib/search.go index c09feff6..0a124e3c 100644 --- a/worker/lib/search.go +++ b/worker/lib/search.go @@ -18,8 +18,8 @@ type searchCriteria struct { Body []string Text []string - WithFlags []models.Flag - WithoutFlags []models.Flag + WithFlags models.Flags + WithoutFlags models.Flags startDate, endDate time.Time } @@ -36,13 +36,13 @@ func GetSearchCriteria(args []string) (*searchCriteria, error) { for _, opt := range opts { switch opt.Option { case 'r': - criteria.WithFlags = append(criteria.WithFlags, models.SeenFlag) + criteria.WithFlags |= models.SeenFlag case 'u': - criteria.WithoutFlags = append(criteria.WithoutFlags, models.SeenFlag) + criteria.WithoutFlags |= models.SeenFlag case 'x': - criteria.WithFlags = append(criteria.WithFlags, getParsedFlag(opt.Value)) + criteria.WithFlags |= getParsedFlag(opt.Value) case 'X': - criteria.WithoutFlags = append(criteria.WithoutFlags, getParsedFlag(opt.Value)) + criteria.WithoutFlags |= getParsedFlag(opt.Value) case 'H': // TODO case 'f': @@ -80,8 +80,8 @@ func GetSearchCriteria(args []string) (*searchCriteria, error) { return criteria, nil } -func getParsedFlag(name string) models.Flag { - var f models.Flag +func getParsedFlag(name string) models.Flags { + var f models.Flags switch strings.ToLower(name) { case "seen": f = models.SeenFlag @@ -117,7 +117,7 @@ func searchMessage(message RawMessage, criteria *searchCriteria, // setup parts of the message to use in the search // this is so that we try to minimise reading unnecessary parts var ( - flags []models.Flag + flags models.Flags header *models.MessageInfo body string all string @@ -188,18 +188,14 @@ func searchMessage(message RawMessage, criteria *searchCriteria, } } } - if criteria.WithFlags != nil { - for _, searchFlag := range criteria.WithFlags { - if !containsFlag(flags, searchFlag) { - return false, nil - } + if criteria.WithFlags != 0 { + if !flags.Has(criteria.WithFlags) { + return false, nil } } - if criteria.WithoutFlags != nil { - for _, searchFlag := range criteria.WithoutFlags { - if containsFlag(flags, searchFlag) { - return false, nil - } + if criteria.WithoutFlags != 0 { + if flags.Has(criteria.WithoutFlags) { + return false, nil } } if parts&DATE > 0 { @@ -221,17 +217,6 @@ func searchMessage(message RawMessage, criteria *searchCriteria, return true, nil } -// containsFlag returns true if searchFlag appears in flags -func containsFlag(flags []models.Flag, searchFlag models.Flag) bool { - match := false - for _, flag := range flags { - if searchFlag == flag { - match = true - } - } - return match -} - // containsSmartCase is a smarter version of strings.Contains for searching. // Is case-insensitive unless substr contains an upper case character func containsSmartCase(s string, substr string) bool { @@ -278,10 +263,10 @@ func getRequiredParts(criteria *searchCriteria) MsgParts { if criteria.Text != nil && len(criteria.Text) > 0 { required |= ALL } - if criteria.WithFlags != nil && len(criteria.WithFlags) > 0 { + if criteria.WithFlags != 0 { required |= FLAGS } - if criteria.WithoutFlags != nil && len(criteria.WithoutFlags) > 0 { + if criteria.WithoutFlags != 0 { required |= FLAGS } diff --git a/worker/lib/sort.go b/worker/lib/sort.go index 1a1bb47f..2828999c 100644 --- a/worker/lib/sort.go +++ b/worker/lib/sort.go @@ -90,18 +90,12 @@ func sortAddresses(messageInfos []*models.MessageInfo, criterion *types.SortCrit } func sortFlags(messageInfos []*models.MessageInfo, criterion *types.SortCriterion, - testFlag models.Flag, + testFlag models.Flags, ) { var slice []*boolStore for _, msgInfo := range messageInfos { - flagPresent := false - for _, flag := range msgInfo.Flags { - if flag == testFlag { - flagPresent = true - } - } slice = append(slice, &boolStore{ - Value: flagPresent, + Value: msgInfo.Flags.Has(testFlag), MsgInfo: msgInfo, }) } |