2019-06-09 18:55:34 +00:00
|
|
|
package msg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"time"
|
|
|
|
|
2019-07-15 17:51:07 +00:00
|
|
|
"git.sr.ht/~sircmpwn/getopt"
|
|
|
|
|
2019-06-09 18:55:34 +00:00
|
|
|
"github.com/gdamore/tcell"
|
|
|
|
|
2019-07-15 17:51:07 +00:00
|
|
|
"git.sr.ht/~sircmpwn/aerc/models"
|
2019-06-09 18:55:34 +00:00
|
|
|
"git.sr.ht/~sircmpwn/aerc/widgets"
|
|
|
|
"git.sr.ht/~sircmpwn/aerc/worker/types"
|
|
|
|
)
|
|
|
|
|
2019-06-27 17:33:11 +00:00
|
|
|
type Read struct{}
|
|
|
|
|
2019-06-09 18:55:34 +00:00
|
|
|
func init() {
|
2019-06-27 17:33:11 +00:00
|
|
|
register(Read{})
|
|
|
|
}
|
|
|
|
|
2019-09-03 19:34:03 +00:00
|
|
|
func (Read) Aliases() []string {
|
2019-06-27 17:33:11 +00:00
|
|
|
return []string{"read", "unread"}
|
|
|
|
}
|
|
|
|
|
2019-09-03 19:34:03 +00:00
|
|
|
func (Read) Complete(aerc *widgets.Aerc, args []string) []string {
|
2019-06-27 17:33:11 +00:00
|
|
|
return nil
|
2019-06-09 18:55:34 +00:00
|
|
|
}
|
|
|
|
|
2019-09-03 19:34:03 +00:00
|
|
|
func (Read) Execute(aerc *widgets.Aerc, args []string) error {
|
2019-07-15 17:51:07 +00:00
|
|
|
opts, optind, err := getopt.Getopts(args, "t")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if optind != len(args) {
|
2019-07-23 18:46:32 +00:00
|
|
|
return errors.New("Usage: " + args[0] + " [-t]")
|
2019-07-15 17:51:07 +00:00
|
|
|
}
|
|
|
|
var toggle bool
|
|
|
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
switch opt.Option {
|
|
|
|
case 't':
|
|
|
|
toggle = true
|
|
|
|
}
|
2019-06-09 18:55:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
widget := aerc.SelectedTab().(widgets.ProvidesMessage)
|
2019-07-14 07:42:24 +00:00
|
|
|
store := widget.Store()
|
|
|
|
if store == nil {
|
|
|
|
return errors.New("Cannot perform action. Messages still loading")
|
|
|
|
}
|
2019-07-10 00:04:21 +00:00
|
|
|
msg, err := widget.SelectedMessage()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-15 17:51:07 +00:00
|
|
|
newReadState := true
|
|
|
|
if toggle {
|
|
|
|
newReadState = true
|
|
|
|
for _, flag := range msg.Flags {
|
|
|
|
if flag == models.SeenFlag {
|
|
|
|
newReadState = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if args[0] == "read" {
|
|
|
|
newReadState = true
|
2019-08-06 04:02:21 +00:00
|
|
|
} else if args[0] == "unread" {
|
|
|
|
newReadState = false
|
2019-07-15 17:51:07 +00:00
|
|
|
}
|
|
|
|
store.Read([]uint32{msg.Uid}, newReadState, func(
|
2019-06-09 18:55:34 +00:00
|
|
|
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
|
|
|
|
}
|