feat: add comments

This commit is contained in:
Krispire Admin
2026-06-13 01:29:55 +05:30
parent 1edee92865
commit 6ebaa36158
11 changed files with 968 additions and 10 deletions
+24 -2
View File
@@ -35,11 +35,12 @@ type PostService struct {
r2 *repository.R2
likes *repository.LikeRepo
mentions *repository.MentionRepo
comments *repository.CommentRepo
notif *NotificationService // optional; set via SetNotifier
}
func NewPostService(posts *repository.PostRepo, r2 *repository.R2, likes *repository.LikeRepo, mentions *repository.MentionRepo) *PostService {
return &PostService{posts: posts, r2: r2, likes: likes, mentions: mentions}
func NewPostService(posts *repository.PostRepo, r2 *repository.R2, likes *repository.LikeRepo, mentions *repository.MentionRepo, comments *repository.CommentRepo) *PostService {
return &PostService{posts: posts, r2: r2, likes: likes, mentions: mentions, comments: comments}
}
// SetNotifier wires the notification seam after construction (the
@@ -97,6 +98,9 @@ func (s *PostService) Get(ctx context.Context, id, viewerID string) (*model.Post
if err := s.mentions.HydrateCounts(ctx, []*model.Post{p}); err != nil {
return nil, err
}
if err := s.comments.HydratePinned(ctx, viewerID, []*model.Post{p}); err != nil {
return nil, err
}
return p, nil
}
@@ -202,9 +206,24 @@ func (s *PostService) ListByHandle(ctx context.Context, handle, viewerID string,
if err := s.hydrateMentionsSlice(ctx, posts); err != nil {
return nil, 0, err
}
if err := s.hydratePinnedSlice(ctx, viewerID, posts); err != nil {
return nil, 0, err
}
return posts, total, nil
}
// hydratePinnedSlice fills PinnedComment on a value slice.
func (s *PostService) hydratePinnedSlice(ctx context.Context, viewerID string, posts []model.Post) error {
if len(posts) == 0 {
return nil
}
ptrs := make([]*model.Post, len(posts))
for i := range posts {
ptrs[i] = &posts[i]
}
return s.comments.HydratePinned(ctx, viewerID, ptrs)
}
// hydrateLikesSlice fills LikesCount + ViewerHasLiked on a value slice.
func (s *PostService) hydrateLikesSlice(ctx context.Context, viewerID string, posts []model.Post) error {
if len(posts) == 0 {
@@ -347,6 +366,9 @@ func (s *PostService) ListMine(ctx context.Context, userID string, limit, offset
if err := s.hydrateMentionsSlice(ctx, posts); err != nil {
return nil, 0, err
}
if err := s.hydratePinnedSlice(ctx, userID, posts); err != nil {
return nil, 0, err
}
return posts, total, nil
}