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:
khannurien
2026-10-09 11:42:01 +00:00
parent 7713c5a1b5
commit 708d113013
44 changed files with 1466 additions and 524 deletions

View File

@@ -164,6 +164,13 @@ export const ORPHANED_ATTACHMENTS_RETENTION_HOURS = 1;
*/
export const UNUSED_INVITES_RETENTION_DAYS = 7;
/**
* How often, in minutes, the `/community` page's figures are recomputed
* (`GERBEUR_COMMUNITY_REFRESH_MINUTES`), 15 by default.
*/
export const COMMUNITY_REFRESH_MINUTES: number =
Number(Deno.env.get("GERBEUR_COMMUNITY_REFRESH_MINUTES")) || 15;
/**
* The site's name (`GERBEUR_SITE_NAME`). It is filled into index.html, the OG
* tags, the icons' manifest and the emails.

View File

@@ -28,6 +28,7 @@ import { up as up0018AttachmentResourceType } from "./migrations/0018_attachment
import { up as up0019UserSettings } from "./migrations/0019_user_settings.ts";
import { up as up0020DigestSchedule } from "./migrations/0020_digest_schedule.ts";
import { up as up0021PageUrls } from "./migrations/0021_page_urls.ts";
import { up as up0022ReserveCommunity } from "./migrations/0022_reserve_community.ts";
interface Migration {
name: string;
@@ -68,6 +69,7 @@ const MIGRATIONS: Migration[] = [
{ name: "0019_user_settings", up: up0019UserSettings },
{ name: "0020_digest_schedule", up: up0020DigestSchedule },
{ name: "0021_page_urls", up: up0021PageUrls },
{ name: "0022_reserve_community", up: up0022ReserveCommunity },
];
/**

View File

@@ -28,7 +28,7 @@ import {
userPath,
} from "../../../shared/paths.ts";
import { type SlugKind, uniqueSlug } from "../../lib/slugify.ts";
import { isReservedSlug } from "../../lib/reserved-slugs.ts";
import { renameReservedCategories } from "../../lib/reserved-slugs.ts";
import { isSelfHost } from "../../lib/self-url.ts";
/** An item's new path, by its v3 slug and by its id. */
@@ -67,29 +67,6 @@ function reslug(db: DatabaseSync, kind: SlugKind): PathsByKey {
return paths;
}
/**
* Renames each category whose slug v4 reserves at the root, such as `hot`,
* to the first of `<slug>-2`, `<slug>-3`… that is free, and logs it.
*/
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}": v4 reserves its slug`,
);
}
}
/** Where each v3 profile tab went: a tab, the overview, or a page of its own. */
const PROFILE_TAB_V4: Record<string, ProfileTab | "" | "/settings"> = {
dumps: "",

View File

@@ -0,0 +1,14 @@
/**
* Reserves `community` at the URL root for the community page, renaming a
* category that had it as its slug.
*
* @module
*/
import type { DatabaseSync } from "node:sqlite";
import { renameReservedCategories } from "../../lib/reserved-slugs.ts";
/** Run once by `runMigrations`. */
export function up(db: DatabaseSync): void {
renameReservedCategories(db);
}

View File

@@ -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);
}
},
};

View File

@@ -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
View 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);
}
}

View File

