// 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, } }