diff --git a/client/client.go b/client/client.go index 14651ca..65fe398 100644 --- a/client/client.go +++ b/client/client.go @@ -66,7 +66,38 @@ func New(userURL string, filters PostsFilter, threaded bool) (Client, error) { client.populateIdMap() if threaded { - client.generateReplies() + client.threadReplies() + + if len(client.orphans) > 0 { + for _, pid := range client.orphans { + statusContext, err := FetchStatusContext(baseURL, pid) + + if err != nil { + return client, err + } + + top := statusContext.Ancestors[0] + + for _, post := range statusContext.Ancestors[1:] { + client.posts = append(client.posts, post) + top.descendants = append(top.descendants, &client.posts[len(client.posts)-1]) + } + + top.descendants = append(top.descendants, &client.posts[client.postIdMap[pid]]) + + for _, post := range statusContext.Descendants { + if post.Account.Id != client.account.Id { + continue + } + + client.posts = append(client.posts, post) + top.descendants = append(top.descendants, &client.posts[len(client.posts)-1]) + } + + client.posts = append(client.posts, top) + client.output = append(client.output, len(client.posts)-1) + } + } } else { for i := range client.posts { client.output = append(client.output, i) @@ -104,7 +135,7 @@ func (c *Client) flushReplies(post *Post, descendants *[]*Post) { } } -func (c *Client) generateReplies() { +func (c *Client) threadReplies() { for i := range c.posts { post := &c.posts[i] if post.InReplyToId == "" { @@ -114,7 +145,6 @@ func (c *Client) generateReplies() { } if _, ok := c.postIdMap[post.InReplyToId]; ok { - // TODO: Exclude from list of posts that gets rendered to disc c.replies[post.InReplyToId] = post.Id } else { c.orphans = append(c.orphans, post.Id) diff --git a/client/post.go b/client/post.go index 8ff772d..f18d845 100644 --- a/client/post.go +++ b/client/post.go @@ -8,6 +8,11 @@ import ( "time" ) +type StatusContext struct { + Ancestors []Post `json:"ancestors"` + Descendants []Post `json:"descendants"` +} + type MediaAttachment struct { Type string `json:"type"` URL string `json:"url"` @@ -137,3 +142,20 @@ func FetchPosts(baseURL string, accountId string, filters PostsFilter) ([]Post, return posts, nil } + +func FetchStatusContext(baseURL, postId string) (StatusContext, error) { + var status StatusContext + statusUrl := fmt.Sprintf( + "%s/api/v1/statuses/%s/context", + baseURL, + postId, + ) + + log.Println(statusUrl) + + if err := Fetch(statusUrl, &status); err != nil { + return status, err + } + + return status, nil +}