summary refs log tree commit diff stats
path: root/worker
diff options
context:
space:
mode:
authorReto Brunner <reto@labrat.space>2019-09-20 19:11:09 +0200
committerDrew DeVault <sir@cmpwn.com>2019-09-20 15:01:07 -0400
commit3ec9fd216d9e3b38d1d5abb5fba24199185f7054 (patch)
treeb218f169bd29f7ad72bb11290987bd62ec48a6ce /worker
parent36af93b8f44886b5f9b3c75d12950ce27b1b4bfc (diff)
downloadaerc-3ec9fd216d9e3b38d1d5abb5fba24199185f7054.tar.gz
notmuch: add sort functionality
Diffstat (limited to 'worker')
-rw-r--r--worker/notmuch/worker.go47
1 files 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
+}