feat: add comments count

This commit is contained in:
Krispire Admin
2026-06-13 02:24:21 +05:30
parent 6ebaa36158
commit 05567ad438
4 changed files with 40 additions and 10 deletions
+33 -7
View File
@@ -178,9 +178,10 @@ func (r *CommentRepo) ListReplies(ctx context.Context, parentID, viewerID string
return out, total, err
}
// HydratePinned attaches each post's pinned comment (if any) to
// Post.PinnedComment in a single query for the page.
func (r *CommentRepo) HydratePinned(ctx context.Context, viewerID string, posts []*model.Post) error {
// Hydrate fills each post's comment summary — its pinned comment (if any) and
// total comment count (top-level + replies) — in two batched queries for the
// page. Drives the card footer's pinned line and comment count.
func (r *CommentRepo) Hydrate(ctx context.Context, viewerID string, posts []*model.Post) error {
ids := make([]string, 0, len(posts))
for _, p := range posts {
if p != nil {
@@ -190,27 +191,52 @@ func (r *CommentRepo) HydratePinned(ctx context.Context, viewerID string, posts
if len(ids) == 0 {
return nil
}
// Pinned comment per post.
q := `SELECT ` + commentSelect + commentFrom + `
WHERE c.post_id = ANY($1) AND c.pinned_at IS NOT NULL`
rows, err := r.pg.Pool.Query(ctx, q, ids, nullUUIDArg(viewerID))
if err != nil {
return fmt.Errorf("hydrate pinned: %w", err)
}
list, err := scanComments(rows)
list, err := scanComments(rows) // closes rows
if err != nil {
return err
}
byPost := make(map[string]*model.Comment, len(list))
pinnedByPost := make(map[string]*model.Comment, len(list))
for i := range list {
byPost[list[i].PostID] = &list[i]
pinnedByPost[list[i].PostID] = &list[i]
}
// Total comment count per post (top-level + replies).
counts := make(map[string]int, len(ids))
crows, err := r.pg.Pool.Query(ctx,
`SELECT post_id, count(*) FROM post_comments WHERE post_id = ANY($1) GROUP BY post_id`,
ids)
if err != nil {
return fmt.Errorf("hydrate comment counts: %w", err)
}
defer crows.Close()
for crows.Next() {
var id string
var n int
if err := crows.Scan(&id, &n); err != nil {
return fmt.Errorf("scan comment count: %w", err)
}
counts[id] = n
}
if err := crows.Err(); err != nil {
return err
}
for _, p := range posts {
if p == nil {
continue
}
if c, ok := byPost[p.ID]; ok {
if c, ok := pinnedByPost[p.ID]; ok {
p.PinnedComment = c
}
p.CommentsCount = counts[p.ID]
}
return nil
}