mirror of
https://github.com/mrusme/neonmodem.git
synced 2024-09-22 04:35:55 -04:00
Implemented dedicated posts loading, part of #10
This commit is contained in:
parent
56b54ebe28
commit
73fb14fa06
@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
const TopicsBaseURL = "/t"
|
const TopicsBaseURL = "/t"
|
||||||
@ -29,7 +30,8 @@ type LatestTopicsResponse struct {
|
|||||||
|
|
||||||
type SingleTopicResponse struct {
|
type SingleTopicResponse struct {
|
||||||
PostStream struct {
|
PostStream struct {
|
||||||
Posts []PostModel `json:"posts"`
|
Posts []PostModel `json:"posts"`
|
||||||
|
Stream []int `json:"stream"`
|
||||||
} `json:"post_stream"`
|
} `json:"post_stream"`
|
||||||
|
|
||||||
TopicModel
|
TopicModel
|
||||||
@ -81,6 +83,11 @@ type TopicsService interface {
|
|||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
id string,
|
id string,
|
||||||
) (*SingleTopicResponse, error)
|
) (*SingleTopicResponse, error)
|
||||||
|
ShowPosts(
|
||||||
|
ctx context.Context,
|
||||||
|
id string,
|
||||||
|
postIDs []int,
|
||||||
|
) (*SingleTopicResponse, error)
|
||||||
ListLatest(
|
ListLatest(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
categorySlug string,
|
categorySlug string,
|
||||||
@ -112,6 +119,33 @@ func (a *TopicServiceHandler) Show(
|
|||||||
return response, nil
|
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
|
// List
|
||||||
func (a *TopicServiceHandler) ListLatest(
|
func (a *TopicServiceHandler) ListLatest(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
|
@ -251,6 +251,23 @@ func (sys *System) LoadPost(p *post.Post) error {
|
|||||||
return err
|
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)
|
converter := md.NewConverter("", true, nil)
|
||||||
|
|
||||||
p.Replies = []reply.Reply{}
|
p.Replies = []reply.Reply{}
|
||||||
|
Loading…
Reference in New Issue
Block a user