fix: add is_nsfw/nsfw_reason to all post queries (profile, detail, saved, liked, chain, focus context)

This commit is contained in:
Patrick Britton 2026-02-07 18:12:35 -06:00
parent 8ea63edf8c
commit 8d00dc4fda

View file

@ -251,7 +251,9 @@ func (r *PostRepository) GetPostsByAuthor(ctx context.Context, authorID string,
CASE WHEN $4 != '' THEN EXISTS(SELECT 1 FROM public.post_likes WHERE post_id = p.id AND user_id = $4::uuid) ELSE FALSE END as is_liked, CASE WHEN $4 != '' THEN EXISTS(SELECT 1 FROM public.post_likes WHERE post_id = p.id AND user_id = $4::uuid) ELSE FALSE END as is_liked,
p.allow_chain, p.visibility, p.allow_chain, p.visibility,
COALESCE((SELECT jsonb_object_agg(emoji, count) FROM (SELECT emoji, COUNT(*) as count FROM public.post_reactions WHERE post_id = p.id GROUP BY emoji) r), '{}'::jsonb) as reaction_counts, COALESCE((SELECT jsonb_object_agg(emoji, count) FROM (SELECT emoji, COUNT(*) as count FROM public.post_reactions WHERE post_id = p.id GROUP BY emoji) r), '{}'::jsonb) as reaction_counts,
CASE WHEN ($4::text) != '' THEN COALESCE((SELECT jsonb_agg(emoji) FROM public.post_reactions WHERE post_id = p.id AND user_id = $4::text::uuid), '[]'::jsonb) ELSE '[]'::jsonb END as my_reactions CASE WHEN ($4::text) != '' THEN COALESCE((SELECT jsonb_agg(emoji) FROM public.post_reactions WHERE post_id = p.id AND user_id = $4::text::uuid), '[]'::jsonb) ELSE '[]'::jsonb END as my_reactions,
COALESCE(p.is_nsfw, FALSE) as is_nsfw,
COALESCE(p.nsfw_reason, '') as nsfw_reason
FROM public.posts p FROM public.posts p
JOIN public.profiles pr ON p.author_id = pr.id JOIN public.profiles pr ON p.author_id = pr.id
LEFT JOIN public.post_metrics m ON p.id = m.post_id LEFT JOIN public.post_metrics m ON p.id = m.post_id
@ -282,6 +284,7 @@ func (r *PostRepository) GetPostsByAuthor(ctx context.Context, authorID string,
&p.ID, &p.AuthorID, &p.CategoryID, &p.Body, &p.ImageURL, &p.VideoURL, &p.ThumbnailURL, &p.DurationMS, &p.Tags, &p.CreatedAt, &p.ID, &p.AuthorID, &p.CategoryID, &p.Body, &p.ImageURL, &p.VideoURL, &p.ThumbnailURL, &p.DurationMS, &p.Tags, &p.CreatedAt,
&p.AuthorHandle, &p.AuthorDisplayName, &p.AuthorAvatarURL, &p.AuthorHandle, &p.AuthorDisplayName, &p.AuthorAvatarURL,
&p.LikeCount, &p.CommentCount, &p.IsLiked, &p.AllowChain, &p.Visibility, &p.Reactions, &p.MyReactions, &p.LikeCount, &p.CommentCount, &p.IsLiked, &p.AllowChain, &p.Visibility, &p.Reactions, &p.MyReactions,
&p.IsNSFW, &p.NSFWReason,
) )
if err != nil { if err != nil {
return nil, err return nil, err
@ -319,7 +322,9 @@ func (r *PostRepository) GetPostByID(ctx context.Context, postID string, userID
pr.handle as author_handle, pr.display_name as author_display_name, COALESCE(pr.avatar_url, '') as author_avatar_url, pr.handle as author_handle, pr.display_name as author_display_name, COALESCE(pr.avatar_url, '') as author_avatar_url,
COALESCE(m.like_count, 0) as like_count, COALESCE(m.comment_count, 0) as comment_count, COALESCE(m.like_count, 0) as like_count, COALESCE(m.comment_count, 0) as comment_count,
CASE WHEN $2 != '' THEN EXISTS(SELECT 1 FROM public.post_likes WHERE post_id = p.id AND user_id = $2::uuid) ELSE FALSE END as is_liked, CASE WHEN $2 != '' THEN EXISTS(SELECT 1 FROM public.post_likes WHERE post_id = p.id AND user_id = $2::uuid) ELSE FALSE END as is_liked,
p.allow_chain, p.visibility p.allow_chain, p.visibility,
COALESCE(p.is_nsfw, FALSE) as is_nsfw,
COALESCE(p.nsfw_reason, '') as nsfw_reason
FROM public.posts p FROM public.posts p
JOIN public.profiles pr ON p.author_id = pr.id JOIN public.profiles pr ON p.author_id = pr.id
LEFT JOIN public.post_metrics m ON p.id = m.post_id LEFT JOIN public.post_metrics m ON p.id = m.post_id
@ -341,6 +346,7 @@ func (r *PostRepository) GetPostByID(ctx context.Context, postID string, userID
&p.AuthorHandle, &p.AuthorDisplayName, &p.AuthorAvatarURL, &p.AuthorHandle, &p.AuthorDisplayName, &p.AuthorAvatarURL,
&p.LikeCount, &p.CommentCount, &p.IsLiked, &p.LikeCount, &p.CommentCount, &p.IsLiked,
&p.AllowChain, &p.Visibility, &p.AllowChain, &p.Visibility,
&p.IsNSFW, &p.NSFWReason,
) )
if err != nil { if err != nil {
return nil, err return nil, err
@ -555,7 +561,9 @@ func (r *PostRepository) GetSavedPosts(ctx context.Context, userID string, limit
p.created_at, p.created_at,
pr.handle as author_handle, pr.display_name as author_display_name, COALESCE(pr.avatar_url, '') as author_avatar_url, pr.handle as author_handle, pr.display_name as author_display_name, COALESCE(pr.avatar_url, '') as author_avatar_url,
COALESCE(m.like_count, 0) as like_count, COALESCE(m.comment_count, 0) as comment_count, COALESCE(m.like_count, 0) as like_count, COALESCE(m.comment_count, 0) as comment_count,
EXISTS(SELECT 1 FROM public.post_likes WHERE post_id = p.id AND user_id = $1::uuid) as is_liked EXISTS(SELECT 1 FROM public.post_likes WHERE post_id = p.id AND user_id = $1::uuid) as is_liked,
COALESCE(p.is_nsfw, FALSE) as is_nsfw,
COALESCE(p.nsfw_reason, '') as nsfw_reason
FROM public.post_saves ps FROM public.post_saves ps
JOIN public.posts p ON ps.post_id = p.id JOIN public.posts p ON ps.post_id = p.id
JOIN public.profiles pr ON p.author_id = pr.id JOIN public.profiles pr ON p.author_id = pr.id
@ -577,6 +585,7 @@ func (r *PostRepository) GetSavedPosts(ctx context.Context, userID string, limit
&p.ID, &p.AuthorID, &p.CategoryID, &p.Body, &p.ImageURL, &p.VideoURL, &p.ThumbnailURL, &p.DurationMS, &p.Tags, &p.CreatedAt, &p.ID, &p.AuthorID, &p.CategoryID, &p.Body, &p.ImageURL, &p.VideoURL, &p.ThumbnailURL, &p.DurationMS, &p.Tags, &p.CreatedAt,
&p.AuthorHandle, &p.AuthorDisplayName, &p.AuthorAvatarURL, &p.AuthorHandle, &p.AuthorDisplayName, &p.AuthorAvatarURL,
&p.LikeCount, &p.CommentCount, &p.IsLiked, &p.LikeCount, &p.CommentCount, &p.IsLiked,
&p.IsNSFW, &p.NSFWReason,
) )
if err != nil { if err != nil {
return nil, err return nil, err
@ -604,7 +613,9 @@ func (r *PostRepository) GetLikedPosts(ctx context.Context, userID string, limit
p.created_at, p.created_at,
pr.handle as author_handle, pr.display_name as author_display_name, COALESCE(pr.avatar_url, '') as author_avatar_url, pr.handle as author_handle, pr.display_name as author_display_name, COALESCE(pr.avatar_url, '') as author_avatar_url,
COALESCE(m.like_count, 0) as like_count, COALESCE(m.comment_count, 0) as comment_count, COALESCE(m.like_count, 0) as like_count, COALESCE(m.comment_count, 0) as comment_count,
TRUE as is_liked TRUE as is_liked,
COALESCE(p.is_nsfw, FALSE) as is_nsfw,
COALESCE(p.nsfw_reason, '') as nsfw_reason
FROM public.post_likes pl FROM public.post_likes pl
JOIN public.posts p ON pl.post_id = p.id JOIN public.posts p ON pl.post_id = p.id
JOIN public.profiles pr ON p.author_id = pr.id JOIN public.profiles pr ON p.author_id = pr.id
@ -626,6 +637,7 @@ func (r *PostRepository) GetLikedPosts(ctx context.Context, userID string, limit
&p.ID, &p.AuthorID, &p.CategoryID, &p.Body, &p.ImageURL, &p.VideoURL, &p.ThumbnailURL, &p.DurationMS, &p.Tags, &p.CreatedAt, &p.ID, &p.AuthorID, &p.CategoryID, &p.Body, &p.ImageURL, &p.VideoURL, &p.ThumbnailURL, &p.DurationMS, &p.Tags, &p.CreatedAt,
&p.AuthorHandle, &p.AuthorDisplayName, &p.AuthorAvatarURL, &p.AuthorHandle, &p.AuthorDisplayName, &p.AuthorAvatarURL,
&p.LikeCount, &p.CommentCount, &p.IsLiked, &p.LikeCount, &p.CommentCount, &p.IsLiked,
&p.IsNSFW, &p.NSFWReason,
) )
if err != nil { if err != nil {
return nil, err return nil, err
@ -656,6 +668,7 @@ func (r *PostRepository) GetPostChain(ctx context.Context, rootID string) ([]mod
p.created_at, p.chain_parent_id, p.created_at, p.chain_parent_id,
pr.handle as author_handle, pr.display_name as author_display_name, COALESCE(pr.avatar_url, '') as author_avatar_url, pr.handle as author_handle, pr.display_name as author_display_name, COALESCE(pr.avatar_url, '') as author_avatar_url,
COALESCE(m.like_count, 0) as like_count, COALESCE(m.comment_count, 0) as comment_count, COALESCE(m.like_count, 0) as like_count, COALESCE(m.comment_count, 0) as comment_count,
COALESCE(p.is_nsfw, FALSE) as is_nsfw, COALESCE(p.nsfw_reason, '') as nsfw_reason,
1 as level 1 as level
FROM public.posts p FROM public.posts p
JOIN public.profiles pr ON p.author_id = pr.id JOIN public.profiles pr ON p.author_id = pr.id
@ -675,6 +688,7 @@ func (r *PostRepository) GetPostChain(ctx context.Context, rootID string) ([]mod
p.created_at, p.chain_parent_id, p.created_at, p.chain_parent_id,
pr.handle as author_handle, pr.display_name as author_display_name, COALESCE(pr.avatar_url, '') as author_avatar_url, pr.handle as author_handle, pr.display_name as author_display_name, COALESCE(pr.avatar_url, '') as author_avatar_url,
COALESCE(m.like_count, 0) as like_count, COALESCE(m.comment_count, 0) as comment_count, COALESCE(m.like_count, 0) as like_count, COALESCE(m.comment_count, 0) as comment_count,
COALESCE(p.is_nsfw, FALSE) as is_nsfw, COALESCE(p.nsfw_reason, '') as nsfw_reason,
oc.level + 1 oc.level + 1
FROM public.posts p FROM public.posts p
JOIN public.profiles pr ON p.author_id = pr.id JOIN public.profiles pr ON p.author_id = pr.id
@ -700,6 +714,7 @@ func (r *PostRepository) GetPostChain(ctx context.Context, rootID string) ([]mod
COALESCE(pr.avatar_url, '') as author_avatar_url, COALESCE(pr.avatar_url, '') as author_avatar_url,
0 as like_count, 0 as like_count,
0 as comment_count, 0 as comment_count,
FALSE as is_nsfw, '' as nsfw_reason,
2 as level 2 as level
FROM public.comments c FROM public.comments c
JOIN public.profiles pr ON c.author_id = pr.id JOIN public.profiles pr ON c.author_id = pr.id
@ -709,13 +724,13 @@ func (r *PostRepository) GetPostChain(ctx context.Context, rootID string) ([]mod
SELECT SELECT
id, author_id, category_id, body, image_url, video_url, thumbnail_url, duration_ms, tags, created_at, chain_parent_id, level, id, author_id, category_id, body, image_url, video_url, thumbnail_url, duration_ms, tags, created_at, chain_parent_id, level,
author_handle, author_display_name, author_avatar_url, author_handle, author_display_name, author_avatar_url,
like_count, comment_count, FALSE as is_liked like_count, comment_count, is_nsfw, nsfw_reason, FALSE as is_liked
FROM object_chain FROM object_chain
UNION ALL UNION ALL
SELECT SELECT
id, author_id, category_id, body, image_url, video_url, thumbnail_url, duration_ms, tags, created_at, chain_parent_id, level, id, author_id, category_id, body, image_url, video_url, thumbnail_url, duration_ms, tags, created_at, chain_parent_id, level,
author_handle, author_display_name, author_avatar_url, author_handle, author_display_name, author_avatar_url,
like_count, comment_count, FALSE as is_liked like_count, comment_count, is_nsfw, nsfw_reason, FALSE as is_liked
FROM comments_chain FROM comments_chain
ORDER BY level ASC, created_at ASC; ORDER BY level ASC, created_at ASC;
` `
@ -733,7 +748,7 @@ func (r *PostRepository) GetPostChain(ctx context.Context, rootID string) ([]mod
err := rows.Scan( err := rows.Scan(
&p.ID, &p.AuthorID, &p.CategoryID, &p.Body, &p.ImageURL, &p.VideoURL, &p.ThumbnailURL, &p.DurationMS, &p.Tags, &p.CreatedAt, &p.ChainParentID, &level, &p.ID, &p.AuthorID, &p.CategoryID, &p.Body, &p.ImageURL, &p.VideoURL, &p.ThumbnailURL, &p.DurationMS, &p.Tags, &p.CreatedAt, &p.ChainParentID, &level,
&p.AuthorHandle, &p.AuthorDisplayName, &p.AuthorAvatarURL, &p.AuthorHandle, &p.AuthorDisplayName, &p.AuthorAvatarURL,
&p.LikeCount, &p.CommentCount, &p.IsLiked, &p.LikeCount, &p.CommentCount, &p.IsNSFW, &p.NSFWReason, &p.IsLiked,
) )
if err != nil { if err != nil {
return nil, err return nil, err
@ -996,7 +1011,9 @@ func (r *PostRepository) GetPostFocusContext(ctx context.Context, postID string,
pr.handle as author_handle, pr.display_name as author_display_name, COALESCE(pr.avatar_url, '') as author_avatar_url, pr.handle as author_handle, pr.display_name as author_display_name, COALESCE(pr.avatar_url, '') as author_avatar_url,
COALESCE(m.like_count, 0) as like_count, COALESCE(m.comment_count, 0) as comment_count, COALESCE(m.like_count, 0) as like_count, COALESCE(m.comment_count, 0) as comment_count,
CASE WHEN $2 != '' THEN EXISTS(SELECT 1 FROM public.post_likes WHERE post_id = p.id AND user_id = $2::uuid) ELSE FALSE END as is_liked, CASE WHEN $2 != '' THEN EXISTS(SELECT 1 FROM public.post_likes WHERE post_id = p.id AND user_id = $2::uuid) ELSE FALSE END as is_liked,
p.allow_chain, p.visibility p.allow_chain, p.visibility,
COALESCE(p.is_nsfw, FALSE) as is_nsfw,
COALESCE(p.nsfw_reason, '') as nsfw_reason
FROM public.posts p FROM public.posts p
JOIN public.profiles pr ON p.author_id = pr.id JOIN public.profiles pr ON p.author_id = pr.id
LEFT JOIN public.post_metrics m ON p.id = m.post_id LEFT JOIN public.post_metrics m ON p.id = m.post_id
@ -1025,6 +1042,7 @@ func (r *PostRepository) GetPostFocusContext(ctx context.Context, postID string,
&p.AuthorHandle, &p.AuthorDisplayName, &p.AuthorAvatarURL, &p.AuthorHandle, &p.AuthorDisplayName, &p.AuthorAvatarURL,
&p.LikeCount, &p.CommentCount, &p.IsLiked, &p.LikeCount, &p.CommentCount, &p.IsLiked,
&p.AllowChain, &p.Visibility, &p.AllowChain, &p.Visibility,
&p.IsNSFW, &p.NSFWReason,
) )
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to scan child post: %w", err) return nil, fmt.Errorf("failed to scan child post: %w", err)
@ -1065,6 +1083,7 @@ func (r *PostRepository) GetPostFocusContext(ctx context.Context, postID string,
&p.AuthorHandle, &p.AuthorDisplayName, &p.AuthorAvatarURL, &p.AuthorHandle, &p.AuthorDisplayName, &p.AuthorAvatarURL,
&p.LikeCount, &p.CommentCount, &p.IsLiked, &p.LikeCount, &p.CommentCount, &p.IsLiked,
&p.AllowChain, &p.Visibility, &p.AllowChain, &p.Visibility,
&p.IsNSFW, &p.NSFWReason,
) )
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to scan parent child post: %w", err) return nil, fmt.Errorf("failed to scan parent child post: %w", err)