import { ok } from "@atcute/client"; import { mainSchema as uploadBlobSchema } from "@atcute/atproto/types/repo/uploadBlob"; import type { Nsid } from "@atcute/lexicons/syntax"; import type { OAuthUserAgent } from "@atcute/oauth-browser-client"; import { createClient } from "$lib/auth/agent"; import { FOLLOW_VIEWER, STAR_VIEWER } from "$lib/api/descriptors"; import { enrich, target } from "$lib/api/enrich"; import { jsonGet } from "$lib/api/_request"; import type { BobbinContext, XrpcRequestInit } from "$lib/api/client"; import { putRecord, type WrittenRecord } from "$lib/api/write"; import type * as ShTangledActorGetTrending from "$lib/api/lexicons/types/sh/tangled/actor/getTrending"; import type { ProfileRecord } from "$lib/api/records"; const PROFILE_COLLECTION = "sh.tangled.actor.profile" as Nsid; const AVATAR_TYPES: Record = { "image/png": true, "image/jpeg": true }; const MAX_AVATAR_BYTES = 1_000_000; export const getTrending = (ctx: BobbinContext, init?: XrpcRequestInit) => jsonGet(ctx, "sh.tangled.actor.getTrending", {}, init); export const getTrendingWithViewerState = ( ctx: BobbinContext, viewer: string, init?: XrpcRequestInit ) => enrich( ctx, { xrpc: "sh.tangled.actor.getTrending", viewer, enrich: [target(FOLLOW_VIEWER, ["people[].did"]), target(STAR_VIEWER, ["repos[].did"])] }, init ); export const uploadProfileAvatar = async ( agent: OAuthUserAgent, image: Blob ): Promise> => { if (!AVATAR_TYPES[image.type]) throw new Error("Choose a PNG or JPEG image."); if (image.size > MAX_AVATAR_BYTES) throw new Error("Profile pictures must be 1 MB or smaller."); const rpc = createClient(agent); const { blob } = await ok(rpc.call(uploadBlobSchema, { input: image })); return blob; }; // the profile record lives at the fixed rkey `self`; put upserts it. export const putProfile = async ( agent: OAuthUserAgent, record: ProfileRecord ): Promise => { return putRecord(agent, PROFILE_COLLECTION, "self", record); };