threading: honor user-defined sort criteria

Apply the user-defined sort criteria to the message with the highest
uid in a threaded discussion. Restore the default sort order when
leaving threading mode.

Commit 7811620eb8 ("threading: implement on-the-fly message
threading") introduced message threading with the threaded messages
being only sorted by their message uids irrespective of the defined sorting
criteria. It did not restore the default sort order either.

Reported-by: Sebastien Binet <s@sbinet.org>
Signed-off-by: Koni Marti <koni.marti@gmail.com>
Acked-by: Robin Jarry <robin@jarry.cc>
This commit is contained in:
Koni Marti 2022-03-08 18:13:39 +01:00 committed by Robin Jarry
parent cc172970a0
commit 65ae87a524
3 changed files with 70 additions and 50 deletions

View file

@ -2,7 +2,6 @@ package lib
import (
"io"
gosort "sort"
"time"
"git.sr.ht/~rjarry/aerc/lib/sort"
@ -339,8 +338,6 @@ func (store *MessageStore) SetBuildThreads(buildThreads bool) {
store.buildThreads = buildThreads
if store.BuildThreads() {
store.runThreadBuilder()
} else {
store.rebuildUids()
}
}
@ -354,46 +351,18 @@ func (store *MessageStore) BuildThreads() bool {
func (store *MessageStore) runThreadBuilder() {
if store.builder == nil {
store.builder = NewThreadBuilder(store, store.worker.Logger)
store.builder = NewThreadBuilder(store.worker.Logger)
for _, msg := range store.Messages {
store.builder.Update(msg)
}
}
store.Threads = store.builder.Threads()
store.rebuildUids()
}
func (store *MessageStore) rebuildUids() {
start := time.Now()
uids := make([]uint32, 0, len(store.Uids()))
if store.BuildThreads() {
gosort.Sort(types.ByUID(store.Threads))
for i := len(store.Threads) - 1; i >= 0; i-- {
store.Threads[i].Walk(func(t *types.Thread, level int, currentErr error) error {
uids = append(uids, t.Uid)
return nil
})
}
uidsReversed := make([]uint32, len(uids))
for i := 0; i < len(uids); i++ {
uidsReversed[i] = uids[len(uids)-1-i]
}
uids = uidsReversed
} else {
uids = store.Uids()
gosort.SliceStable(uids, func(i, j int) bool { return uids[i] < uids[j] })
}
var uids []uint32
if store.filter {
store.results = uids
uids = store.results
} else {
store.uids = uids
uids = store.uids
}
elapsed := time.Since(start)
store.worker.Logger.Println("Store: Rebuilding UIDs took", elapsed)
store.Threads = store.builder.Threads(uids)
}
func (store *MessageStore) Delete(uids []uint32,
@ -472,6 +441,13 @@ func (store *MessageStore) Answered(uids []uint32, answered bool,
}
func (store *MessageStore) Uids() []uint32 {
if store.BuildThreads() && store.builder != nil {
if uids := store.builder.Uids(); len(uids) > 0 {
return uids
}
}
if store.filter {
return store.results
}