1
0
mirror of https://github.com/mrusme/neonmodem.git synced 2024-06-16 06:25:23 +00:00

Implemented dedicated posts loading, part of #10

This commit is contained in:
マリウス 2023-01-10 13:04:47 -05:00
parent 56b54ebe28
commit 73fb14fa06
No known key found for this signature in database
GPG Key ID: 272ED814BF63261F
2 changed files with 52 additions and 1 deletions

View File

@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net/http"
"strconv"
)
const TopicsBaseURL = "/t"
@ -29,7 +30,8 @@ type LatestTopicsResponse struct {
type SingleTopicResponse struct {
PostStream struct {
Posts []PostModel `json:"posts"`
Posts []PostModel `json:"posts"`
Stream []int `json:"stream"`
} `json:"post_stream"`
TopicModel
@ -81,6 +83,11 @@ type TopicsService interface {
ctx context.Context,
id string,
) (*SingleTopicResponse, error)
ShowPosts(
ctx context.Context,
id string,
postIDs []int,
) (*SingleTopicResponse, error)
ListLatest(
ctx context.Context,
categorySlug string,
@ -112,6 +119,33 @@ func (a *TopicServiceHandler) Show(
return response, nil
}
// ShowPosts
func (a *TopicServiceHandler) ShowPosts(
ctx context.Context,
id string,
postIDs []int,
) (*SingleTopicResponse, error) {
uri := TopicsBaseURL + "/" + id + "/posts.json"
req, err := a.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, err
}
q := req.URL.Query()
for _, postID := range postIDs {
q.Add("post_ids[]", strconv.Itoa(postID))
}
req.URL.RawQuery = q.Encode()
response := new(SingleTopicResponse)
if err = a.client.Do(ctx, req, response); err != nil {
return nil, err
}
return response, nil
}
// List
func (a *TopicServiceHandler) ListLatest(
ctx context.Context,

View File

@ -251,6 +251,23 @@ func (sys *System) LoadPost(p *post.Post) error {
return err
}
// API seems to return 20 posts by default. If the stream is greater than 20
// posts, we need to fetch the latest posts on our own, as we'd only get the
// first 20 posts otherwise.
if len(item.PostStream.Stream) > 20 {
replies, err := sys.client.Topics.ShowPosts(
context.Background(),
p.ID,
item.PostStream.Stream[len(item.PostStream.Stream)-20:],
)
if err != nil {
sys.logger.Error(err)
return err
}
item.PostStream.Posts = replies.PostStream.Posts
}
converter := md.NewConverter("", true, nil)
p.Replies = []reply.Reply{}