Something went wrong. Try again.
Browser extension: detect and subscribe to standard.site publications on ATProto substandard.blog
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364// The stansite label vocabulary and the operations the service performs on it.//// A label is a statement about an account, not about a post. The values come// from the panoptiblog dataset's account rows, so the labeler and the rankings// page describe an account the same way. The signature is over the DAG-CBOR// encoding of the label without `sig`, as com.atproto.label.defs specifies.
import { Secp256k1Keypair, verifySignature } from '@atproto/crypto'import { cborEncode } from '@atproto/lex-cbor'
/** Values the service may emit. A dataset label outside this list is ignored. */export const LABEL_VALUES = [ 'blogged-recently', 'stopped-blogging', 'multiple-blogs', 'new-blogger', 'prolific', 'no-posts', 'top-one-percent-publication', 'top-five-percent-publication', 'top-one-percent-publisher', 'top-five-percent-publisher', 'top-one-percent-real-blogger', 'top-five-percent-real-blogger', 'early-adopter',]
/** * Declarations published in app.bsky.labeler.service. `inform` and `ignore` * throughout: a publication history is context, not a content warning. */export const LABEL_DEFINITIONS = [ { identifier: 'blogged-recently', severity: 'inform', blurs: 'none', defaultSetting: 'ignore', locales: [ { lang: 'en', name: 'blogged recently', description: 'The account published on a standard.site publication in the last 30 days.', }, ], }, { identifier: 'stopped-blogging', severity: 'inform', blurs: 'none', defaultSetting: 'ignore', locales: [ { lang: 'en', name: 'stopped blogging', description: 'The account has publications but has not published in 180 days.', }, ], }, { identifier: 'multiple-blogs', severity: 'inform', blurs: 'none', defaultSetting: 'ignore', locales: [ { lang: 'en', name: 'multiple blogs', description: 'The account owns more than one standard.site publication.', }, ], }, { identifier: 'new-blogger', severity: 'inform', blurs: 'none', defaultSetting: 'ignore', locales: [ { lang: 'en', name: 'new blogger', description: 'The account first published less than 90 days ago.', }, ], }, { identifier: 'prolific', severity: 'inform', blurs: 'none', defaultSetting: 'ignore', locales: [ { lang: 'en', name: 'prolific', description: 'The account has published at least 100,000 words.', }, ], }, { identifier: 'no-posts', severity: 'inform', blurs: 'none', defaultSetting: 'ignore', locales: [ { lang: 'en', name: 'no posts', description: 'The account owns a publication but has published no documents.', }, ], }, { identifier: 'top-one-percent-publication', severity: 'inform', blurs: 'none', defaultSetting: 'ignore', locales: [ { lang: 'en', name: 'top 1% publication', description: 'One of the account’s publications is in the top 1% of ranked publications.', }, ], }, { identifier: 'top-five-percent-publication', severity: 'inform', blurs: 'none', defaultSetting: 'ignore', locales: [ { lang: 'en', name: 'top 5% publication', description: 'One of the account’s publications is in the top 5% of ranked publications.', }, ], }, { identifier: 'top-one-percent-publisher', severity: 'inform', blurs: 'none', defaultSetting: 'ignore', locales: [ { lang: 'en', name: 'top 1% publisher', description: 'The account is in the top 1% of ranked publisher accounts.', }, ], }, { identifier: 'top-five-percent-publisher', severity: 'inform', blurs: 'none', defaultSetting: 'ignore', locales: [ { lang: 'en', name: 'top 5% publisher', description: 'The account is in the top 5% of ranked publisher accounts.', }, ], }, { identifier: 'top-one-percent-real-blogger', severity: 'inform', blurs: 'none', defaultSetting: 'ignore', locales: [ { lang: 'en', name: 'top 1% real blogger', description: 'One publication is in the top 1% of active publications with at least five posts and two thousand words.', }, ], }, { identifier: 'top-five-percent-real-blogger', severity: 'inform', blurs: 'none', defaultSetting: 'ignore', locales: [ { lang: 'en', name: 'top 5% real blogger', description: 'One publication is in the top 5% of active publications with at least five posts and two thousand words.', }, ], }, { identifier: 'early-adopter', severity: 'inform', blurs: 'none', defaultSetting: 'ignore', locales: [ { lang: 'en', name: 'early adopter', description: 'The account published one of the first 500 standard.site publications still indexed.', }, ], },]
/** * Current positive labels for every account in the dataset. Labels are * deduplicated by account and value; the dataset may repeat one at a boundary. */export function labelsFromDataset(dataset) { const cts = typeof dataset?.generatedAt === 'string' ? dataset.generatedAt : new Date().toISOString() const seen = new Set() const labels = [] for (const account of dataset?.accounts ?? []) { if (typeof account?.did !== 'string') continue const values = [...(account.labels ?? [])] if (account.earlyAdopter) values.push('early-adopter') addPercentile(values, account.bestPublicationPercentile, 'top', 'publication') addPercentile(values, account.publisherPercentile, 'top', 'publisher') addPercentile(values, account.bestRealBloggerPercentile, 'top', 'real-blogger') for (const val of values) { if (!LABEL_VALUES.includes(val)) continue const key = `${account.did}|${val}` if (seen.has(key)) continue seen.add(key) labels.push({ uri: account.did, val, cts }) } } return labels}
/** A percentile field becomes the 1% and 5% label for its cohort. */function addPercentile(values, percentile, prefix, cohort) { if (!Number.isFinite(percentile)) return if (percentile <= 1) values.push(`${prefix}-one-percent-${cohort}`) else if (percentile <= 5) values.push(`${prefix}-five-percent-${cohort}`)}
/** * Sign one label. `src` is the labeler account's DID, which is not derivable * from the signing key: the key's did:key is what a verifier resolves from the * account's DID document. */export async function signLabel(label, { src, keypair }) { const unsigned = { ver: 1, src, uri: label.uri, val: label.val, cts: label.cts, } if (label.neg === true) unsigned.neg = true const signature = await keypair.sign(cborEncode(unsigned)) return { ...unsigned, sig: Buffer.from(signature).toString('base64') }}
/** Verify a label against the labeler's did:key. Used by the tests. */export async function verifyLabel(label, didKey) { const { sig, ...unsigned } = label if (typeof sig !== 'string') return false return verifySignature(didKey, cborEncode(unsigned), Buffer.from(sig, 'base64'))}
/** * What changed between two full label sets. A value that disappeared gets a * negation rather than being silently dropped: clients keep showing what they * were told until they are told otherwise. */export function diffLabels(previous, next) { const key = (label) => `${label.uri}|${label.val}` const before = new Set(previous.map(key)) const after = new Set(next.map(key)) return { added: next.filter((label) => !before.has(key(label))), removed: previous .filter((label) => !after.has(key(label))) .map((label) => ({ ...label, neg: true })), }}
/** A DID in a `uriPatterns` entry: exact, `prefix*`, or `*`. */export function matchesUri(uri, pattern) { if (pattern === '*') return true if (pattern.endsWith('*')) return uri.startsWith(pattern.slice(0, -1)) return uri === pattern}
/** * The queryLabels page. `cursor` is the index into the filtered list; the * lexicon leaves its shape to the service, and a client only has to hand back * what it was given. */export function queryLabels(labels, { uriPatterns, sources, limit = 50, cursor } = {}) { const patterns = asArray(uriPatterns).filter((value) => typeof value === 'string') const sourceSet = new Set(asArray(sources).filter((value) => typeof value === 'string')) const matched = labels.filter((label) => { if (sourceSet.size > 0 && !sourceSet.has(label.src)) return false if (patterns.length === 0) return true return patterns.some((pattern) => matchesUri(label.uri, pattern)) }) const pageSize = Math.max(1, Math.min(250, Number(limit) || 50)) const start = Math.max(0, Number(cursor) || 0) const page = matched.slice(start, start + pageSize) const next = start + page.length return { labels: page, cursor: next < matched.length ? String(next) : undefined, }}
function asArray(value) { if (value === undefined) return [] return Array.isArray(value) ? value : [value]}
/** A keypair from the private key the deployment keeps in a secret. */export async function keypairFromHex(hex) { if (typeof hex !== 'string' || hex.length === 0) { throw new Error('LABELER_SIGNING_KEY is not set') } return Secp256k1Keypair.import(hex.trim(), { exportable: false })}
/** The labeler's DID document for a did:web identity. */export function didDocument({ did, publicKeyMultibase, pds, endpoint }) { const document = { '@context': ['https://www.w3.org/ns/did/v1'], id: did, verificationMethod: [ { id: `${did}#atproto`, type: 'Multikey', controller: did, publicKeyMultibase, }, ], service: [ { id: '#atproto_labeler', type: 'AtprotoLabeler', serviceEndpoint: endpoint, }, ], } if (pds) { document.service.unshift({ id: '#atproto_pds', type: 'AtprotoPersonalDataServer', serviceEndpoint: pds, }) } return document}
/** The record the labeler account publishes to its own repo. */export function serviceRecord(createdAt = new Date().toISOString()) { return { $type: 'app.bsky.labeler.service', policies: { labelValues: LABEL_VALUES, labelValueDefinitions: LABEL_DEFINITIONS, }, createdAt, }}