| 1 |
package msg |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"slices" |
| 6 |
"strings" |
| 7 |
"sync" |
| 8 |
"time" |
| 9 |
|
| 10 |
"git.sr.ht/~rjarry/aerc/app" |
| 11 |
"git.sr.ht/~rjarry/aerc/commands" |
| 12 |
"git.sr.ht/~rjarry/aerc/lib" |
| 13 |
"git.sr.ht/~rjarry/aerc/lib/log" |
| 14 |
"git.sr.ht/~rjarry/aerc/models" |
| 15 |
"git.sr.ht/~rjarry/aerc/worker/types" |
| 16 |
) |
| 17 |
|
| 18 |
const ( |
| 19 |
ARCHIVE_FLAT = "flat" |
| 20 |
ARCHIVE_YEAR = "year" |
| 21 |
ARCHIVE_MONTH = "month" |
| 22 |
) |
| 23 |
|
| 24 |
var ARCHIVE_TYPES = []string{ARCHIVE_FLAT, ARCHIVE_YEAR, ARCHIVE_MONTH} |
| 25 |
|
| 26 |
type Archive struct { |
| 27 |
MultiFileStrategy *types.MultiFileStrategy `opt:"-m" action:"ParseMFS" complete:"CompleteMFS" desc:"Multi-file strategy."` |
| 28 |
Type string `opt:"type" action:"ParseArchiveType" metavar:"flat|year|month" complete:"CompleteType" desc:"Archiving scheme."` |
| 29 |
} |
| 30 |
|
| 31 |
func (a *Archive) ParseMFS(arg string) error { |
| 32 |
if arg != "" { |
| 33 |
mfs, ok := types.StrToStrategy[arg] |
| 34 |
if !ok { |
| 35 |
return fmt.Errorf("invalid multi-file strategy %s", arg) |
| 36 |
} |
| 37 |
a.MultiFileStrategy = &mfs |
| 38 |
} |
| 39 |
return nil |
| 40 |
} |
| 41 |
|
| 42 |
func (a *Archive) ParseArchiveType(arg string) error { |
| 43 |
if slices.Contains(ARCHIVE_TYPES, arg) { |
| 44 |
a.Type = arg |
| 45 |
return nil |
| 46 |
} |
| 47 |
return fmt.Errorf("invalid archive type") |
| 48 |
} |
| 49 |
|
| 50 |
func init() { |
| 51 |
commands.Register(Archive{}) |
| 52 |
} |
| 53 |
|
| 54 |
func (Archive) Description() string { |
| 55 |
return "Move the selected message to the archive." |
| 56 |
} |
| 57 |
|
| 58 |
func (Archive) Context() commands.CommandContext { |
| 59 |
return commands.MESSAGE_LIST | commands.MESSAGE_VIEWER |
| 60 |
} |
| 61 |
|
| 62 |
func (Archive) Aliases() []string { |
| 63 |
return []string{"archive"} |
| 64 |
} |
| 65 |
|
| 66 |
func (Archive) CompleteMFS(arg string) []string { |
| 67 |
return commands.FilterList(types.StrategyStrs(), arg, nil) |
| 68 |
} |
| 69 |
|
| 70 |
func (*Archive) CompleteType(arg string) []string { |
| 71 |
return commands.FilterList(ARCHIVE_TYPES, arg, nil) |
| 72 |
} |
| 73 |
|
| 74 |
func (a Archive) Execute(args []string) error { |
| 75 |
h := newHelper() |
| 76 |
msgs, err := h.messages() |
| 77 |
if err != nil { |
| 78 |
return err |
| 79 |
} |
| 80 |
acct, err := h.account() |
| 81 |
if err != nil { |
| 82 |
return err |
| 83 |
} |
| 84 |
store, err := h.store() |
| 85 |
if err != nil { |
| 86 |
return err |
| 87 |
} |
| 88 |
return archive(msgs, a.MultiFileStrategy, a.Type, acct, store) |
| 89 |
} |
| 90 |
|
| 91 |
func archive(msgs []*models.MessageInfo, mfs *types.MultiFileStrategy, |
| 92 |
archiveType string, acct *app.AccountView, store *lib.MessageStore, |
| 93 |
) error { |
| 94 |
var uids []models.UID |
| 95 |
for _, msg := range msgs { |
| 96 |
uids = append(uids, msg.Uid) |
| 97 |
} |
| 98 |
archiveDir := acct.AccountConfig().Archive |
| 99 |
marker := store.Marker() |
| 100 |
marker.ClearVisualMark() |
| 101 |
next := findNextNonDeleted(uids, store) |
| 102 |
|
| 103 |
var uidMap map[string][]models.UID |
| 104 |
switch archiveType { |
| 105 |
case ARCHIVE_MONTH: |
| 106 |
uidMap = groupBy(msgs, func(msg *models.MessageInfo) string { |
| 107 |
dir := strings.Join([]string{ |
| 108 |
archiveDir, |
| 109 |
fmt.Sprintf("%d", msg.Envelope.Date.Year()), |
| 110 |
fmt.Sprintf("%02d", msg.Envelope.Date.Month()), |
| 111 |
}, acct.Worker().PathSeparator(), |
| 112 |
) |
| 113 |
return dir |
| 114 |
}) |
| 115 |
case ARCHIVE_YEAR: |
| 116 |
uidMap = groupBy(msgs, func(msg *models.MessageInfo) string { |
| 117 |
dir := strings.Join([]string{ |
| 118 |
archiveDir, |
| 119 |
fmt.Sprintf("%v", msg.Envelope.Date.Year()), |
| 120 |
}, acct.Worker().PathSeparator(), |
| 121 |
) |
| 122 |
return dir |
| 123 |
}) |
| 124 |
case ARCHIVE_FLAT: |
| 125 |
uidMap = make(map[string][]models.UID) |
| 126 |
uidMap[archiveDir] = commands.UidsFromMessageInfos(msgs) |
| 127 |
} |
| 128 |
|
| 129 |
var wg sync.WaitGroup |
| 130 |
wg.Add(len(uidMap)) |
| 131 |
success := true |
| 132 |
|
| 133 |
for dir, uids := range uidMap { |
| 134 |
store.Move(uids, dir, true, mfs, func( |
| 135 |
msg types.WorkerMessage, |
| 136 |
) { |
| 137 |
switch msg := msg.(type) { |
| 138 |
case *types.Done: |
| 139 |
wg.Done() |
| 140 |
case *types.Error: |
| 141 |
app.PushError(msg.Error.Error()) |
| 142 |
success = false |
| 143 |
wg.Done() |
| 144 |
marker.Remark() |
| 145 |
} |
| 146 |
}) |
| 147 |
} |
| 148 |
// we need to do that in the background, else we block the main thread |
| 149 |
go func() { |
| 150 |
defer log.PanicHandler() |
| 151 |
|
| 152 |
wg.Wait() |
| 153 |
if success { |
| 154 |
var s string |
| 155 |
if len(uids) > 1 { |
| 156 |
s = "%d messages archived to %s" |
| 157 |
} else { |
| 158 |
s = "%d message archived to %s" |
| 159 |
} |
| 160 |
app.PushStatus(fmt.Sprintf(s, len(uids), archiveDir), 10*time.Second) |
| 161 |
handleDone(acct, next, store) |
| 162 |
} |
| 163 |
}() |
| 164 |
return nil |
| 165 |
} |
| 166 |
|
| 167 |
func groupBy(msgs []*models.MessageInfo, |
| 168 |
grouper func(*models.MessageInfo) string, |
| 169 |
) map[string][]models.UID { |
| 170 |
m := make(map[string][]models.UID) |
| 171 |
for _, msg := range msgs { |
| 172 |
group := grouper(msg) |
| 173 |
m[group] = append(m[group], msg.Uid) |
| 174 |
} |
| 175 |
return m |
| 176 |
} |