From 61bec8904e5fd04721e07886e33723ed7f5fc03e Mon Sep 17 00:00:00 2001 From: dusk Date: Sat, 23 Nov 2024 02:40:32 +0300 Subject: [PATCH] refactor: move bsky code into its own lib file --- src/lib/bluesky.ts | 20 ++++++++++++++++++++ src/lib/index.ts | 8 -------- src/routes/log/create/+server.ts | 8 ++------ 3 files changed, 22 insertions(+), 14 deletions(-) create mode 100644 src/lib/bluesky.ts diff --git a/src/lib/bluesky.ts b/src/lib/bluesky.ts new file mode 100644 index 0000000..ef17783 --- /dev/null +++ b/src/lib/bluesky.ts @@ -0,0 +1,20 @@ +import { env } from '$env/dynamic/private' +import { Agent, CredentialSession } from '@atproto/api' +import { get, writable } from 'svelte/store' + +const bskyClient = writable(null) + +export const getBskyClient = async () => { + let client = get(bskyClient) + if (client === null) { + client = await loginToBsky() + bskyClient.set(client) + } + return client +} + +const loginToBsky = async () => { + const creds = new CredentialSession(new URL("https://bsky.social")) + await creds.login({ identifier: 'gaze.systems', password: env.BSKY_PASSWORD ?? "" }) + return new Agent(creds) +} \ No newline at end of file diff --git a/src/lib/index.ts b/src/lib/index.ts index 8168953..2c64a4f 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -2,7 +2,6 @@ import type { Cookies } from '@sveltejs/kit' import { env } from '$env/dynamic/private' import { get, writable } from 'svelte/store' import { existsSync, readFileSync } from 'fs' -import { Agent, CredentialSession } from '@atproto/api' import SGDB from 'steamgriddb' export const scopeCookies = (cookies: Cookies, path: string) => { @@ -22,13 +21,6 @@ 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')); -export const loginToBsky = async () => { - const creds = new CredentialSession(new URL("https://bsky.social")) - await creds.login({ identifier: 'gaze.systems', password: env.BSKY_PASSWORD ?? "" }) - return new Agent(creds) -} -export const bskyClient = writable(null) - const cachedLastTrack = writable<{track: LastTrack | null, since: number}>({track: null, since: 0}) export type LastTrack = {name: string, artist: string, image: string | null, link: string} export const lastFmGetNowPlaying: () => Promise = async () => { diff --git a/src/routes/log/create/+server.ts b/src/routes/log/create/+server.ts index 9092434..ec89ce9 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, loginToBsky } from '$lib'; +import { getBskyClient } from '$lib/bluesky.ts'; import { createNote } from '$lib/notes.js'; import { RichText } from '@atproto/api'; import { get } from 'svelte/store'; @@ -23,11 +23,7 @@ export const POST = async ({ request }) => { const noteId = createNote({ content: noteData.content, published }) // bridge to bsky if want to bridge if (noteData.bskyPosse) { - let client = get(bskyClient) - if (client === null) { - client = await loginToBsky() - bskyClient.set(client) - } + let client = await getBskyClient() const rt = new RichText({ text: `${noteData.content} (${PUBLIC_BASE_URL}/log?id=${noteId})`, })