feat(admin): hide posts without deleting (Gitea #10 Phase 3, part 1/4)

Adds Post.hiddenAt (nullable, db push applied) plus MODERATOR+ admin
mutations hidePost/unhidePost, each logging HIDE_POST/UNHIDE_POST to
ModerationLog with the standard moderatorRole+ipAddress stamp.

hidePost requires a non-empty reason (visible to nobody but audit
trail); unhidePost is a plain reversal. The admin posts grid shows a
"Hidden" overlay badge and swaps the delete-adjacent action button
between hide/unhide based on hiddenAt.

Every reader-facing post query now filters hiddenAt: null so a hidden
post disappears everywhere without touching its row: feed (discovery
candidates, Postgres fallback, and the Redis-postId lookup — the same
place blocked-pet filtering already happens, so an already-fanned-out
hidden post silently drops out at read time), feed backfill on new
follows, explore trending, profile grid, hashtag search, and the
milestones tab.

Admin-facing queries (listPosts, analytics bucketing) are deliberately
NOT filtered — moderators need to see hidden posts to unhide them, and
volume analytics shouldn't be skewed by moderation actions.

Gitea #10
This commit is contained in:
admin committed 2026-08-12 14:23:00 +02:00
1 parent 9f9db9e742
commit a29c57e37a
9 files changed
+158 -5

No files matched your search

+8 -1
View File
@@ -246,6 +246,13 @@ model Post {
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
// Moderation "hide without delete" (Gitea #10 Phase 3) — set by an admin
// HIDE_POST action, cleared by UNHIDE_POST. Data stays intact for record
// purposes; every reader-facing query (feed/explore/profile/search/
// hashtag/milestones) filters hiddenAt: null. Admin views intentionally
// do NOT filter on this so moderators can still see + unhide.
hiddenAt DateTime?
// Denormalized engagement counters — kept in sync transactionally at each
// reaction/comment/repost create+delete site (reactions.ts, comments.ts,
// reposts.ts, admin.ts deleteComment). Avoids live _count aggregation on
@@ -686,7 +693,7 @@ model AdRepost {
model ModerationLog {
id String @id @default(cuid())
action String @db.VarChar(50) // DELETE_POST | DELETE_COMMENT | BAN_USER | UNBAN_USER | DELETE_IMAGE | DISMISS_REPORT | CREATE_AD | UPDATE_AD | DELETE_AD | GRANT_ROLE | REVOKE_ROLE | WARN_USER | VERIFY_PET | UNVERIFY_PET | DELETE_ACCOUNT | APPROVE_VERIFICATION_REQUEST | REJECT_VERIFICATION_REQUEST
action String @db.VarChar(50) // DELETE_POST | DELETE_COMMENT | BAN_USER | UNBAN_USER | DELETE_IMAGE | DISMISS_REPORT | CREATE_AD | UPDATE_AD | DELETE_AD | GRANT_ROLE | REVOKE_ROLE | WARN_USER | VERIFY_PET | UNVERIFY_PET | DELETE_ACCOUNT | APPROVE_VERIFICATION_REQUEST | REJECT_VERIFICATION_REQUEST | HIDE_POST | UNHIDE_POST
targetType String @db.VarChar(20) // POST | COMMENT | USER | IMAGE | AD | REPORT | PET
targetId String
reason String? @db.VarChar(500)
+88 -1
View File
@@ -3,7 +3,7 @@
import { useEffect, useState } from "react";
import { useInfiniteQuery, useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { useTRPC } from "@/trpc/client";
import { Trash2, MessageSquare, Loader2, ChevronDown, Play, ImageOff, PawPrint } from "lucide-react";
import { Trash2, MessageSquare, Loader2, ChevronDown, Play, ImageOff, PawPrint, EyeOff, Eye } from "lucide-react";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter } from "@/components/ui/dialog";
@@ -45,7 +45,9 @@ export default function AdminPostsPage() {
}
const [commentsPostId, setCommentsPostId] = useState<string | null>(null);
const [deleteTarget, setDeleteTarget] = useState<{ id: string; type: "post" | "comment" } | null>(null);
const [hideTargetId, setHideTargetId] = useState<string | null>(null);
const [reason, setReason] = useState("");
const [hideReason, setHideReason] = useState("");
const { data, fetchNextPage, hasNextPage, isFetchingNextPage, isLoading } = useInfiniteQuery(
trpc.admin.listPosts.infiniteQueryOptions(
@@ -85,6 +87,29 @@ export default function AdminPostsPage() {
})
);
const hidePost = useMutation(
trpc.admin.hidePost.mutationOptions({
onSuccess: () => {
void qc.invalidateQueries({ queryKey: trpc.admin.listPosts.queryKey() });
setHideTargetId(null);
setHideReason("");
},
})
);
const unhidePost = useMutation(
trpc.admin.unhidePost.mutationOptions({
onSuccess: () => {
void qc.invalidateQueries({ queryKey: trpc.admin.listPosts.queryKey() });
},
})
);
function confirmHide() {
if (!hideTargetId || !hideReason.trim()) return;
hidePost.mutate({ postId: hideTargetId, reason: hideReason.trim() });
}
function confirmDelete() {
if (!deleteTarget) return;
if (deleteTarget.type === "post") {
@@ -180,6 +205,14 @@ export default function AdminPostsPage() {
<Play className="h-3.5 w-3.5 text-white fill-white" aria-hidden="true" />
</div>
)}
{post.hiddenAt && (
<div className="absolute inset-0 flex items-center justify-center bg-black/60">
<span className="flex items-center gap-1.5 rounded-full bg-zinc-900/90 px-2.5 py-1 text-xs font-medium text-zinc-200">
<EyeOff className="h-3.5 w-3.5" aria-hidden="true" />
Hidden
</span>
</div>
)}
</div>
<div className="flex flex-1 flex-col gap-2 p-3">
@@ -209,6 +242,26 @@ export default function AdminPostsPage() {
>
<MessageSquare className="h-4 w-4" />
</Button>
{post.hiddenAt ? (
<Button
size="sm"
variant="ghost"
className="text-zinc-400 hover:text-zinc-100"
disabled={unhidePost.isPending}
onClick={() => unhidePost.mutate({ postId: post.id })}
>
<Eye className="h-4 w-4" />
</Button>
) : (
<Button
size="sm"
variant="ghost"
className="text-zinc-400 hover:text-zinc-100"
onClick={() => setHideTargetId(post.id)}
>
<EyeOff className="h-4 w-4" />
</Button>
)}
<Button
size="sm"
variant="ghost"
@@ -316,6 +369,40 @@ export default function AdminPostsPage() {
</DialogFooter>
</DialogContent>
</Dialog>
{/* Hide post dialog */}
<Dialog
open={!!hideTargetId}
onOpenChange={(o) => { if (!o) { setHideTargetId(null); setHideReason(""); } }}
>
<DialogContent className="bg-zinc-900 border-zinc-800 text-zinc-100 max-w-sm">
<DialogHeader>
<DialogTitle>Hide post</DialogTitle>
</DialogHeader>
<p className="text-sm text-zinc-400">
Hides the post everywhere (feed, explore, profile, search, hashtags) without
deleting it — reversible via the eye icon.
</p>
<Input
placeholder="Reason (required)"
value={hideReason}
onChange={(e) => setHideReason(e.target.value)}
className="bg-zinc-800 border-zinc-700 text-zinc-100 placeholder:text-zinc-500"
/>
<DialogFooter className="gap-2">
<Button variant="ghost" className="text-zinc-400" onClick={() => setHideTargetId(null)}>
Cancel
</Button>
<Button
onClick={confirmHide}
disabled={hidePost.isPending || !hideReason.trim()}
>
{hidePost.isPending && <Loader2 className="h-4 w-4 animate-spin mr-2" />}
Hide
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
</div>
);
}
+1 -1
View File
@@ -57,7 +57,7 @@ export async function backfillOnFollow(
limit = 20
): Promise<void> {
const recentPosts = await prisma.post.findMany({
where: { petId: followeePetId },
where: { petId: followeePetId, hiddenAt: null },
orderBy: { createdAt: "desc" },
take: limit,
select: { id: true, createdAt: true },
+54
View File
@@ -288,6 +288,60 @@ export const adminRouter = router({
return { success: true };
}),
hidePost: protectedProcedure
.input(z.object({ postId: z.string().min(1), reason: z.string().min(1).max(500) }))
.mutation(async ({ ctx, input }) => {
const callerRole = await assertAdmin(ctx.prisma, ctx.userId, AdminRoleType.MODERATOR);
const post = await ctx.prisma.post.findUnique({ where: { id: input.postId } });
if (!post) throw new TRPCError({ code: "NOT_FOUND" });
await ctx.prisma.$transaction([
ctx.prisma.post.update({
where: { id: input.postId },
data: { hiddenAt: new Date() },
}),
ctx.prisma.moderationLog.create({
data: {
action: "HIDE_POST",
targetType: "POST",
targetId: input.postId,
reason: input.reason,
moderatorId: ctx.userId,
moderatorRole: callerRole,
ipAddress: ctx.ip,
},
}),
]);
return { success: true };
}),
unhidePost: protectedProcedure
.input(z.object({ postId: z.string().min(1) }))
.mutation(async ({ ctx, input }) => {
const callerRole = await assertAdmin(ctx.prisma, ctx.userId, AdminRoleType.MODERATOR);
const post = await ctx.prisma.post.findUnique({ where: { id: input.postId } });
if (!post) throw new TRPCError({ code: "NOT_FOUND" });
await ctx.prisma.$transaction([
ctx.prisma.post.update({
where: { id: input.postId },
data: { hiddenAt: null },
}),
ctx.prisma.moderationLog.create({
data: {
action: "UNHIDE_POST",
targetType: "POST",
targetId: input.postId,
reason: null,
moderatorId: ctx.userId,
moderatorRole: callerRole,
ipAddress: ctx.ip,
},
}),
]);
return { success: true };
}),
listComments: protectedProcedure
.input(z.object({ postId: z.string().min(1) }))
.query(async ({ ctx, input }) => {
+1 -1
View File
@@ -60,7 +60,7 @@ export const exploreRouter = router({
const [posts, hashtags] = await Promise.all([
ctx.prisma.post.findMany({
where: { createdAt: { gte: since48h }, type: "PHOTO" },
where: { createdAt: { gte: since48h }, type: "PHOTO", hiddenAt: null },
include: {
images: { orderBy: { position: "asc" } },
pet: { select: { id: true, name: true, avatarKey: true } },
+3
View File
@@ -63,6 +63,7 @@ async function getDiscoveryPosts(
const candidates = await prisma.post.findMany({
where: {
hiddenAt: null,
pet: {
speciesId: viewerPet.speciesId,
id: { notIn: [viewerPet.id, ...blockedPetIds] },
@@ -122,6 +123,7 @@ export const feedRouter = router({
const rawPosts = await ctx.prisma.post.findMany({
where: {
hiddenAt: null,
petId: { in: petIds, notIn: blockedPetIds },
...(cursorDate ? { createdAt: { lt: cursorDate } } : {}),
},
@@ -165,6 +167,7 @@ export const feedRouter = router({
const rawPosts = await ctx.prisma.post.findMany({
where: {
id: { in: postIds },
hiddenAt: null,
pet: { id: { notIn: blockedPetIds } },
},
include: postInclude,
+1 -1
View File
@@ -86,7 +86,7 @@ export const milestonesRouter = router({
)
.query(async ({ ctx, input }) => {
return ctx.prisma.post.findMany({
where: { petId: input.petId, type: "MILESTONE" },
where: { petId: input.petId, type: "MILESTONE", hiddenAt: null },
include: {
milestone: true,
images: { orderBy: { position: "asc" } },
+1
View File
@@ -207,6 +207,7 @@ export const postsRouter = router({
const posts = await ctx.prisma.post.findMany({
where: {
petId: input.petId,
hiddenAt: null,
...(input.cursor
? { createdAt: { lt: new Date(input.cursor) } }
: {}),
+1
View File
@@ -66,6 +66,7 @@ export const searchRouter = router({
const rows = await ctx.prisma.postHashtag.findMany({
where: {
hashtagId: hashtag.id,
post: { hiddenAt: null },
...(input.cursor ? { postId: { lt: input.cursor } } : {}),
},
take: input.limit + 1,