Fix bug in thread building using status context

It cannot be assumed that an ancestor will exist
when building a thread using the status context
API. Use the current orphan as the starting point
if no ancestor exists.
This commit is contained in:
Gabriel Garrido 2024-08-03 08:33:33 +02:00
parent 33c6f3081c
commit 41663032f9
2 changed files with 18 additions and 8 deletions

View file

@ -114,7 +114,14 @@ func (c *Client) buildOrphans() error {
return err
}
top := statusContext.Ancestors[0]
var top Post;
// When building a thread from the status context endpoint,
// start from the greatest ancestor and add the other ancestors
// below it as descendants.
// Otherwise, use the orphan as the start.
if len(statusContext.Ancestors) > 0 {
top = statusContext.Ancestors[0]
for i := range statusContext.Ancestors[1:] {
post := statusContext.Ancestors[i+1]
@ -123,6 +130,9 @@ func (c *Client) buildOrphans() error {
}
top.descendants = append(top.descendants, c.postIdMap[postId])
} else {
top = *c.postIdMap[postId]
}
for i := range statusContext.Descendants {
post := statusContext.Descendants[i]