Default threading to false, and slight threading refactor

This commit is contained in:
Gabriel Garrido 2024-05-19 17:46:14 +02:00
parent 4158621927
commit b5ead62468
2 changed files with 20 additions and 17 deletions

View file

@ -80,7 +80,15 @@ func New(userURL string, filters PostsFilter, threaded bool) (Client, error) {
} }
if threaded { 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 return client, nil
@ -143,13 +151,13 @@ func (c *Client) flushReplies(post *Post, descendants *[]*Post) {
} }
} }
func (c *Client) threadReplies(posts []Post) { func (c *Client) threadPost(postId string) {
for i := range posts { post := c.postIdMap[postId]
post := &posts[i]
if post.InReplyToId == "" { if post.InReplyToId == "" {
c.flushReplies(post, &post.descendants) c.flushReplies(post, &post.descendants)
c.output = append(c.output, post.Id) c.output = append(c.output, post.Id)
continue return
} }
if _, ok := c.postIdMap[post.InReplyToId]; ok { if _, ok := c.postIdMap[post.InReplyToId]; ok {
@ -157,9 +165,4 @@ func (c *Client) threadReplies(posts []Post) {
} else { } else {
c.orphans = append(c.orphans, post.Id) c.orphans = append(c.orphans, post.Id)
} }
}
if len(c.orphans) > 0 {
c.buildOrphans()
}
} }

View file

@ -23,7 +23,7 @@ func main() {
persistFirst := flag.String("persist-first", "", "Location to persist the post id of the first post returned") 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") 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") 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") 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") porcelain := flag.Bool("porcelain", false, "Prints the amount of fetched posts to stdout in a parsable manner")