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>
This commit is contained in:
Julian Pidancet 2022-10-26 22:29:06 +02:00 committed by Robin Jarry
parent fbff8cf0ac
commit c31a5fc33d
2 changed files with 69 additions and 89 deletions

View File

@ -8,6 +8,7 @@ import (
"strconv" "strconv"
"git.sr.ht/~rjarry/aerc/logging" "git.sr.ht/~rjarry/aerc/logging"
"git.sr.ht/~rjarry/aerc/worker/types"
) )
func (w *worker) handleNotmuchEvent(et eventType) error { func (w *worker) handleNotmuchEvent(et eventType) error {
@ -28,22 +29,18 @@ func (w *worker) handleUpdateDirCounts(ev eventType) error {
} }
for name := range folders { for name := range folders {
query := fmt.Sprintf("folder:%s", strconv.Quote(name)) query := fmt.Sprintf("folder:%s", strconv.Quote(name))
info, err := w.buildDirInfo(name, query, true) w.w.PostMessage(&types.DirectoryInfo{
if err != nil { Info: w.getDirectoryInfo(name, query),
logging.Errorf("could not gather DirectoryInfo: %v", err) SkipSort: true,
continue }, nil)
}
w.w.PostMessage(info, nil)
} }
} }
for name, query := range w.nameQueryMap { for name, query := range w.nameQueryMap {
info, err := w.buildDirInfo(name, query, true) w.w.PostMessage(&types.DirectoryInfo{
if err != nil { Info: w.getDirectoryInfo(name, query),
logging.Errorf("could not gather DirectoryInfo: %v", err) SkipSort: true,
continue }, nil)
}
w.w.PostMessage(info, nil)
} }
return nil return nil
} }

View File

@ -245,83 +245,66 @@ func (w *worker) handleListDirectories(msg *types.ListDirectories) error {
return nil return nil
} }
func (w *worker) gatherDirectoryInfo(name string, query string) ( func (w *worker) getDirectoryInfo(name string, query string) *models.DirectoryInfo {
*types.DirectoryInfo, error, dirInfo := &models.DirectoryInfo{
) { Name: name,
return w.buildDirInfo(name, query, false) Flags: []string{},
} ReadOnly: false,
// total messages
Exists: 0,
// new messages since mailbox was last opened
Recent: 0,
// total unread
Unseen: 0,
AccurateCounts: true,
func (w *worker) buildDirInfo(name string, query string, skipSort bool) ( Caps: &models.Capabilities{
*types.DirectoryInfo, error, Sort: true,
) { Thread: true,
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,
},
}, },
} }
return info, nil
}
func (w *worker) emitDirectoryInfo(name string) error { count, err := w.db.QueryCountMessages(query)
query, _ := w.queryFromName(name)
info, err := w.gatherDirectoryInfo(name, query)
if err != nil { if err != nil {
return err return dirInfo
} }
w.w.PostMessage(info, nil) dirInfo.Exists = count.Exists
return nil dirInfo.Unseen = count.Unread
}
// queryFromName either returns the friendly ID if aliased or the name itself return dirInfo
// 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
} }
func (w *worker) handleOpenDirectory(msg *types.OpenDirectory) error { func (w *worker) handleOpenDirectory(msg *types.OpenDirectory) error {
logging.Infof("opening %s", msg.Directory) logging.Infof("opening %s", msg.Directory)
// try the friendly name first, if that fails assume it's a query
var isQuery bool var isDynamicFolder bool
w.query, isQuery = w.queryFromName(msg.Directory) q := ""
w.currentQueryName = msg.Directory if w.store != nil {
info, err := w.gatherDirectoryInfo(msg.Directory, w.query) folders, _ := w.store.FolderMap()
if err != nil { if _, ok := folders[msg.Directory]; ok {
return err q = fmt.Sprintf("folder:%s", strconv.Quote(msg.Directory))
}
} }
info.Message = types.RespondTo(msg) if q == "" {
w.w.PostMessage(info, nil) var ok bool
if isQuery { q, ok = w.nameQueryMap[msg.Directory]
w.w.PostMessage(info, nil) 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) w.done(msg)
return nil return nil
@ -475,9 +458,9 @@ func (w *worker) handleAnsweredMessages(msg *types.AnsweredMessages) error {
continue continue
} }
} }
if err := w.emitDirectoryInfo(w.currentQueryName); err != nil { w.w.PostMessage(&types.DirectoryInfo{
logging.Errorf("could not emit directory info: %v", err) Info: w.getDirectoryInfo(w.currentQueryName, w.query),
} }, nil)
w.done(msg) w.done(msg)
return nil return nil
} }
@ -502,9 +485,9 @@ func (w *worker) handleFlagMessages(msg *types.FlagMessages) error {
continue continue
} }
} }
if err := w.emitDirectoryInfo(w.currentQueryName); err != nil { w.w.PostMessage(&types.DirectoryInfo{
logging.Errorf("could not emit directory info: %v", err) Info: w.getDirectoryInfo(w.currentQueryName, w.query),
} }, nil)
w.done(msg) w.done(msg)
return nil return nil
} }
@ -551,9 +534,9 @@ func (w *worker) handleModifyLabels(msg *types.ModifyLabels) error {
} }
// and update the list of possible tags // and update the list of possible tags
w.emitLabelList() w.emitLabelList()
if err = w.emitDirectoryInfo(w.currentQueryName); err != nil { w.w.PostMessage(&types.DirectoryInfo{
logging.Errorf("could not emit directory info: %v", err) Info: w.getDirectoryInfo(w.currentQueryName, w.query),
} }, nil)
w.done(msg) w.done(msg)
return nil return nil
} }
@ -879,9 +862,9 @@ func (w *worker) handleAppendMessage(msg *types.AppendMessage) error {
if _, err := w.db.IndexFile(filename); err != nil { if _, err := w.db.IndexFile(filename); err != nil {
return err return err
} }
if err := w.emitDirectoryInfo(w.currentQueryName); err != nil { w.w.PostMessage(&types.DirectoryInfo{
logging.Errorf("could not emit directory info: %v", err) Info: w.getDirectoryInfo(w.currentQueryName, w.query),
} }, nil)
w.done(msg) w.done(msg)
return nil return nil
} }