// The publication's standing in panoptiblog, the rankings the site publishes. // // The dataset is one static file on substandard.blog. It is somebody else's // read — nothing here writes — and the popup can live without it, so every // failure costs the row rather than the popup. import { cached } from './cache' import { type RequestOptions, requestJson } from './http' export const PANOPTIBLOG_URL = 'https://substandard.blog/panoptiblog/data.json' export const PANOPTIBLOG_PAGE = 'https://substandard.blog/panoptiblog' /** Third-party and optional: fail fast rather than hold the card open. */ const OPTIONS: RequestOptions = { timeoutMs: 8_000, retries: 1 } export interface PanoptiblogPublication { uri: string did: string rank?: number name: string url?: string description?: string wordCount: number docCount: number readingMinutes?: number readingHours?: number subscribers: number recommends: number topics?: string[] score: number } export interface PanoptiblogAccount { did: string handle?: string rank?: number wordsRank?: number publicationCount: number wordCount: number readingMinutes?: number readingHours?: number labels: string[] score: number } export interface PanoptiblogData { generatedAt: string totals: Record publications: PanoptiblogPublication[] accounts: PanoptiblogAccount[] method?: { weights?: Record } } /** * The dataset, or null when it cannot be read. Cached in `world` for the same * day as every other stranger's data: the ranking changes when the scheduled * job runs, not between two popup opens. */ export async function fetchPanoptiblog(refresh = false): Promise { try { return await cached( { scope: 'world', subject: 'panoptiblog', name: 'data' }, async () => { const data = await requestJson(PANOPTIBLOG_URL, OPTIONS) if (!Array.isArray(data?.publications) || !Array.isArray(data?.accounts)) { throw new Error('panoptiblog dataset has the wrong shape') } return data }, { refresh }, ) } catch (err) { console.debug('[substandard] panoptiblog dataset unavailable', err) return null } } /** The publication's own row, when it ranked. */ export function publicationStanding( data: PanoptiblogData | null, uri: string, ): PanoptiblogPublication | undefined { return data?.publications.find((publication) => publication.uri === uri) } /** The account's row, which carries the labels even when its publication did not rank. */ export function accountStanding( data: PanoptiblogData | null, did: string, ): PanoptiblogAccount | undefined { return data?.accounts.find((account) => account.did === did) }