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:
parent
fbff8cf0ac
commit
c31a5fc33d
2 changed files with 69 additions and 89 deletions
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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{
|
||||
func (w *worker) getDirectoryInfo(name string, query string) *models.DirectoryInfo {
|
||||
dirInfo := &models.DirectoryInfo{
|
||||
Name: name,
|
||||
Flags: []string{},
|
||||
ReadOnly: false,
|
||||
// total messages
|
||||
Exists: count.Exists,
|
||||
Exists: 0,
|
||||
// new messages since mailbox was last opened
|
||||
Recent: 0,
|
||||
// total unread
|
||||
Unseen: count.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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue