diff --git a/app/api/nodes/route.ts b/app/api/nodes/route.ts index a499ad6..4efeed2 100644 --- a/app/api/nodes/route.ts +++ b/app/api/nodes/route.ts @@ -133,6 +133,7 @@ export async function POST(request: NextRequest) { // Create the thread posts let previousPost: { uri: string; cid: string } | null = null; + let rootPost: { uri: string; cid: string } | null = null; const threadUris: string[] = []; for (let i = 0; i < chunks.length; i++) { @@ -169,12 +170,10 @@ export async function POST(request: NextRequest) { tags: ['ponderants-node'], }; - // If not first post, add reply reference - if (previousPost && threadUris.length > 0) { - // Get the first post (root) CID from the stored thread data - const rootCid = atp_cid; // First post's CID + // If not first post, add reply reference to create a thread + if (previousPost && rootPost) { postRecord.reply = { - root: { uri: threadUris[0], cid: rootCid }, + root: { uri: rootPost.uri, cid: rootPost.cid }, parent: { uri: previousPost.uri, cid: previousPost.cid }, }; } @@ -189,8 +188,9 @@ export async function POST(request: NextRequest) { threadUris.push(response.data.uri); previousPost = { uri: response.data.uri, cid: response.data.cid }; - // Store the first post's URI as the main atp_uri + // Store the first post's URI and CID as both the root and the main atp_uri if (isFirstPost) { + rootPost = { uri: response.data.uri, cid: response.data.cid }; atp_uri = response.data.uri; atp_cid = response.data.cid; }