2019-06-08 19:41:56 +02:00
|
|
|
package msg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"path"
|
2019-12-18 06:34:02 +01:00
|
|
|
"sync"
|
2020-05-28 16:32:32 +02:00
|
|
|
"time"
|
2019-06-08 19:41:56 +02:00
|
|
|
|
2021-11-05 10:19:46 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/commands"
|
2022-08-22 18:20:17 +02:00
|
|
|
"git.sr.ht/~rjarry/aerc/lib"
|
2022-10-07 18:00:31 +02:00
|
|
|
"git.sr.ht/~rjarry/aerc/lib/ui"
|
2022-03-22 09:52:27 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/logging"
|
2021-11-05 10:19:46 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/models"
|
|
|
|
"git.sr.ht/~rjarry/aerc/widgets"
|
|
|
|
"git.sr.ht/~rjarry/aerc/worker/types"
|
2019-06-08 19:41:56 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
ARCHIVE_FLAT = "flat"
|
|
|
|
ARCHIVE_YEAR = "year"
|
|
|
|
ARCHIVE_MONTH = "month"
|
|
|
|
)
|
|
|
|
|
2019-06-27 19:33:11 +02:00
|
|
|
type Archive struct{}
|
|
|
|
|
2019-06-08 19:41:56 +02:00
|
|
|
func init() {
|
2019-06-27 19:33:11 +02:00
|
|
|
register(Archive{})
|
|
|
|
}
|
|
|
|
|
2019-09-03 21:34:03 +02:00
|
|
|
func (Archive) Aliases() []string {
|
2019-06-27 19:33:11 +02:00
|
|
|
return []string{"archive"}
|
|
|
|
}
|
|
|
|
|
2019-09-03 21:34:03 +02:00
|
|
|
func (Archive) Complete(aerc *widgets.Aerc, args []string) []string {
|
2020-04-11 04:12:38 +02:00
|
|
|
valid := []string{"flat", "year", "month"}
|
2022-03-06 03:58:07 +01:00
|
|
|
return commands.CompletionFromList(aerc, valid, args)
|
2019-06-08 19:41:56 +02:00
|
|
|
}
|
|
|
|
|
2019-09-03 21:34:03 +02:00
|
|
|
func (Archive) Execute(aerc *widgets.Aerc, args []string) error {
|
2019-06-08 19:41:56 +02:00
|
|
|
if len(args) != 2 {
|
|
|
|
return errors.New("Usage: archive <flat|year|month>")
|
|
|
|
}
|
2019-12-18 06:34:02 +01:00
|
|
|
h := newHelper(aerc)
|
|
|
|
acct, err := h.account()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-06-08 19:41:56 +02:00
|
|
|
}
|
2019-12-18 06:34:02 +01:00
|
|
|
store, err := h.store()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-07-14 09:42:24 +02:00
|
|
|
}
|
2019-12-18 06:34:02 +01:00
|
|
|
msgs, err := h.messages()
|
2019-07-10 02:04:21 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-06-08 19:41:56 +02:00
|
|
|
archiveDir := acct.AccountConfig().Archive
|
2022-07-31 16:41:21 +02:00
|
|
|
var uids []uint32
|
|
|
|
for _, msg := range msgs {
|
|
|
|
uids = append(uids, msg.Uid)
|
|
|
|
}
|
2022-08-08 22:21:41 +02:00
|
|
|
marker := store.Marker()
|
|
|
|
marker.ClearVisualMark()
|
2022-08-22 18:20:17 +02:00
|
|
|
next := findNextNonDeleted(uids, store)
|
2019-06-08 19:41:56 +02:00
|
|
|
|
2019-12-18 06:34:02 +01:00
|
|
|
var uidMap map[string][]uint32
|
2019-06-08 19:41:56 +02:00
|
|
|
switch args[1] {
|
|
|
|
case ARCHIVE_MONTH:
|
2019-12-18 06:34:02 +01:00
|
|
|
uidMap = groupBy(msgs, func(msg *models.MessageInfo) string {
|
|
|
|
dir := path.Join(archiveDir,
|
|
|
|
fmt.Sprintf("%d", msg.Envelope.Date.Year()),
|
|
|
|
fmt.Sprintf("%02d", msg.Envelope.Date.Month()))
|
|
|
|
return dir
|
|
|
|
})
|
2019-06-08 19:41:56 +02:00
|
|
|
case ARCHIVE_YEAR:
|
2019-12-18 06:34:02 +01:00
|
|
|
uidMap = groupBy(msgs, func(msg *models.MessageInfo) string {
|
|
|
|
dir := path.Join(archiveDir, fmt.Sprintf("%v",
|
|
|
|
msg.Envelope.Date.Year()))
|
|
|
|
return dir
|
|
|
|
})
|
2019-06-08 19:41:56 +02:00
|
|
|
case ARCHIVE_FLAT:
|
2019-12-18 06:34:02 +01:00
|
|
|
uidMap = make(map[string][]uint32)
|
|
|
|
uidMap[archiveDir] = commands.UidsFromMessageInfos(msgs)
|
2019-06-08 19:41:56 +02:00
|
|
|
}
|
|
|
|
|
2019-12-18 06:34:02 +01:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(len(uidMap))
|
|
|
|
success := true
|
2019-06-08 19:41:56 +02:00
|
|
|
|
2019-12-18 06:34:02 +01:00
|
|
|
for dir, uids := range uidMap {
|
|
|
|
store.Move(uids, dir, true, func(
|
2022-07-31 22:16:40 +02:00
|
|
|
msg types.WorkerMessage,
|
|
|
|
) {
|
2019-12-18 06:34:02 +01:00
|
|
|
switch msg := msg.(type) {
|
|
|
|
case *types.Done:
|
|
|
|
wg.Done()
|
|
|
|
case *types.Error:
|
2021-01-30 13:51:32 +01:00
|
|
|
aerc.PushError(msg.Error.Error())
|
2019-12-18 06:34:02 +01:00
|
|
|
success = false
|
|
|
|
wg.Done()
|
2022-08-08 22:21:41 +02:00
|
|
|
marker.Remark()
|
2019-12-18 06:34:02 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
// we need to do that in the background, else we block the main thread
|
|
|
|
go func() {
|
2022-03-22 09:52:27 +01:00
|
|
|
defer logging.PanicHandler()
|
|
|
|
|
2019-12-18 06:34:02 +01:00
|
|
|
wg.Wait()
|
|
|
|
if success {
|
2020-05-28 16:32:32 +02:00
|
|
|
aerc.PushStatus("Messages archived.", 10*time.Second)
|
2022-08-22 18:20:17 +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)
|
|
|
|
acct.Messages().Select(-1)
|
2022-10-07 18:00:31 +02:00
|
|
|
ui.Invalidate()
|
2022-08-22 18:20:17 +02:00
|
|
|
return
|
|
|
|
}
|
2022-10-03 23:56:07 +02:00
|
|
|
lib.NewMessageStoreView(next, mv.MessageView().SeenFlagSet(),
|
2022-10-03 23:56:06 +02:00
|
|
|
store, aerc.Crypto, aerc.DecryptKeys,
|
2022-08-22 18:20:17 +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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if next == nil {
|
|
|
|
// We archived the last message, select the new last message
|
|
|
|
// instead of the first message
|
|
|
|
acct.Messages().Select(-1)
|
|
|
|
}
|
|
|
|
}
|
2019-06-08 19:41:56 +02:00
|
|
|
}
|
2019-12-18 06:34:02 +01:00
|
|
|
}()
|
2019-06-08 19:41:56 +02:00
|
|
|
return nil
|
|
|
|
}
|
2019-12-18 06:34:02 +01:00
|
|
|
|
|
|
|
func groupBy(msgs []*models.MessageInfo,
|
2022-07-31 22:16:40 +02:00
|
|
|
grouper func(*models.MessageInfo) string,
|
|
|
|
) map[string][]uint32 {
|
2019-12-18 06:34:02 +01:00
|
|
|
m := make(map[string][]uint32)
|
|
|
|
for _, msg := range msgs {
|
|
|
|
group := grouper(msg)
|
|
|
|
m[group] = append(m[group], msg.Uid)
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|