aboutsummaryrefslogtreecommitdiffstats
path: root/commands/msg/delete.go
blob: d90132aac934e3b2ec62a4f5d367b2a63b3bea9a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package msg

import (
	"errors"
	"time"

	"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"
)

type Delete struct{}

func init() {
	register(Delete{})
}

func (Delete) Aliases() []string {
	return []string{"delete", "delete-message"}
}

func (Delete) Complete(aerc *widgets.Aerc, args []string) []string {
	return nil
}

func (Delete) Execute(aerc *widgets.Aerc, args []string) error {
	if len(args) != 1 {
		return errors.New("Usage: :delete")
	}

	h := newHelper(aerc)
	store, err := h.store()
	if err != nil {
		return err
	}
	uids, err := h.markedOrSelectedUids()
	if err != nil {
		return err
	}
	acct, err := h.account()
	if err != nil {
		return err
	}
	//caution, can be nil
	next := findNextNonDeleted(uids, store)
	store.ClearVisualMark()
	store.Delete(uids, func(msg types.WorkerMessage) {
		switch msg := msg.(type) {
		case *types.Done:
			aerc.PushStatus("Messages deleted.", 10*time.Second)
			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().Select(0)
						acct.Messages().Invalidate()
						return
					}
					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)
						})
				}
			} else {
				if next == nil {
					// We deleted the last message, select the new last message
					// instead of the first message
					acct.Messages().Select(0)
				}
			}
			acct.Messages().Invalidate()
		case *types.Error:
			store.Remark()
			aerc.PushError(msg.Error.Error())
		case *types.Unsupported:
			store.Remark()
			// notmuch doesn't support it, we want the user to know
			aerc.PushError(" error, unsupported for this worker")
		}
	})
	return nil
}

func findNextNonDeleted(deleted []uint32, store *lib.MessageStore) *models.MessageInfo {
	var next, previous *models.MessageInfo
	stepper := []func(){store.Next, store.Prev}
	for _, stepFn := range stepper {
		for {
			next = store.Selected()
			if next != nil && !contains(deleted, next.Uid) {
				return next
			}
			if next == nil || previous == next {
				break
			}
			stepFn()
			previous = next
		}
	}

	if next != nil {
		store.Select(next.Uid)
	}
	return next
}

func contains(uids []uint32, uid uint32) bool {
	for _, item := range uids {
		if item == uid {
			return true
		}
	}
	return false
}