From 3ec9fd216d9e3b38d1d5abb5fba24199185f7054 Mon Sep 17 00:00:00 2001 From: Reto Brunner Date: Fri, 20 Sep 2019 19:11:09 +0200 Subject: notmuch: add sort functionality --- worker/notmuch/worker.go | 47 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/worker/notmuch/worker.go b/worker/notmuch/worker.go index 3d48d8c..96adc29 100644 --- a/worker/notmuch/worker.go +++ b/worker/notmuch/worker.go @@ -14,6 +14,7 @@ import ( "git.sr.ht/~sircmpwn/aerc/lib/uidstore" "git.sr.ht/~sircmpwn/aerc/models" "git.sr.ht/~sircmpwn/aerc/worker/handlers" + "git.sr.ht/~sircmpwn/aerc/worker/lib" notmuch "git.sr.ht/~sircmpwn/aerc/worker/notmuch/lib" "git.sr.ht/~sircmpwn/aerc/worker/types" "github.com/mitchellh/go-homedir" @@ -26,11 +27,12 @@ func init() { var errUnsupported = fmt.Errorf("unsupported command") type worker struct { - w *types.Worker - query string - uidStore *uidstore.Store - nameQueryMap map[string]string - db *notmuch.DB + w *types.Worker + query string + uidStore *uidstore.Store + nameQueryMap map[string]string + db *notmuch.DB + currentSortCriteria []*types.SortCriterion } // NewWorker creates a new maildir worker with the provided worker. @@ -183,6 +185,7 @@ func (w *worker) handleOpenDirectory(msg *types.OpenDirectory) error { func (w *worker) handleFetchDirectoryContents( msg *types.FetchDirectoryContents) error { + w.currentSortCriteria = msg.SortCriteria err := w.emitDirectoryContents(msg) if err != nil { return err @@ -409,9 +412,14 @@ func (w *worker) emitDirectoryContents(parent types.WorkerMessage) error { if err != nil { return fmt.Errorf("could not fetch uids: %v", err) } + sortedUids, err := w.sort(uids, w.currentSortCriteria) + if err != nil { + w.w.Logger.Printf("error sorting directory: %v", err) + return err + } w.w.PostMessage(&types.DirectoryContents{ Message: types.RespondTo(parent), - Uids: uids, + Uids: sortedUids, }, nil) return nil } @@ -428,3 +436,30 @@ func (w *worker) emitMessageInfo(m *Message, }, nil) return nil } + +func (w *worker) sort(uids []uint32, + criteria []*types.SortCriterion) ([]uint32, error) { + if len(criteria) == 0 { + return uids, nil + } + var msgInfos []*models.MessageInfo + for _, uid := range uids { + m, err := w.msgFromUid(uid) + if err != nil { + w.w.Logger.Printf("could not get message: %v", err) + continue + } + info, err := m.MessageInfo() + if err != nil { + w.w.Logger.Printf("could not get message info: %v", err) + continue + } + msgInfos = append(msgInfos, info) + } + sortedUids, err := lib.Sort(msgInfos, criteria) + if err != nil { + w.w.Logger.Printf("could not sort the messages: %v", err) + return nil, err + } + return sortedUids, nil +} -- cgit v1.2.3