refactor: move bsky code into its own lib file

This commit is contained in:
dusk 2024-11-23 02:40:32 +03:00
parent efdce954ac
commit 61bec8904e
Signed by: dusk
SSH Key Fingerprint: SHA256:Abmvag+juovVufZTxyWY8KcVgrznxvBjQpJesv071Aw
3 changed files with 22 additions and 14 deletions

20
src/lib/bluesky.ts Normal file
View File

@ -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 | Agent>(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)
}

View File

@ -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 | Agent>(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<LastTrack | null> = async () => {

View File

@ -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})`,
})