A decentralized music tracking and discovery platform built on AT Protocol 馃幍 rocksky.app
spotify atproto lastfm musicbrainz scrobbling listenbrainz
99
fork

Configure Feed

Select the types of activity you want to include in your feed.

at main 56 lines 1.4 kB view raw
1import { Agent, AtpAgent } from "@atproto/api"; 2import { ctx } from "context"; 3import { eq } from "drizzle-orm"; 4import authSessions from "schema/auth-session"; 5import extractPdsFromDid from "./extractPdsFromDid"; 6import { env } from "./env"; 7import { logger } from "logger"; 8 9export async function createAgent(did: string, handle: string): Promise<Agent> { 10 const pds = await extractPdsFromDid(did); 11 const agent = new AtpAgent({ 12 service: new URL(pds), 13 }); 14 15 try { 16 const [data] = await ctx.db 17 .select() 18 .from(authSessions) 19 .where(eq(authSessions.key, did)) 20 .execute(); 21 22 if (!data) { 23 throw new Error("No session found"); 24 } 25 26 await agent.resumeSession(JSON.parse(data.session)); 27 return agent; 28 } catch (e) { 29 logger.error`Resuming session ${did}`; 30 await ctx.db 31 .delete(authSessions) 32 .where(eq(authSessions.key, did)) 33 .execute(); 34 35 await agent.login({ 36 identifier: handle, 37 password: env.ROCKSKY_PASSWORD, 38 }); 39 40 await ctx.db 41 .insert(authSessions) 42 .values({ 43 key: did, 44 session: JSON.stringify(agent.session), 45 }) 46 .onConflictDoUpdate({ 47 target: authSessions.key, 48 set: { session: JSON.stringify(agent.session) }, 49 }) 50 .execute(); 51 52 logger.info`Logged in as ${handle}`; 53 54 return agent; 55 } 56}