Fix federation flows and add versioned updater

This commit is contained in:
cyph3rasi
2026-03-07 21:55:55 -08:00
parent 5d524a0354
commit 1b0b3d8d52
24 changed files with 1486 additions and 142 deletions
+29 -7
View File
@@ -1,6 +1,6 @@
import { NextResponse } from 'next/server';
import { db, posts, users, likes } from '@/db';
import { eq, desc, and, inArray, lt } from 'drizzle-orm';
import { eq, desc, and, inArray, lt, sql } from 'drizzle-orm';
import { fetchSwarmUserProfile, isSwarmNode } from '@/lib/swarm/interactions';
import { discoverNode } from '@/lib/swarm/discovery';
@@ -51,8 +51,8 @@ export async function GET(request: Request, context: RouteContext) {
displayName: profile.displayName || profile.handle,
avatarUrl: profile.avatarUrl,
};
const posts = profileData.posts.map((post: any) => ({
const remotePosts = profileData.posts.map((post: any) => ({
id: post.id,
content: post.content,
createdAt: post.createdAt,
@@ -70,7 +70,7 @@ export async function GET(request: Request, context: RouteContext) {
originalPostId: post.id,
}));
return NextResponse.json({ posts, nextCursor: null });
return NextResponse.json({ posts: remotePosts, nextCursor: null });
}
return NextResponse.json({ posts: [] });
@@ -108,13 +108,35 @@ export async function GET(request: Request, context: RouteContext) {
avatarUrl: profile.avatarUrl,
};
const posts = profileData.posts.map((post: any) => ({
const swarmPostIds = profileData.posts.map((post: any) => `swarm:${remote.domain}:${post.id}`);
let localReplyCounts = new Map<string, number>();
if (swarmPostIds.length > 0) {
const counts = await db.select({
swarmReplyToId: posts.swarmReplyToId,
replyCount: sql<number>`count(*)::int`,
})
.from(posts)
.where(and(
inArray(posts.swarmReplyToId, swarmPostIds),
eq(posts.isRemoved, false)
))
.groupBy(posts.swarmReplyToId);
localReplyCounts = new Map(
counts
.filter(row => row.swarmReplyToId)
.map(row => [row.swarmReplyToId as string, row.replyCount])
);
}
const remotePosts = profileData.posts.map((post: any) => ({
id: post.id,
content: post.content,
createdAt: post.createdAt,
likesCount: post.likesCount || 0,
repostsCount: post.repostsCount || 0,
repliesCount: post.repliesCount || 0,
repliesCount: (post.repliesCount || 0) + (localReplyCounts.get(`swarm:${remote.domain}:${post.id}`) || 0),
author,
media: post.media || [],
linkPreviewUrl: post.linkPreviewUrl || null,
@@ -126,7 +148,7 @@ export async function GET(request: Request, context: RouteContext) {
originalPostId: post.id,
}));
return NextResponse.json({ posts, nextCursor: null });
return NextResponse.json({ posts: remotePosts, nextCursor: null });
}
return NextResponse.json({ posts: [] });