aerc/commands/msg/delete.go

109 lines
2.3 KiB
Go
Raw Normal View History

package msg
2019-03-21 04:23:38 +01:00
import (
"errors"
"time"
2019-05-14 22:34:42 +02:00
"git.sr.ht/~rjarry/aerc/lib"
"git.sr.ht/~rjarry/aerc/models"
"git.sr.ht/~rjarry/aerc/widgets"
"git.sr.ht/~rjarry/aerc/worker/types"
2019-03-21 04:23:38 +01:00
)
type Delete struct{}
2019-03-21 04:23:38 +01:00
func init() {
register(Delete{})
}
func (Delete) Aliases() []string {
return []string{"delete", "delete-message"}
}
func (Delete) Complete(aerc *widgets.Aerc, args []string) []string {
return nil
2019-03-21 04:23:38 +01:00
}
func (Delete) Execute(aerc *widgets.Aerc, args []string) error {
2019-03-21 04:23:38 +01:00
if len(args) != 1 {
return errors.New("Usage: :delete")
2019-03-21 04:23:38 +01:00
}
2019-12-18 06:34:04 +01:00
h := newHelper(aerc)
store, err := h.store()
if err != nil {
return err
2019-03-21 04:23:38 +01:00
}
uids, err := h.markedOrSelectedUids()
2019-12-18 06:34:04 +01:00
if err != nil {
return err
}
2019-12-18 06:34:04 +01:00
acct, err := h.account()
if err != nil {
return err
2019-07-03 03:46:05 +02:00
}
2019-12-18 06:34:04 +01:00
store.Delete(uids, func(msg types.WorkerMessage) {
2019-05-14 22:34:42 +02:00
switch msg := msg.(type) {
case *types.Done:
aerc.PushStatus("Messages deleted.", 10*time.Second)
2019-05-14 22:34:42 +02:00
case *types.Error:
aerc.PushError(msg.Error.Error())
case *types.Unsupported:
// notmuch doesn't support it, we want the user to know
aerc.PushError(" error, unsupported for this worker")
2019-05-14 22:34:42 +02:00
}
})
2019-12-18 06:34:04 +01:00
//caution, can be nil
next := findNextNonDeleted(uids, store)
mv, isMsgView := h.msgProvider.(*widgets.MessageViewer)
if isMsgView {
if !aerc.Config().Ui.NextMessageOnDelete {
aerc.RemoveTab(h.msgProvider)
} else {
// no more messages in the list
if next == nil {
aerc.RemoveTab(h.msgProvider)
acct.Messages().Invalidate()
2019-12-18 06:34:04 +01:00
return nil
}
lib.NewMessageStoreView(next, store, aerc.Crypto, aerc.DecryptKeys,
func(view lib.MessageView, err error) {
if err != nil {
aerc.PushError(err.Error())
return
}
nextMv := widgets.NewMessageViewer(acct, aerc.Config(), view)
aerc.ReplaceTab(mv, nextMv, next.Envelope.Subject)
})
2019-12-18 06:34:04 +01:00
}
}
acct.Messages().Invalidate()
2019-03-21 04:23:38 +01:00
return nil
}
2019-12-18 06:34:04 +01:00
func findNextNonDeleted(deleted []uint32, store *lib.MessageStore) *models.MessageInfo {
selected := store.Selected()
if !contains(deleted, selected.Uid) {
return selected
}
store.Next()
next := store.Selected()
if next == selected || next == nil {
// the last message is in the deleted state or doesn't exist
return nil
2019-12-18 06:34:04 +01:00
}
return next
2019-12-18 06:34:04 +01:00
}
func contains(uids []uint32, uid uint32) bool {
for _, item := range uids {
if item == uid {
return true
}
}
return false
}