Something went wrong. Try again.
Monorepo for Tangled tangled.org
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657import { error } from "@sveltejs/kit";import type { Did, Nsid, ResourceUri } from "@atcute/lexicons/syntax";import type { OAuthUserAgent } from "@atcute/oauth-browser-client";import { awaitDeletedRecord, awaitIndexedOrSaved } from "$lib/api/awaitRecord";import type { BobbinContext } from "$lib/api/client";import { resolveMiniDoc } from "$lib/api/identity";import { orNull } from "$lib/api/load";import { getString, type RecordView, type StringRecord } from "$lib/api/records";import { rkeyFromUri } from "$lib/api/uri";import { deleteRecord, putRecord, type WrittenRecord } from "$lib/api/write";
const STRING_COLLECTION = "sh.tangled.string" as Nsid;
export interface StringSubject { did: Did; handle: string; uri: ResourceUri; record: RecordView<StringRecord>;}
export const loadStringSubject = async ( ctx: BobbinContext, handle: string, rkey: string): Promise<StringSubject> => { const doc = await orNull(resolveMiniDoc(ctx, handle)); if (!doc) error(404, "String not found");
const uri = `at://${doc.did}/${STRING_COLLECTION}/${rkey}` as ResourceUri; const record = await orNull(getString(ctx, uri)); if (!record) error(404, "String not found");
return { did: doc.did as Did, handle: doc.handle, uri, record };};
export const putString = async ( agent: OAuthUserAgent, bobbin: BobbinContext, rkey: string, record: StringRecord, swapRecord?: string): Promise<WrittenRecord> => { const written = await putRecord(agent, STRING_COLLECTION, rkey, record, swapRecord); await awaitIndexedOrSaved(bobbin, written); return written;};
export const deleteString = async ( agent: OAuthUserAgent, bobbin: BobbinContext, uri: ResourceUri, swapRecord?: string): Promise<boolean> => { await deleteRecord(agent, STRING_COLLECTION, rkeyFromUri(uri), swapRecord); return awaitDeletedRecord(bobbin, uri);};