try to flatten self replies in threading

This commit is contained in:
Ted Unangst 2023-06-13 19:18:10 -04:00
parent 7feb254618
commit 23f9a3b8bf
1 changed files with 19 additions and 8 deletions

27
web.go
View File

@ -1019,21 +1019,30 @@ func trackback(xid string, r *http.Request) {
} }
func threadsort(honks []*Honk) []*Honk { func threadsort(honks []*Honk) []*Honk {
honkx := make(map[string]*Honk)
kids := make(map[string][]*Honk) kids := make(map[string][]*Honk)
for _, h := range honks { for _, h := range honks {
kids[h.RID] = append(kids[h.RID], h) honkx[h.XID] = h
rid := h.RID
kids[rid] = append(kids[rid], h)
} }
done := make(map[*Honk]bool) done := make(map[*Honk]bool)
var thread []*Honk var thread []*Honk
var grabkids func(p *Honk) var nextlevel func(p *Honk)
level := 0 level := 0
grabkids = func(p *Honk) { nextlevel = func(p *Honk) {
if level > 4 { if level > 4 {
p.Style += fmt.Sprintf(" level%d", 4) p.Style += fmt.Sprintf(" level%d", 4)
} else { } else {
p.Style += fmt.Sprintf(" level%d", level) p.Style += fmt.Sprintf(" level%d", level)
} }
level++ levelup := true
if pp := honkx[p.RID]; pp != nil && p.Honker != pp.Honker {
levelup = false
}
if levelup {
level++
}
childs := kids[p.XID] childs := kids[p.XID]
sort.Slice(childs, func(i, j int) bool { sort.Slice(childs, func(i, j int) bool {
return childs[i].Date.Before(childs[j].Date) return childs[i].Date.Before(childs[j].Date)
@ -1041,22 +1050,24 @@ func threadsort(honks []*Honk) []*Honk {
for _, h := range childs { for _, h := range childs {
done[h] = true done[h] = true
thread = append(thread, h) thread = append(thread, h)
grabkids(h) nextlevel(h)
}
if levelup {
level--
} }
level--
} }
for _, h := range honks { for _, h := range honks {
if h.RID == "" { if h.RID == "" {
done[h] = true done[h] = true
thread = append(thread, h) thread = append(thread, h)
grabkids(h) nextlevel(h)
} }
} }
for _, h := range honks { for _, h := range honks {
if !done[h] { if !done[h] {
done[h] = true done[h] = true
thread = append(thread, h) thread = append(thread, h)
grabkids(h) nextlevel(h)
} }
} }
return thread return thread