diff --git a/client/client.go b/client/client.go index d193f8f..94bc4e0 100644 --- a/client/client.go +++ b/client/client.go @@ -80,7 +80,15 @@ func New(userURL string, filters PostsFilter, threaded bool) (Client, error) { } if threaded { - client.threadReplies(posts) + for _, post := range posts { + client.threadPost(post.Id) + } + + if len(client.orphans) > 0 { + if err := client.buildOrphans(); err != nil { + return client, nil + } + } } return client, nil @@ -143,23 +151,18 @@ func (c *Client) flushReplies(post *Post, descendants *[]*Post) { } } -func (c *Client) threadReplies(posts []Post) { - for i := range posts { - post := &posts[i] - if post.InReplyToId == "" { - c.flushReplies(post, &post.descendants) - c.output = append(c.output, post.Id) - continue - } +func (c *Client) threadPost(postId string) { + post := c.postIdMap[postId] - if _, ok := c.postIdMap[post.InReplyToId]; ok { - c.replies[post.InReplyToId] = post.Id - } else { - c.orphans = append(c.orphans, post.Id) - } + if post.InReplyToId == "" { + c.flushReplies(post, &post.descendants) + c.output = append(c.output, post.Id) + return } - if len(c.orphans) > 0 { - c.buildOrphans() + if _, ok := c.postIdMap[post.InReplyToId]; ok { + c.replies[post.InReplyToId] = post.Id + } else { + c.orphans = append(c.orphans, post.Id) } } diff --git a/main.go b/main.go index f33f5b7..3e0b18a 100644 --- a/main.go +++ b/main.go @@ -23,7 +23,7 @@ func main() { persistFirst := flag.String("persist-first", "", "Location to persist the post id of the first post returned") persistLast := flag.String("persist-last", "", "Location to persist the post id of the last post returned") templateFile := flag.String("template", "", "Template to use for post rendering, if passed") - threaded := flag.Bool("threaded", true, "Thread replies for a post in a single file") + threaded := flag.Bool("threaded", false, "Thread replies for a post in a single file") filenameTemplate := flag.String("filename", "", "Template for post filename") porcelain := flag.Bool("porcelain", false, "Prints the amount of fetched posts to stdout in a parsable manner")