Fix reply consistency and source validation
This commit is contained in:
@@ -70,7 +70,7 @@ export async function GET(request: NextRequest, context: RouteContext) {
|
||||
createdAt: post.createdAt.toISOString(),
|
||||
likesCount: post.likesCount,
|
||||
repostsCount: post.repostsCount,
|
||||
repliesCount: post.repliesCount,
|
||||
repliesCount: replies.length,
|
||||
author: {
|
||||
handle: author.handle,
|
||||
displayName: author.displayName,
|
||||
|
||||
@@ -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 });
|
||||
|
||||
@@ -12,6 +12,9 @@ export interface SwarmPost {
|
||||
id: string;
|
||||
content: string;
|
||||
createdAt: string;
|
||||
isReply?: boolean;
|
||||
replyToId?: string | null;
|
||||
swarmReplyToId?: string | null;
|
||||
author: {
|
||||
handle: string;
|
||||
displayName: string;
|
||||
@@ -84,6 +87,8 @@ export async function GET(request: NextRequest) {
|
||||
id: posts.id,
|
||||
content: posts.content,
|
||||
createdAt: posts.createdAt,
|
||||
replyToId: posts.replyToId,
|
||||
swarmReplyToId: posts.swarmReplyToId,
|
||||
isNsfw: posts.isNsfw,
|
||||
likesCount: posts.likesCount,
|
||||
repostsCount: posts.repostsCount,
|
||||
@@ -120,6 +125,9 @@ export async function GET(request: NextRequest) {
|
||||
id: post.id,
|
||||
content: post.content,
|
||||
createdAt: post.createdAt.toISOString(),
|
||||
isReply: Boolean(post.replyToId || post.swarmReplyToId),
|
||||
replyToId: post.replyToId,
|
||||
swarmReplyToId: post.swarmReplyToId,
|
||||
author: {
|
||||
handle: post.authorHandle,
|
||||
displayName: post.authorDisplayName || post.authorHandle,
|
||||
|
||||
Reference in New Issue
Block a user