about summary refs log tree commit diff stats
path: root/worker/maildir
diff options
context:
space:
mode:
authorJeffas <dev@jeffas.io>2020-02-28 00:22:35 +0000
committerDrew DeVault <sir@cmpwn.com>2020-02-28 09:26:17 -0500
commit01c96e78dfe88056e0e6ff4155354f7a985c9af9 (patch)
tree6eb877576d5a899e014e81cc248e4c3d931a57e7 /worker/maildir
parentd6696f34b83044d4b9e7e714e75b8ff02d54cbc0 (diff)
downloadaerc-01c96e78dfe88056e0e6ff4155354f7a985c9af9.tar.gz
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.
Diffstat (limited to 'worker/maildir')
-rw-r--r--worker/maildir/worker.go56
1 files changed, 50 insertions, 6 deletions
diff --git a/worker/maildir/worker.go b/worker/maildir/worker.go
index 3c15f2c..17123c6 100644
--- a/worker/maildir/worker.go
+++ b/worker/maildir/worker.go
@@ -96,7 +96,7 @@ func (w *Worker) handleFSEvent(ev fsnotify.Event) {
 	w.worker.PostMessage(&types.DirectoryContents{
 		Uids: sortedUids,
 	}, nil)
-	dirInfo := w.getDirectoryInfo()
+	dirInfo := w.getDirectoryInfo(w.selectedName)
 	dirInfo.Recent = len(newUnseen)
 	w.worker.PostMessage(&types.DirectoryInfo{
 		Info: dirInfo,
@@ -114,9 +114,9 @@ func (w *Worker) err(msg types.WorkerMessage, err error) {
 	}, nil)
 }
 
-func (w *Worker) getDirectoryInfo() *models.DirectoryInfo {
+func (w *Worker) getDirectoryInfo(name string) *models.DirectoryInfo {
 	dirInfo := &models.DirectoryInfo{
-		Name:     w.selectedName,
+		Name:     name,
 		Flags:    []string{},
 		ReadOnly: false,
 		// total messages
@@ -125,15 +125,27 @@ func (w *Worker) getDirectoryInfo() *models.DirectoryInfo {
 		Recent: 0,
 		// total unread
 		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 {
 		w.worker.Logger.Printf("could not get uids: %v", err)
 		return dirInfo
 	}
 	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 {
-		message, err := w.c.Message(*w.selected, uid)
+		message, err := w.c.Message(dir, uid)
 		if err != nil {
 			w.worker.Logger.Printf("could not get message: %v", err)
 			continue
@@ -153,6 +165,7 @@ func (w *Worker) getDirectoryInfo() *models.DirectoryInfo {
 			dirInfo.Unseen++
 		}
 	}
+	dirInfo.Unseen += dirInfo.Recent
 	return dirInfo
 }
 
@@ -229,6 +242,10 @@ func (w *Worker) handleListDirectories(msg *types.ListDirectories) error {
 				Attributes: []string{},
 			},
 		}, nil)
+
+		w.worker.PostMessage(&types.DirectoryInfo{
+			Info: w.getDirectoryInfo(name),
+		}, nil)
 	}
 	return nil
 }
@@ -265,7 +282,7 @@ func (w *Worker) handleOpenDirectory(msg *types.OpenDirectory) error {
 
 	// TODO: why does this need to be sent twice??
 	info := &types.DirectoryInfo{
-		Info: w.getDirectoryInfo(),
+		Info: w.getDirectoryInfo(msg.Directory),
 	}
 	w.worker.PostMessage(info, nil)
 	w.worker.PostMessage(info, nil)
@@ -392,6 +409,10 @@ func (w *Worker) handleFetchMessageBodyPart(
 		Info:    info,
 	}, nil)
 
+	w.worker.PostMessage(&types.DirectoryInfo{
+		Info: w.getDirectoryInfo(w.selectedName),
+	}, 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)
 		return err
 	}
+
+	w.worker.PostMessage(&types.DirectoryInfo{
+		Info: w.getDirectoryInfo(w.selectedName),
+	}, nil)
+
 	return nil
 }
 
@@ -452,10 +478,15 @@ func (w *Worker) handleReadMessages(msg *types.ReadMessages) error {
 			w.err(msg, err)
 			continue
 		}
+
 		w.worker.PostMessage(&types.MessageInfo{
 			Message: types.RespondTo(msg),
 			Info:    info,
 		}, nil)
+
+		w.worker.PostMessage(&types.DirectoryInfo{
+			Info: w.getDirectoryInfo(w.selectedName),
+		}, nil)
 	}
 	return nil
 }
@@ -466,6 +497,15 @@ func (w *Worker) handleCopyMessages(msg *types.CopyMessages) error {
 	if err != nil {
 		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
 }
 
@@ -482,6 +522,10 @@ func (w *Worker) handleAppendMessage(msg *types.AppendMessage) error {
 		w.worker.Logger.Printf("could not write message to destination: %v", err)
 		return err
 	}
+
+	w.worker.PostMessage(&types.DirectoryInfo{
+		Info: w.getDirectoryInfo(msg.Destination),
+	}, nil)
 	return nil
 }