2024-11-23 02:40:32 +03:00
|
|
|
import { env } from '$env/dynamic/private'
|
2024-11-23 04:28:16 +03:00
|
|
|
import { Agent, CredentialSession, RichText } from '@atproto/api'
|
2024-11-23 02:40:32 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-11-23 04:28:16 +03:00
|
|
|
export const postToBsky = async (text: string) => {
|
|
|
|
let client = await getBskyClient()
|
|
|
|
const rt = new RichText({ text })
|
|
|
|
await rt.detectFacets(client)
|
2024-11-23 05:09:48 +03:00
|
|
|
const {uri} = await client.post({
|
2024-11-23 04:28:16 +03:00
|
|
|
text: rt.text,
|
|
|
|
facets: rt.facets,
|
|
|
|
})
|
2024-11-23 05:09:48 +03:00
|
|
|
return `https://bsky.gaze.systems/post/${uri.split('/').pop()}`
|
2024-11-23 04:28:16 +03:00
|
|
|
}
|
|
|
|
|
2024-11-23 02:40:32 +03:00
|
|
|
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)
|
|
|
|
}
|