diff --git a/system/discourse/api/topics.go b/system/discourse/api/topics.go index 7b20ea2..4d6e2a0 100644 --- a/system/discourse/api/topics.go +++ b/system/discourse/api/topics.go @@ -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, diff --git a/system/discourse/discourse.go b/system/discourse/discourse.go index 17b2879..1c7b781 100644 --- a/system/discourse/discourse.go +++ b/system/discourse/discourse.go @@ -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{}