Something went wrong. Try again.
Browser extension: detect and subscribe to standard.site publications on ATProto substandard.blog
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117// The signed-in half of panoptiblog. panoptiblog-account.js imports this only// when a reader signs in, comes back from the authorization server, or already// has a session: the OAuth client opens IndexedDB and a BroadcastChannel as// soon as it loads, and a logged-out reader should pay for none of it.//// The client is public (no secret): its metadata is served at its client_id,// web/public/panoptiblog-client-metadata.json, and inlined here from that same// file so the two cannot drift.
import { Agent } from "@atproto/api";import { BrowserOAuthClient } from "@atproto/oauth-client-browser";import clientMetadata from "../../public/panoptiblog-client-metadata.json";import { SUBSCRIPTION_COLLECTION, isMissingRecord, parseAtUri, subscriptionRecord } from "./panoptiblog-follow.js";
const PAGE_SIZE = 100;// A ceiling on pages, so a repo that keeps returning cursors cannot spin the// page forever. 10,000 subscriptions is far past any real reader.const MAX_PAGES = 100;
let client;const deletedListeners = new Set();
function getClient() { client ??= new BrowserOAuthClient({ clientMetadata, // Same resolver as the extension (src/lib/oauth.ts). handleResolver: "https://bsky.social", onSessionDeleted: (sub, cause) => { for (const listener of deletedListeners) listener(sub, cause); }, }); return client;}
/** Called when the client drops a session: refresh failed, or revoked elsewhere. */export function onSessionDeleted(listener) { deletedListeners.add(listener);}
/** * Finish a sign-in redirect if the URL carries one, or restore the stored * session. Undefined when there is no session; throws when the callback or * the restore fails. */export async function resume() { const result = await getClient().init(); return result ? account(result.session) : undefined;}
/** Navigates away to the reader's authorization server. */export function signIn(handle) { return getClient().signIn(handle);}
export async function signOut(did) { await getClient().revoke(did);}
function account(session) { const agent = new Agent(session); const did = session.did; return { did,
/** The handle the repo claims, when its DID document agrees. */ async handle() { const { data } = await agent.com.atproto.repo.describeRepo({ repo: did }); return data.handleIsCorrect ? data.handle : undefined; },
/** Every subscription record, unparsed: {uri, value}[]. */ async subscriptions() { const records = []; let cursor; for (let page = 0; page < MAX_PAGES; page++) { const { data } = await agent.com.atproto.repo.listRecords({ repo: did, collection: SUBSCRIPTION_COLLECTION, limit: PAGE_SIZE, cursor, }); records.push(...data.records); if (!data.cursor || data.records.length === 0) break; cursor = data.cursor; } console.debug(`[panoptiblog] read ${records.length} subscription records`); return records; },
/** Creates one record and returns its key. */ async subscribe(publication) { const { data } = await agent.com.atproto.repo.createRecord({ repo: did, collection: SUBSCRIPTION_COLLECTION, record: subscriptionRecord(publication), }); const rkey = parseAtUri(data.uri)?.rkey; if (!rkey) throw new Error(`unexpected subscription uri: ${data.uri}`); return rkey; },
/** Deletes one record. A record that is already gone counts as deleted. */ async unsubscribe(rkey) { try { await agent.com.atproto.repo.deleteRecord({ repo: did, collection: SUBSCRIPTION_COLLECTION, rkey, }); } catch (err) { if (!isMissingRecord(err)) throw err; console.debug("[panoptiblog] subscription record was already gone", rkey); } }, };}