@@ -105,6 +105,8 @@ export interface DigestContent {
notifications: string;
followed: string;
chat: string;
/** The community page's view of the cadence's period. */
community: string;
settings: string;
/** The one-click page that turns this cadence off. */
unsubscribe: string;
@@ -124,11 +126,11 @@ const ACTIVITY_ORDER: ActivityKind[] = [
function countLabel(kind: ActivityKind, n: number): string {
switch (kind) {
case "upvotes":
return i18n._("email.digest.count.upvotes", { n });
return i18n._("digest.count.upvotes", { n });
case "followers":
return i18n._("email.digest.count.followers", { n });
case "comments":
return i18n._("email.digest.count.comments", { n });
return i18n._("digest.count.comments", { n });
case "mentions":
return i18n._("email.digest.count.mentions", { n });
case "likes":
@@ -173,8 +175,8 @@ const addedToLine = (playlist: string) =>
const uploadLabel = () => i18n._("email.digest.upload");
const invitedLine = (inviter: string, reader: string) =>
inviter === reader
? i18n._("email.digest.invitedByYou")
: i18n._("email.digest.invitedBy", { username: inviter });
? i18n._("digest.invitedByYou")
: i18n._("digest.invitedBy", { username: inviter });
const andMoreLine = (n: number) => i18n._("email.digest.andMore", { n });
const playlistMeta = (owner: string, n: number) =>
i18n._("email.digest.playlistMeta", { username: owner, n });
@@ -286,9 +288,7 @@ export function renderDigestEmail(
md.push(
`## ${
escMd(
monthly
? i18n._("email.digest.topMonth")
: i18n._("email.digest.topWeek"),
monthly ? i18n._("digest.topMonth") : i18n._("digest.topWeek"),
)
}`,
);
@@ -304,20 +304,20 @@ export function renderDigestEmail(
if (community) {
const { dumps, upvotes, comments } = community.totals;
md.push(
`## ${escMd(i18n._("email.digest.community"))}`,
`## ${escMd(i18n._("digest.community"))}`,
);
if (monthly) {
md.push(
[
figure(
dumps,
i18n._("email.digest.stat.dumps", { n: dumps }),
i18n._("digest.stat.dumps", { n: dumps }),
),
figure(upvotes, countLabel("upvotes", upvotes)),
figure(comments, countLabel("comments", comments)),
figure(
community.chatMessages,
i18n._("email.digest.stat.chat", { n: community.chatMessages }),
i18n._("digest.stat.chat", { n: community.chatMessages }),
),
].join(" · "),
);
@@ -326,7 +326,7 @@ export function renderDigestEmail(
md.push(
`### ${
escMd(
i18n._("email.digest.newMembers"),
i18n._("digest.newMembers"),
)
}`,
[
@@ -345,7 +345,7 @@ export function renderDigestEmail(
}
if (community.playlists.length > 0) {
md.push(
`### ${escMd(i18n._("email.digest.newPlaylists"))}`,
`### ${escMd(i18n._("digest.newPlaylists"))}`,
[
...community.playlists.map((p) =>
`- ${mdLink(p.title, p.url)} · ${
@@ -378,6 +378,12 @@ export function renderDigestEmail(
}*`,
);
}
md.push(`${
mdLink(
i18n._("email.digest.communityPage"),
content.links.community,
)
} →`);
}
md.push(

View File

@@ -1,6 +1,6 @@
/**
* Builds the Oak application and, when run directly, starts the digests'
* scheduler and listens on {@link LISTEN_HOST}:{@link PORT}. A request goes
* Builds the Oak application and, when run directly, starts the recurring
* jobs and listens on {@link LISTEN_HOST}:{@link PORT}. A request goes
* through the error middleware, the OG tags and CORS, then the routers, one
* per resource of the API (`API.md` lists them), and last the built frontend,
* which answers every path no router did.
@@ -15,6 +15,7 @@ import attachmentsRouter from "./routes/attachments.ts";
import avatarsRouter from "./routes/avatars.ts";
import categoriesRouter from "./routes/categories.ts";
import chatRouter from "./routes/chat.ts";
import communityRouter from "./routes/community.ts";
import commentsRouter from "./routes/comments.ts";
import dumpsRouter from "./routes/dumps.ts";
import filesRouter from "./routes/files.ts";
@@ -36,7 +37,11 @@ import { ALLOWED_ORIGINS, LISTEN_HOST, PORT, PUBLIC_URL } from "./config.ts";
import { errorMiddleware } from "./middleware/error.ts";
import { ogMiddleware } from "./middleware/og.ts";
import { routeStaticFilesFrom } from "./lib/static.ts";
import { startDigestScheduler } from "./services/digest-service.ts";
import { startJobs } from "./lib/scheduler.ts";
import { rateLimitSweepJob } from "./lib/rate-limit.ts";
import { digestJob } from "./services/digest-service.ts";
import { communityJob } from "./services/community-service.ts";
import { pingJob, typingExpiryJob } from "./services/ws-service.ts";
/**
* The whole server, exported so the tests' helpers can send it requests
@@ -76,6 +81,7 @@ for (
categoriesRouter,
searchRouter,
chatRouter,
communityRouter,
invitesRouter,
attachmentsRouter,
remoteRouter,
@@ -100,7 +106,13 @@ app.addEventListener(
);
if (import.meta.main) {
startDigestScheduler();
startJobs([
digestJob,
communityJob,
pingJob,
typingExpiryJob,
rateLimitSweepJob,
]);
await app.listen({ hostname: LISTEN_HOST, port: PORT });
}

37
api/routes/community.ts Normal file
View File

@@ -0,0 +1,37 @@
/**
* Serves the figures of the client's `/community` page, which
* `community-service.ts` recomputes on a timer.
*
* - `GET /api/community?period=weekly|monthly` — signed in. Answers the
* site's figures over the last week (by default) or month.
*
* @module
*/
import { Router } from "@oak/oak";
import type { APIResponse } from "../../shared/api.ts";
import type { CommunityStats } from "../../shared/community.ts";
import { isDigestCadence } from "../../shared/settings.ts";
import { APIException } from "../lib/api-exception.ts";
import { type AuthContext, authMiddleware } from "../middleware/auth.ts";
import { getCommunityStats } from "../services/community-service.ts";
/**
* Mounted by `api/main.ts` behind the error, Open Graph and CORS middleware.
* An `APIException` thrown by a handler becomes the JSON error response there.
*/
const router: Router = new Router({ prefix: "/api/community" });
router.get("/", authMiddleware, (ctx: AuthContext) => {
const period = ctx.request.url.searchParams.get("period") ?? "weekly";
if (!isDigestCadence(period)) {
throw new APIException(400, "period must be weekly or monthly");
}
const responseBody: APIResponse<CommunityStats> = {
success: true,
data: getCommunityStats(period),
};
ctx.response.body = responseBody;
});
export default router;

View File

@@ -0,0 +1,93 @@
/**
* 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(),
};

View File

@@ -1,14 +1,15 @@
/**
* Sends each user their weekly digest and monthly recap, unless they turned
* it off. `main.ts` starts {@link startDigestScheduler}, which every quarter
* of an hour sends the digests that are due; `digest_schedule` remembers, for
* it off. `main.ts` starts {@link digestJob}, which every quarter of an hour
* sends the digests that are due; `digest_schedule` remembers, for
* each user and cadence, the start of the period their next one covers.
*
* A period is 7 days or 30, rolling from when the user's schedule began, so
* the digests spread over the week instead of leaving together. A digest
* covers the period just ended, or the last full period when the server was
* down longer than one; a digest with nothing to show isn't sent, and either
* way the next period begins.
* way the next period begins. The top dumps and the community section come
* from `community-service.ts`, which the `/community` page reads too.
*
* @module
*/
@@ -20,7 +21,17 @@ import {
DIGEST_CADENCES,
DIGEST_SETTING,
type DigestCadence,
PERIOD_MS,
} from "../../shared/settings.ts";
import type { CommunityStats } from "../../shared/community.ts";
import type { Dump } from "../../shared/dump.ts";
import {
DUMP_SELECT_COLUMNS_ALIASED,
dumpRowToApi,
isDumpRow,
} from "../model/dump.ts";
import { parseRows } from "../model/rows.ts";
import type { Job } from "../lib/scheduler.ts";
import { SETTINGS_COLUMNS } from "../model/settings.ts";
import type {
CommentLikedData,
@@ -33,7 +44,12 @@ import type {
UserFollowedData,
UserMentionedData,
} from "../../shared/notification.ts";
import { dumpPath, feedPath, playlistPath } from "../../shared/paths.ts";
import {
communityPath,
dumpPath,
feedPath,
playlistPath,
} from "../../shared/paths.ts";
import {
type ActivityKind,
type DigestContent,
@@ -47,13 +63,8 @@ import { isEmailEnabled, sendEmail } from "./email-service.ts";
import { getSettings } from "./settings-service.ts";
import { dumpFileVersion } from "./dump-service.ts";
import { withCurrentRefs } from "./notification-service.ts";
import { communityStatsOf } from "./community-service.ts";
const DAY_MS = 86_400_000;
/** How long each cadence's period lasts. */
export const PERIOD_MS: Record<DigestCadence, number> = {
weekly: 7 * DAY_MS,
monthly: 30 * DAY_MS,
};
const TICK_MS = 15 * 60_000;
// What one digest shows at most, as the drafts settled it.
@@ -94,55 +105,21 @@ function quoteOf(markdown: string | undefined | null): string | undefined {
: flat;
}
/** The columns {@link toDigestDump} reads, selected from `dumps d`. */
const DUMP_COLS = `d.id, d.slug, d.title, d.kind, d.url, d.user_id,
d.created_at, d.updated_at, d.file_mime, d.file_size, d.rich_content,
d.custom_thumbnail_mime, d.vote_count, u.username AS poster,
(SELECT COUNT(*) FROM comments c WHERE c.dump_id = d.id AND c.deleted = 0) AS comment_count`;
interface DumpRow {
id: string;
slug: string;
title: string;
kind: string;
url: string | null;
user_id: string;
created_at: string;
updated_at: string | null;
file_mime: string | null;
file_size: number | null;
rich_content: string | null;
custom_thumbnail_mime: string | null;
vote_count: number;
poster: string;
comment_count: number;
}
/**
* Returns the image a mail client can load for `row`, which must be public:
* Returns the image a mail client can load for `dump`, which must be public:
* the thumbnails route for a custom thumbnail or an image or video upload,
* else the link preview's image, through the image proxy when it is plain
* http, which a mail client may refuse.
*/
function thumbnailOf(row: DumpRow): string | null {
const isMedia = row.kind === "file" &&
/^(image|video)\//.test(row.file_mime ?? "");
if (row.custom_thumbnail_mime || isMedia) {
const version = dumpFileVersion({
fileSize: row.file_size ?? undefined,
createdAt: new Date(row.created_at),
updatedAt: row.updated_at ? new Date(row.updated_at) : undefined,
});
return `${PUBLIC_URL}/api/dumps/${row.id}/thumbnail?v=${version}`;
}
let thumbnailUrl: unknown;
try {
thumbnailUrl = row.rich_content
? JSON.parse(row.rich_content)?.thumbnailUrl
: undefined;
} catch {
return null;
function thumbnailOf(dump: Dump): string | null {
const isMedia = dump.kind === "file" &&
/^(image|video)\//.test(dump.fileMime ?? "");
if (dump.thumbnailMime || isMedia) {
return `${PUBLIC_URL}/api/dumps/${dump.id}/thumbnail?v=${
dumpFileVersion(dump)
}`;
}
const thumbnailUrl = dump.richContent?.thumbnailUrl;
if (typeof thumbnailUrl !== "string") return null;
if (thumbnailUrl.startsWith("https://")) return thumbnailUrl;
if (thumbnailUrl.startsWith("http://")) {
@@ -153,26 +130,24 @@ function thumbnailOf(row: DumpRow): string | null {
return null;
}
function toDigestDump(row: DumpRow, readerId: string): DigestDump {
function toDigestDump(dump: Dump, readerId: string): DigestDump {
let domain: string | null = null;
if (row.kind === "url" && row.url) {
if (dump.kind === "url" && dump.url) {
try {
domain = new URL(row.url).hostname.replace(/^www\./, "");
domain = new URL(dump.url).hostname.replace(/^www\./, "");
} catch {
domain = null;
}
}
return {
title: row.title,
url: `${FRONTEND_URL}${
dumpPath({ ownerUsername: row.poster, slug: row.slug })
}`,
poster: row.poster,
title: dump.title,
url: `${FRONTEND_URL}${dumpPath(dump)}`,
poster: dump.ownerUsername,
domain,
thumbnail: thumbnailOf(row),
votes: row.vote_count,
comments: row.comment_count,
mine: row.user_id === readerId,
thumbnail: thumbnailOf(dump),
votes: dump.voteCount,
comments: dump.commentCount,
mine: dump.userId === readerId,
};
}
@@ -339,119 +314,59 @@ function highlightOf(
*/
function networkOf(readerId: string, w: Window): DigestContent["network"] {
const rows = db.prepare(
`SELECT ${DUMP_COLS}, NULL AS playlist_title, d.created_at AS at
FROM dumps d JOIN users u ON u.id = d.user_id
`SELECT ${DUMP_SELECT_COLUMNS_ALIASED}, NULL AS playlist_title, d.created_at AS at
FROM dumps d
WHERE d.is_private = 0 AND d.user_id != :reader
AND d.created_at >= :from AND d.created_at < :to
AND d.user_id IN (SELECT followed_user_id FROM follows
WHERE follower_id = :reader AND followed_user_id IS NOT NULL)
UNION ALL
SELECT ${DUMP_COLS}, p.title AS playlist_title, pd.added_at AS at
SELECT ${DUMP_SELECT_COLUMNS_ALIASED}, p.title AS playlist_title, pd.added_at AS at
FROM playlist_dumps pd
JOIN playlists p ON p.id = pd.playlist_id
JOIN dumps d ON d.id = pd.dump_id
JOIN users u ON u.id = d.user_id
WHERE p.is_public = 1 AND d.is_private = 0 AND d.user_id != :reader
AND pd.added_at >= :from AND pd.added_at < :to
AND pd.playlist_id IN (SELECT followed_playlist_id FROM follows
WHERE follower_id = :reader AND followed_playlist_id IS NOT NULL);`,
).all({ reader: readerId, from: w.from, to: w.to }) as unknown as (DumpRow & {
playlist_title: string | null;
at: string;
})[];
).all({ reader: readerId, from: w.from, to: w.to });
const found = parseRows(rows, isDumpRow, (row) => ({
dump: dumpRowToApi(row),
playlistTitle: row.playlist_title as string | null,
at: row.at as string,
}));
// A dump both posted by someone followed and added to a followed playlist
// is listed once, as the post.
const byId = new Map<string, (typeof rows)[number]>();
for (const row of rows) {
const seen = byId.get(row.id);
if (!seen || (seen.playlist_title && !row.playlist_title)) {
byId.set(row.id, row);
const byId = new Map<string, (typeof found)[number]>();
for (const item of found) {
const seen = byId.get(item.dump.id);
if (!seen || (seen.playlistTitle && !item.playlistTitle)) {
byId.set(item.dump.id, item);
}
}
const sorted = [...byId.values()].sort((a, b) =>
b.vote_count - a.vote_count || b.at.localeCompare(a.at)
b.dump.voteCount - a.dump.voteCount || b.at.localeCompare(a.at)
);
if (sorted.length === 0) return null;
return {
dumps: sorted.slice(0, NETWORK_MAX).map((row) => ({
...toDigestDump(row, readerId),
...(row.playlist_title ? { addedTo: row.playlist_title } : {}),
dumps: sorted.slice(0, NETWORK_MAX).map(({ dump, playlistTitle }) => ({
...toDigestDump(dump, readerId),
...(playlistTitle ? { addedTo: playlistTitle } : {}),
})),
more: Math.max(0, sorted.length - NETWORK_MAX),
};
}
/**
* Returns the period's most upvoted public dumps among those posted in it,
* the newer first on a tie, as the "hot" tab would.
* Returns the community section from `stats`, the site's figures for the
* period, leaving the reader out of who joined.
*/
function topOf(
function communityOf(
readerId: string,
cadence: DigestCadence,
w: Window,
): DigestDump[] {
const rows = db.prepare(
`SELECT ${DUMP_COLS} FROM dumps d JOIN users u ON u.id = d.user_id
WHERE d.is_private = 0 AND d.vote_count > 0
AND d.created_at >= ? AND d.created_at < ?
ORDER BY d.vote_count DESC, d.created_at DESC
LIMIT ?;`,
).all(w.from, w.to, TOP_MAX[cadence]) as unknown as DumpRow[];
return rows.map((row) => toDigestDump(row, readerId));
}
const countOf = (sql: string, ...params: string[]) =>
(db.prepare(sql).get(...params) as { n: number }).n;
/**
* Returns what the whole site did in the period: who joined, the new public
* playlists, the chat's activity and the totals.
*/
function communityOf(readerId: string, w: Window): DigestContent["community"] {
const members = db.prepare(
`SELECT u.username, i.username AS invited_by
FROM users u LEFT JOIN users i ON i.id = u.invited_by
WHERE u.created_at >= ? AND u.created_at < ? AND u.id != ?
ORDER BY u.created_at;`,
).all(w.from, w.to, readerId) as {
username: string;
invited_by: string | null;
}[];
const playlists = db.prepare(
`SELECT p.id, p.slug, p.title, u.username AS owner,
(SELECT COUNT(*) FROM playlist_dumps pd WHERE pd.playlist_id = p.id) AS dumps
FROM playlists p JOIN users u ON u.id = p.user_id
WHERE p.is_public = 1 AND p.created_at >= ? AND p.created_at < ?
ORDER BY dumps DESC, p.created_at DESC;`,
).all(w.from, w.to) as {
id: string;
slug: string;
title: string;
owner: string;
dumps: number;
}[];
const chatMessages = countOf(
`SELECT COUNT(*) AS n FROM chat_messages WHERE created_at >= ? AND created_at < ?;`,
w.from,
w.to,
);
const totals = {
dumps: countOf(
`SELECT COUNT(*) AS n FROM dumps WHERE is_private = 0 AND created_at >= ? AND created_at < ?;`,
w.from,
w.to,
),
upvotes: countOf(
`SELECT COUNT(*) AS n FROM votes WHERE created_at >= ? AND created_at < ?;`,
w.from,
w.to,
),
comments: countOf(
`SELECT COUNT(*) AS n FROM comments WHERE deleted = 0 AND created_at >= ? AND created_at < ?;`,
w.from,
w.to,
),
};
stats: CommunityStats,
): DigestContent["community"] {
const members = stats.members.filter((m) => m.id !== readerId);
const { playlists, chatMessages, totals } = stats;
if (
members.length + playlists.length + chatMessages + totals.dumps +
totals.upvotes + totals.comments === 0
@@ -459,16 +374,14 @@ function communityOf(readerId: string, w: Window): DigestContent["community"] {
return {
members: members.slice(0, MEMBERS_MAX).map((m) => ({
username: m.username,
invitedBy: m.invited_by,
invitedBy: m.invitedByUsername ?? null,
})),
membersMore: Math.max(0, members.length - MEMBERS_MAX),
playlists: playlists.slice(0, PLAYLISTS_MAX).map((p) => ({
title: p.title,
url: `${FRONTEND_URL}${
playlistPath({ ownerUsername: p.owner, slug: p.slug })
}`,
owner: p.owner,
dumps: p.dumps,
url: `${FRONTEND_URL}${playlistPath(p)}`,
owner: p.ownerUsername,
dumps: p.dumpCount ?? 0,
})),
playlistsMore: Math.max(0, playlists.length - PLAYLISTS_MAX),
chatMessages,
@@ -493,8 +406,11 @@ export function buildDigest(
const weekly = cadence === "weekly";
const activity = weekly ? activityOf(readerId, w) : null;
const network = weekly ? networkOf(readerId, w) : null;
const top = topOf(readerId, cadence, w);
const community = communityOf(readerId, w);
const stats = communityStatsOf(from, to);
const top = stats.top.slice(0, TOP_MAX[cadence]).map((d) =>
toDigestDump(d, readerId)
);
const community = communityOf(readerId, stats);
if (!activity && !network && top.length === 0 && !community) return null;
return {
cadence,
@@ -509,6 +425,7 @@ export function buildDigest(
notifications: `${FRONTEND_URL}/notifications`,
followed: `${FRONTEND_URL}${feedPath(undefined, "follow")}`,
chat: `${FRONTEND_URL}/?chat=open`,
community: `${FRONTEND_URL}${communityPath(cadence)}`,
settings: `${FRONTEND_URL}/settings`,
unsubscribe: `${FRONTEND_URL}/unsubscribe?token=${
encodeURIComponent(token)
@@ -616,19 +533,14 @@ export async function sendDueDigests(now: Date = new Date()): Promise<number> {
/**
* Sends the due digests a minute after the server starts, then every quarter
* of an hour. A run still sending when the next is due makes that one skip.
* `main.ts` calls it when run directly, never in the tests.
* of an hour. `main.ts` starts it.
*/
export function startDigestScheduler(): void {
let running = false;
const tick = () => {
if (running) return;
running = true;
sendDueDigests()
.then((n) => n > 0 && console.log(`[digest] sent ${n}`))
.catch((err) => console.error("[digest] run failed:", err))
.finally(() => running = false);
};
setTimeout(tick, 60_000);
setInterval(tick, TICK_MS);
}
export const digestJob: Job = {
name: "digest",
everyMs: TICK_MS,
firstAfterMs: 60_000,
async run() {
const n = await sendDueDigests();
if (n > 0) console.log(`[digest] sent ${n}`);
},
};

View File

@@ -390,6 +390,26 @@ export function listDumps(
return pageOf(items, limit);
}
/**
* Returns the `limit` most upvoted public dumps posted from `from` to `to`,
* the newer first on a tie, leaving out those no one upvoted. The community
* service gathers them for the `/community` page and the digests.
*
* @throws {APIException} 500 when a row doesn't parse as a dump.
*/
export function listTopDumps(from: Date, to: Date, limit: number): Dump[] {
const rows = db.prepare(
`SELECT ${SELECT_COLS} FROM dumps
WHERE is_private = 0 AND vote_count > 0
AND created_at >= ? AND created_at < ?
ORDER BY vote_count DESC, created_at DESC
LIMIT ?;`,
).all(from.toISOString(), to.toISOString(), limit);
const items = parseRows(rows, isDumpRow, dumpRowToApi);
attachCategoryIds(items);
return items;
}
/**
* Returns the dumps `requestingUserId` may see that were posted with the same
* link as `url`, newest first, for the create form's duplicate notice

View File

@@ -180,6 +180,20 @@ export function searchPlaylists(
return parseRows(rows, isPlaylistRow, playlistRowToApi);
}
/**
* Returns the public playlists created from `from` to `to`, the fullest first
* and the newer on a tie. The community service gathers them for the
* `/community` page and the digests.
*/
export function listNewPublicPlaylists(from: Date, to: Date): Playlist[] {
const rows = db.prepare(
`SELECT ${PLAYLIST_SELECT}
WHERE p.is_public = 1 AND p.created_at >= ? AND p.created_at < ?
ORDER BY dump_count DESC, p.created_at DESC;`,
).all(from.toISOString(), to.toISOString());
return parseRows(rows, isPlaylistRow, playlistRowToApi);
}
/**
* Returns a page of `userId`'s playlists, newest first, for
* `GET /api/users/:key/playlists`. The private ones are listed only when

View File

@@ -314,6 +314,18 @@ export function searchUsersGlobal(query: string, limit: number): PublicUser[] {
return parseRows(rows, isUserRow, userRowToApi).map(toPublicUser);
}
/**
* Returns the users who joined from `from` to `to`, in sign-up order, each
* with the name of whoever invited them. The community service gathers them
* for the `/community` page and the digests.
*/
export function listNewUsers(from: Date, to: Date): PublicUser[] {
const rows = db.prepare(
`${USER_SELECT} WHERE u.created_at >= ? AND u.created_at < ? ORDER BY u.created_at;`,
).all(from.toISOString(), to.toISOString());
return parseRows(rows, isUserRow, userRowToApi).map(toPublicUser);
}
/**
* Applies `request` to user `userId` and returns the updated user. It trusts
* `request`: the routes pick the fields a caller may change. The username

View File

@@ -5,8 +5,8 @@
* it sends. The services and routes that change dumps, comments, playlists and
* users call the `broadcast*` functions here after the change is stored. A
* `broadcast*` function sends to everyone unless its doc names an audience, so
* its caller must not pass it anything private. The module also pings every
* socket and closes the ones that stop answering.
* its caller must not pass it anything private. Its jobs ping every socket,
* closing the ones that stop answering, and expire the typing indicators.
*
* @module
*/
@@ -21,6 +21,7 @@ import {
isMemberVisibleTo,
type PlaylistMember,
} from "../lib/playlist-members.ts";
import type { Job } from "../lib/scheduler.ts";
/**
* Created by the WebSocket route for each socket it opens, and kept here until
@@ -421,19 +422,25 @@ export function setClientTyping(client: WsClient, typing: boolean): void {
if (typing !== wasTyping) broadcastTyping();
}
// Expires the typing users who went quiet without a false, such as by closing
// the tab.
setInterval(() => {
const now = Date.now();
let changed = false;
for (const client of clients) {
if (client.typingAt && now - client.typingAt >= TYPING_TTL) {
client.typingAt = undefined;
changed = true;
/**
* Expires the typing users who went quiet without a false, such as by closing
* the tab, every 2 seconds. `main.ts` starts it.
*/
export const typingExpiryJob: Job = {
name: "typing",
everyMs: 2_000,
run() {
const now = Date.now();
let changed = false;
for (const client of clients) {
if (client.typingAt && now - client.typingAt >= TYPING_TTL) {
client.typingAt = undefined;
changed = true;
}
}
}
if (changed) broadcastTyping();
}, 2_000);
if (changed) broadcastTyping();
},
};
/**
* Checks whether `userId` is reading the chat on any of their sockets. The
@@ -461,19 +468,27 @@ export function handleClientPong(client: WsClient): void {
const PING_INTERVAL = 30_000;
const PING = JSON.stringify({ type: "ping" } satisfies ServerToClientMessage);
setInterval(() => {
for (const client of clients) {
if (client.socket.readyState !== WebSocket.OPEN) {
clients.delete(client);
continue;
/**
* Pings every client each {@link PING_INTERVAL}, closing the ones that didn't
* answer the previous ping. `main.ts` starts it.
*/
export const pingJob: Job = {
name: "ping",
everyMs: PING_INTERVAL,
run() {
for (const client of clients) {
if (client.socket.readyState !== WebSocket.OPEN) {
clients.delete(client);
continue;
}
// Undefined, not false, before the first ping.
if (client.pongReceived === false) {
client.socket.close(1001, "Ping timeout");
clients.delete(client);
continue;
}
client.pongReceived = false;
sendRaw(client.socket, PING);
}
// Undefined, not false, before the first ping.
if (client.pongReceived === false) {
client.socket.close(1001, "Ping timeout");
clients.delete(client);
continue;
}
client.pongReceived = false;
sendRaw(client.socket, PING);
}
}, PING_INTERVAL);
},
};

View File

@@ -28,14 +28,14 @@ Deno.test("runMigrations: a fresh database is left as schema.sql made it", () =>
.get() as {
n: number;
};
assertEquals(applied.n, 21);
assertEquals(applied.n, 22);
// Running again applies nothing.
runMigrations(db);
assertEquals(
(db.prepare(`SELECT COUNT(*) as n FROM schema_migrations;`).get() as {
n: number;
}).n,
21,
22,
);
db.close();
});

View File

@@ -0,0 +1,21 @@
import "../../setup.ts";
import { assertEquals } from "@std/assert";
import { DatabaseSync } from "node:sqlite";
import { up as up0022 } from "../../../db/migrations/0022_reserve_community.ts";
Deno.test("migration 0022 renames a category named community", () => {
const db = new DatabaseSync(":memory:");
db.exec(`CREATE TABLE categories (id TEXT PRIMARY KEY, slug TEXT);`);
for (const slug of ["community", "community-2", "music"]) {
db.prepare(`INSERT INTO categories (id, slug) VALUES (?, ?);`).run(
crypto.randomUUID(),
slug,
);
}
up0022(db);
up0022(db); // idempotent
const slugs = (db.prepare(`SELECT slug FROM categories ORDER BY slug;`)
.all() as { slug: string }[]).map((r) => r.slug);
assertEquals(slugs, ["community-2", "community-3", "music"]);
db.close();
});

View File

@@ -3,7 +3,17 @@ import { assert, assertFalse } from "@std/assert";
import { isReservedSlug } from "../../lib/reserved-slugs.ts";
Deno.test("category slugs: the feed tabs, app pages and server paths are reserved", () => {
for (const word of ["hot", "follow", "settings", "manage", "api", "ws"]) {
for (
const word of [
"hot",
"follow",
"settings",
"manage",
"community",
"api",
"ws",
]
) {
assert(isReservedSlug(word), word);
}
// v3's words are free again: nothing lives at /dumps or /users now.

View File

@@ -0,0 +1,53 @@
import "../setup.ts";
import { assertEquals } from "@std/assert";
import { FakeTime } from "@std/testing/time";
import { startJobs } from "../../lib/scheduler.ts";
Deno.test("startJobs: waits for the first run, then repeats, skipping a run still going", async () => {
using time = new FakeTime();
let runs = 0;
let release = () => {};
startJobs([{
name: "slow",
everyMs: 1_000,
firstAfterMs: 500,
run: () => {
runs++;
return new Promise<void>((resolve) => release = resolve);
},
}]);
await time.tickAsync(499);
assertEquals(runs, 0);
await time.tickAsync(1);
assertEquals(runs, 1);
// Still running at the next tick: skipped.
await time.tickAsync(1_000);
assertEquals(runs, 1);
release();
await time.tickAsync(1_000);
assertEquals(runs, 2);
});
Deno.test("startJobs: a failed run is logged, and the next one runs anyway", async () => {
using time = new FakeTime();
const errors: unknown[][] = [];
const consoleError = console.error;
console.error = (...args: unknown[]) => errors.push(args);
try {
let runs = 0;
startJobs([{
name: "flaky",
everyMs: 1_000,
run: () => {
runs++;
throw new Error("boom");
},
}]);
await time.tickAsync(2_000);
assertEquals(runs, 2);
assertEquals(errors.length, 2);
assertEquals(errors[0][0], "[flaky] run failed:");
} finally {
console.error = consoleError;
}
});

View File

@@ -6,6 +6,7 @@ const links = {
notifications: "https://g.test/notifications",
followed: "https://g.test/~/followed",
chat: "https://g.test/?chat=open",
community: "https://g.test/community/monthly",
settings: "https://g.test/users/ann/~/settings",
unsubscribe: "https://g.test/unsubscribe?token=t",
};
@@ -99,6 +100,10 @@ Deno.test("renderDigestEmail: the monthly recap leads with a wide image and the
assertStringIncludes(email.html, "1,284");
assertStringIncludes(email.html, "invited by you");
assertStringIncludes(email.html, "Turn off monthly recaps");
assertStringIncludes(
email.text,
"[See it all on the community page](https://g.test/community/monthly)",
);
// Only #2 and #3 get a thumbnail beside the hero.
assert(email.html.match(/<img[^>]*width="64"/g)?.length === 2);
});

View File

@@ -0,0 +1,35 @@
import { createUser, db, get, insertDump, test } from "../helpers.ts";
import { assertEquals } from "@std/assert";
import type { CommunityStats } from "../../../shared/community.ts";
import { refreshCommunityStats } from "../../services/community-service.ts";
function vote(dumpId: string, n: number) {
db.prepare(`UPDATE dumps SET vote_count = ? WHERE id = ?;`).run(n, dumpId);
}
test("GET /api/community: signed in only, for a week or a month", async () => {
const ann = await createUser();
vote(
insertDump({
userId: ann.id,
title: "Hit",
createdAt: new Date(Date.now() - 1000).toISOString(),
}).id,
1,
);
refreshCommunityStats();
assertEquals((await get("/api/community")).status, 401);
assertEquals(
(await get("/api/community?period=daily", { auth: ann.auth })).status,
400,
);
const week = await get<CommunityStats>("/api/community", { auth: ann.auth });
assertEquals(week.status, 200);
assertEquals(week.data.top.map((d) => d.title), ["Hit"]);
assertEquals(week.data.members.map((m) => m.username), [ann.username]);
const month = await get<CommunityStats>("/api/community?period=monthly", {
auth: ann.auth,
});
assertEquals(month.data.totals.dumps, 1);
});

View File

@@ -0,0 +1,88 @@
import {
createUser,
db,
insertComment,
insertDump,
insertPlaylist,
test,
} from "../helpers.ts";
import { assertEquals } from "@std/assert";
import {
communityStatsOf,
getCommunityStats,
refreshCommunityStats,
} from "../../services/community-service.ts";
const DAY = 86_400_000;
const ago = (ms: number) => new Date(Date.now() - ms).toISOString();
const week = () =>
[new Date(Date.now() - 7 * DAY), new Date(Date.now() + 60_000)] as const;
function vote(dumpId: string, n: number) {
db.prepare(`UPDATE dumps SET vote_count = ? WHERE id = ?;`).run(n, dumpId);
}
test("communityStatsOf: the period's top public dumps, members, public playlists and totals", async () => {
const ann = await createUser({ createdAt: ago(20 * DAY) });
const bob = await createUser({ invitedBy: ann.id, createdAt: ago(2 * DAY) });
const cy = await createUser({ createdAt: ago(DAY) });
const hit = insertDump({ userId: bob.id, title: "Hit" });
vote(hit.id, 5);
const tie = insertDump({ userId: ann.id, title: "Tie", createdAt: ago(DAY) });
vote(tie.id, 2);
const newer = insertDump({ userId: ann.id, title: "Newer" });
vote(newer.id, 2);
insertDump({ userId: ann.id, title: "No votes" });
vote(insertDump({ userId: ann.id, title: "Private", isPrivate: true }).id, 9);
vote(
insertDump({ userId: ann.id, title: "Too old", createdAt: ago(10 * DAY) })
.id,
9,
);
insertComment({ dumpId: hit.id, userId: cy.id, body: "nice" });
insertPlaylist({ userId: ann.id, title: "Small" });
const full = insertPlaylist({ userId: bob.id, title: "Full" });
insertPlaylist({ userId: bob.id, title: "Hidden", isPublic: false });
db.prepare(
`INSERT INTO playlist_dumps (playlist_id, dump_id, position, added_at) VALUES (?, ?, 0, ?), (?, ?, 1, ?);`,
).run(full.id, hit.id, ago(0), full.id, tie.id, ago(0));
const stats = communityStatsOf(...week());
assertEquals(stats.top.map((d) => d.title), ["Hit", "Newer", "Tie"]);
assertEquals(stats.top[0].ownerUsername, bob.username);
assertEquals(
stats.members.map((m) => [m.username, m.invitedByUsername]),
[[bob.username, ann.username], [cy.username, undefined]],
);
assertEquals(
stats.playlists.map((p) => [p.title, p.dumpCount]),
[["Full", 2], ["Small", 0]],
);
assertEquals(stats.totals, { dumps: 4, upvotes: 0, comments: 1 });
assertEquals(stats.chatMessages, 0);
});
test("getCommunityStats: serves the last refresh, of the week or the month", async () => {
const ann = await createUser({ createdAt: ago(20 * DAY) });
vote(
insertDump({ userId: ann.id, title: "Month", createdAt: ago(20 * DAY) })
.id,
1,
);
refreshCommunityStats();
assertEquals(getCommunityStats("weekly").top, []);
assertEquals(getCommunityStats("monthly").top.map((d) => d.title), [
"Month",
]);
assertEquals(getCommunityStats("monthly").members.length, 1);
vote(
insertDump({ userId: ann.id, title: "Later", createdAt: ago(1000) }).id,
1,
);
assertEquals(getCommunityStats("weekly").top, []);
refreshCommunityStats();
assertEquals(getCommunityStats("weekly").top.map((d) => d.title), [
"Later",
]);
});

View File

@@ -11,10 +11,10 @@ import {
import { assert, assertEquals, assertFalse } from "@std/assert";
import {
buildDigest,
PERIOD_MS,
sendDueDigests,
syncDigestSchedules,
} from "../../services/digest-service.ts";
import { PERIOD_MS } from "../../../shared/settings.ts";
import {
getSettings,
updateSettings,