File size: 11,479 Bytes
492326f 879c715 492326f a1ab21c 492326f c6302b1 b260282 bf25558 b260282 bf25558 b260282 879c715 492326f 879c715 492326f 879c715 492326f 879c715 492326f 879c715 492326f 879c715 492326f 879c715 492326f 879c715 c6302b1 492326f 879c715 c6302b1 492326f 879c715 c6302b1 492326f 879c715 c6302b1 a1ab21c b260282 a1ab21c b260282 c6302b1 b260282 c6302b1 b260282 492326f 879c715 492326f 879c715 492326f 879c715 492326f 879c715 492326f 879c715 492326f 879c715 492326f 879c715 492326f b260282 879c715 a1ab21c b260282 bf25558 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 | import type {
FreeLabelGraph,
FreeLabelDetail as ApiFreeLabelDetail,
LabelsSummary,
PooledTweet,
ServiceAccountScope,
ServiceAccountsSnapshot,
ServiceAccountSummary,
IssuedServiceAccountCredential,
} from "@xtap-pool/shared";
import { communityGroups } from "./communities";
export type TweetRecord = {
tweet: PooledTweet;
contributors: readonly string[];
};
export type TweetPage = {
records: readonly TweetRecord[];
nextCursor?: string;
};
export type ContributorStats = {
username: string;
tweetCount: number;
lastPooledAt: string;
};
export type Me = {
username: string;
isAdmin: boolean;
};
export type MemberOrgGrant = {
name: string;
sub: string;
display_name?: string;
};
export type PoolSnapshot = {
version: 1;
admins: readonly string[];
members: readonly string[];
member_orgs: readonly MemberOrgGrant[];
bootstrap_admins: readonly string[];
updated_at: string;
updated_by?: string;
source: "dataset" | "bootstrap";
config_error?: string;
};
export type AdminPoolResponse = {
pool: PoolSnapshot;
viewer: { username: string };
};
export type AdminEnrichment = {
contract_hash: string;
totals: {
pending: number;
running: number;
retrying: number;
blocked: number;
completed: number;
};
worker_recently_completed: boolean;
freshness_lag_seconds?: number;
recent_errors: readonly { error_class: string; count: number }[];
};
export type AdminFreeLabels = {
registry_revision: number;
labels: readonly {
name: string;
status: "candidate" | "approved" | "rejected";
unit_count: number;
reason?: string;
}[];
candidates: readonly {
name: string;
unit_count: number;
distinct_authors: number;
distinct_days: number;
representative_quotes: readonly { unit_id: string; tweet_id: string; quote: string }[];
}[];
};
/**
* Enrichment API shapes (labels/free labels/graph). Local contract types for
* the endpoints described in docs/labels-and-free-labels-implementation-plan.md.
*/
export type LabelStat = {
name: string;
description?: string;
count: number;
};
export type FreeLabelSummary = {
name: string;
post_count: number;
};
export type RelatedFreeLabel = {
name: string;
shared: number;
};
export type FreeLabelDetail = {
name: string;
related: readonly RelatedFreeLabel[];
post_count: number;
};
export type FreeLabelGraphData = {
nodes: readonly { id: string; name: string; docs: number; group: number }[];
links: readonly { source: string; target: string; weight: number }[];
};
/** Free label listing (approved names only). */
export type FreeLabelStat = { name: string; count: number };
export type Filters = {
contributors: readonly string[];
labels: readonly string[];
freeLabel: string;
q: string;
since: string;
until: string;
hasMedia: boolean;
isArticle: boolean;
dedup: boolean;
};
export const defaultFilters: Filters = {
contributors: [],
labels: [],
freeLabel: "",
q: "",
since: "",
until: "",
hasMedia: false,
isArticle: false,
dedup: true,
};
function nonEmpty(value: string): string | undefined {
return value === "" ? undefined : value;
}
function flag(active: boolean): string | undefined {
return active ? "true" : undefined;
}
/** Serialize UI filter state into /api/tweets query parameters. */
export function tweetsQueryString(filters: Filters, cursor?: string): string {
const until = nonEmpty(filters.until);
const entries: [string, string | undefined][] = [
["contributors", nonEmpty(filters.contributors.join(","))],
["labels", nonEmpty(filters.labels.join(","))],
["free_label", nonEmpty(filters.freeLabel)],
["q", nonEmpty(filters.q)],
["since", nonEmpty(filters.since)],
["until", until === undefined ? undefined : `${until}T23:59:59.999Z`],
["has_media", flag(filters.hasMedia)],
["is_article", flag(filters.isArticle)],
["dedup", String(filters.dedup)],
["cursor", cursor],
];
const params = new URLSearchParams();
for (const [key, value] of entries) {
if (value !== undefined) params.set(key, value);
}
return params.toString();
}
async function getJson<T>(path: string): Promise<T | undefined> {
const response = await fetch(path, { headers: { accept: "application/json" } });
if (response.status === 401) return undefined;
if (!response.ok) throw new Error(`request failed: ${String(response.status)} ${path}`);
return (await response.json()) as T;
}
async function sendJson<T>(
path: string,
method: "POST" | "PUT" | "DELETE",
body?: unknown,
): Promise<T> {
const response = await fetch(path, {
method,
headers: {
accept: "application/json",
...(body === undefined ? {} : { "content-type": "application/json" }),
},
...(body === undefined ? {} : { body: JSON.stringify(body) }),
});
if (!response.ok) throw new Error(`request failed: ${String(response.status)} ${path}`);
return (await response.json()) as T;
}
/** Current viewer, or undefined when not signed in. */
export async function fetchMe(): Promise<Me | undefined> {
const me = await getJson<Partial<Me> & { username: string }>("/api/me");
return me === undefined ? undefined : { username: me.username, isAdmin: me.isAdmin === true };
}
export async function fetchTweets(filters: Filters, cursor?: string): Promise<TweetPage> {
const page = await getJson<TweetPage>(`/api/tweets?${tweetsQueryString(filters, cursor)}`);
if (page === undefined) throw new Error("session expired");
return page;
}
export async function fetchContributors(): Promise<ContributorStats[]> {
const body = await getJson<{ contributors: ContributorStats[] }>("/api/contributors");
if (body === undefined) throw new Error("session expired");
return body.contributors;
}
/** Preset label taxonomy with counts. */
export async function fetchLabels(): Promise<LabelStat[]> {
const body = await getJson<LabelsSummary>("/api/labels");
if (body === undefined) throw new Error("session expired");
return body.labels.map(({ name, description, count }) => ({
name,
description,
count,
}));
}
/**
* Approved free-label vocabulary with unit counts.
*/
export async function fetchFreeLabels(): Promise<FreeLabelSummary[]> {
const body = await getJson<{ free_labels: FreeLabelStat[] }>("/api/free-labels");
if (body === undefined) throw new Error("session expired");
return body.free_labels.map((entry) => ({
name: entry.name,
post_count: entry.count,
}));
}
/** One approved free label with related approved labels. */
export async function fetchFreeLabel(name: string): Promise<FreeLabelDetail> {
const body = await getJson<ApiFreeLabelDetail>(`/api/free-labels/${encodeURIComponent(name)}`);
if (body === undefined) throw new Error("session expired");
return {
name: body.name,
related: body.related.map((entry) => ({
name: entry.name,
shared: entry.shared_units,
})),
post_count: body.tweet_count,
};
}
/** Bounded co-occurrence subgraph of the top approved free labels. */
export async function fetchFreeLabelGraph(top: number): Promise<FreeLabelGraphData> {
const body = await getJson<FreeLabelGraph>(`/api/graph?top=${String(top)}`);
if (body === undefined) throw new Error("session expired");
const groups = communityGroups(
body.nodes.map((node) => node.name),
body.links,
);
return {
nodes: body.nodes.map((node) => ({
id: node.name,
name: node.name,
docs: node.unit_count,
group: groups[node.name] ?? -1,
})),
links: body.links,
};
}
export async function fetchAdminPool(): Promise<AdminPoolResponse> {
const body = await getJson<AdminPoolResponse>("/api/admin/pool");
if (body === undefined) throw new Error("session expired");
return body;
}
export async function fetchAdminEnrichment(): Promise<AdminEnrichment> {
const body = await getJson<AdminEnrichment>("/api/admin/enrichment");
if (body === undefined) throw new Error("session expired");
return body;
}
export async function fetchAdminFreeLabels(): Promise<AdminFreeLabels> {
const body = await getJson<AdminFreeLabels>("/api/admin/free-labels");
if (body === undefined) throw new Error("session expired");
return body;
}
export async function repairPoolConfig(): Promise<PoolSnapshot> {
return (await sendJson<{ pool: PoolSnapshot }>("/api/admin/pool/repair", "POST")).pool;
}
export async function fetchAdminServiceAccounts(): Promise<ServiceAccountsSnapshot> {
const body = await getJson<{ service_accounts: ServiceAccountsSnapshot }>(
"/api/admin/service-accounts",
);
if (body === undefined) throw new Error("session expired");
return body.service_accounts;
}
export async function issueServiceAccount(
name: string,
scopes: readonly ServiceAccountScope[],
): Promise<IssuedServiceAccountCredential> {
return sendJson<IssuedServiceAccountCredential>("/api/admin/service-accounts", "POST", {
name,
scopes,
});
}
export async function rotateServiceAccount(
accountId: string,
): Promise<IssuedServiceAccountCredential> {
return sendJson<IssuedServiceAccountCredential>(
`/api/admin/service-accounts/${encodeURIComponent(accountId)}/keys`,
"POST",
);
}
export async function revokeServiceAccount(accountId: string): Promise<ServiceAccountSummary> {
return (
await sendJson<{ account: ServiceAccountSummary }>(
`/api/admin/service-accounts/${encodeURIComponent(accountId)}`,
"DELETE",
)
).account;
}
export async function revokeServiceAccountKey(
accountId: string,
keyId: string,
): Promise<ServiceAccountSummary> {
return (
await sendJson<{ account: ServiceAccountSummary }>(
`/api/admin/service-accounts/${encodeURIComponent(accountId)}/keys/${encodeURIComponent(keyId)}`,
"DELETE",
)
).account;
}
export async function repairServiceAccounts(): Promise<ServiceAccountsSnapshot> {
return (
await sendJson<{ service_accounts: ServiceAccountsSnapshot }>(
"/api/admin/service-accounts/repair",
"POST",
)
).service_accounts;
}
export async function addPoolMember(username: string): Promise<PoolSnapshot> {
return (
await sendJson<{ pool: PoolSnapshot }>(
`/api/admin/members/${encodeURIComponent(username)}`,
"PUT",
)
).pool;
}
export async function removePoolMember(username: string): Promise<PoolSnapshot> {
return (
await sendJson<{ pool: PoolSnapshot }>(
`/api/admin/members/${encodeURIComponent(username)}`,
"DELETE",
)
).pool;
}
export async function addPoolAdmin(username: string): Promise<PoolSnapshot> {
return (
await sendJson<{ pool: PoolSnapshot }>(
`/api/admin/admins/${encodeURIComponent(username)}`,
"PUT",
)
).pool;
}
export async function removePoolAdmin(username: string): Promise<PoolSnapshot> {
return (
await sendJson<{ pool: PoolSnapshot }>(
`/api/admin/admins/${encodeURIComponent(username)}`,
"DELETE",
)
).pool;
}
export async function addPoolMemberOrg(orgName: string): Promise<PoolSnapshot> {
return (
await sendJson<{ pool: PoolSnapshot }>(
`/api/admin/member-orgs/${encodeURIComponent(orgName)}`,
"PUT",
)
).pool;
}
export async function removePoolMemberOrg(orgName: string): Promise<PoolSnapshot> {
return (
await sendJson<{ pool: PoolSnapshot }>(
`/api/admin/member-orgs/${encodeURIComponent(orgName)}`,
"DELETE",
)
).pool;
}
|