Skip to content

Commit

Permalink
Implemented reply chunks querying for mrusme#10
Browse files Browse the repository at this point in the history
  • Loading branch information
mrusme committed Jan 10, 2023
1 parent a4e4db9 commit 51cb1d9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
4 changes: 3 additions & 1 deletion models/post/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ type Post struct {

Forum forum.Forum

Replies []reply.Reply
TotalReplies int
CurrentRepliesStartIDX int
Replies []reply.Reply

URL string

Expand Down
24 changes: 22 additions & 2 deletions system/discourse/discourse.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ func (sys *System) ListPosts(forumID string) ([]post.Post, error) {
SysIDX: sys.ID,
},

TotalReplies: 0,
CurrentRepliesStartIDX: -1,

URL: fmt.Sprintf("%s/t/%d", baseURL, i.ID),

SysIDX: sys.ID,
Expand All @@ -254,11 +257,28 @@ func (sys *System) LoadPost(p *post.Post) error {
// 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 {
p.TotalReplies = len(item.PostStream.Stream)
if p.TotalReplies > 20 {
if p.CurrentRepliesStartIDX == -1 ||
// Explain to me standard GoFmt logic:
p.CurrentRepliesStartIDX > (p.TotalReplies-20) {
p.CurrentRepliesStartIDX = (p.TotalReplies - 20)
// /)_-)
}

var postIDs []int
if p.CurrentRepliesStartIDX > 0 {
postIDs = append(postIDs,
item.PostStream.Stream[0])
p.CurrentRepliesStartIDX++
}
postIDs = append(postIDs,
item.PostStream.Stream[p.CurrentRepliesStartIDX:]...)

replies, err := sys.client.Topics.ShowPosts(
context.Background(),
p.ID,
item.PostStream.Stream[len(item.PostStream.Stream)-20:],
postIDs,
)
if err != nil {
sys.logger.Error(err)
Expand Down

0 comments on commit 51cb1d9

Please sign in to comment.