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 {
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)
}
}

View file

@ -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")