v4: a /community page shows the site's week and month, from the same figures as the digests, and every recurring server task is a job of one scheduler — the digests' top dumps and community section (who joined, the new public playlists, the chat and the totals) existed only in emails. community-service.ts now gathers them for a period from listTopDumps, listNewUsers and listNewPublicPlaylists, each in its own service and returning the API's Dump, PublicUser and Playlist; the digest builds its sections from the same call, its email-only DumpRow and DUMP_COLS replaced by one Dump mapper that networkOf uses too, and links its community section to the page. The page, /community for the last week and /community/monthly for the month, signed in only and linked from the avatar menu, reads GET /api/community?period=, which serves figures kept in memory and recomputed every GERBEUR_COMMUNITY_REFRESH_MINUTES (15 by default). It shows the four figures, the top dumps and the new playlists as live cards, and the new members as UserCards, the follow tab's user row made shared. Its words are the emails' own: the messages both use are renamed from email.digest.* to digest.*, as digest.weekly.off was. api/lib/scheduler.ts's startJobs runs the digests, the community refresh, the WebSocket ping, the typing expiry and the rate-limit sweep, which main.ts starts, so none of them runs on import any more. PERIOD_MS moves to shared/settings.ts. community is reserved as a category slug, and migration 0022 renames a category that had it, with renameReservedCategories moved out of 0021 into reserved-slugs.ts. A group's dump or playlist cards now fill its column. 262 backend tests and 678 frontend tests pass, and deno check, tsc, eslint, doc:lint and i18n:compile --strict are clean; checked in screenshots in Smooth light at 1280 and 390px, Smooth dark with mocked playlists, Brutalist dark at 390px and Kanagawa dark at 1280px.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -6,6 +6,8 @@
|
||||
* @module
|
||||
*/
|
||||
|
||||
import type { Job } from "./scheduler.ts";
|
||||
|
||||
interface Bucket {
|
||||
tokens: number;
|
||||
at: number;
|
||||
@@ -42,11 +44,18 @@ export function takeToken(key: string, limit: RateLimit): boolean {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Every limit in use refills within the hour, so an older bucket is full and
|
||||
// dropping it changes nothing.
|
||||
setInterval(() => {
|
||||
const cutoff = Date.now() - 60 * 60_000;
|
||||
for (const [key, bucket] of buckets) {
|
||||
if (bucket.at < cutoff) buckets.delete(key);
|
||||
}
|
||||
}, 60_000);
|
||||
/**
|
||||
* Drops the buckets untouched for an hour, every minute. Every limit in use
|
||||
* refills within the hour, so such a bucket is full and dropping it changes
|
||||
* nothing. `main.ts` starts it.
|
||||
*/
|
||||
export const rateLimitSweepJob: Job = {
|
||||
name: "rate-limit",
|
||||
everyMs: 60_000,
|
||||
run() {
|
||||
const cutoff = Date.now() - 60 * 60_000;
|
||||
for (const [key, bucket] of buckets) {
|
||||
if (bucket.at < cutoff) buckets.delete(key);
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
@@ -2,7 +2,8 @@
|
||||
* Checks which slugs a category may take. A category's feed lives at the URL
|
||||
* root (`/music`), so its slug must not shadow another top-level page. The
|
||||
* category service runs this check when a category is created or renamed, and
|
||||
* migration 0021 renamed the categories it newly reserved.
|
||||
* the migrations that reserve more words rename the categories that had one
|
||||
* with {@link renameReservedCategories}.
|
||||
*
|
||||
* Dump and playlist slugs reserve nothing: they live under their owner's
|
||||
* `dumps/` and `playlists/`, where nothing else is named by a user. A user
|
||||
@@ -11,6 +12,7 @@
|
||||
* @module
|
||||
*/
|
||||
|
||||
import type { DatabaseSync } from "node:sqlite";
|
||||
import { FEED_TABS } from "../../shared/paths.ts";
|
||||
|
||||
/**
|
||||
@@ -29,6 +31,7 @@ export const RESERVED_SLUGS: readonly string[] = [
|
||||
"unsubscribe",
|
||||
"settings",
|
||||
"manage",
|
||||
"community",
|
||||
"api",
|
||||
"ws",
|
||||
"assets",
|
||||
@@ -39,3 +42,27 @@ export const RESERVED_SLUGS: readonly string[] = [
|
||||
export function isReservedSlug(slug: string): boolean {
|
||||
return RESERVED_SLUGS.includes(slug);
|
||||
}
|
||||
|
||||
/**
|
||||
* Renames each category whose slug is now reserved, such as `hot`, to the
|
||||
* first of `<slug>-2`, `<slug>-3`… that is free, and logs it. Migration 0021
|
||||
* runs it for v4's words, and 0022 for `community`.
|
||||
*/
|
||||
export function renameReservedCategories(db: DatabaseSync): void {
|
||||
const rows = db.prepare(`SELECT id, slug FROM categories;`).all() as {
|
||||
id: string;
|
||||
slug: string;
|
||||
}[];
|
||||
const taken = new Set(rows.map((r) => r.slug));
|
||||
for (const { id, slug } of rows) {
|
||||
if (!isReservedSlug(slug)) continue;
|
||||
let n = 2;
|
||||
while (taken.has(`${slug}-${n}`) || isReservedSlug(`${slug}-${n}`)) n++;
|
||||
const renamed = `${slug}-${n}`;
|
||||
taken.add(renamed);
|
||||
db.prepare(`UPDATE categories SET slug = ? WHERE id = ?;`).run(renamed, id);
|
||||
console.log(
|
||||
`[migrate] category "${slug}" is now "${renamed}": its slug is reserved`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
46
api/lib/scheduler.ts
Normal file
46
api/lib/scheduler.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* Runs the server's recurring tasks. Each module that needs one exports a
|
||||
* {@link Job}, and `main.ts` passes them all to {@link startJobs} when it runs
|
||||
* directly, so nothing recurs in the tests, which call a job's `run` when
|
||||
* they need it.
|
||||
*
|
||||
* @module
|
||||
*/
|
||||
|
||||
/** A recurring task, exported by its module for `main.ts` to start. */
|
||||
export interface Job {
|
||||
/** Names it in the log, as `[digest]`. */
|
||||
name: string;
|
||||
/** How long from one run's start to the next. */
|
||||
everyMs: number;
|
||||
/** How long after the start the first run waits, `everyMs` by default. */
|
||||
firstAfterMs?: number;
|
||||
/** Logs what it did itself, if anything; its errors are logged here. */
|
||||
run(): void | Promise<void>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts running each of `jobs` on its own timer. A run still going when the
|
||||
* next is due makes that one skip, and a run that throws or rejects is logged
|
||||
* and the next one runs anyway.
|
||||
*/
|
||||
export function startJobs(jobs: readonly Job[]): void {
|
||||
for (const job of jobs) {
|
||||
let running = false;
|
||||
const tick = async () => {
|
||||
if (running) return;
|
||||
running = true;
|
||||
try {
|
||||
await job.run();
|
||||
} catch (err) {
|
||||
console.error(`[${job.name}] run failed:`, err);
|
||||
} finally {
|
||||
running = false;
|
||||
}
|
||||
};
|
||||
setTimeout(() => {
|
||||
tick();
|
||||
setInterval(tick, job.everyMs);
|
||||
}, job.firstAfterMs ?? job.everyMs);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user