Remove redudant posts list

This commit is contained in:
Gabriel Garrido 2024-05-18 23:35:15 +02:00
parent 8fff8b6e09
commit 26205dc995

View file

@ -7,15 +7,19 @@ import (
) )
type Client struct { type Client struct {
handle string handle string
baseURL string baseURL string
filters PostsFilter filters PostsFilter
account Account account Account
posts []Post // Map of Post.InReplyToId:PostId. Tracks the replies on a 1:1 basis.
replies map[string]string replies map[string]string
orphans []string // List of Post.Id. Tracks posts whose parent is not within the bounds of
postIdMap map[string]int // the returned posts.
output []int orphans []string
// Map of Post.Id:*Post.
postIdMap map[string]*Post
// List of Post.Id. Tracks the posts which will be written as individual files.
output []string
} }
type PostsFilter struct { type PostsFilter struct {
@ -51,26 +55,35 @@ func New(userURL string, filters PostsFilter, threaded bool) (Client, error) {
return client, err return client, err
} }
postIdMap := make(map[string]*Post)
var orphans []string var orphans []string
var output []string
for i := range posts {
post := posts[i]
postIdMap[post.Id] = &post
if !threaded {
output = append(output, post.Id)
}
}
client = Client{ client = Client{
baseURL: baseURL, baseURL: baseURL,
handle: handle, handle: handle,
filters: filters, filters: filters,
account: account, account: account,
posts: posts, postIdMap: postIdMap,
postIdMap: make(map[string]int),
replies: make(map[string]string), replies: make(map[string]string),
orphans: orphans, orphans: orphans,
output: output,
} }
client.populateIdMap()
if threaded { if threaded {
client.threadReplies() client.threadReplies(posts)
if len(client.orphans) > 0 { if len(client.orphans) > 0 {
for _, pid := range client.orphans { for _, postId := range client.orphans {
statusContext, err := FetchStatusContext(baseURL, pid) statusContext, err := FetchStatusContext(baseURL, postId)
if err != nil { if err != nil {
return client, err return client, err
@ -78,30 +91,28 @@ func New(userURL string, filters PostsFilter, threaded bool) (Client, error) {
top := statusContext.Ancestors[0] top := statusContext.Ancestors[0]
for _, post := range statusContext.Ancestors[1:] { for i := range statusContext.Ancestors[1:] {
client.posts = append(client.posts, post) post := statusContext.Ancestors[i+1]
top.descendants = append(top.descendants, &client.posts[len(client.posts)-1]) client.postIdMap[post.Id] = &post
top.descendants = append(top.descendants, &post)
} }
top.descendants = append(top.descendants, &client.posts[client.postIdMap[pid]]) top.descendants = append(top.descendants, client.postIdMap[postId])
for _, post := range statusContext.Descendants { for i := range statusContext.Descendants {
post := statusContext.Descendants[i]
if post.Account.Id != client.account.Id { if post.Account.Id != client.account.Id {
continue continue
} }
client.posts = append(client.posts, post) client.postIdMap[post.Id] = &post
top.descendants = append(top.descendants, &client.posts[len(client.posts)-1]) top.descendants = append(top.descendants, &post)
} }
client.posts = append(client.posts, top) client.postIdMap[top.Id] = &top
client.output = append(client.output, len(client.posts)-1) client.output = append(client.output, top.Id)
} }
} }
} else {
for i := range client.posts {
client.output = append(client.output, i)
}
} }
return client, nil return client, nil
@ -115,32 +126,26 @@ func (c Client) Posts() []*Post {
var p []*Post var p []*Post
for _, i := range c.output { for _, i := range c.output {
p = append(p, &c.posts[i]) p = append(p, c.postIdMap[i])
} }
return p return p
} }
func (c *Client) populateIdMap() {
for i, post := range c.posts {
c.postIdMap[post.Id] = i
}
}
func (c *Client) flushReplies(post *Post, descendants *[]*Post) { func (c *Client) flushReplies(post *Post, descendants *[]*Post) {
if pid, ok := c.replies[post.Id]; ok { if pid, ok := c.replies[post.Id]; ok {
reply := c.posts[c.postIdMap[pid]] reply := c.postIdMap[pid]
*descendants = append(*descendants, &reply) *descendants = append(*descendants, reply)
c.flushReplies(&reply, descendants) c.flushReplies(reply, descendants)
} }
} }
func (c *Client) threadReplies() { func (c *Client) threadReplies(posts []Post) {
for i := range c.posts { for i := range posts {
post := &c.posts[i] post := &posts[i]
if post.InReplyToId == "" { if post.InReplyToId == "" {
c.flushReplies(post, &post.descendants) c.flushReplies(post, &post.descendants)
c.output = append(c.output, i) c.output = append(c.output, post.Id)
continue continue
} }