Rate-limiting on messages.send, reports.create, reactions.toggle, follows.create, reposts.create. Unique constraints on Report to stop report-flooding. Mux webhook now fails hard in production without a signing secret instead of skipping verification. Admin-panel outline buttons fixed on 4 more pages (same white-on-dark bug as the Users page). Timing-safe comparison for ADMIN_SECRET/CRON_SECRET. Fixed duplicate HSTS header (Cloudflare's stronger one was being shadowed by the app's own). tRPC now returns clean JSON 401s instead of HTML redirects when unauthenticated, and no longer blocks public procedures for logged-out visitors. 14 previously-silent catch blocks now log to GlitchTip. invite/page.tsx and EmergencyVetSection.tsx fully localized (were 100% hardcoded German). Stale TODO comments removed, auth.test.ts scaffold rewritten to match the actual lazy-upsert pattern instead of a Clerk webhook flow that was never built.
121 lines
3.5 KiB
TypeScript
121 lines
3.5 KiB
TypeScript
import "server-only";
|
|
import { z } from "zod";
|
|
import { TRPCError } from "@trpc/server";
|
|
import { router, protectedProcedure, rateLimited } from "@/trpc/init";
|
|
import { createMentionNotifications } from "@/lib/mention-helpers";
|
|
import { assertPetOwnership } from "@/lib/assert-pet-ownership";
|
|
import { notify } from "@/lib/notify";
|
|
|
|
export const commentsRouter = router({
|
|
create: protectedProcedure
|
|
.use(rateLimited("comments.create", 20, 300))
|
|
.input(
|
|
z.object({
|
|
postId: z.string().min(1),
|
|
petId: z.string().min(1),
|
|
body: z.string().min(1).max(500),
|
|
})
|
|
)
|
|
.mutation(async ({ ctx, input }) => {
|
|
await assertPetOwnership(ctx.prisma, input.petId, ctx.userId);
|
|
|
|
const [comment, post] = await ctx.prisma.$transaction([
|
|
ctx.prisma.comment.create({
|
|
data: {
|
|
postId: input.postId,
|
|
petId: input.petId,
|
|
body: input.body,
|
|
},
|
|
include: {
|
|
pet: { select: { id: true, name: true, avatarKey: true } },
|
|
},
|
|
}),
|
|
ctx.prisma.post.update({
|
|
where: { id: input.postId },
|
|
data: { commentCount: { increment: 1 } },
|
|
select: { petId: true },
|
|
}),
|
|
]);
|
|
|
|
// NOTF-03: notify post owner (skip self-comments)
|
|
if (post.petId !== input.petId) {
|
|
notify(ctx.prisma, {
|
|
recipientPetId: post.petId,
|
|
type: "COMMENT",
|
|
actorPetId: input.petId,
|
|
postId: input.postId,
|
|
commentId: comment.id,
|
|
});
|
|
}
|
|
|
|
// Notify all @mentioned pets in the comment body
|
|
createMentionNotifications(
|
|
ctx.prisma,
|
|
input.body,
|
|
input.petId,
|
|
input.postId,
|
|
comment.id
|
|
).catch((err) => console.error("[comments] mention notifications failed", err));
|
|
|
|
return comment;
|
|
}),
|
|
|
|
listByPost: protectedProcedure
|
|
.input(
|
|
z.object({
|
|
postId: z.string().min(1),
|
|
limit: z.number().int().min(1).max(50).default(20),
|
|
cursor: z.string().optional(), // comment id cursor
|
|
})
|
|
)
|
|
.query(async ({ ctx, input }) => {
|
|
const comments = await ctx.prisma.comment.findMany({
|
|
where: {
|
|
postId: input.postId,
|
|
...(input.cursor ? { id: { gt: input.cursor } } : {}),
|
|
},
|
|
orderBy: { createdAt: "asc" },
|
|
take: input.limit + 1,
|
|
include: {
|
|
pet: { select: { id: true, name: true, avatarKey: true } },
|
|
},
|
|
});
|
|
|
|
const hasNextPage = comments.length > input.limit;
|
|
if (hasNextPage) comments.pop();
|
|
return {
|
|
comments,
|
|
nextCursor: hasNextPage ? comments[comments.length - 1]!.id : null,
|
|
};
|
|
}),
|
|
|
|
delete: protectedProcedure
|
|
.input(z.object({ commentId: z.string().min(1), petId: z.string().min(1) }))
|
|
.mutation(async ({ ctx, input }): Promise<void> => {
|
|
await assertPetOwnership(ctx.prisma, input.petId, ctx.userId);
|
|
|
|
const comment = await ctx.prisma.comment.findUnique({
|
|
where: { id: input.commentId },
|
|
});
|
|
|
|
if (!comment) {
|
|
throw new TRPCError({ code: "NOT_FOUND" });
|
|
}
|
|
|
|
if (comment.petId !== input.petId) {
|
|
throw new TRPCError({
|
|
code: "FORBIDDEN",
|
|
message: "You can only delete your own comments.",
|
|
});
|
|
}
|
|
|
|
await ctx.prisma.$transaction([
|
|
ctx.prisma.comment.delete({ where: { id: input.commentId } }),
|
|
ctx.prisma.post.update({
|
|
where: { id: comment.postId },
|
|
data: { commentCount: { decrement: 1 } },
|
|
}),
|
|
]);
|
|
}),
|
|
});
|