improve References header collapse

This commit is contained in:
TJP 2023-05-18 09:52:34 -06:00
parent 33e7c374be
commit 802cb6a6d5
1 changed files with 20 additions and 12 deletions

32
main.go
View File

@ -202,29 +202,37 @@ func (b backend) AllowPost() bool {
return true
}
func (b backend) findParent(child *nntp.Article) *nntp.Article {
ref := child.Header.Get("References")
func (b backend) findOP(ref string) *nntp.Article {
if ref == "" {
return nil
}
// all references should have the same OP so just take the first
msgID := strings.SplitN(ref, " ", 2)[0]
var parent *nntp.Article
// traverse backwards expecting most reply activity concentrated late in the total history
for i := len(b.messages) - 1; i >= 0; i-- {
a := b.messages[i]
if a.MessageID() == ref {
parent = a
ref = a.Header.Get("References")
if ref == "" {
break
}
article := b.messages[i]
if article.MessageID() != msgID {
continue
}
gpID := article.Header.Get("References")
if gpID != "" {
return b.findOP(gpID)
}
return article
}
return parent
return nil
}
func (b backend) Post(article *nntp.Article) error {
b.logger("Post()")
parent := b.findParent(article)
// iris replies are all made to a top-level post, there is no grandchild nesting.
//
// but NNTP supports this, so collapse the provided "References" header up to the OP.
parent := b.findOP(article.Header.Get("References"))
if parent != nil {
article.Header.Set("References", parent.MessageID())
}