2022-02-24 00:41:13 +01:00
|
|
|
package lib
|
|
|
|
|
|
|
|
import (
|
2022-07-26 15:41:13 +02:00
|
|
|
"sync"
|
2022-02-24 00:41:13 +01:00
|
|
|
"time"
|
|
|
|
|
2022-10-20 16:43:41 +02:00
|
|
|
"git.sr.ht/~rjarry/aerc/lib/iterator"
|
2022-07-19 22:31:51 +02:00
|
|
|
"git.sr.ht/~rjarry/aerc/logging"
|
2022-02-24 00:41:13 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/models"
|
|
|
|
"git.sr.ht/~rjarry/aerc/worker/types"
|
|
|
|
"github.com/gatherstars-com/jwz"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ThreadBuilder struct {
|
2022-07-26 15:41:13 +02:00
|
|
|
sync.Mutex
|
2022-02-24 00:41:13 +01:00
|
|
|
threadBlocks map[uint32]jwz.Threadable
|
|
|
|
messageidToUid map[string]uint32
|
|
|
|
seen map[uint32]bool
|
2022-03-08 18:13:39 +01:00
|
|
|
threadedUids []uint32
|
2022-10-20 16:43:41 +02:00
|
|
|
iterFactory iterator.Factory
|
2022-02-24 00:41:13 +01:00
|
|
|
}
|
|
|
|
|
2022-10-20 16:43:41 +02:00
|
|
|
func NewThreadBuilder(i iterator.Factory) *ThreadBuilder {
|
2022-02-24 00:41:13 +01:00
|
|
|
tb := &ThreadBuilder{
|
|
|
|
threadBlocks: make(map[uint32]jwz.Threadable),
|
|
|
|
messageidToUid: make(map[string]uint32),
|
|
|
|
seen: make(map[uint32]bool),
|
2022-10-20 16:43:41 +02:00
|
|
|
iterFactory: i,
|
2022-02-24 00:41:13 +01:00
|
|
|
}
|
|
|
|
return tb
|
|
|
|
}
|
|
|
|
|
2022-03-08 18:13:39 +01:00
|
|
|
// Uids returns the uids in threading order
|
|
|
|
func (builder *ThreadBuilder) Uids() []uint32 {
|
2022-07-26 15:41:13 +02:00
|
|
|
builder.Lock()
|
|
|
|
defer builder.Unlock()
|
|
|
|
|
2022-03-08 18:13:39 +01:00
|
|
|
if builder.threadedUids == nil {
|
|
|
|
return []uint32{}
|
|
|
|
}
|
|
|
|
return builder.threadedUids
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update updates the thread builder with a new message header
|
2022-02-24 00:41:13 +01:00
|
|
|
func (builder *ThreadBuilder) Update(msg *models.MessageInfo) {
|
2022-07-26 15:41:13 +02:00
|
|
|
builder.Lock()
|
|
|
|
defer builder.Unlock()
|
|
|
|
|
2022-02-24 00:41:13 +01:00
|
|
|
if msg != nil {
|
|
|
|
if threadable := newThreadable(msg); threadable != nil {
|
|
|
|
builder.messageidToUid[threadable.MessageThreadID()] = msg.Uid
|
|
|
|
builder.threadBlocks[msg.Uid] = threadable
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-08 18:13:39 +01:00
|
|
|
// Threads returns a slice of threads for the given list of uids
|
2022-10-20 16:43:44 +02:00
|
|
|
func (builder *ThreadBuilder) Threads(uids []uint32, inverse bool) []*types.Thread {
|
2022-07-26 15:41:13 +02:00
|
|
|
builder.Lock()
|
|
|
|
defer builder.Unlock()
|
|
|
|
|
2022-02-24 00:41:13 +01:00
|
|
|
start := time.Now()
|
|
|
|
|
2022-03-08 18:13:39 +01:00
|
|
|
threads := builder.buildAercThreads(builder.generateStructure(uids), uids)
|
|
|
|
|
|
|
|
// sort threads according to uid ordering
|
|
|
|
builder.sortThreads(threads, uids)
|
|
|
|
|
|
|
|
// rebuild uids from threads
|
2022-10-20 16:43:44 +02:00
|
|
|
builder.RebuildUids(threads, inverse)
|
2022-02-24 00:41:13 +01:00
|
|
|
|
|
|
|
elapsed := time.Since(start)
|
2022-07-19 22:31:51 +02:00
|
|
|
logging.Infof("%d threads created in %s", len(threads), elapsed)
|
2022-02-24 00:41:13 +01:00
|
|
|
|
|
|
|
return threads
|
|
|
|
}
|
|
|
|
|
2022-03-08 18:13:39 +01:00
|
|
|
func (builder *ThreadBuilder) generateStructure(uids []uint32) jwz.Threadable {
|
2022-02-24 00:41:13 +01:00
|
|
|
jwzThreads := make([]jwz.Threadable, 0, len(builder.threadBlocks))
|
2022-03-08 18:13:39 +01:00
|
|
|
for _, uid := range uids {
|
2022-02-24 00:41:13 +01:00
|
|
|
if thr, ok := builder.threadBlocks[uid]; ok {
|
|
|
|
jwzThreads = append(jwzThreads, thr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
threader := jwz.NewThreader()
|
|
|
|
threadStructure, err := threader.ThreadSlice(jwzThreads)
|
|
|
|
if err != nil {
|
2022-07-19 22:31:51 +02:00
|
|
|
logging.Errorf("failed slicing threads: %v", err)
|
2022-02-24 00:41:13 +01:00
|
|
|
}
|
|
|
|
return threadStructure
|
|
|
|
}
|
|
|
|
|
2022-03-08 18:13:39 +01:00
|
|
|
func (builder *ThreadBuilder) buildAercThreads(structure jwz.Threadable, uids []uint32) []*types.Thread {
|
2022-02-24 00:41:13 +01:00
|
|
|
threads := make([]*types.Thread, 0, len(builder.threadBlocks))
|
|
|
|
if structure == nil {
|
2022-03-08 18:13:39 +01:00
|
|
|
for _, uid := range uids {
|
2022-02-24 00:41:13 +01:00
|
|
|
threads = append(threads, &types.Thread{Uid: uid})
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// fill threads with nil messages
|
2022-03-08 18:13:39 +01:00
|
|
|
for _, uid := range uids {
|
2022-02-24 00:41:13 +01:00
|
|
|
if _, ok := builder.threadBlocks[uid]; !ok {
|
|
|
|
threads = append(threads, &types.Thread{Uid: uid})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// append the on-the-fly created aerc threads
|
|
|
|
root := &types.Thread{Uid: 0}
|
|
|
|
builder.seen = make(map[uint32]bool)
|
|
|
|
builder.buildTree(structure, root)
|
|
|
|
for iter := root.FirstChild; iter != nil; iter = iter.NextSibling {
|
|
|
|
iter.Parent = nil
|
|
|
|
threads = append(threads, iter)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return threads
|
|
|
|
}
|
|
|
|
|
|
|
|
// buildTree recursively translates the jwz threads structure into aerc threads
|
|
|
|
// builder.seen is used to avoid potential double-counting and should be empty
|
|
|
|
// on first call of this function
|
|
|
|
func (builder *ThreadBuilder) buildTree(treeNode jwz.Threadable, target *types.Thread) {
|
|
|
|
if treeNode == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// deal with child
|
|
|
|
uid, ok := builder.messageidToUid[treeNode.MessageThreadID()]
|
|
|
|
if _, seen := builder.seen[uid]; ok && !seen {
|
|
|
|
builder.seen[uid] = true
|
|
|
|
childNode := &types.Thread{Uid: uid, Parent: target}
|
|
|
|
target.OrderedInsert(childNode)
|
|
|
|
builder.buildTree(treeNode.GetChild(), childNode)
|
|
|
|
} else {
|
|
|
|
builder.buildTree(treeNode.GetChild(), target)
|
|
|
|
}
|
|
|
|
|
|
|
|
// deal with siblings
|
|
|
|
for next := treeNode.GetNext(); next != nil; next = next.GetNext() {
|
|
|
|
|
|
|
|
uid, ok := builder.messageidToUid[next.MessageThreadID()]
|
|
|
|
if _, seen := builder.seen[uid]; ok && !seen {
|
|
|
|
builder.seen[uid] = true
|
|
|
|
nn := &types.Thread{Uid: uid, Parent: target}
|
|
|
|
target.OrderedInsert(nn)
|
|
|
|
builder.buildTree(next.GetChild(), nn)
|
|
|
|
} else {
|
|
|
|
builder.buildTree(next.GetChild(), target)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-08 18:13:39 +01:00
|
|
|
func (builder *ThreadBuilder) sortThreads(threads []*types.Thread, orderedUids []uint32) {
|
|
|
|
types.SortThreadsBy(threads, orderedUids)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RebuildUids rebuilds the uids from the given slice of threads
|
2022-10-20 16:43:44 +02:00
|
|
|
func (builder *ThreadBuilder) RebuildUids(threads []*types.Thread, inverse bool) {
|
2022-03-08 18:13:39 +01:00
|
|
|
uids := make([]uint32, 0, len(threads))
|
2022-10-20 16:43:41 +02:00
|
|
|
iterT := builder.iterFactory.NewIterator(threads)
|
|
|
|
for iterT.Next() {
|
2022-10-20 16:43:44 +02:00
|
|
|
var threaduids []uint32
|
2022-10-20 16:43:41 +02:00
|
|
|
_ = iterT.Value().(*types.Thread).Walk(
|
|
|
|
func(t *types.Thread, level int, currentErr error) error {
|
2022-10-20 16:43:44 +02:00
|
|
|
threaduids = append(threaduids, t.Uid)
|
2022-10-20 16:43:41 +02:00
|
|
|
return nil
|
|
|
|
})
|
2022-10-20 16:43:44 +02:00
|
|
|
if inverse {
|
|
|
|
for j := len(threaduids) - 1; j >= 0; j-- {
|
|
|
|
uids = append(uids, threaduids[j])
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
uids = append(uids, threaduids...)
|
|
|
|
}
|
2022-03-08 18:13:39 +01:00
|
|
|
}
|
2022-10-20 16:43:41 +02:00
|
|
|
result := make([]uint32, 0, len(uids))
|
|
|
|
iterU := builder.iterFactory.NewIterator(uids)
|
|
|
|
for iterU.Next() {
|
|
|
|
result = append(result, iterU.Value().(uint32))
|
2022-03-08 18:13:39 +01:00
|
|
|
}
|
2022-10-20 16:43:41 +02:00
|
|
|
builder.threadedUids = result
|
2022-03-08 18:13:39 +01:00
|
|
|
}
|
|
|
|
|
2022-02-24 00:41:13 +01:00
|
|
|
// threadable implements the jwz.threadable interface which is required for the
|
|
|
|
// jwz threading algorithm
|
|
|
|
type threadable struct {
|
|
|
|
MsgInfo *models.MessageInfo
|
|
|
|
MessageId string
|
|
|
|
Next jwz.Threadable
|
|
|
|
Parent jwz.Threadable
|
|
|
|
Child jwz.Threadable
|
|
|
|
Dummy bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func newThreadable(msg *models.MessageInfo) *threadable {
|
|
|
|
msgid, err := msg.MsgId()
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &threadable{
|
|
|
|
MessageId: msgid,
|
|
|
|
MsgInfo: msg,
|
|
|
|
Next: nil,
|
|
|
|
Parent: nil,
|
|
|
|
Child: nil,
|
|
|
|
Dummy: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *threadable) MessageThreadID() string {
|
|
|
|
return t.MessageId
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *threadable) MessageThreadReferences() []string {
|
|
|
|
if t.IsDummy() || t.MsgInfo == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2022-03-23 20:25:37 +01:00
|
|
|
irp, err := t.MsgInfo.InReplyTo()
|
|
|
|
if err != nil {
|
|
|
|
irp = ""
|
|
|
|
}
|
2022-02-24 00:41:13 +01:00
|
|
|
refs, err := t.MsgInfo.References()
|
|
|
|
if err != nil || len(refs) == 0 {
|
2022-03-23 20:25:37 +01:00
|
|
|
if irp == "" {
|
2022-02-24 00:41:13 +01:00
|
|
|
return nil
|
|
|
|
}
|
2022-03-23 20:25:37 +01:00
|
|
|
refs = []string{irp}
|
2022-02-24 00:41:13 +01:00
|
|
|
}
|
2022-03-23 20:25:37 +01:00
|
|
|
return cleanRefs(t.MessageThreadID(), irp, refs)
|
2022-03-22 00:50:02 +01:00
|
|
|
}
|
|
|
|
|
2022-03-23 20:25:37 +01:00
|
|
|
// cleanRefs cleans up the references headers for threading
|
|
|
|
// 1) message-id should not be part of the references
|
|
|
|
// 2) no message-id should occur twice (avoid circularities)
|
|
|
|
// 3) in-reply-to header should not be at the beginning
|
|
|
|
func cleanRefs(m, irp string, refs []string) []string {
|
2022-03-22 00:50:02 +01:00
|
|
|
considered := make(map[string]interface{})
|
|
|
|
cleanRefs := make([]string, 0, len(refs))
|
|
|
|
for _, r := range refs {
|
|
|
|
if _, seen := considered[r]; r != m && !seen {
|
|
|
|
considered[r] = nil
|
|
|
|
cleanRefs = append(cleanRefs, r)
|
|
|
|
}
|
|
|
|
}
|
2022-03-23 20:25:37 +01:00
|
|
|
if irp != "" && len(cleanRefs) > 0 {
|
|
|
|
if cleanRefs[0] == irp {
|
|
|
|
cleanRefs = append(cleanRefs[1:], irp)
|
|
|
|
}
|
|
|
|
}
|
2022-03-22 00:50:02 +01:00
|
|
|
return cleanRefs
|
2022-02-24 00:41:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *threadable) Subject() string {
|
|
|
|
// deactivate threading by subject for now
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *threadable) SimplifiedSubject() string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *threadable) SubjectIsReply() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *threadable) SetNext(next jwz.Threadable) {
|
|
|
|
t.Next = next
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *threadable) SetChild(kid jwz.Threadable) {
|
|
|
|
t.Child = kid
|
|
|
|
if kid != nil {
|
|
|
|
kid.SetParent(t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *threadable) SetParent(parent jwz.Threadable) {
|
|
|
|
t.Parent = parent
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *threadable) GetNext() jwz.Threadable {
|
|
|
|
return t.Next
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *threadable) GetChild() jwz.Threadable {
|
|
|
|
return t.Child
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *threadable) GetParent() jwz.Threadable {
|
|
|
|
return t.Parent
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *threadable) GetDate() time.Time {
|
|
|
|
if t.IsDummy() {
|
|
|
|
if t.GetChild() != nil {
|
|
|
|
return t.GetChild().GetDate()
|
|
|
|
}
|
|
|
|
return time.Unix(0, 0)
|
|
|
|
}
|
|
|
|
if t.MsgInfo == nil || t.MsgInfo.Envelope == nil {
|
|
|
|
return time.Unix(0, 0)
|
|
|
|
}
|
|
|
|
return t.MsgInfo.Envelope.Date
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *threadable) MakeDummy(forID string) jwz.Threadable {
|
|
|
|
return &threadable{
|
|
|
|
MessageId: forID,
|
|
|
|
Dummy: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *threadable) IsDummy() bool {
|
|
|
|
return t.Dummy
|
|
|
|
}
|