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
+36 -2
View File
@@ -324,9 +324,12 @@ func (r *NotificationRepo) ListByUser(ctx context.Context, userID string, limit,
rows, err := r.pg.Pool.Query(ctx, `
SELECT n.id, n.user_id, n.actor_id, n.kind, n.post_id, n.actor_count,
n.created_at, n.read_at,
u.name, u.handle, u.avatar_url
u.name, u.handle, u.avatar_url,
pu.handle
FROM notifications n
LEFT JOIN users u ON u.id = n.actor_id
LEFT JOIN posts p ON p.id = n.post_id
LEFT JOIN users pu ON pu.id = p.user_id
WHERE n.user_id = $1
AND NOT EXISTS (
SELECT 1 FROM notification_prefs np
@@ -344,7 +347,8 @@ func (r *NotificationRepo) ListByUser(ctx context.Context, userID string, limit,
var n model.Notification
if err := rows.Scan(&n.ID, &n.UserID, &n.ActorID, &n.Kind, &n.PostID, &n.ActorCount,
&n.CreatedAt, &n.ReadAt,
&n.ActorName, &n.ActorHandle, &n.ActorAvatar); err != nil {
&n.ActorName, &n.ActorHandle, &n.ActorAvatar,
&n.PostAuthorHandle); err != nil {
return nil, fmt.Errorf("scan notification: %w", err)
}
out = append(out, n)
@@ -400,6 +404,36 @@ func (r *NotificationRepo) CreateMention(ctx context.Context, userID, actorID, p
return nil
}
// CreateComment persists a comment/reply notification linked to the post.
// kind is 'post_comment' (to the post author) or 'comment_reply' (to the
// replied-to comment's author). Like mentions, no aggregation.
func (r *NotificationRepo) CreateComment(ctx context.Context, userID, actorID, postID, kind string) error {
if _, err := r.pg.Pool.Exec(ctx,
`INSERT INTO notifications (user_id, actor_id, kind, post_id)
VALUES ($1, $2, $3, $4)`,
userID, actorID, kind, postID,
); err != nil {
return fmt.Errorf("create comment notification: %w", err)
}
return nil
}
// PostAuthorHandle returns the handle of the user who owns the post, for
// building a deep-link. Empty string (no error) when the post is gone.
func (r *NotificationRepo) PostAuthorHandle(ctx context.Context, postID string) (string, error) {
var h string
err := r.pg.Pool.QueryRow(ctx,
`SELECT u.handle FROM posts p JOIN users u ON u.id = p.user_id WHERE p.id = $1`,
postID).Scan(&h)
if errors.Is(err, pgx.ErrNoRows) {
return "", nil
}
if err != nil {
return "", fmt.Errorf("post author handle: %w", err)
}
return h, nil
}
// CountUnread returns how many of a recipient's notifications are unread,
// excluding kinds the user has turned off for the in-app channel (matching
// ListByUser, so the badge never counts rows the feed won't show).