Update DirectoryInfo handling for maildir

This ensures that the directory info is up to date on events in the
maildir worker. This also sets up the initial dirinfo for other
directories and updates them when using built-in commands.

FS events are still only watched for the selected directory. This should
be changed in a future patch to watch other directories too in order to
cover UI updates for folders when an event occurs in a non-selected
folder.
This commit is contained in:
Jeffas 2020-02-28 00:22:35 +00:00 committed by Drew DeVault
parent d6696f34b8
commit 01c96e78df

View file

@ -96,7 +96,7 @@ func (w *Worker) handleFSEvent(ev fsnotify.Event) {
w.worker.PostMessage(&types.DirectoryContents{ w.worker.PostMessage(&types.DirectoryContents{
Uids: sortedUids, Uids: sortedUids,
}, nil) }, nil)
dirInfo := w.getDirectoryInfo() dirInfo := w.getDirectoryInfo(w.selectedName)
dirInfo.Recent = len(newUnseen) dirInfo.Recent = len(newUnseen)
w.worker.PostMessage(&types.DirectoryInfo{ w.worker.PostMessage(&types.DirectoryInfo{
Info: dirInfo, Info: dirInfo,
@ -114,9 +114,9 @@ func (w *Worker) err(msg types.WorkerMessage, err error) {
}, nil) }, nil)
} }
func (w *Worker) getDirectoryInfo() *models.DirectoryInfo { func (w *Worker) getDirectoryInfo(name string) *models.DirectoryInfo {
dirInfo := &models.DirectoryInfo{ dirInfo := &models.DirectoryInfo{
Name: w.selectedName, Name: name,
Flags: []string{}, Flags: []string{},
ReadOnly: false, ReadOnly: false,
// total messages // total messages
@ -125,15 +125,27 @@ func (w *Worker) getDirectoryInfo() *models.DirectoryInfo {
Recent: 0, Recent: 0,
// total unread // total unread
Unseen: 0, Unseen: 0,
AccurateCounts: true,
} }
uids, err := w.c.UIDs(*w.selected)
dir := w.c.Dir(name)
uids, err := w.c.UIDs(dir)
if err != nil { if err != nil {
w.worker.Logger.Printf("could not get uids: %v", err) w.worker.Logger.Printf("could not get uids: %v", err)
return dirInfo return dirInfo
} }
dirInfo.Exists = len(uids) dirInfo.Exists = len(uids)
recent, err := dir.UnseenCount()
if err != nil {
w.worker.Logger.Printf("could not get unseen count: %v", err)
}
dirInfo.Recent = recent
for _, uid := range uids { for _, uid := range uids {
message, err := w.c.Message(*w.selected, uid) message, err := w.c.Message(dir, uid)
if err != nil { if err != nil {
w.worker.Logger.Printf("could not get message: %v", err) w.worker.Logger.Printf("could not get message: %v", err)
continue continue
@ -153,6 +165,7 @@ func (w *Worker) getDirectoryInfo() *models.DirectoryInfo {
dirInfo.Unseen++ dirInfo.Unseen++
} }
} }
dirInfo.Unseen += dirInfo.Recent
return dirInfo return dirInfo
} }
@ -229,6 +242,10 @@ func (w *Worker) handleListDirectories(msg *types.ListDirectories) error {
Attributes: []string{}, Attributes: []string{},
}, },
}, nil) }, nil)
w.worker.PostMessage(&types.DirectoryInfo{
Info: w.getDirectoryInfo(name),
}, nil)
} }
return nil return nil
} }
@ -265,7 +282,7 @@ func (w *Worker) handleOpenDirectory(msg *types.OpenDirectory) error {
// TODO: why does this need to be sent twice?? // TODO: why does this need to be sent twice??
info := &types.DirectoryInfo{ info := &types.DirectoryInfo{
Info: w.getDirectoryInfo(), Info: w.getDirectoryInfo(msg.Directory),
} }
w.worker.PostMessage(info, nil) w.worker.PostMessage(info, nil)
w.worker.PostMessage(info, nil) w.worker.PostMessage(info, nil)
@ -392,6 +409,10 @@ func (w *Worker) handleFetchMessageBodyPart(
Info: info, Info: info,
}, nil) }, nil)
w.worker.PostMessage(&types.DirectoryInfo{
Info: w.getDirectoryInfo(w.selectedName),
}, nil)
return nil return nil
} }
@ -430,6 +451,11 @@ func (w *Worker) handleDeleteMessages(msg *types.DeleteMessages) error {
w.worker.Logger.Printf("error removing some messages: %v", err) w.worker.Logger.Printf("error removing some messages: %v", err)
return err return err
} }
w.worker.PostMessage(&types.DirectoryInfo{
Info: w.getDirectoryInfo(w.selectedName),
}, nil)
return nil return nil
} }
@ -452,10 +478,15 @@ func (w *Worker) handleReadMessages(msg *types.ReadMessages) error {
w.err(msg, err) w.err(msg, err)
continue continue
} }
w.worker.PostMessage(&types.MessageInfo{ w.worker.PostMessage(&types.MessageInfo{
Message: types.RespondTo(msg), Message: types.RespondTo(msg),
Info: info, Info: info,
}, nil) }, nil)
w.worker.PostMessage(&types.DirectoryInfo{
Info: w.getDirectoryInfo(w.selectedName),
}, nil)
} }
return nil return nil
} }
@ -466,6 +497,15 @@ func (w *Worker) handleCopyMessages(msg *types.CopyMessages) error {
if err != nil { if err != nil {
return err return err
} }
w.worker.PostMessage(&types.DirectoryInfo{
Info: w.getDirectoryInfo(w.selectedName),
}, nil)
w.worker.PostMessage(&types.DirectoryInfo{
Info: w.getDirectoryInfo(msg.Destination),
}, nil)
return nil return nil
} }
@ -482,6 +522,10 @@ func (w *Worker) handleAppendMessage(msg *types.AppendMessage) error {
w.worker.Logger.Printf("could not write message to destination: %v", err) w.worker.Logger.Printf("could not write message to destination: %v", err)
return err return err
} }
w.worker.PostMessage(&types.DirectoryInfo{
Info: w.getDirectoryInfo(msg.Destination),
}, nil)
return nil return nil
} }