import { 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(); 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(); 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(); /** * 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;