refactor: move bsky code into its own lib file
This commit is contained in:
parent
efdce954ac
commit
61bec8904e
20
src/lib/bluesky.ts
Normal file
20
src/lib/bluesky.ts
Normal 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)
|
||||||
|
}
|
@ -2,7 +2,6 @@ import type { Cookies } from '@sveltejs/kit'
|
|||||||
import { env } from '$env/dynamic/private'
|
import { env } from '$env/dynamic/private'
|
||||||
import { get, writable } from 'svelte/store'
|
import { get, writable } from 'svelte/store'
|
||||||
import { existsSync, readFileSync } from 'fs'
|
import { existsSync, readFileSync } from 'fs'
|
||||||
import { Agent, CredentialSession } from '@atproto/api'
|
|
||||||
import SGDB from 'steamgriddb'
|
import SGDB from 'steamgriddb'
|
||||||
|
|
||||||
export const scopeCookies = (cookies: Cookies, path: string) => {
|
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 visitCountFile = `${env.WEBSITE_DATA_DIR}/visitcount`
|
||||||
export const visitCount = writable(parseInt(existsSync(visitCountFile) ? readFileSync(visitCountFile).toString() : '0'));
|
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})
|
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 type LastTrack = {name: string, artist: string, image: string | null, link: string}
|
||||||
export const lastFmGetNowPlaying: () => Promise<LastTrack | null> = async () => {
|
export const lastFmGetNowPlaying: () => Promise<LastTrack | null> = async () => {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { env } from '$env/dynamic/private';
|
import { env } from '$env/dynamic/private';
|
||||||
import { PUBLIC_BASE_URL } from '$env/static/public';
|
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 { createNote } from '$lib/notes.js';
|
||||||
import { RichText } from '@atproto/api';
|
import { RichText } from '@atproto/api';
|
||||||
import { get } from 'svelte/store';
|
import { get } from 'svelte/store';
|
||||||
@ -23,11 +23,7 @@ export const POST = async ({ request }) => {
|
|||||||
const noteId = createNote({ content: noteData.content, published })
|
const noteId = createNote({ content: noteData.content, published })
|
||||||
// bridge to bsky if want to bridge
|
// bridge to bsky if want to bridge
|
||||||
if (noteData.bskyPosse) {
|
if (noteData.bskyPosse) {
|
||||||
let client = get(bskyClient)
|
let client = await getBskyClient()
|
||||||
if (client === null) {
|
|
||||||
client = await loginToBsky()
|
|
||||||
bskyClient.set(client)
|
|
||||||
}
|
|
||||||
const rt = new RichText({
|
const rt = new RichText({
|
||||||
text: `${noteData.content} (${PUBLIC_BASE_URL}/log?id=${noteId})`,
|
text: `${noteData.content} (${PUBLIC_BASE_URL}/log?id=${noteId})`,
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user