Fixed post duplication

This commit is contained in:
Christopher
2026-01-22 16:36:32 -08:00
parent d2d8d9b746
commit 7ff8b36ead
4 changed files with 143 additions and 57 deletions
@@ -134,6 +134,11 @@ export async function POST(request: Request, context: RouteContext) {
activityId,
});
// Update the user's following count
await db.update(users)
.set({ followingCount: currentUser.followingCount + 1 })
.where(eq(users.id, currentUser.id));
// Cache the remote user's recent posts in the background
const origin = new URL(request.url).origin;
cacheRemoteUserPosts(remoteProfile, targetHandle, origin, 20)
@@ -251,6 +256,12 @@ export async function DELETE(request: Request, context: RouteContext) {
return NextResponse.json({ error: result.error || 'Failed to unfollow remote user' }, { status: 502 });
}
await db.delete(remoteFollows).where(eq(remoteFollows.id, existingRemoteFollow.id));
// Update the user's following count
await db.update(users)
.set({ followingCount: Math.max(0, currentUser.followingCount - 1) })
.where(eq(users.id, currentUser.id));
return NextResponse.json({ success: true, following: false, remote: true });
}
+31 -10
View File
@@ -1,5 +1,5 @@
import { NextResponse } from 'next/server';
import { db, follows, users } from '@/db';
import { db, follows, users, remoteFollows } from '@/db';
import { eq } from 'drizzle-orm';
type RouteContext = { params: Promise<{ handle: string }> };
@@ -28,7 +28,7 @@ export async function GET(request: Request, context: RouteContext) {
return NextResponse.json({ error: 'User not found' }, { status: 404 });
}
// Get following
// Get local following
const userFollowing = await db.query.follows.findMany({
where: eq(follows.followerId, user.id),
with: {
@@ -37,15 +37,36 @@ export async function GET(request: Request, context: RouteContext) {
limit,
});
const localFollowing = userFollowing.map(f => ({
id: f.following.id,
handle: f.following.handle,
displayName: f.following.displayName,
avatarUrl: f.following.avatarUrl,
bio: f.following.bio,
isRemote: false,
}));
// Get remote following
const userRemoteFollowing = await db.query.remoteFollows.findMany({
where: eq(remoteFollows.followerId, user.id),
limit,
});
const remoteFollowing = userRemoteFollowing.map(f => ({
id: f.targetActorUrl,
handle: f.targetHandle,
displayName: f.targetHandle.split('@')[0], // Use username part as display name
avatarUrl: null,
bio: null,
isRemote: true,
}));
// Merge and return
const allFollowing = [...localFollowing, ...remoteFollowing].slice(0, limit);
return NextResponse.json({
following: userFollowing.map(f => ({
id: f.following.id,
handle: f.following.handle,
displayName: f.following.displayName,
avatarUrl: f.following.avatarUrl,
bio: f.following.bio,
})),
nextCursor: userFollowing.length === limit ? userFollowing[userFollowing.length - 1]?.id : null,
following: allFollowing,
nextCursor: allFollowing.length === limit ? allFollowing[allFollowing.length - 1]?.id : null,
});
} catch (error) {
console.error('Get following error:', error);