From 89d936b96a9b626a996ddb61828b028d221e694a Mon Sep 17 00:00:00 2001 From: dusk Date: Wed, 30 Oct 2024 23:14:45 +0900 Subject: [PATCH] fix: create the bsky client in the create note route --- src/lib/index.ts | 6 +++--- src/routes/log/create/+server.ts | 11 ++++++++--- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/lib/index.ts b/src/lib/index.ts index acad6b4..87429b1 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -21,9 +21,9 @@ export const scopeCookies = (cookies: Cookies, path: string) => { export const visitCountFile = `${env.WEBSITE_DATA_DIR}/visitcount` export const visitCount = writable(parseInt(existsSync(visitCountFile) ? readFileSync(visitCountFile).toString() : '0')); -const loginToBsky = () => { +export const loginToBsky = async () => { const creds = new CredentialSession(new URL("https://bsky.social")) - creds.login({identifier: 'gaze.systems', password: env.BSKY_PASSWORD ?? "" }) + await creds.login({identifier: 'gaze.systems', password: env.BSKY_PASSWORD ?? "" }) return new Agent(creds) } -export const bskyClient = writable(loginToBsky()) \ No newline at end of file +export const bskyClient = writable(null) diff --git a/src/routes/log/create/+server.ts b/src/routes/log/create/+server.ts index d269a82..55e64bb 100644 --- a/src/routes/log/create/+server.ts +++ b/src/routes/log/create/+server.ts @@ -1,6 +1,6 @@ import { env } from '$env/dynamic/private'; import { PUBLIC_BASE_URL } from '$env/static/public'; -import { bskyClient } from '$lib'; +import { bskyClient, loginToBsky } from '$lib'; import { createNote } from '$lib/notes.js'; import { get } from 'svelte/store'; @@ -20,7 +20,12 @@ export const POST = async ({ request }) => { const noteId = createNote({ content: noteData.content, published }) // bridge to bsky if want to bridge if (noteData.bskyPosse ?? null !== null) { - await get(bskyClient).post({text: `${noteData.content} (${PUBLIC_BASE_URL}/log?id=${noteId})`}) + let client = get(bskyClient) + if (client === null) { + client = await loginToBsky() + bskyClient.set(client) + } + await client.post({text: `${noteData.content} (${PUBLIC_BASE_URL}/log?id=${noteId})`}) } // send back created note id return new Response(JSON.stringify({ noteId }), { @@ -29,4 +34,4 @@ export const POST = async ({ request }) => { 'cache-control': 'no-store', } }) -}; \ No newline at end of file +};