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:
@@ -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}`);
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user