diff options
author | Julian Pidancet <julian.pidancet@oracle.com> | 2022-10-26 22:29:06 +0200 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2022-10-27 21:45:31 +0200 |
commit | c31a5fc33dbf807a4514722785ebb70b0171c736 (patch) | |
tree | 9fbc3fd675012a293ed21d41384bcfe82f0f58dd | |
parent | fbff8cf0ac577d5e7d981b54efd2919212fd1db1 (diff) | |
download | aerc-c31a5fc33dbf807a4514722785ebb70b0171c736.tar.gz |
notmuch: simplify DirectoryInfo emitting logic
Refactor the code emitting DirectoryInfo messages. Reduce function
call indirections by retiring gatherDirectoryInfo(), buildDirInfo() and
emitDirectoryInfo(), and replacing them with getDirectoryInfo()
(aligning the code with what is done in the maildir worker by the same
occasion).
Also merge queryFromName(), which no longer needs to be called from
different places, in handleOpenDirectory().
Signed-off-by: Julian Pidancet <julian.pidancet@oracle.com>
Acked-by: Robin Jarry <robin@jarry.cc>
Acked-by: Tim Culverhouse <tim@timculverhouse.com>
-rw-r--r-- | worker/notmuch/eventhandlers.go | 21 | ||||
-rw-r--r-- | worker/notmuch/worker.go | 139 |
2 files changed, 70 insertions, 90 deletions
diff --git a/worker/notmuch/eventhandlers.go b/worker/notmuch/eventhandlers.go index ab180467..38875060 100644 --- a/worker/notmuch/eventhandlers.go +++ b/worker/notmuch/eventhandlers.go @@ -8,6 +8,7 @@ import ( "strconv" "git.sr.ht/~rjarry/aerc/logging" + "git.sr.ht/~rjarry/aerc/worker/types" ) func (w *worker) handleNotmuchEvent(et eventType) error { @@ -28,22 +29,18 @@ func (w *worker) handleUpdateDirCounts(ev eventType) error { } for name := range folders { query := fmt.Sprintf("folder:%s", strconv.Quote(name)) - info, err := w.buildDirInfo(name, query, true) - if err != nil { - logging.Errorf("could not gather DirectoryInfo: %v", err) - continue - } - w.w.PostMessage(info, nil) + w.w.PostMessage(&types.DirectoryInfo{ + Info: w.getDirectoryInfo(name, query), + SkipSort: true, + }, nil) } } for name, query := range w.nameQueryMap { - info, err := w.buildDirInfo(name, query, true) - if err != nil { - logging.Errorf("could not gather DirectoryInfo: %v", err) - continue - } - w.w.PostMessage(info, nil) + w.w.PostMessage(&types.DirectoryInfo{ + Info: w.getDirectoryInfo(name, query), + SkipSort: true, + }, nil) } return nil } diff --git a/worker/notmuch/worker.go b/worker/notmuch/worker.go index 210291bc..b592e947 100644 --- a/worker/notmuch/worker.go +++ b/worker/notmuch/worker.go @@ -245,83 +245,66 @@ func (w *worker) handleListDirectories(msg *types.ListDirectories) error { return nil } -func (w *worker) gatherDirectoryInfo(name string, query string) ( - *types.DirectoryInfo, error, -) { - return w.buildDirInfo(name, query, false) -} - -func (w *worker) buildDirInfo(name string, query string, skipSort bool) ( - *types.DirectoryInfo, error, -) { - count, err := w.db.QueryCountMessages(query) - if err != nil { - return nil, err - } - info := &types.DirectoryInfo{ - SkipSort: skipSort, - Info: &models.DirectoryInfo{ - Name: name, - Flags: []string{}, - ReadOnly: false, - // total messages - Exists: count.Exists, - // new messages since mailbox was last opened - Recent: 0, - // total unread - Unseen: count.Unread, - AccurateCounts: true, - - Caps: &models.Capabilities{ - Sort: true, - Thread: true, - }, +func (w *worker) getDirectoryInfo(name string, query string) *models.DirectoryInfo { + dirInfo := &models.DirectoryInfo{ + Name: name, + Flags: []string{}, + ReadOnly: false, + // total messages + Exists: 0, + // new messages since mailbox was last opened + Recent: 0, + // total unread + Unseen: 0, + AccurateCounts: true, + + Caps: &models.Capabilities{ + Sort: true, + Thread: true, }, } - return info, nil -} -func (w *worker) emitDirectoryInfo(name string) error { - query, _ := w.queryFromName(name) - info, err := w.gatherDirectoryInfo(name, query) + count, err := w.db.QueryCountMessages(query) if err != nil { - return err + return dirInfo } - w.w.PostMessage(info, nil) - return nil -} + dirInfo.Exists = count.Exists + dirInfo.Unseen = count.Unread -// queryFromName either returns the friendly ID if aliased or the name itself -// assuming it to be the query -func (w *worker) queryFromName(name string) (string, bool) { - // try the friendly name first, if that fails assume it's a query - q, ok := w.nameQueryMap[name] - if !ok { - if w.store != nil { - folders, _ := w.store.FolderMap() - if _, ok := folders[name]; ok { - return fmt.Sprintf("folder:%s", strconv.Quote(name)), true - } - } - return name, true - } - return q, false + return dirInfo } func (w *worker) handleOpenDirectory(msg *types.OpenDirectory) error { logging.Infof("opening %s", msg.Directory) - // try the friendly name first, if that fails assume it's a query - var isQuery bool - w.query, isQuery = w.queryFromName(msg.Directory) - w.currentQueryName = msg.Directory - info, err := w.gatherDirectoryInfo(msg.Directory, w.query) - if err != nil { - return err + + var isDynamicFolder bool + q := "" + if w.store != nil { + folders, _ := w.store.FolderMap() + if _, ok := folders[msg.Directory]; ok { + q = fmt.Sprintf("folder:%s", strconv.Quote(msg.Directory)) + } } - info.Message = types.RespondTo(msg) - w.w.PostMessage(info, nil) - if isQuery { - w.w.PostMessage(info, nil) + if q == "" { + var ok bool + q, ok = w.nameQueryMap[msg.Directory] + if !ok { + q = msg.Directory + isDynamicFolder = true + } + } + w.query = q + w.currentQueryName = msg.Directory + + w.w.PostMessage(&types.DirectoryInfo{ + Info: w.getDirectoryInfo(msg.Directory, w.query), + Message: types.RespondTo(msg), + }, nil) + if isDynamicFolder { + w.w.PostMessage(&types.DirectoryInfo{ + Info: w.getDirectoryInfo(msg.Directory, w.query), + Message: types.RespondTo(msg), + }, nil) } w.done(msg) return nil @@ -475,9 +458,9 @@ func (w *worker) handleAnsweredMessages(msg *types.AnsweredMessages) error { continue } } - if err := w.emitDirectoryInfo(w.currentQueryName); err != nil { - logging.Errorf("could not emit directory info: %v", err) - } + w.w.PostMessage(&types.DirectoryInfo{ + Info: w.getDirectoryInfo(w.currentQueryName, w.query), + }, nil) w.done(msg) return nil } @@ -502,9 +485,9 @@ func (w *worker) handleFlagMessages(msg *types.FlagMessages) error { continue } } - if err := w.emitDirectoryInfo(w.currentQueryName); err != nil { - logging.Errorf("could not emit directory info: %v", err) - } + w.w.PostMessage(&types.DirectoryInfo{ + Info: w.getDirectoryInfo(w.currentQueryName, w.query), + }, nil) w.done(msg) return nil } @@ -551,9 +534,9 @@ func (w *worker) handleModifyLabels(msg *types.ModifyLabels) error { } // and update the list of possible tags w.emitLabelList() - if err = w.emitDirectoryInfo(w.currentQueryName); err != nil { - logging.Errorf("could not emit directory info: %v", err) - } + w.w.PostMessage(&types.DirectoryInfo{ + Info: w.getDirectoryInfo(w.currentQueryName, w.query), + }, nil) w.done(msg) return nil } @@ -879,9 +862,9 @@ func (w *worker) handleAppendMessage(msg *types.AppendMessage) error { if _, err := w.db.IndexFile(filename); err != nil { return err } - if err := w.emitDirectoryInfo(w.currentQueryName); err != nil { - logging.Errorf("could not emit directory info: %v", err) - } + w.w.PostMessage(&types.DirectoryInfo{ + Info: w.getDirectoryInfo(w.currentQueryName, w.query), + }, nil) w.done(msg) return nil } |