Something went wrong. Try again.
Monorepo for Tangled tangled.org
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116import type { BobbinContext, XrpcRequestInit } from "$lib/api/client";import type { Nsid } from "@atcute/lexicons/syntax";import { INVALID_HANDLE, type MiniDoc } from "$lib/api/identity";import { jsonPost } from "$lib/api/_request";import { didFromUri, rkeyFromUri } from "$lib/api/uri";
// payload types, also the keys payloads land under in the data sidecarexport const TYPE_COUNT = "sh.tangled.query.enrichResponse#count";export const TYPE_VIEWER = "sh.tangled.query.enrichResponse#viewer";export const TYPE_MINIDOC = "blue.microcosm.identity.resolveMiniDoc";
export type PayloadType = typeof TYPE_COUNT | typeof TYPE_VIEWER | typeof TYPE_MINIDOC;
export type RecordPath = string & {};export type EnvelopePath = ".repo" | ".collection" | ".rkey" | ".";export type LinkSource = `${Nsid}:${RecordPath | EnvelopePath}`;
// a descriptor is also the key its payload reads back under, so the readers// take the whole descriptor, a request and its reads can't drift apartexport interface LinkDescriptor<T extends PayloadType = PayloadType> { source: LinkSource; type: T; targets?: RecordPath[];}
export type CountDescriptor = LinkDescriptor<typeof TYPE_COUNT>;export type ViewerDescriptor = LinkDescriptor<typeof TYPE_VIEWER>;export type DocDescriptor = LinkDescriptor<typeof TYPE_MINIDOC>;
// data[ref][source][type] = payload, payload shape depends on the typeexport type Sidecar = Record<string, Record<string, Record<string, unknown>>>;
export interface Enriched<O> { output: O; data: Sidecar;}
export interface EnrichRequest { xrpc: string; params?: Record<string, unknown>; enrich: LinkDescriptor[]; viewer?: string;}
export const target = <T extends PayloadType>( descriptor: LinkDescriptor<T>, targets: RecordPath[]): LinkDescriptor<T> => ({ ...descriptor, targets });
export const targetAll = <T extends PayloadType>( descriptors: LinkDescriptor<T>[], targets: RecordPath[]): LinkDescriptor<T>[] => descriptors.map((descriptor) => target(descriptor, targets));
// bobbin rejects viewer payloads with no viewer, so a logged-out request drops// them here instead of at every call siteexport const enrich = <O>( ctx: BobbinContext, req: EnrichRequest, init?: XrpcRequestInit): Promise<Enriched<O>> => jsonPost<Enriched<O>>( ctx, "sh.tangled.query.enrichResponse", { ...req, enrich: req.viewer ? req.enrich : req.enrich.filter((descriptor) => descriptor.type !== TYPE_VIEWER) }, init );
const payloadOf = ( data: Sidecar, ref: string | undefined, { source, type }: LinkDescriptor): unknown => (ref !== undefined ? data[ref]?.[source]?.[type] : undefined);
export const countOf = (data: Sidecar, ref: string | undefined, of: CountDescriptor): number => (payloadOf(data, ref, of) as number | undefined) ?? 0;
// undefined means no viewer or inapplicable descriptor, null means no matching recordexport const viewerUriOf = ( data: Sidecar, ref: string | undefined, of: ViewerDescriptor): string | null | undefined => payloadOf(data, ref, of) as string | null | undefined;
export const viewerRkeyOf = ( data: Sidecar, ref: string | undefined, of: ViewerDescriptor): string | null | undefined => { const uri = viewerUriOf(data, ref, of); return uri ? rkeyFromUri(uri) : uri;};
export const miniDocOf = ( data: Sidecar, did: string | undefined, of: DocDescriptor): MiniDoc | undefined => payloadOf(data, did, of) as MiniDoc | undefined;
export const handleOf = (data: Sidecar, did: string | undefined, of: DocDescriptor): string => miniDocOf(data, did, of)?.handle ?? INVALID_HANDLE;
export const authorOf = ( data: Sidecar, uri: string, of: DocDescriptor): { did: string; handle: string } => { const did = didFromUri(uri); return { did, handle: handleOf(data, did, of) };};