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
|
|
|
|
2019-12-18 06:34:02 +01:00
|
|
|
"git.sr.ht/~sircmpwn/aerc/commands"
|
|
|
|
"git.sr.ht/~sircmpwn/aerc/models"
|
2019-06-08 19:41:56 +02:00
|
|
|
"git.sr.ht/~sircmpwn/aerc/widgets"
|
|
|
|
"git.sr.ht/~sircmpwn/aerc/worker/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
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"}
|
|
|
|
return commands.CompletionFromList(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
|
2019-06-11 07:05:55 +02:00
|
|
|
store.Next()
|
2020-06-09 21:13:13 +02:00
|
|
|
acct.Messages().Invalidate()
|
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(
|
|
|
|
msg types.WorkerMessage) {
|
|
|
|
switch msg := msg.(type) {
|
|
|
|
case *types.Done:
|
|
|
|
wg.Done()
|
|
|
|
case *types.Error:
|
2020-05-28 16:32:42 +02:00
|
|
|
aerc.PushError(" " + msg.Error.Error())
|
2019-12-18 06:34:02 +01:00
|
|
|
success = false
|
|
|
|
wg.Done()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
// we need to do that in the background, else we block the main thread
|
|
|
|
go func() {
|
|
|
|
wg.Wait()
|
|
|
|
if success {
|
2020-05-28 16:32:32 +02:00
|
|
|
aerc.PushStatus("Messages archived.", 10*time.Second)
|
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,
|
|
|
|
grouper func(*models.MessageInfo) string) map[string][]uint32 {
|
|
|
|
m := make(map[string][]uint32)
|
|
|
|
for _, msg := range msgs {
|
|
|
|
group := grouper(msg)
|
|
|
|
m[group] = append(m[group], msg.Uid)
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|