From 0163498448b0a9c78b40545c124f22f7ee3eb1bb Mon Sep 17 00:00:00 2001 From: krustowski Date: Thu, 12 Sep 2024 01:24:51 +0200 Subject: [PATCH] implement reply count logic to paginator on BE --- pkg/backend/posts/controllers.go | 2 +- pkg/backend/posts/paginate.go | 21 +++++++++++++-------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/pkg/backend/posts/controllers.go b/pkg/backend/posts/controllers.go index 6f0e54c3..383b0189 100644 --- a/pkg/backend/posts/controllers.go +++ b/pkg/backend/posts/controllers.go @@ -84,7 +84,7 @@ func getPosts(w http.ResponseWriter, r *http.Request) { // fetch page according to the logged user pExport, uExport := GetOnePage(opts) if pExport == nil || uExport == nil { - resp.Message = "error while requesting more page, one exported map is nil!" + resp.Message = "error while requesting more pages, one exported map is nil!" resp.Code = http.StatusInternalServerError l.Println(resp.Message, resp.Code) diff --git a/pkg/backend/posts/paginate.go b/pkg/backend/posts/paginate.go index 9826d50f..1ec83db1 100644 --- a/pkg/backend/posts/paginate.go +++ b/pkg/backend/posts/paginate.go @@ -59,17 +59,22 @@ func GetOnePage(opts PageOptions) (map[string]models.Post, map[string]models.Use flowList = opts.FlowList } - // filter out all posts for such callerID + // assign reply count to each post for _, post := range allPosts { + if post.ReplyToID == "" { + continue + } + // calculate the reply count for each post - /*if post.ReplyToID != "" { - origo, found := allPosts[post.ReplyToID] - if found { - origo.ReplyCount++ - allPosts[post.ReplyToID] = origo - } - }*/ + origo, found := allPosts[post.ReplyToID] + if found { + origo.ReplyCount++ + allPosts[origo.ID] = origo + } + } + // filter out all posts for such callerID + for _, post := range allPosts { // check the caller's flow list, skip on unfollowed, or unknown user if value, found := flowList[post.Nickname]; !found || !value { continue