fix: post using reply with parent post
All checks were successful
create archive with lfs / tag (push) Successful in 10s

This commit is contained in:
dusk 2024-12-16 19:54:39 +03:00
parent 7440f6139e
commit a3d79509a3
Signed by: dusk
SSH Key Fingerprint: SHA256:Abmvag+juovVufZTxyWY8KcVgrznxvBjQpJesv071Aw

View File

@ -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)