Something went wrong. Try again.
Monorepo for Tangled
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263import { ok } from "@atcute/client";import { mainSchema as uploadBlobSchema } from "@atcute/atproto/types/repo/uploadBlob";import { mainSchema as putRecordSchema } from "@atcute/atproto/types/repo/putRecord";import type { Nsid, RecordKey } from "@atcute/lexicons/syntax";import type { OAuthUserAgent } from "@atcute/oauth-browser-client";import { createClient } from "$lib/auth/agent";import { FOLLOW_VIEWER, STAR_VIEWER } from "./descriptors";import { enrich, target } from "./enrich";import { jsonGet } from "./_request";import type { BobbinContext, XrpcRequestInit } from "./client";import type * as ShTangledActorGetTrending from "./lexicons/types/sh/tangled/actor/getTrending";import type { ProfileRecord } from "./records";
const PROFILE_COLLECTION = "sh.tangled.actor.profile" as Nsid;
const AVATAR_TYPES: Record<string, true> = { "image/png": true, "image/jpeg": true };const MAX_AVATAR_BYTES = 1_000_000;
export const getTrending = (ctx: BobbinContext, init?: XrpcRequestInit) => jsonGet<ShTangledActorGetTrending.$output>(ctx, "sh.tangled.actor.getTrending", {}, init);
export const getTrendingWithViewerState = ( ctx: BobbinContext, viewer: string, init?: XrpcRequestInit) => enrich<ShTangledActorGetTrending.$output>( 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<NonNullable<ProfileRecord["avatar"]>> => { 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<void> => { const rpc = createClient(agent); await ok( rpc.call(putRecordSchema, { input: { repo: agent.sub, collection: PROFILE_COLLECTION, rkey: "self" as RecordKey, record } }) );};