feat(swarm): Add real-time post fetching and reply aggregation for federated nodes
- Add swarm post ID parsing (format: swarm:domain:uuid) in local post endpoint - Implement real-time fetching from origin node with 10s timeout - Transform remote post data to match local format with swarm metadata - Add reply aggregation from remote node with proper ID transformation - Implement like status checking for authenticated users on remote posts - Refactor swarm post endpoint to include reply data in response - Add media transformation with swarm-prefixed IDs for remote posts - Improve error handling with fallback to 404 for unreachable nodes - Enable seamless cross-node post viewing with federated author information
This commit is contained in:
@@ -1,90 +1,102 @@
|
||||
/**
|
||||
* Swarm Post Endpoint
|
||||
* Swarm Post Detail Endpoint
|
||||
*
|
||||
* GET: Returns a single post for swarm requests
|
||||
* GET: Get a post's details for other swarm nodes
|
||||
*/
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { db, posts, users, media } from '@/db';
|
||||
import { eq } from 'drizzle-orm';
|
||||
import { db, posts } from '@/db';
|
||||
import { eq, desc, and } from 'drizzle-orm';
|
||||
|
||||
type RouteContext = { params: Promise<{ id: string }> };
|
||||
|
||||
/**
|
||||
* GET /api/swarm/posts/[id]
|
||||
*
|
||||
* Returns a single post with author info.
|
||||
* Used by other nodes to fetch post details.
|
||||
* Returns post details including replies for swarm federation.
|
||||
*/
|
||||
export async function GET(request: NextRequest, context: RouteContext) {
|
||||
try {
|
||||
const { id: postId } = await context.params;
|
||||
|
||||
if (!db) {
|
||||
return NextResponse.json({ error: 'Database not available' }, { status: 503 });
|
||||
}
|
||||
|
||||
const nodeDomain = process.env.NEXT_PUBLIC_NODE_DOMAIN || 'localhost';
|
||||
const { id: postId } = await context.params;
|
||||
|
||||
// Find the post with author
|
||||
// Find the post
|
||||
const post = await db.query.posts.findFirst({
|
||||
where: eq(posts.id, postId),
|
||||
with: { author: true },
|
||||
with: {
|
||||
author: true,
|
||||
media: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!post) {
|
||||
if (!post || post.isRemoved) {
|
||||
return NextResponse.json({ error: 'Post not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
if (post.isRemoved) {
|
||||
return NextResponse.json({ error: 'Post not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
// Get media for the post
|
||||
const postMedia = await db
|
||||
.select({ url: media.url, mimeType: media.mimeType, altText: media.altText })
|
||||
.from(media)
|
||||
.where(eq(media.postId, postId));
|
||||
|
||||
const author = post.author as {
|
||||
handle: string;
|
||||
displayName: string | null;
|
||||
avatarUrl: string | null;
|
||||
isNsfw: boolean;
|
||||
};
|
||||
|
||||
return NextResponse.json({
|
||||
id: post.id,
|
||||
content: post.content,
|
||||
createdAt: post.createdAt.toISOString(),
|
||||
author: {
|
||||
handle: author.handle,
|
||||
displayName: author.displayName || author.handle,
|
||||
avatarUrl: author.avatarUrl || undefined,
|
||||
isNsfw: author.isNsfw,
|
||||
// Get replies
|
||||
const replies = await db.query.posts.findMany({
|
||||
where: and(
|
||||
eq(posts.replyToId, postId),
|
||||
eq(posts.isRemoved, false)
|
||||
),
|
||||
with: {
|
||||
author: true,
|
||||
media: true,
|
||||
},
|
||||
nodeDomain,
|
||||
isNsfw: post.isNsfw || author.isNsfw,
|
||||
likesCount: post.likesCount,
|
||||
repostsCount: post.repostsCount,
|
||||
repliesCount: post.repliesCount,
|
||||
media: postMedia.length > 0 ? postMedia.map(m => ({
|
||||
url: m.url,
|
||||
mimeType: m.mimeType || undefined,
|
||||
altText: m.altText || undefined,
|
||||
})) : undefined,
|
||||
linkPreviewUrl: post.linkPreviewUrl || undefined,
|
||||
linkPreviewTitle: post.linkPreviewTitle || undefined,
|
||||
linkPreviewDescription: post.linkPreviewDescription || undefined,
|
||||
linkPreviewImage: post.linkPreviewImage || undefined,
|
||||
replyToId: post.replyToId || undefined,
|
||||
repostOfId: post.repostOfId || undefined,
|
||||
orderBy: [desc(posts.createdAt)],
|
||||
limit: 50,
|
||||
});
|
||||
|
||||
const author = post.author as any;
|
||||
|
||||
return NextResponse.json({
|
||||
post: {
|
||||
id: post.id,
|
||||
content: post.content,
|
||||
createdAt: post.createdAt.toISOString(),
|
||||
likesCount: post.likesCount,
|
||||
repostsCount: post.repostsCount,
|
||||
repliesCount: post.repliesCount,
|
||||
author: {
|
||||
handle: author.handle,
|
||||
displayName: author.displayName,
|
||||
avatarUrl: author.avatarUrl,
|
||||
},
|
||||
media: (post.media as any[])?.map(m => ({
|
||||
url: m.url,
|
||||
altText: m.altText,
|
||||
})) || [],
|
||||
linkPreviewUrl: post.linkPreviewUrl,
|
||||
linkPreviewTitle: post.linkPreviewTitle,
|
||||
linkPreviewDescription: post.linkPreviewDescription,
|
||||
linkPreviewImage: post.linkPreviewImage,
|
||||
},
|
||||
replies: replies.map(r => {
|
||||
const replyAuthor = r.author as any;
|
||||
return {
|
||||
id: r.id,
|
||||
content: r.content,
|
||||
createdAt: r.createdAt.toISOString(),
|
||||
likesCount: r.likesCount,
|
||||
repostsCount: r.repostsCount,
|
||||
repliesCount: r.repliesCount,
|
||||
author: {
|
||||
handle: replyAuthor.handle,
|
||||
displayName: replyAuthor.displayName,
|
||||
avatarUrl: replyAuthor.avatarUrl,
|
||||
},
|
||||
media: (r.media as any[])?.map(m => ({
|
||||
url: m.url,
|
||||
altText: m.altText,
|
||||
})) || [],
|
||||
};
|
||||
}),
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Swarm post error:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to fetch post' },
|
||||
{ status: 500 }
|
||||
);
|
||||
console.error('[Swarm] Post detail error:', error);
|
||||
return NextResponse.json({ error: 'Failed to get post' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user