Something went wrong. Try again.
softfork of @juliet.paris parfait forked from juliet.paris/parfait
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167import { buildFetchHandler, Client, ok, type FetchHandler, simpleFetchHandler,} from "@atcute/client";
import { type Agent, agent } from "../auth/session";import { appview, appviewProxyFor } from "./appview";import { watchExpiry } from "./expiry";
export const DISCOVER_FEED = "at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.generator/whats-hot";
/** Bluesky's own moderation service. */export const BSKY_LABELER = "did:plc:ar7c4by46qjdydhdevvrndac";
/** * Whether Bluesky's moderation applies to everyone, as it does through Bluesky's own AppView. * Through another, which moderates for itself, it's a labeler like any other: subscribed to, or * not. */export const BSKY_ALWAYS = appview.id === "bluesky";
/** The moderation services each account subscribes to, besides Bluesky's, by DID. */const labelers = new Map<string, string[]>();export const setLabelers = (account: string, dids: string[]) => labelers.set(account, dids);
/** * Asks the AppView to include labels from the account's moderation services, and, through * Bluesky's, Bluesky's own. Without this header it sends only those it applies by default, and * subscriptions would have nothing to act on. */const withLabelers = (handler: FetchHandler, account?: string): FetchHandler => (pathname, init) => { const headers = new Headers(init.headers); const theirs = (account && labelers.get(account)) || []; const accepted = [ ...(BSKY_ALWAYS ? [`${BSKY_LABELER};redact`] : []), ...theirs.filter((did) => !BSKY_ALWAYS || did !== BSKY_LABELER), ]; // None: left to the AppView, which applies its own (Bluesky's, through Bluesky's), where an // empty list would ask for none at all. if (accepted.length) headers.set("atproto-accept-labelers", accepted.join(", ")); return handler(pathname, { ...init, headers }); };
const publicClient = new Client({ handler: withLabelers(simpleFetchHandler({ service: appview.publicUrl ?? appview.url })),});
/** * While developing: says so, once per place, when a page reaches for the main account directly * instead of its own (`useAccount`), which would read or act as the wrong account once a tab * can belong to another. */const flagged = new Set<string>();const flagPageUse = (name: string) => { if (!import.meta.env.DEV) return; const stack = new Error().stack ?? ""; const caller = stack.split("\n").find((line) => line.includes("/src/views/")); if (!caller || flagged.has(caller)) return; flagged.add(caller); console.warn( `${name}() used from a page; use useAccount() so it follows the page's account.`, caller.trim(), );};
/** An account's connections: Bluesky reads through its PDS, its PDS itself, and its chat. */type Clients = { rpc: Client; pds: Client; chat: Client };
/** Made once per signed-in account, and kept while it's in use. */const clients = new WeakMap<Agent, Clients>();
/** * The connections for an account, or for being signed out: Bluesky reads go through the * account's PDS to the AppView chosen (Bluesky's, unless another is; see appview.ts), or to it * directly with no account. */export const clientsFor = (account: Agent | undefined): Clients | undefined => { if (!account) return; let found = clients.get(account); if (!found) { // Every request watched, so a sign-in that has run out is noticed wherever it shows. const handler = watchExpiry(account); found = { rpc: new Client({ handler: withLabelers(buildFetchHandler(handler), account.sub), proxy: appviewProxyFor(account.session.token.scope), }), pds: new Client({ handler }), chat: new Client({ handler, proxy: "did:web:api.bsky.chat#bsky_chat" }), }; clients.set(account, found); } return found;};
/** * An account acting: who it is, and its connections. Likes, follows and posts are made by one, * so a tab seen as another of your accounts acts as that account. */export type Acting = { did: string; pds: Client; rpc: Client; /** The address of the account's own server (its PDS). */ service: string;};
/** Acting as an account, or nothing when signed out. */export const actingAs = (account: Agent | undefined): Acting | undefined => { const found = clientsFor(account); return account && found ? { did: account.sub, pds: found.pds, rpc: found.rpc, service: account.session.info.aud } : undefined;};
/** The public AppView, for reading while signed out. */export const publicRpc = () => publicClient;
/** * Bluesky's AppView itself, unsigned: for what the public one refuses (searching posts), signed * out, or where an account's sign-in doesn't reach. Read as anyone: no likes or follows of yours. */const appviewClient = new Client({ handler: withLabelers(simpleFetchHandler({ service: appview.url })),});export const appviewRpc = () => appviewClient;
/** * Your main account's own PDS, without the AppView proxy: for what it keeps itself, like your * preferences. Pages use their own account's instead (see `useAccount`). */export const pdsRpc = (): Client | undefined => { flagPageUse("pdsRpc"); return clientsFor(agent())?.pds;};
/** * Bluesky reads as your main account: through its PDS to the Bluesky AppView when signed in, the * public AppView otherwise. Reads `agent()`, so calling it inside a tracking scope re-runs on * sign-in and sign-out. Pages use their own account's instead (see `useAccount`). */export const rpc = (): Client => { flagPageUse("rpc"); return clientsFor(agent())?.rpc ?? publicClient;};
/** * A DID for a handle or DID, as record addresses need. Always asked of the public server: it's * public information, and your sign-in's permissions don't cover it. */export const didOf = async (actor: string) => actor.startsWith("did:") ? actor : ( await ok( publicClient.get("com.atproto.identity.resolveHandle", { params: { handle: actor as any }, }), ) ).did;