Recreate thread for orphaned posts

This commit is contained in:
Gabriel Garrido 2024-05-18 22:42:59 +02:00
parent 3cf2b77edb
commit 8fff8b6e09
2 changed files with 55 additions and 3 deletions

View file

@ -66,7 +66,38 @@ func New(userURL string, filters PostsFilter, threaded bool) (Client, error) {
client.populateIdMap() client.populateIdMap()
if threaded { 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 { } else {
for i := range client.posts { for i := range client.posts {
client.output = append(client.output, i) 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 { for i := range c.posts {
post := &c.posts[i] post := &c.posts[i]
if post.InReplyToId == "" { if post.InReplyToId == "" {
@ -114,7 +145,6 @@ func (c *Client) generateReplies() {
} }
if _, ok := c.postIdMap[post.InReplyToId]; ok { if _, ok := c.postIdMap[post.InReplyToId]; ok {
// TODO: Exclude from list of posts that gets rendered to disc
c.replies[post.InReplyToId] = post.Id c.replies[post.InReplyToId] = post.Id
} else { } else {
c.orphans = append(c.orphans, post.Id) c.orphans = append(c.orphans, post.Id)

View file

@ -8,6 +8,11 @@ import (
"time" "time"
) )
type StatusContext struct {
Ancestors []Post `json:"ancestors"`
Descendants []Post `json:"descendants"`
}
type MediaAttachment struct { type MediaAttachment struct {
Type string `json:"type"` Type string `json:"type"`
URL string `json:"url"` URL string `json:"url"`
@ -137,3 +142,20 @@ func FetchPosts(baseURL string, accountId string, filters PostsFilter) ([]Post,
return posts, nil 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
}