a tool for shared writing and social publishing
0
fork

Configure Feed

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

extract out agent function from bsky data routes

+6 -79
+3 -39
app/api/bsky/quotes/route.ts
··· 1 - import { Agent, lexToJson } from "@atproto/api"; 2 - import { cookies } from "next/headers"; 1 + import { lexToJson } from "@atproto/api"; 3 2 import { NextRequest } from "next/server"; 4 - import { createOauthClient } from "src/atproto-oauth"; 5 - import { supabaseServerClient } from "supabase/serverClient"; 3 + import { getAgent } from "../agent"; 6 4 7 5 export const runtime = "nodejs"; 8 6 9 - async function getAuthenticatedAgent(): Promise<Agent | null> { 10 - try { 11 - const cookieStore = await cookies(); 12 - const authToken = 13 - cookieStore.get("auth_token")?.value || 14 - cookieStore.get("external_auth_token")?.value; 15 - 16 - if (!authToken || authToken === "null") return null; 17 - 18 - const { data } = await supabaseServerClient 19 - .from("email_auth_tokens") 20 - .select("identities(atp_did)") 21 - .eq("id", authToken) 22 - .eq("confirmed", true) 23 - .single(); 24 - 25 - const did = data?.identities?.atp_did; 26 - if (!did) return null; 27 - 28 - const oauthClient = await createOauthClient(); 29 - const session = await oauthClient.restore(did); 30 - return new Agent(session); 31 - } catch (error) { 32 - console.error("Failed to get authenticated agent:", error); 33 - return null; 34 - } 35 - } 36 - 37 7 export async function GET(req: NextRequest) { 38 8 try { 39 9 const searchParams = req.nextUrl.searchParams; ··· 48 18 ); 49 19 } 50 20 51 - // Try to use authenticated agent if user is logged in, otherwise fall back to public API 52 - let agent = await getAuthenticatedAgent(); 53 - if (!agent) { 54 - agent = new Agent({ 55 - service: "https://public.api.bsky.app", 56 - }); 57 - } 21 + const agent = await getAgent(); 58 22 59 23 const response = await agent.app.bsky.feed.getQuotes({ 60 24 uri,
+3 -40
app/api/bsky/thread/route.ts
··· 1 - import { Agent, lexToJson } from "@atproto/api"; 2 - import { ThreadViewPost } from "@atproto/api/dist/client/types/app/bsky/feed/defs"; 3 - import { cookies } from "next/headers"; 1 + import { lexToJson } from "@atproto/api"; 4 2 import { NextRequest } from "next/server"; 5 - import { createOauthClient } from "src/atproto-oauth"; 6 - import { supabaseServerClient } from "supabase/serverClient"; 3 + import { getAgent } from "../agent"; 7 4 8 5 export const runtime = "nodejs"; 9 6 10 - async function getAuthenticatedAgent(): Promise<Agent | null> { 11 - try { 12 - const cookieStore = await cookies(); 13 - const authToken = 14 - cookieStore.get("auth_token")?.value || 15 - cookieStore.get("external_auth_token")?.value; 16 - 17 - if (!authToken || authToken === "null") return null; 18 - 19 - const { data } = await supabaseServerClient 20 - .from("email_auth_tokens") 21 - .select("identities(atp_did)") 22 - .eq("id", authToken) 23 - .eq("confirmed", true) 24 - .single(); 25 - 26 - const did = data?.identities?.atp_did; 27 - if (!did) return null; 28 - 29 - const oauthClient = await createOauthClient(); 30 - const session = await oauthClient.restore(did); 31 - return new Agent(session); 32 - } catch (error) { 33 - console.error("Failed to get authenticated agent:", error); 34 - return null; 35 - } 36 - } 37 - 38 7 export async function GET(req: NextRequest) { 39 8 try { 40 9 const searchParams = req.nextUrl.searchParams; ··· 49 18 ); 50 19 } 51 20 52 - // Try to use authenticated agent if user is logged in, otherwise fall back to public API 53 - let agent = await getAuthenticatedAgent(); 54 - if (!agent) { 55 - agent = new Agent({ 56 - service: "https://public.api.bsky.app", 57 - }); 58 - } 21 + const agent = await getAgent(); 59 22 60 23 const response = await agent.getPostThread({ 61 24 uri,