From a3d79509a3fb5b1245e63896f3465eab9d85db77 Mon Sep 17 00:00:00 2001 From: dusk Date: Mon, 16 Dec 2024 19:54:39 +0300 Subject: [PATCH] fix: post using reply with parent post --- src/routes/log/create/+server.ts | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/src/routes/log/create/+server.ts b/src/routes/log/create/+server.ts index c4370ee..6c9fa06 100644 --- a/src/routes/log/create/+server.ts +++ b/src/routes/log/create/+server.ts @@ -2,7 +2,7 @@ import { env } from '$env/dynamic/private'; import { PUBLIC_BASE_URL } from '$env/static/public'; import { getBskyClient } from '$lib/bluesky.js'; import { createNote, findReplyRoot, genNoteId, readNote, type Note } from '$lib/notes'; -import type { Post, ReplyRef } from '@skyware/bot'; +import type { Post, PostPayload, PostReference, ReplyRef } from '@skyware/bot'; interface NoteData { content: string, @@ -38,33 +38,25 @@ export const POST = async ({ request }) => { const postContent = `${noteData.content} (${PUBLIC_BASE_URL}/log?id=${noteId})` try { const bot = await getBskyClient() + let postPayload: PostPayload = { + text: postContent, + createdAt: new Date(note.published), + external: noteData.embedUri, + } + let postRef: PostReference // find parent and reply posts let replyRef: ReplyRef | null = null if (noteData.replyTo !== undefined && repliedNote !== null) { const getBskyUri = (note: Note) => { return note.outgoingLinks?.find((v) => {return v.name === "bsky"})?.link } const parentUri = getBskyUri(repliedNote) - let parentPost: Post | null = null if (parentUri !== undefined) { - parentPost = await bot.getPost(parentUri) - } - const rootUri = getBskyUri(findReplyRoot(noteData.replyTo).rootNote) - let rootPost: Post | null = null - if (rootUri !== undefined) { - rootPost = await bot.getPost(rootUri) - } - if (parentPost !== null && rootPost !== null) { - replyRef = { - parent: parentPost, - root: rootPost, - } + const parentPost = await bot.getPost(parentUri) + postRef = await parentPost.reply(postPayload) } + throw "a reply was requested but no reply is found" + } else { + postRef = await bot.post(postPayload) } - const postRef = await bot.post({ - text: postContent, - createdAt: new Date(note.published), - replyRef: replyRef ?? undefined, - external: noteData.embedUri, - }) note.outgoingLinks?.push({name: "bsky", link: postRef.uri}) } catch(why) { console.log(`failed to post note #${noteId} to bsky: `, why)