feat: per-category email prefs, admin ops webhooks, per-user feature toggles (v0.61.0)

- Notification email preferences: every push category (follow, comment,
  reply, reaction, rating, mention, leftoverExpiring, shoppingList) now has
  an independent email toggle, plus a Weekly Digest toggle. Previously email
  sent unconditionally whenever the recipient had one; now gated the same
  way push already was. The weekly-digest cron route excludes opted-out
  users.

- Admin-only site-wide webhooks (Admin → Webhooks): new signups, support
  tickets, and reports filed can now fire an HMAC-signed HTTP webhook
  (Slack/Discord/ops alerting), independent of the existing per-user
  webhooks (which stay scoped to a user's own recipe/meal-plan/shopping-list
  events). Signing/delivery logic factored into lib/webhook-delivery.ts and
  shared by both dispatchers instead of duplicated.

- Settings → Features: users can hide Nutrition, Pantry, Meal Plan,
  Shopping Lists, Collections, or Messages from their own nav. Purely
  cosmetic — hidden pages stay reachable by direct link, nothing is
  access-restricted.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Arnaud
2026-07-20 23:07:28 +02:00
parent cced962bff
commit c5e1643d39
40 changed files with 18383 additions and 79 deletions
@@ -8,6 +8,7 @@ import {
ratings,
userFollows,
favorites,
userNotificationPrefs,
eq,
and,
gte,
@@ -21,10 +22,11 @@ import { sendEmail, weeklyDigestHtml } from "@/lib/email";
// schedule (see compose.prod.yml). Not part of the public API surface;
// protected by a shared secret rather than user auth.
//
// Computes, for every user: new followers / new comments / new ratings on
// their recipes in the last 7 days, plus a site-wide top-3 trending list, and
// emails a summary. Sends to all users (all users have a non-null email) —
// there's no per-user opt-out preference yet; out of scope for this pass.
// Computes, for every opted-in user: new followers / new comments / new
// ratings on their recipes in the last 7 days, plus a site-wide top-3
// trending list, and emails a summary. Excludes users who turned off
// "Weekly digest" in Settings → Notifications (userNotificationPrefs.weeklyDigestEmail,
// default true — no row means opted in).
const CHUNK_SIZE = 20;
@@ -56,8 +58,12 @@ export async function POST(req: NextRequest) {
const weekAgo = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000);
const baseUrl = process.env["BETTER_AUTH_URL"] ?? "http://localhost:3000";
const [allUsers, followerRows, commentRows, ratingRows, trending] = await Promise.all([
const [allUsers, optedOutRows, followerRows, commentRows, ratingRows, trending] = await Promise.all([
db.select({ id: users.id, email: users.email }).from(users),
db
.select({ userId: userNotificationPrefs.userId })
.from(userNotificationPrefs)
.where(eq(userNotificationPrefs.weeklyDigestEmail, false)),
db
.select({ userId: userFollows.followingId, n: count() })
.from(userFollows)
@@ -92,6 +98,9 @@ export async function POST(req: NextRequest) {
.limit(3),
]);
const optedOut = new Set(optedOutRows.map((r) => r.userId));
const recipients = allUsers.filter((u) => !optedOut.has(u.id));
const followerMap = new Map(followerRows.map((r) => [r.userId, r.n]));
const commentMap = new Map(commentRows.map((r) => [r.userId, r.n]));
const ratingMap = new Map(ratingRows.map((r) => [r.userId, r.n]));
@@ -100,7 +109,7 @@ export async function POST(req: NextRequest) {
let sent = 0;
let failed = 0;
for (const batch of chunk(allUsers, CHUNK_SIZE)) {
for (const batch of chunk(recipients, CHUNK_SIZE)) {
const results = await Promise.allSettled(
batch.map((user) => {
const newFollowers = followerMap.get(user.id) ?? 0;
@@ -127,5 +136,5 @@ export async function POST(req: NextRequest) {
}
}
return NextResponse.json({ ok: true, totalUsers: allUsers.length, sent, failed });
return NextResponse.json({ ok: true, totalUsers: allUsers.length, optedOut: optedOut.size, sent, failed });
}