From 1b6ce56164179f5449c5dab1876fd03a235e616d Mon Sep 17 00:00:00 2001 From: Koni Marti Date: Wed, 23 Mar 2022 20:25:37 +0100 Subject: [PATCH] threading: fix msg-id order in references header Fix order in the references header when an in-reply-to msg-id is erroneously added at the beginning instead of at the end. Add description to the function that cleans up the reference headers for threading. Reported-by: Evan Gates Signed-off-by: Koni Marti Acked-by: Robin Jarry --- lib/threadbuilder.go | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/threadbuilder.go b/lib/threadbuilder.go index 9137fc1..334a846 100644 --- a/lib/threadbuilder.go +++ b/lib/threadbuilder.go @@ -192,18 +192,25 @@ func (t *threadable) MessageThreadReferences() []string { if t.IsDummy() || t.MsgInfo == nil { return nil } + irp, err := t.MsgInfo.InReplyTo() + if err != nil { + irp = "" + } refs, err := t.MsgInfo.References() if err != nil || len(refs) == 0 { - inreplyto, err := t.MsgInfo.InReplyTo() - if err != nil { + if irp == "" { return nil } - refs = []string{inreplyto} + refs = []string{irp} } - return cleanRefs(t.MessageThreadID(), refs) + return cleanRefs(t.MessageThreadID(), irp, refs) } -func cleanRefs(m string, refs []string) []string { +// 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 { considered := make(map[string]interface{}) cleanRefs := make([]string, 0, len(refs)) for _, r := range refs { @@ -212,6 +219,11 @@ func cleanRefs(m string, refs []string) []string { cleanRefs = append(cleanRefs, r) } } + if irp != "" && len(cleanRefs) > 0 { + if cleanRefs[0] == irp { + cleanRefs = append(cleanRefs[1:], irp) + } + } return cleanRefs }