From 41663032f9cf88bf21f6df47f9aee56c3da34076 Mon Sep 17 00:00:00 2001 From: Gabriel Garrido Date: Sat, 3 Aug 2024 08:33:33 +0200 Subject: [PATCH] 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. --- client/client.go | 24 +++++++++++++++++------- main.go | 2 +- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/client/client.go b/client/client.go index c504707..2eee43e 100644 --- a/client/client.go +++ b/client/client.go @@ -114,16 +114,26 @@ func (c *Client) buildOrphans() error { return err } - top := statusContext.Ancestors[0] + var top Post; - for i := range statusContext.Ancestors[1:] { - post := statusContext.Ancestors[i+1] - c.postIdMap[post.Id] = &post - top.descendants = append(top.descendants, &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] + c.postIdMap[post.Id] = &post + top.descendants = append(top.descendants, &post) + } + + top.descendants = append(top.descendants, c.postIdMap[postId]) + } else { + top = *c.postIdMap[postId] } - top.descendants = append(top.descendants, c.postIdMap[postId]) - for i := range statusContext.Descendants { post := statusContext.Descendants[i] if post.Account.Id != c.account.Id { diff --git a/main.go b/main.go index 730e8c9..d9c0cdf 100644 --- a/main.go +++ b/main.go @@ -43,7 +43,7 @@ func main() { MinId: *minId, OnlyMedia: *onlyMedia, Pinned: *pinned, - Tagged: *tagged, + Tagged: *tagged, }, client.ClientOptions{ Threaded: *threaded, Visibility: *visibility,