Add :read and :unread commands

This commit is contained in:
Drew DeVault 2019-06-09 14:55:34 -04:00
parent 05f00f0153
commit 6271d455eb
5 changed files with 78 additions and 0 deletions

38
commands/msg/read.go Normal file
View file

@ -0,0 +1,38 @@
package msg
import (
"errors"
"time"
"github.com/gdamore/tcell"
"git.sr.ht/~sircmpwn/aerc/widgets"
"git.sr.ht/~sircmpwn/aerc/worker/types"
)
func init() {
register("read", Read)
register("unread", Read)
}
func Read(aerc *widgets.Aerc, args []string) error {
if len(args) != 1 {
return errors.New("Usage: " + args[0])
}
widget := aerc.SelectedTab().(widgets.ProvidesMessage)
msg := widget.SelectedMessage()
store := widget.Store()
store.Read([]uint32{msg.Uid}, args[0] == "read", func(
msg types.WorkerMessage) {
switch msg := msg.(type) {
case *types.Done:
aerc.PushStatus("Messages updated.", 10*time.Second)
case *types.Error:
aerc.PushStatus(" "+msg.Error.Error(), 10*time.Second).
Color(tcell.ColorDefault, tcell.ColorRed)
}
})
return nil
}

View file

@ -265,3 +265,17 @@ func (store *MessageStore) Move(uids []uint32, dest string, createDest bool,
store.update()
}
func (store *MessageStore) Read(uids []uint32, read bool,
cb func(msg types.WorkerMessage)) {
var set imap.SeqSet
for _, uid := range uids {
set.AddNum(uid)
}
store.worker.PostAction(&types.ReadMessages{
Read: read,
Uids: set,
}, cb)
}

View file

@ -41,3 +41,20 @@ func (imapw *IMAPWorker) handleDeleteMessages(msg *types.DeleteMessages) {
imapw.worker.PostMessage(&types.Done{types.RespondTo(msg)}, nil)
}
}
func (imapw *IMAPWorker) handleReadMessages(msg *types.ReadMessages) {
item := imap.FormatFlagsOp(imap.AddFlags, true)
flags := []interface{}{imap.SeenFlag}
if !msg.Read {
item = imap.FormatFlagsOp(imap.RemoveFlags, true)
flags = []interface{}{imap.SeenFlag}
}
if err := imapw.client.UidStore(&msg.Uids, item, flags, nil); err != nil {
imapw.worker.PostMessage(&types.Error{
Message: types.RespondTo(msg),
Error: err,
}, nil)
return
}
imapw.worker.PostMessage(&types.Done{types.RespondTo(msg)}, nil)
}

View file

@ -137,6 +137,8 @@ func (w *IMAPWorker) handleMessage(msg types.WorkerMessage) error {
w.handleFetchFullMessages(msg)
case *types.DeleteMessages:
w.handleDeleteMessages(msg)
case *types.ReadMessages:
w.handleReadMessages(msg)
case *types.CopyMessages:
w.handleCopyMessages(msg)
case *types.AppendMessage:

View file

@ -108,6 +108,13 @@ type DeleteMessages struct {
Uids imap.SeqSet
}
// Marks messages as read or unread
type ReadMessages struct {
Message
Read bool
Uids imap.SeqSet
}
type CopyMessages struct {
Message
Destination string