| 1 |
package types |
| 2 |
|
| 3 |
import ( |
| 4 |
"cmp" |
| 5 |
"errors" |
| 6 |
"fmt" |
| 7 |
"slices" |
| 8 |
|
| 9 |
"git.sr.ht/~rjarry/aerc/lib/log" |
| 10 |
"git.sr.ht/~rjarry/aerc/models" |
| 11 |
) |
| 12 |
|
| 13 |
type Thread struct { |
| 14 |
Uid models.UID |
| 15 |
Parent *Thread |
| 16 |
PrevSibling *Thread |
| 17 |
NextSibling *Thread |
| 18 |
FirstChild *Thread |
| 19 |
|
| 20 |
Hidden int // if this flag is not zero the message isn't rendered in the UI |
| 21 |
Deleted bool // if this flag is set the message was deleted |
| 22 |
|
| 23 |
// if this flag is set the message is the root of an incomplete thread |
| 24 |
Dummy bool |
| 25 |
|
| 26 |
// Context indicates the message doesn't match the mailbox / query but |
| 27 |
// is displayed for context |
| 28 |
Context bool |
| 29 |
} |
| 30 |
|
| 31 |
// AddChild appends the child node at the end of the existing children of t. |
| 32 |
func (t *Thread) AddChild(child *Thread) { |
| 33 |
t.InsertCmp(child, func(_, _ *Thread) bool { return true }) |
| 34 |
} |
| 35 |
|
| 36 |
// OrderedInsert inserts the child node in ascending order among the existing |
| 37 |
// children based on their respective UIDs. |
| 38 |
func (t *Thread) OrderedInsert(child *Thread) { |
| 39 |
t.InsertCmp(child, func(child, iter *Thread) bool { return child.Uid > iter.Uid }) |
| 40 |
} |
| 41 |
|
| 42 |
// InsertCmp inserts child as a child node into t in ascending order. The |
| 43 |
// ascending order is determined by the bigger function that compares the child |
| 44 |
// with the existing children. It should return true when the child is bigger |
| 45 |
// than the other, and false otherwise. |
| 46 |
func (t *Thread) InsertCmp(child *Thread, bigger func(*Thread, *Thread) bool) { |
| 47 |
if t.FirstChild == nil { |
| 48 |
t.FirstChild = child |
| 49 |
} else { |
| 50 |
start := &Thread{NextSibling: t.FirstChild} |
| 51 |
var iter *Thread |
| 52 |
for iter = start; iter.NextSibling != nil && |
| 53 |
bigger(child, iter.NextSibling); iter = iter.NextSibling { |
| 54 |
} |
| 55 |
child.NextSibling = iter.NextSibling |
| 56 |
iter.NextSibling = child |
| 57 |
t.FirstChild = start.NextSibling |
| 58 |
} |
| 59 |
child.Parent = t |
| 60 |
} |
| 61 |
|
| 62 |
func (t *Thread) Walk(walkFn NewThreadWalkFn) error { |
| 63 |
err := newWalk(t, walkFn, 0, nil) |
| 64 |
if errors.Is(err, ErrSkipThread) { |
| 65 |
return nil |
| 66 |
} |
| 67 |
return err |
| 68 |
} |
| 69 |
|
| 70 |
// Root returns the root thread of the thread tree |
| 71 |
func (t *Thread) Root() *Thread { |
| 72 |
if t == nil { |
| 73 |
return nil |
| 74 |
} |
| 75 |
var iter *Thread |
| 76 |
for iter = t; iter.Parent != nil; iter = iter.Parent { |
| 77 |
} |
| 78 |
return iter |
| 79 |
} |
| 80 |
|
| 81 |
// Uids returns all associated uids for the given thread and its children |
| 82 |
func (t *Thread) Uids() []models.UID { |
| 83 |
if t == nil { |
| 84 |
return nil |
| 85 |
} |
| 86 |
uids := make([]models.UID, 0) |
| 87 |
err := t.Walk(func(node *Thread, _ int, _ error) error { |
| 88 |
uids = append(uids, node.Uid) |
| 89 |
return nil |
| 90 |
}) |
| 91 |
if err != nil { |
| 92 |
log.Errorf("walk to collect uids failed: %v", err) |
| 93 |
} |
| 94 |
return uids |
| 95 |
} |
| 96 |
|
| 97 |
// MaxUid returns the highest UID among the thread and its children |
| 98 |
func (t *Thread) MaxUid() models.UID { |
| 99 |
var max models.UID |
| 100 |
|
| 101 |
_ = t.Walk(func(node *Thread, _ int, currentErr error) error { |
| 102 |
if node.Deleted || node.Hidden > 0 { |
| 103 |
return nil |
| 104 |
} |
| 105 |
if node.Uid > max { |
| 106 |
max = node.Uid |
| 107 |
} |
| 108 |
return nil |
| 109 |
}) |
| 110 |
return max |
| 111 |
} |
| 112 |
|
| 113 |
func (t *Thread) String() string { |
| 114 |
if t == nil { |
| 115 |
return "<nil>" |
| 116 |
} |
| 117 |
var parent models.UID |
| 118 |
if t.Parent != nil { |
| 119 |
parent = t.Parent.Uid |
| 120 |
} |
| 121 |
var next models.UID |
| 122 |
if t.NextSibling != nil { |
| 123 |
next = t.NextSibling.Uid |
| 124 |
} |
| 125 |
var child models.UID |
| 126 |
if t.FirstChild != nil { |
| 127 |
child = t.FirstChild.Uid |
| 128 |
} |
| 129 |
return fmt.Sprintf( |
| 130 |
"[%s] (parent:%s, next:%s, child:%s)", |
| 131 |
t.Uid, parent, next, child, |
| 132 |
) |
| 133 |
} |
| 134 |
|
| 135 |
func newWalk(node *Thread, walkFn NewThreadWalkFn, lvl int, ce error) error { |
| 136 |
if node == nil { |
| 137 |
return nil |
| 138 |
} |
| 139 |
err := walkFn(node, lvl, ce) |
| 140 |
if err != nil { |
| 141 |
return err |
| 142 |
} |
| 143 |
for child := node.FirstChild; child != nil; child = child.NextSibling { |
| 144 |
err = newWalk(child, walkFn, lvl+1, err) |
| 145 |
if errors.Is(err, ErrSkipThread) { |
| 146 |
err = nil |
| 147 |
continue |
| 148 |
} else if err != nil { |
| 149 |
return err |
| 150 |
} |
| 151 |
} |
| 152 |
return nil |
| 153 |
} |
| 154 |
|
| 155 |
var ErrSkipThread = errors.New("skip this Thread") |
| 156 |
|
| 157 |
type NewThreadWalkFn func(t *Thread, level int, currentErr error) error |
| 158 |
|
| 159 |
func getMaxValue(thread *Thread, uidMap map[models.UID]int) int { |
| 160 |
var max int |
| 161 |
|
| 162 |
_ = thread.Walk(func(t *Thread, _ int, currentErr error) error { |
| 163 |
if t.Deleted || t.Hidden > 0 { |
| 164 |
return nil |
| 165 |
} |
| 166 |
if uidMap[t.Uid] > max { |
| 167 |
max = uidMap[t.Uid] |
| 168 |
} |
| 169 |
return nil |
| 170 |
}) |
| 171 |
return max |
| 172 |
} |
| 173 |
|
| 174 |
func SortThreadsBy(toSort []*Thread, sortBy []models.UID) { |
| 175 |
// build a map from sortBy |
| 176 |
uidMap := make(map[models.UID]int) |
| 177 |
for i, uid := range sortBy { |
| 178 |
uidMap[uid] = i |
| 179 |
} |
| 180 |
// sortslice of toSort with less function of indexing the map sortBy |
| 181 |
slices.SortFunc(toSort, func(a, b *Thread) int { |
| 182 |
return cmp.Compare(getMaxValue(a, uidMap), getMaxValue(b, uidMap)) |
| 183 |
}) |
| 184 |
} |