/** Read side: Juno's own appview (apps/appview), reached over Railway's private network in prod. */ const APPVIEW = (process.env.APPVIEW_URL ?? 'http://127.0.0.1:3001').replace(/\/$/, '') export const EVENT = 'community.lexicon.calendar.event' export const RSVP = 'community.lexicon.calendar.rsvp' export type RsvpStatus = 'going' | 'interested' | 'notgoing' export class AppviewError extends Error { status: number constructor(status: number, message: string) { super(message) this.status = status } } async function call(nsid: string, params: Record, body?: unknown): Promise { const qs = new URLSearchParams() for (const [k, v] of Object.entries(params)) if (v !== undefined) qs.set(k, v) const res = await fetch(`${APPVIEW}/xrpc/${nsid}?${qs}`, { method: body ? 'POST' : 'GET', headers: body ? { 'content-type': 'application/json' } : undefined, body: body ? JSON.stringify(body) : undefined, signal: AbortSignal.timeout(15_000), }) if (!res.ok) throw new AppviewError(res.status, `${nsid}: ${res.status} ${await res.text().catch(() => '')}`) return (await res.json()) as T } export const appview = { feed: (viewer: string, cursor?: string, q?: string, exclude: { actors?: string[]; events?: string[]; curated?: string[] } = {}) => call('dev.mozzius.juno.getFeed', { viewer, cursor, q }, exclude), event: (uri: string, viewer: string) => call('dev.mozzius.juno.getEvent', { uri, viewer }), actorEvents: (actor: string, viewer: string) => call('dev.mozzius.juno.getActorEvents', { actor, viewer }), /** Read-after-write: have the appview re-read these records from the PDS before we respond. */ changes: (after: number) => fetch(`${APPVIEW}/internal/changes?after=${after}`, { signal: AbortSignal.timeout(10_000) }).then((r) => r.json() as Promise<{ changes: any[]; latest: number }>), attendees: (uri: string) => fetch(`${APPVIEW}/internal/attendees?uri=${encodeURIComponent(uri)}`, { signal: AbortSignal.timeout(10_000) }).then( (r) => r.json() as Promise<{ attendees: { did: string; status: string }[] }>, ), stats: async () => (await fetch(`${APPVIEW}/stats`, { signal: AbortSignal.timeout(5000) })).json(), notify: (...uris: string[]) => call('dev.mozzius.juno.notifyOfUpdate', {}, { uris }), } // Bluesky's public appview, for the signed-in user's profile + handle typeahead. const BSKY = 'https://public.api.bsky.app' const str = (v: unknown) => (typeof v === 'string' && v.trim() ? v : undefined) export type Profile = { did: string; handle?: string; displayName?: string; avatar?: string } export async function bskyProfile(actor: string): Promise { const res = await fetch(`${BSKY}/xrpc/app.bsky.actor.getProfile?actor=${encodeURIComponent(actor)}`) if (!res.ok) return { did: actor } const p = await res.json() return { did: p.did, handle: p.handle, displayName: str(p.displayName), avatar: str(p.avatar) } } export async function typeahead(q: string): Promise { if (!q) return [] const res = await fetch(`${BSKY}/xrpc/app.bsky.actor.searchActorsTypeahead?q=${encodeURIComponent(q)}&limit=6`) if (!res.ok) return [] const { actors } = await res.json() return (actors as any[]).map((p) => ({ did: p.did, handle: p.handle, displayName: str(p.displayName), avatar: str(p.avatar) })) }