2372f5c054
- Add new swarm interaction endpoints for follow, unfollow, like, unlike, mention, and repost actions - Create interactions.ts library module to handle federated interaction logic - Add swarm post detail endpoint for retrieving individual post information - Update post interaction routes to support federated operations - Replace logo.svg with new logotext.svg for updated branding - Update login page and sidebar components with new branding assets - Enhance swarm discovery and type definitions to support new interaction patterns - Update user follow endpoint to work with federated swarm interactions
64 lines
1.7 KiB
TypeScript
64 lines
1.7 KiB
TypeScript
/**
|
|
* Swarm Unlike Endpoint
|
|
*
|
|
* POST: Receive an unlike from another swarm node
|
|
*/
|
|
|
|
import { NextRequest, NextResponse } from 'next/server';
|
|
import { db, posts } from '@/db';
|
|
import { eq } from 'drizzle-orm';
|
|
import { z } from 'zod';
|
|
|
|
const swarmUnlikeSchema = z.object({
|
|
postId: z.string().uuid(),
|
|
unlike: z.object({
|
|
actorHandle: z.string(),
|
|
actorNodeDomain: z.string(),
|
|
interactionId: z.string(),
|
|
timestamp: z.string(),
|
|
}),
|
|
});
|
|
|
|
/**
|
|
* POST /api/swarm/interactions/unlike
|
|
*
|
|
* Receives an unlike from another swarm node.
|
|
*/
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
if (!db) {
|
|
return NextResponse.json({ error: 'Database not available' }, { status: 503 });
|
|
}
|
|
|
|
const body = await request.json();
|
|
const data = swarmUnlikeSchema.parse(body);
|
|
|
|
// Find the target post
|
|
const post = await db.query.posts.findFirst({
|
|
where: eq(posts.id, data.postId),
|
|
});
|
|
|
|
if (!post) {
|
|
return NextResponse.json({ error: 'Post not found' }, { status: 404 });
|
|
}
|
|
|
|
// Decrement like count (don't go below 0)
|
|
await db.update(posts)
|
|
.set({ likesCount: Math.max(0, post.likesCount - 1) })
|
|
.where(eq(posts.id, data.postId));
|
|
|
|
console.log(`[Swarm] Received unlike from ${data.unlike.actorHandle}@${data.unlike.actorNodeDomain} on post ${data.postId}`);
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
message: 'Unlike received',
|
|
});
|
|
} catch (error) {
|
|
if (error instanceof z.ZodError) {
|
|
return NextResponse.json({ error: 'Invalid request', details: error.issues }, { status: 400 });
|
|
}
|
|
console.error('[Swarm] Unlike error:', error);
|
|
return NextResponse.json({ error: 'Failed to process unlike' }, { status: 500 });
|
|
}
|
|
}
|