feat(swarm): Add direct swarm interaction delivery for likes, reposts, and unreosts

- Add swarm post handling to like endpoint with direct delivery to origin node
- Add swarm post handling to repost endpoint with direct delivery to origin node
- Add new unrepost endpoint for removing reposts from swarm posts
- Implement deliverSwarmUnrepost function in interactions library
- Extract swarm domain and post ID validation for all interaction types
- Return appropriate error responses for invalid swarm post IDs
- Log successful swarm interaction deliveries for debugging
- Improve separation between local post and swarm post handling logic
- Enable federated users to interact with remote swarm posts directly without local caching
This commit is contained in:
Christomatt
2026-01-26 13:04:05 +01:00
parent 12f515b7fb
commit f361e13e53
4 changed files with 222 additions and 6 deletions
+68 -2
View File
@@ -41,7 +41,41 @@ export async function POST(request: Request, context: RouteContext) {
return NextResponse.json({ error: 'Account restricted' }, { status: 403 });
}
// Check if post exists
// Handle swarm posts (format: swarm:domain:uuid)
if (postId.startsWith('swarm:')) {
const targetDomain = extractSwarmDomain(postId);
const originalPostId = postId.split(':')[2];
if (!targetDomain || !originalPostId) {
return NextResponse.json({ error: 'Invalid swarm post ID' }, { status: 400 });
}
// Deliver repost directly to the origin node
const { deliverSwarmRepost } = await import('@/lib/swarm/interactions');
const result = await deliverSwarmRepost(targetDomain, {
postId: originalPostId,
repost: {
actorHandle: user.handle,
actorDisplayName: user.displayName || user.handle,
actorAvatarUrl: user.avatarUrl || undefined,
actorNodeDomain: nodeDomain,
repostId: crypto.randomUUID(),
interactionId: crypto.randomUUID(),
timestamp: new Date().toISOString(),
},
});
if (!result.success) {
console.error(`[Swarm] Repost delivery failed: ${result.error}`);
return NextResponse.json({ error: 'Failed to deliver repost to remote node' }, { status: 502 });
}
console.log(`[Swarm] Repost delivered to ${targetDomain} for post ${originalPostId}`);
return NextResponse.json({ success: true, reposted: true });
}
// Local post - check if it exists
const originalPost = await db.query.posts.findFirst({
where: eq(posts.id, postId),
});
@@ -196,12 +230,44 @@ export async function DELETE(request: Request, context: RouteContext) {
try {
const user = await requireAuth();
const { id: postId } = await context.params;
const nodeDomain = process.env.NEXT_PUBLIC_NODE_DOMAIN || 'localhost:3000';
if (user.isSuspended || user.isSilenced) {
return NextResponse.json({ error: 'Account restricted' }, { status: 403 });
}
// Check if original post exists
// Handle swarm posts (format: swarm:domain:uuid)
if (postId.startsWith('swarm:')) {
const targetDomain = extractSwarmDomain(postId);
const originalPostId = postId.split(':')[2];
if (!targetDomain || !originalPostId) {
return NextResponse.json({ error: 'Invalid swarm post ID' }, { status: 400 });
}
// Deliver unrepost directly to the origin node
const { deliverSwarmUnrepost } = await import('@/lib/swarm/interactions');
const result = await deliverSwarmUnrepost(targetDomain, {
postId: originalPostId,
unrepost: {
actorHandle: user.handle,
actorNodeDomain: nodeDomain,
interactionId: crypto.randomUUID(),
timestamp: new Date().toISOString(),
},
});
if (!result.success) {
console.error(`[Swarm] Unrepost delivery failed: ${result.error}`);
return NextResponse.json({ error: 'Failed to deliver unrepost to remote node' }, { status: 502 });
}
console.log(`[Swarm] Unrepost delivered to ${targetDomain} for post ${originalPostId}`);
return NextResponse.json({ success: true, reposted: false });
}
// Local post - check if original post exists
const originalPost = await db.query.posts.findFirst({
where: eq(posts.id, postId),
});