a tool for shared writing and social publishing
at main 98 lines 2.6 kB view raw
1"use server"; 2import { Vercel } from "@vercel/sdk"; 3import { cookies } from "next/headers"; 4 5import { Database } from "supabase/database.types"; 6import { createServerClient } from "@supabase/ssr"; 7import { getIdentityData } from "actions/getIdentityData"; 8 9const VERCEL_TOKEN = process.env.VERCEL_TOKEN; 10const vercel = new Vercel({ 11 bearerToken: VERCEL_TOKEN, 12}); 13 14let supabase = createServerClient<Database>( 15 process.env.NEXT_PUBLIC_SUPABASE_API_URL as string, 16 process.env.SUPABASE_SERVICE_ROLE_KEY as string, 17 { cookies: {} }, 18); 19 20export async function addDomain(domain: string) { 21 let identity = await getIdentityData(); 22 if (!identity || (!identity.email && !identity.atp_did)) return {}; 23 if ( 24 domain.includes("leaflet.pub") && 25 (!identity.email || 26 ![ 27 "celine@hyperlink.academy", 28 "brendan@hyperlink.academy", 29 "jared@hyperlink.academy", 30 "brendan.schlagel@gmail.com", 31 ].includes(identity.email)) 32 ) 33 return {}; 34 return await createDomain(domain, identity.email, identity.id); 35} 36 37export async function addPublicationDomain( 38 domain: string, 39 publication_uri: string, 40) { 41 let identity = await getIdentityData(); 42 if (!identity || !identity.atp_did) return {}; 43 let { data: publication } = await supabase 44 .from("publications") 45 .select("*") 46 .eq("uri", publication_uri) 47 .single(); 48 49 if (publication?.identity_did !== identity.atp_did) return {}; 50 let { error } = await createDomain(domain, null, identity.id); 51 if (error) return { error }; 52 await supabase.from("publication_domains").insert({ 53 publication: publication_uri, 54 identity: identity.atp_did, 55 domain, 56 }); 57 return {}; 58} 59 60async function createDomain( 61 domain: string, 62 email: string | null, 63 identity_id: string, 64) { 65 try { 66 await vercel.projects.addProjectDomain({ 67 idOrName: "prj_9jX4tmYCISnm176frFxk07fF74kG", 68 teamId: "team_42xaJiZMTw9Sr7i0DcLTae9d", 69 requestBody: { 70 name: domain, 71 }, 72 }); 73 } catch (e) { 74 console.log(e); 75 let error: "unknown-error" | "invalid_domain" | "domain_already_in_use" = 76 "unknown-error"; 77 if ((e as any).rawValue) { 78 error = 79 (e as { rawValue?: { error?: { code?: "invalid_domain" } } })?.rawValue 80 ?.error?.code || "unknown-error"; 81 } 82 if ((e as any).body) { 83 try { 84 error = JSON.parse((e as any).body)?.error?.code || "unknown-error"; 85 } catch (e) {} 86 } 87 88 return { error }; 89 } 90 91 await supabase.from("custom_domains").insert({ 92 domain, 93 identity: email, 94 confirmed: false, 95 identity_id, 96 }); 97 return {}; 98}