archive: adapt to ProvidesMessages

This commit is contained in:
Reto Brunner 2019-12-18 06:34:02 +01:00 committed by Drew DeVault
parent 5da58c669c
commit 884784ac32
1 changed files with 57 additions and 22 deletions

View File

@ -4,10 +4,13 @@ import (
"errors" "errors"
"fmt" "fmt"
"path" "path"
"sync"
"time" "time"
"github.com/gdamore/tcell" "github.com/gdamore/tcell"
"git.sr.ht/~sircmpwn/aerc/commands"
"git.sr.ht/~sircmpwn/aerc/models"
"git.sr.ht/~sircmpwn/aerc/widgets" "git.sr.ht/~sircmpwn/aerc/widgets"
"git.sr.ht/~sircmpwn/aerc/worker/types" "git.sr.ht/~sircmpwn/aerc/worker/types"
) )
@ -36,16 +39,16 @@ func (Archive) Execute(aerc *widgets.Aerc, args []string) error {
if len(args) != 2 { if len(args) != 2 {
return errors.New("Usage: archive <flat|year|month>") return errors.New("Usage: archive <flat|year|month>")
} }
widget := aerc.SelectedTab().(widgets.ProvidesMessage) h := newHelper(aerc)
acct := widget.SelectedAccount() acct, err := h.account()
if acct == nil { if err != nil {
return errors.New("No account selected") return err
} }
store := widget.Store() store, err := h.store()
if store == nil { if err != nil {
return errors.New("Cannot perform action. Messages still loading") return err
} }
msg, err := widget.SelectedMessage() msgs, err := h.messages()
if err != nil { if err != nil {
return err return err
} }
@ -53,28 +56,60 @@ func (Archive) Execute(aerc *widgets.Aerc, args []string) error {
store.Next() store.Next()
acct.Messages().Scroll() acct.Messages().Scroll()
var uidMap map[string][]uint32
switch args[1] { switch args[1] {
case ARCHIVE_MONTH: case ARCHIVE_MONTH:
archiveDir = path.Join(archiveDir, uidMap = groupBy(msgs, func(msg *models.MessageInfo) string {
fmt.Sprintf("%d", msg.Envelope.Date.Year()), dir := path.Join(archiveDir,
fmt.Sprintf("%02d", msg.Envelope.Date.Month())) fmt.Sprintf("%d", msg.Envelope.Date.Year()),
fmt.Sprintf("%02d", msg.Envelope.Date.Month()))
return dir
})
case ARCHIVE_YEAR: case ARCHIVE_YEAR:
archiveDir = path.Join(archiveDir, fmt.Sprintf("%v", uidMap = groupBy(msgs, func(msg *models.MessageInfo) string {
msg.Envelope.Date.Year())) dir := path.Join(archiveDir, fmt.Sprintf("%v",
msg.Envelope.Date.Year()))
return dir
})
case ARCHIVE_FLAT: case ARCHIVE_FLAT:
// deliberately left blank uidMap = make(map[string][]uint32)
uidMap[archiveDir] = commands.UidsFromMessageInfos(msgs)
} }
store.Move([]uint32{msg.Uid}, archiveDir, true, func( var wg sync.WaitGroup
msg types.WorkerMessage) { wg.Add(len(uidMap))
success := true
switch msg := msg.(type) { for dir, uids := range uidMap {
case *types.Done: store.Move(uids, dir, true, func(
msg types.WorkerMessage) {
switch msg := msg.(type) {
case *types.Done:
wg.Done()
case *types.Error:
aerc.PushStatus(" "+msg.Error.Error(), 10*time.Second).
Color(tcell.ColorDefault, tcell.ColorRed)
success = false
wg.Done()
}
})
}
// we need to do that in the background, else we block the main thread
go func() {
wg.Wait()
if success {
aerc.PushStatus("Messages archived.", 10*time.Second) aerc.PushStatus("Messages archived.", 10*time.Second)
case *types.Error:
aerc.PushStatus(" "+msg.Error.Error(), 10*time.Second).
Color(tcell.ColorDefault, tcell.ColorRed)
} }
}) }()
return nil return nil
} }
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
}