Something went wrong. Try again.
Browser extension: detect and subscribe to standard.site publications on ATProto substandard.blog
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204// Moderation labels on the publication, from the labelers the reader listens// to.//// Advisory and opt-in: the extension asks and renders the answer, and a// labeler that cannot be reached leaves no trace but a debug log// (src/lib/labels.ts). A label that Bluesky's own interpretation puts behind a// click puts the card behind one too, until the reader asks for it — which is// why this card owns `#pub-body`'s visibility as well as its own row.
import { cached } from '../../lib/cache'import type { LabelView } from '../../lib/labels'import { isScopeError } from '../../lib/scope'import type { PageState } from '../../lib/types'import { $, TONE_ICONS } from '../dom'import { badge } from '../badge'import type { Card, CardHost } from './card'
/** * @atproto/api is most of the remaining popup download, and it is only needed * once there is a publication to label. Import the labels module (which * carries it) from `load`, never at module scope, so a popup on a page with no * publication does not parse the lexicon bundle. */type LabelsModule = typeof import('../../lib/labels')let labelsModule: Promise<LabelsModule> | undefinedfunction labelsApi(): Promise<LabelsModule> { return (labelsModule ??= import('../../lib/labels'))}
let labels: { key: string; views: LabelView[] } | undefinedlet loadedFor: string | undefined/** Article the reader has asked to see past its cover, this open only. */let uncovered: string | undefined
/** * What this set of labels is about: the publication, plus the document when * there is one. Both the cache key and the "show anyway" decision use it, so * moving to another article in the same publication asks again instead of * drawing the previous article's labels. */function labelKey(state: PageState | undefined): string | undefined { const pub = state?.pub if (!pub) return undefined return state?.doc ? `${pub.uri}|${state.doc.uri}` : pub.uri}
export const labelsCard: Card = { id: 'labels',
mount(host) { $('label-show').addEventListener('click', () => { uncovered = labelKey(host.state()) host.rerender() }) },
async load(host, refresh) { const state = host.state() const pub = state?.pub const key = labelKey(state) if (!pub || !key) return if (!refresh && loadedFor === key) return loadedFor = key // The record uris as well as the account: a labeler can label one document // without labelling everything its author ever wrote. const api = await labelsApi() const subjects = [pub.did, pub.uri, ...(state?.doc ? [state.doc.uri] : [])] const external = await cached( { scope: 'world', subject: key, name: 'labels' }, async () => api.labelsFor(subjects, await activeLabelers(api, host, refresh)), { refresh }, ) // The record's own labels are not a network read; they come from the // publication and document values already in state. They are merged after // the cached labeler answers so a refresh of one does not drop the other. const author = { did: pub.did, handle: pub.handle } const own = [ ...api.selfLabelViews(pub.record, author), ...(state?.doc ? api.selfLabelViews(state.doc.record, author) : []), ] labels = { key, views: api.sortLabelViews(mergeViews(own, external)) } host.rerender() },
render(host) { const key = labelKey(host.state()) const views = key && labels?.key === key ? labels.views : [] const row = $('labels') row.replaceChildren(...views.map(labelPill)) row.hidden = views.length === 0
// Covering is the strongest thing any one label asks for, so the cover // shows that label and the rest stay as pills behind it. const cover = views.find((v) => v.hides) const covered = !!cover && uncovered !== key $('label-cover').hidden = !covered $('pub-body').hidden = covered if (!cover) return // An alert label's cover is red, like its pill and like the status pills of // that severity; anything milder keeps the amber caution shape. $('label-cover').classList.toggle('alert', cover.severity === 'alert') $('label-cover-icon').innerHTML = TONE_ICONS[cover.severity === 'alert' ? 'alert' : 'warn'] $('label-cover-name').textContent = cover.name const desc = $('label-cover-desc') desc.textContent = cover.description ?? '' desc.hidden = !cover.description const byAuthor = cover.source === 'author' $('label-cover-src').replaceChildren( badge({ kind: 'labeler', size: 'sm', label: byAuthor ? 'the author' : `@${cover.labeler.handle ?? cover.labeler.did}`, title: byAuthor ? 'Labeled by the author' : 'Labeler', }), ) },
reset() { // The labels came from the old account's labeler subscriptions. labels = undefined loadedFor = undefined },}
/** * Which labelers to ask. The account's own Bluesky subscriptions when they * can be read — subscribe to a labeler in any client and its labels show up * here — and the local list (or Bluesky's moderation service) when they * cannot: signed out, offline, or a session granted before this extension * asked for the preferences scope. * * Cached with the rest of what we read from that account's repo, since it is * the same answer for every publication. Refresh bypasses it, which is how * someone who has just subscribed to a labeler sees its labels without * waiting. */async function activeLabelers( api: LabelsModule, host: CardHost, refresh: boolean,): Promise<string[]> { const did = host.session()?.did if (!did) return api.labelerDids() try { return await cached( { scope: 'own', subject: did, name: 'labelers' }, async () => { const { restoreAgent } = await import('../../lib/oauth') const restored = await restoreAgent() if (!('agent' in restored)) throw new Error('no restorable session') const dids = api.withDefaultLabeler(await api.subscribedLabelerDids(restored.agent)) console.debug(`[substandard] ${dids.length} subscribed labeler(s)`) return dids }, { refresh }, ) } catch (err) { // Advisory either way, so this falls back rather than surfacing: the // labels a stale session can still see are better than none. console.debug( '[substandard] could not read labeler subscriptions', isScopeError(err) ? '(session predates the preferences scope)' : '', err, ) return api.labelerDids() }}
/** One view per source and value; two labelers saying the same thing both stay. */function mergeViews(own: LabelView[], external: LabelView[]): LabelView[] { const seen = new Set<string>() const views: LabelView[] = [] for (const view of [...own, ...external]) { const key = `${view.source ?? 'labeler'}:${view.labeler.did}:${view.value}` if (seen.has(key)) continue seen.add(key) views.push(view) } return views}
function labelPill(view: LabelView): HTMLElement { // The severity shape is part of the badge's media (see src/popup/badge.ts); // `label-pill` stays as a hook for the docs capture. const byAuthor = view.source === 'author' return badge({ kind: 'labeler', size: 'sm', tone: view.severity === 'alert' ? 'alert' : view.severity === 'inform' ? 'warn' : 'neutral', className: 'label-pill', label: view.name, // The whole judgement — what it means and who made it — without a second // row of UI in a 400px popup. title: [ view.description, byAuthor ? 'Labeled by the author' : `Labeled by @${view.labeler.handle ?? view.labeler.did}`, ] .filter(Boolean) .join('\n'), })}