2019-09-20 00:37:44 +02:00
|
|
|
package lib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
2021-11-05 10:19:46 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/models"
|
|
|
|
"git.sr.ht/~rjarry/aerc/worker/types"
|
2020-11-10 19:57:09 +01:00
|
|
|
"github.com/emersion/go-message/mail"
|
2019-09-20 00:37:44 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func Sort(messageInfos []*models.MessageInfo,
|
2022-07-31 22:16:40 +02:00
|
|
|
criteria []*types.SortCriterion,
|
|
|
|
) ([]uint32, error) {
|
2019-09-20 00:37:44 +02:00
|
|
|
// loop through in reverse to ensure we sort by non-primary fields first
|
|
|
|
for i := len(criteria) - 1; i >= 0; i-- {
|
|
|
|
criterion := criteria[i]
|
|
|
|
switch criterion.Field {
|
|
|
|
case types.SortArrival:
|
2020-02-29 02:27:31 +01:00
|
|
|
sortSlice(criterion, messageInfos, func(i, j int) bool {
|
|
|
|
return messageInfos[i].InternalDate.Before(messageInfos[j].InternalDate)
|
|
|
|
})
|
2019-09-20 00:37:44 +02:00
|
|
|
case types.SortCc:
|
2020-02-29 02:27:31 +01:00
|
|
|
sortAddresses(messageInfos, criterion,
|
2020-11-10 19:57:09 +01:00
|
|
|
func(msgInfo *models.MessageInfo) []*mail.Address {
|
2019-09-20 00:37:44 +02:00
|
|
|
return msgInfo.Envelope.Cc
|
|
|
|
})
|
|
|
|
case types.SortDate:
|
2020-02-29 02:27:31 +01:00
|
|
|
sortSlice(criterion, messageInfos, func(i, j int) bool {
|
|
|
|
return messageInfos[i].Envelope.Date.Before(messageInfos[j].Envelope.Date)
|
|
|
|
})
|
2019-09-20 00:37:44 +02:00
|
|
|
case types.SortFrom:
|
2020-02-29 02:27:31 +01:00
|
|
|
sortAddresses(messageInfos, criterion,
|
2020-11-10 19:57:09 +01:00
|
|
|
func(msgInfo *models.MessageInfo) []*mail.Address {
|
2019-09-20 00:37:44 +02:00
|
|
|
return msgInfo.Envelope.From
|
|
|
|
})
|
|
|
|
case types.SortRead:
|
2020-02-29 02:27:31 +01:00
|
|
|
sortFlags(messageInfos, criterion, models.SeenFlag)
|
2019-09-20 00:37:44 +02:00
|
|
|
case types.SortSize:
|
2020-02-29 02:27:31 +01:00
|
|
|
sortSlice(criterion, messageInfos, func(i, j int) bool {
|
|
|
|
return messageInfos[i].Size < messageInfos[j].Size
|
|
|
|
})
|
2019-09-20 00:37:44 +02:00
|
|
|
case types.SortSubject:
|
2020-02-29 02:27:31 +01:00
|
|
|
sortStrings(messageInfos, criterion,
|
2019-09-20 00:37:44 +02:00
|
|
|
func(msgInfo *models.MessageInfo) string {
|
|
|
|
subject := strings.ToLower(msgInfo.Envelope.Subject)
|
|
|
|
subject = strings.TrimPrefix(subject, "re: ")
|
|
|
|
return strings.TrimPrefix(subject, "fwd: ")
|
|
|
|
})
|
|
|
|
case types.SortTo:
|
2020-02-29 02:27:31 +01:00
|
|
|
sortAddresses(messageInfos, criterion,
|
2020-11-10 19:57:09 +01:00
|
|
|
func(msgInfo *models.MessageInfo) []*mail.Address {
|
2019-09-20 00:37:44 +02:00
|
|
|
return msgInfo.Envelope.To
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var uids []uint32
|
|
|
|
// copy in reverse as msgList displays backwards
|
|
|
|
for i := len(messageInfos) - 1; i >= 0; i-- {
|
|
|
|
uids = append(uids, messageInfos[i].Uid)
|
|
|
|
}
|
|
|
|
return uids, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func sortAddresses(messageInfos []*models.MessageInfo, criterion *types.SortCriterion,
|
2022-07-31 22:16:40 +02:00
|
|
|
getValue func(*models.MessageInfo) []*mail.Address,
|
|
|
|
) {
|
2020-02-29 02:27:31 +01:00
|
|
|
sortSlice(criterion, messageInfos, func(i, j int) bool {
|
|
|
|
addressI, addressJ := getValue(messageInfos[i]), getValue(messageInfos[j])
|
2020-11-10 19:57:09 +01:00
|
|
|
var firstI, firstJ *mail.Address
|
2020-02-29 02:27:31 +01:00
|
|
|
if len(addressI) > 0 {
|
|
|
|
firstI = addressI[0]
|
|
|
|
}
|
|
|
|
if len(addressJ) > 0 {
|
|
|
|
firstJ = addressJ[0]
|
|
|
|
}
|
2022-07-31 14:32:48 +02:00
|
|
|
if firstI != nil && firstJ != nil {
|
2020-11-10 19:57:09 +01:00
|
|
|
getName := func(addr *mail.Address) string {
|
2020-02-29 02:27:31 +01:00
|
|
|
if addr.Name != "" {
|
|
|
|
return addr.Name
|
|
|
|
} else {
|
2020-08-19 12:01:45 +02:00
|
|
|
return addr.Address
|
2020-02-29 02:27:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return getName(firstI) < getName(firstJ)
|
2022-07-31 14:32:48 +02:00
|
|
|
} else {
|
|
|
|
return firstI != nil && firstJ == nil
|
2020-02-29 02:27:31 +01:00
|
|
|
}
|
|
|
|
})
|
2019-09-20 00:37:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func sortFlags(messageInfos []*models.MessageInfo, criterion *types.SortCriterion,
|
2022-07-31 22:16:40 +02:00
|
|
|
testFlag models.Flag,
|
|
|
|
) {
|
2019-09-20 00:37:44 +02:00
|
|
|
var slice []*boolStore
|
|
|
|
for _, msgInfo := range messageInfos {
|
|
|
|
flagPresent := false
|
|
|
|
for _, flag := range msgInfo.Flags {
|
|
|
|
if flag == testFlag {
|
|
|
|
flagPresent = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
slice = append(slice, &boolStore{
|
|
|
|
Value: flagPresent,
|
|
|
|
MsgInfo: msgInfo,
|
|
|
|
})
|
|
|
|
}
|
2020-02-29 02:27:31 +01:00
|
|
|
sortSlice(criterion, slice, func(i, j int) bool {
|
|
|
|
valI, valJ := slice[i].Value, slice[j].Value
|
|
|
|
return valI && !valJ
|
|
|
|
})
|
2019-09-20 00:37:44 +02:00
|
|
|
for i := 0; i < len(messageInfos); i++ {
|
|
|
|
messageInfos[i] = slice[i].MsgInfo
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func sortStrings(messageInfos []*models.MessageInfo, criterion *types.SortCriterion,
|
2022-07-31 22:16:40 +02:00
|
|
|
getValue func(*models.MessageInfo) string,
|
|
|
|
) {
|
2019-09-20 00:37:44 +02:00
|
|
|
var slice []*lexiStore
|
|
|
|
for _, msgInfo := range messageInfos {
|
|
|
|
slice = append(slice, &lexiStore{
|
|
|
|
Value: getValue(msgInfo),
|
|
|
|
MsgInfo: msgInfo,
|
|
|
|
})
|
|
|
|
}
|
2020-02-29 02:27:31 +01:00
|
|
|
sortSlice(criterion, slice, func(i, j int) bool {
|
|
|
|
return slice[i].Value < slice[j].Value
|
|
|
|
})
|
2019-09-20 00:37:44 +02:00
|
|
|
for i := 0; i < len(messageInfos); i++ {
|
|
|
|
messageInfos[i] = slice[i].MsgInfo
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type lexiStore struct {
|
|
|
|
Value string
|
|
|
|
MsgInfo *models.MessageInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
type boolStore struct {
|
|
|
|
Value bool
|
|
|
|
MsgInfo *models.MessageInfo
|
|
|
|
}
|
|
|
|
|
2020-02-29 02:27:31 +01:00
|
|
|
func sortSlice(criterion *types.SortCriterion, slice interface{}, less func(i, j int) bool) {
|
2019-09-20 00:37:44 +02:00
|
|
|
if criterion.Reverse {
|
2020-02-29 02:27:31 +01:00
|
|
|
sort.SliceStable(slice, func(i, j int) bool {
|
|
|
|
return less(j, i)
|
|
|
|
})
|
2019-09-20 00:37:44 +02:00
|
|
|
} else {
|
2020-02-29 02:27:31 +01:00
|
|
|
sort.SliceStable(slice, less)
|
2019-09-20 00:37:44 +02:00
|
|
|
}
|
|
|
|
}
|