2019-06-02 07:15:04 +02:00
|
|
|
package msg
|
2019-03-21 04:23:38 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2020-05-28 16:32:32 +02:00
|
|
|
"time"
|
2019-05-14 22:34:42 +02:00
|
|
|
|
2021-11-05 10:19:46 +01: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
|
|
|
)
|
|
|
|
|
2019-06-27 19:33:11 +02:00
|
|
|
type Delete struct{}
|
|
|
|
|
2019-03-21 04:23:38 +01:00
|
|
|
func init() {
|
2019-06-27 19:33:11 +02:00
|
|
|
register(Delete{})
|
|
|
|
}
|
|
|
|
|
2019-09-03 21:34:03 +02:00
|
|
|
func (Delete) Aliases() []string {
|
2019-06-27 19:33:11 +02:00
|
|
|
return []string{"delete", "delete-message"}
|
|
|
|
}
|
|
|
|
|
2019-09-03 21:34:03 +02:00
|
|
|
func (Delete) Complete(aerc *widgets.Aerc, args []string) []string {
|
2019-06-27 19:33:11 +02:00
|
|
|
return nil
|
2019-03-21 04:23:38 +01:00
|
|
|
}
|
|
|
|
|
2019-09-03 21:34:03 +02:00
|
|
|
func (Delete) Execute(aerc *widgets.Aerc, args []string) error {
|
2019-03-21 04:23:38 +01:00
|
|
|
if len(args) != 1 {
|
2019-05-20 00:23:34 +02:00
|
|
|
return errors.New("Usage: :delete")
|
2019-03-21 04:23:38 +01:00
|
|
|
}
|
2019-06-02 07:15:04 +02: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
|
|
|
}
|
2020-05-09 11:50:30 +02:00
|
|
|
uids, err := h.markedOrSelectedUids()
|
2019-12-18 06:34:04 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-07-14 09:42:24 +02:00
|
|
|
}
|
2019-12-18 06:34:04 +01:00
|
|
|
acct, err := h.account()
|
2019-07-10 02:04:21 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-07-03 03:46:05 +02:00
|
|
|
}
|
2022-08-05 04:43:48 +02:00
|
|
|
sel := store.Selected()
|
2022-08-08 22:21:41 +02:00
|
|
|
marker := store.Marker()
|
|
|
|
marker.ClearVisualMark()
|
2022-07-31 22:16:40 +02:00
|
|
|
// caution, can be nil
|
2022-07-24 04:03:45 +02:00
|
|
|
next := findNextNonDeleted(uids, store)
|
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:
|
2020-05-28 16:32:32 +02:00
|
|
|
aerc.PushStatus("Messages deleted.", 10*time.Second)
|
2022-07-24 04:03:45 +02:00
|
|
|
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)
|
2022-08-12 23:15:43 +02:00
|
|
|
acct.Messages().Select(-1)
|
2022-07-24 04:03:45 +02:00
|
|
|
acct.Messages().Invalidate()
|
|
|
|
return
|
|
|
|
}
|
2022-10-03 23:56:06 +02:00
|
|
|
lib.NewMessageStoreView(next, acct.UiConfig().AutoMarkRead,
|
|
|
|
store, aerc.Crypto, aerc.DecryptKeys,
|
2022-07-24 04:03:45 +02:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
2022-07-24 04:03:46 +02:00
|
|
|
} else {
|
|
|
|
if next == nil {
|
|
|
|
// We deleted the last message, select the new last message
|
|
|
|
// instead of the first message
|
2022-08-12 23:15:43 +02:00
|
|
|
acct.Messages().Select(-1)
|
2022-07-24 04:03:46 +02:00
|
|
|
}
|
2022-07-24 04:03:45 +02:00
|
|
|
}
|
2019-05-14 22:34:42 +02:00
|
|
|
case *types.Error:
|
2022-08-08 22:21:41 +02:00
|
|
|
marker.Remark()
|
2022-08-05 04:43:48 +02:00
|
|
|
store.Select(sel.Uid)
|
2021-01-30 13:51:32 +01:00
|
|
|
aerc.PushError(msg.Error.Error())
|
2020-07-03 08:12:19 +02:00
|
|
|
case *types.Unsupported:
|
2022-08-08 22:21:41 +02:00
|
|
|
marker.Remark()
|
2022-08-05 04:43:48 +02:00
|
|
|
store.Select(sel.Uid)
|
2020-07-03 08:12:19 +02:00
|
|
|
// 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-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 {
|
2022-07-26 11:30:27 +02:00
|
|
|
var next, previous *models.MessageInfo
|
|
|
|
stepper := []func(){store.Next, store.Prev}
|
|
|
|
for _, stepFn := range stepper {
|
2022-08-12 23:15:43 +02:00
|
|
|
previous = nil
|
2022-07-26 11:30:27 +02:00
|
|
|
for {
|
|
|
|
next = store.Selected()
|
|
|
|
if next != nil && !contains(deleted, next.Uid) {
|
2022-08-12 23:15:43 +02:00
|
|
|
if _, deleted := store.Deleted[next.Uid]; !deleted {
|
|
|
|
return next
|
|
|
|
}
|
2022-07-26 11:30:27 +02:00
|
|
|
}
|
|
|
|
if next == nil || previous == next {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
stepFn()
|
|
|
|
previous = next
|
|
|
|
}
|
2019-12-18 06:34:04 +01:00
|
|
|
}
|
2022-03-09 22:48:00 +01:00
|
|
|
|
2022-07-26 11:30:27 +02:00
|
|
|
if next != nil {
|
|
|
|
store.Select(next.Uid)
|
2019-12-18 06:34:04 +01:00
|
|
|
}
|
2022-03-09 22:48:00 +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
|
|
|
|
}
|