Fix reply consistency and source validation

This commit is contained in:
cyph3rasi
2026-03-08 18:29:42 -07:00
parent a8f104ee14
commit 5f21ea8e5d
20 changed files with 545 additions and 276 deletions
+21 -15
View File
@@ -7,7 +7,7 @@
import { NextRequest, NextResponse } from 'next/server';
import { db, posts, users, media, notifications } from '@/db';
import { eq, desc, and } from 'drizzle-orm';
import { eq, desc, and, sql } from 'drizzle-orm';
import { z } from 'zod';
import { verifySwarmRequest } from '@/lib/swarm/signature';
import { upsertRemoteUser } from '@/lib/swarm/user-cache';
@@ -31,6 +31,20 @@ const swarmReplySchema = z.object({
}),
});
async function syncParentReplyCount(postId: string) {
const [{ count }] = await db
.select({ count: sql<number>`count(*)::int` })
.from(posts)
.where(and(
eq(posts.replyToId, postId),
eq(posts.isRemoved, false)
));
await db.update(posts)
.set({ repliesCount: Number(count || 0) })
.where(eq(posts.id, postId));
}
/**
* POST /api/swarm/replies
*
@@ -129,9 +143,7 @@ export async function POST(request: NextRequest) {
);
}
await db.update(posts)
.set({ repliesCount: parentPost.repliesCount + 1 })
.where(eq(posts.id, data.postId));
await syncParentReplyCount(data.postId);
if (parentPost.userId !== remoteUser.id) {
await db.insert(notifications).values({
@@ -183,21 +195,15 @@ export async function DELETE(request: NextRequest) {
return NextResponse.json({ success: true, message: 'Reply not found or already deleted' });
}
// Decrement parent's reply count
if (existingReply.replyToId) {
const parentPost = await db.query.posts.findFirst({
where: eq(posts.id, existingReply.replyToId),
});
if (parentPost && parentPost.repliesCount > 0) {
await db.update(posts)
.set({ repliesCount: parentPost.repliesCount - 1 })
.where(eq(posts.id, existingReply.replyToId));
}
}
const parentReplyToId = existingReply.replyToId;
// Delete the reply
await db.delete(posts).where(eq(posts.id, existingReply.id));
if (parentReplyToId) {
await syncParentReplyCount(parentReplyToId);
}
console.log(`[Swarm] Deleted reply ${swarmReplyId} from ${nodeDomain}`);
return NextResponse.json({ success: true });