Files
gerbeur/api/services/community-service.ts
khannurien 708d113013 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>
2026-10-09 11:42:01 +00:00

94 lines
2.9 KiB
TypeScript

/**
* Gathers what the whole site did over a period, for the `/community` page
* and the digests' shared sections. The digests ask {@link communityStatsOf}
* for each reader's own period. The page reads the figures of the week and
* the month ending at the last refresh, which {@link communityJob} redoes
* every {@link COMMUNITY_REFRESH_MINUTES}, so a visit runs no query.
*
* @module
*/
import { db } from "../db/connection.ts";
import { COMMUNITY_REFRESH_MINUTES } from "../config.ts";
import type { CommunityStats } from "../../shared/community.ts";
import {
DIGEST_CADENCES,
type DigestCadence,
PERIOD_MS,
} from "../../shared/settings.ts";
import type { Job } from "../lib/scheduler.ts";
import { listTopDumps } from "./dump-service.ts";
import { listNewPublicPlaylists } from "./playlist-service.ts";
import { listNewUsers } from "./user-service.ts";
/** How many top dumps a period keeps, as many as the monthly recap shows. */
const TOP_MAX = 10;
/** Returns how many rows of `table` were created from `from` to `to`. */
function countOf(
table: string,
from: string,
to: string,
where = "1",
): number {
return (db.prepare(
`SELECT COUNT(*) AS n FROM ${table}
WHERE ${where} AND created_at >= ? AND created_at < ?;`,
).get(from, to) as { n: number }).n;
}
/**
* Returns what the site did from `from` to `to`: its most upvoted public
* dumps, who joined, the new public playlists, the chat's messages and the
* totals.
*/
export function communityStatsOf(from: Date, to: Date): CommunityStats {
const [f, t] = [from.toISOString(), to.toISOString()];
return {
from,
to,
top: listTopDumps(from, to, TOP_MAX),
members: listNewUsers(from, to),
playlists: listNewPublicPlaylists(from, to),
chatMessages: countOf("chat_messages", f, t),
totals: {
dumps: countOf("dumps", f, t, "is_private = 0"),
upvotes: countOf("votes", f, t),
comments: countOf("comments", f, t, "deleted = 0"),
},
};
}
/** The figures {@link getCommunityStats} serves, by period. */
const latest = new Map<DigestCadence, CommunityStats>();
/** Recomputes the figures of each period, ending at `now`. */
export function refreshCommunityStats(now: Date = new Date()): void {
for (const period of DIGEST_CADENCES) {
latest.set(
period,
communityStatsOf(new Date(now.getTime() - PERIOD_MS[period]), now),
);
}
}
/**
* Returns the figures of `period` as of the last refresh, for
* `GET /api/community`, refreshing them first if there are none yet.
*/
export function getCommunityStats(period: DigestCadence): CommunityStats {
if (!latest.has(period)) refreshCommunityStats();
return latest.get(period)!;
}
/**
* Refreshes the figures at startup, then every
* {@link COMMUNITY_REFRESH_MINUTES}. `main.ts` starts it.
*/
export const communityJob: Job = {
name: "community",
everyMs: COMMUNITY_REFRESH_MINUTES * 60_000,
firstAfterMs: 0,
run: () => refreshCommunityStats(),
};