Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.
TypeScript i18nextnext-authnextjspostgresqlprismareact-emailt3-stacktailwindcsstrpcturborepotypescriptzod
✨ Per-owner daily budget for email sent on a host's behalf (#3459)
Luke Vellaa882ceacebdb
diff --git a/apps/web/public/locales/en/app.json b/apps/web/public/locales/en/app.jsonindex 7459bdcb9..c11515da7 100644--- a/apps/web/public/locales/en/app.json+++ b/apps/web/public/locales/en/app.json@@ -388,6 +388,7 @@"fileTooLarge": "File too large","finalize": "Finalize","finalizeAndNotify": "{count, plural, =0 {Finalize} one {Finalize and notify # participant} other {Finalize and notify # participants}}",+ "finalizeNotifyBudgetSkipped": "{count, plural, one {# participant wasn't emailed} other {# participants weren't emailed}} because this account reached its daily email limit. Share the poll link with them instead.","finalizePoll": "Finalize poll","finalizePollDescription": "Pick the date that works best.","finalizePollError": "Failed to finalize poll",diff --git a/apps/web/src/features/email-budget/constants.ts b/apps/web/src/features/email-budget/constants.tsnew file mode 100644index 000000000..4389289ca--- /dev/null+++ b/apps/web/src/features/email-budget/constants.ts@@ -0,0 +1,11 @@+// Recipients one poll owner may cause Rallly to email in a window, across+// every send made on their behalf. The median scheduled poll emails 4+// participants and 99% of polls have under 24 responses, so a real host+// never gets near this; a host spraying addresses stops at it.+export const EMAIL_BUDGET_RECIPIENTS_PER_DAY = 200;+export const EMAIL_BUDGET_WINDOW = "24 h";++export const EMAIL_BUDGET_KINDS = [+ "participant_confirmation",+ "scheduled_event_invite",+] as const;diff --git a/apps/web/src/features/email-budget/mutations.test.ts b/apps/web/src/features/email-budget/mutations.test.tsnew file mode 100644index 000000000..cad6f0579--- /dev/null+++ b/apps/web/src/features/email-budget/mutations.test.ts@@ -0,0 +1,101 @@+import { Effect, Layer } from "effect";+import { describe, expect, it, vi } from "vitest";++vi.mock("@/env", () => ({ env: {} }));++import { chargeEmailBudget } from "./mutations";+import { EmailBudgetStore, EmailBudgetUnavailable } from "./service";++// Mirrors the real store: a reservation is granted what is left, and the+// whole request is counted against the key.+function stubStore(budget: number) {+ const spent = new Map<string, number>();+ const reserve = vi.fn(({ key, points }: { key: string; points: number }) =>+ Effect.sync(() => {+ const used = spent.get(key) ?? 0;+ spent.set(key, used + points);+ return Math.max(0, Math.min(points, budget - used));+ }),+ );+ const layer = Layer.succeed(+ EmailBudgetStore,+ EmailBudgetStore.of({ reserve }),+ );+ return { layer, reserve };+}++function charge(+ layer: Layer.Layer<EmailBudgetStore>,+ input: Partial<Parameters<typeof chargeEmailBudget>[0]> & {+ recipients: number;+ },+) {+ return Effect.runPromise(+ chargeEmailBudget({+ ownerId: "user_1",+ tier: "hobby",+ pollId: "poll_1",+ kind: "scheduled_event_invite",+ ...input,+ }).pipe(Effect.provide(layer)),+ );+}++describe("chargeEmailBudget", () => {+ it("charges one point per recipient against the poll owner", async () => {+ const store = stubStore(10);+ expect(await charge(store.layer, { recipients: 4 })).toEqual({+ allowed: 4,+ skipped: 0,+ });+ expect(store.reserve).toHaveBeenCalledWith({+ key: "email-budget:user_1",+ points: 4,+ });+ });++ it("skips the sends past the cap and charges only what it allows", async () => {+ const store = stubStore(10);+ await charge(store.layer, { recipients: 7 });+ expect(await charge(store.layer, { recipients: 5 })).toEqual({+ allowed: 3,+ skipped: 2,+ });+ expect(await charge(store.layer, { recipients: 1 })).toEqual({+ allowed: 0,+ skipped: 1,+ });+ });++ it("keeps a separate budget per owner, guests included", async () => {+ const store = stubStore(2);+ await charge(store.layer, { ownerId: "guest_1", recipients: 2 });+ expect(await charge(store.layer, { recipients: 2 })).toEqual({+ allowed: 2,+ skipped: 0,+ });+ });++ it("never charges a Pro space", async () => {+ const store = stubStore(0);+ expect(await charge(store.layer, { tier: "pro", recipients: 50 })).toEqual({+ allowed: 50,+ skipped: 0,+ });+ expect(store.reserve).not.toHaveBeenCalled();+ });++ it("allows every recipient when the reservation fails", async () => {+ const layer = Layer.succeed(+ EmailBudgetStore,+ EmailBudgetStore.of({+ reserve: () =>+ Effect.fail(new EmailBudgetUnavailable({ cause: "down" })),+ }),+ );+ expect(await charge(layer, { recipients: 3 })).toEqual({+ allowed: 3,+ skipped: 0,+ });+ });+});diff --git a/apps/web/src/features/email-budget/mutations.ts b/apps/web/src/features/email-budget/mutations.tsnew file mode 100644index 000000000..ea55bf17e--- /dev/null+++ b/apps/web/src/features/email-budget/mutations.ts@@ -0,0 +1,72 @@+import "server-only";++import { createLogger } from "@rallly/logger";+import { Effect } from "effect";+import type { SpaceTier } from "@/features/space/schema";+import { EMAIL_BUDGET_RECIPIENTS_PER_DAY } from "./constants";+import { EmailBudgetStore } from "./service";+import type { EmailBudgetKind } from "./types";++const logger = createLogger("email-budget");++/**+ * Spends one point per recipient of an email sent on a poll owner's behalf+ * and answers how many of them may be sent; the caller sends the first+ * `allowed` and skips the rest. Pro spaces are exempt; `tier` is the+ * resolved tier, so an instance without billing is never charged. Emails+ * the recipient asked for themselves (their own notifications, login codes,+ * account mail) are never charged. A store failure allows every send: the budget bounds+ * abuse, it is not a gate the product depends on.+ */+export const chargeEmailBudget = Effect.fn("emailBudget.chargeEmailBudget")(+ function* ({+ ownerId,+ tier,+ pollId,+ kind,+ recipients,+ }: {+ ownerId: string | null;+ tier: SpaceTier;+ pollId: string;+ kind: EmailBudgetKind;+ recipients: number;+ }) {+ if (recipients <= 0 || !ownerId || tier === "pro") {+ return { allowed: Math.max(0, recipients), skipped: 0 };+ }++ const store = yield* EmailBudgetStore;+ const key = `email-budget:${ownerId}`;+ const event = {+ ownerId,+ pollId,+ kind,+ tier,+ recipients,+ budget: EMAIL_BUDGET_RECIPIENTS_PER_DAY,+ };++ const allowed = yield* store.reserve({ key, points: recipients }).pipe(+ Effect.catchTag("EmailBudgetUnavailable", (error) =>+ Effect.sync(() => {+ logger.error(+ { ...event, error: error.cause },+ "Email budget unavailable, sending without it",+ );+ return recipients;+ }),+ ),+ );++ const skipped = recipients - allowed;+ if (skipped > 0) {+ logger.warn(+ { ...event, allowed, skipped, reason: "budget_exhausted" },+ "Email budget exhausted, skipping sends",+ );+ }++ return { allowed, skipped };+ },+);diff --git a/apps/web/src/features/email-budget/service.ts b/apps/web/src/features/email-budget/service.tsnew file mode 100644index 000000000..5bcac72db--- /dev/null+++ b/apps/web/src/features/email-budget/service.ts@@ -0,0 +1,47 @@+import "server-only";++import { Context, Data, Effect, Layer } from "effect";+import { createBudget } from "@/lib/rate-limit";+import {+ EMAIL_BUDGET_RECIPIENTS_PER_DAY,+ EMAIL_BUDGET_WINDOW,+} from "./constants";++/** The budget store could not be read or written. */+export class EmailBudgetUnavailable extends Data.TaggedError(+ "EmailBudgetUnavailable",+)<{ cause: unknown }> {}++// Module level so the in-memory fallback keeps its counts across requests.+const budget = createBudget(+ EMAIL_BUDGET_RECIPIENTS_PER_DAY,+ EMAIL_BUDGET_WINDOW,+);++/**+ * Takes up to `points` from a key's allowance in one atomic step and+ * answers how many it got. With rate limiting off there is no allowance and+ * every reservation is granted in full.+ */+export class EmailBudgetStore extends Context.Service<+ EmailBudgetStore,+ {+ reserve(input: {+ key: string;+ points: number;+ }): Effect.Effect<number, EmailBudgetUnavailable>;+ }+>()("rallly/email-budget/EmailBudgetStore") {+ static readonly layer = Layer.succeed(+ EmailBudgetStore,+ EmailBudgetStore.of({+ reserve: ({ key, points }) =>+ budget+ ? Effect.tryPromise({+ try: () => budget.reserve(key, points),+ catch: (cause) => new EmailBudgetUnavailable({ cause }),+ })+ : Effect.succeed(points),+ }),+ );+}diff --git a/apps/web/src/features/email-budget/types.ts b/apps/web/src/features/email-budget/types.tsnew file mode 100644index 000000000..93b6947b7--- /dev/null+++ b/apps/web/src/features/email-budget/types.ts@@ -0,0 +1,3 @@+import type { EMAIL_BUDGET_KINDS } from "./constants";++export type EmailBudgetKind = (typeof EMAIL_BUDGET_KINDS)[number];diff --git a/apps/web/src/features/poll/components/finalize-poll-dialog.tsx b/apps/web/src/features/poll/components/finalize-poll-dialog.tsxindex ef74b7846..c14609b05 100644--- a/apps/web/src/features/poll/components/finalize-poll-dialog.tsx+++ b/apps/web/src/features/poll/components/finalize-poll-dialog.tsx@@ -2,6 +2,7 @@import { posthog } from "@rallly/posthog/client";import { cn } from "@rallly/ui";+import { Alert, AlertDescription } from "@rallly/ui/alert";import { Button } from "@rallly/ui/button";import { Checkbox } from "@rallly/ui/checkbox";import type { DialogProps } from "@rallly/ui/dialog";@@ -17,7 +18,13 @@ import {import { Label } from "@rallly/ui/label";import { toast } from "@rallly/ui/sonner";import { SuccessCheck, SuccessCheckIcon } from "@rallly/ui/success-check";-import { CalendarIcon, MapPinIcon, StarIcon, VideoIcon } from "lucide-react";+import {+ AlertTriangleIcon,+ CalendarIcon,+ MapPinIcon,+ StarIcon,+ VideoIcon,+} from "lucide-react";import { useRouter } from "next/navigation";import React from "react";import {@@ -577,10 +584,12 @@ function FinalizeWizard({ onClose }: { onClose: () => void }) {);const option = options.find((o) => o.id === optionId);const [reviewSite, setReviewSite] = React.useState<ReviewSite | null>(null);+ const [skippedNotifications, setSkippedNotifications] = React.useState(0);const finalize = trpc.polls.book.useMutation({onSuccess: (data) => {setReviewSite(data?.reviewSite ?? null);+ setSkippedNotifications(data?.skippedNotifications ?? 0);setStep("done");// The page behind the dialog picks up the scheduled status.router.refresh();@@ -641,6 +650,18 @@ function FinalizeWizard({ onClose }: { onClose: () => void }) {</DialogDescription></DialogHeader></div>+ {skippedNotifications > 0 ? (+ <Alert variant="warning">+ <AlertTriangleIcon />+ <AlertDescription>+ <Trans+ i18nKey="finalizeNotifyBudgetSkipped"+ defaults="{count, plural, one {# participant wasn't emailed} other {# participants weren't emailed}} because this account reached its daily email limit. Share the poll link with them instead."+ values={{ count: skippedNotifications }}+ />+ </AlertDescription>+ </Alert>+ ) : null}{reviewSite ? <ReviewRequestCard site={reviewSite} /> : null}<Button className="w-full" onClick={onClose}><Trans i18nKey="backToPoll" defaults="Back to poll" />diff --git a/apps/web/src/features/poll/participant/actions.ts b/apps/web/src/features/poll/participant/actions.tsindex c5b517196..ead775c36 100644--- a/apps/web/src/features/poll/participant/actions.ts+++ b/apps/web/src/features/poll/participant/actions.ts@@ -4,9 +4,13 @@ import { sendNewParticipantEmail } from "@rallly/emails/templates/new-participanimport { sendNewParticipantConfirmationEmail } from "@rallly/emails/templates/new-participant-confirmation";import { createLogger } from "@rallly/logger";import { absoluteUrl, shortUrl } from "@rallly/utils/absolute-url";+import { Effect } from "effect";import { after } from "next/server";import { getInstanceBranding, getSpaceBranding } from "@/emails/branding";import { env } from "@/env";+import { resolveSpaceTier } from "@/features/billing/utils";+import { chargeEmailBudget } from "@/features/email-budget/mutations";+import { EmailBudgetStore } from "@/features/email-budget/service";import { getNotificationRecipient } from "@/features/notifications/data";import { createUnsubscribeToken } from "@/features/notifications/utils";import {@@ -28,6 +32,7 @@ import {updateParticipantVotesSchema,} from "@/features/poll/participant/schema";import { getLocale } from "@/i18n/server/get-locale";+import { runtime } from "@/lib/effect/runtime";import { AppError } from "@/lib/errors/app-error";import { flushPostHog, track, trackSystemEvent } from "@/lib/posthog";import {@@ -200,8 +205,22 @@ export const addParticipantAction = anyUserActionClientconst { participant, poll, editToken, viaInvite, totalResponses } = result;if (email) {- after(async () =>- sendNewParticipantConfirmationEmail({+ after(async () => {+ // Sent to an address the responder typed, so it is charged to the+ // poll owner: a host cannot use their poll to mail strangers.+ const { allowed } = await runtime.runPromise(+ chargeEmailBudget({+ ownerId: poll.userId,+ tier: resolveSpaceTier(poll.space?.tier ?? "hobby"),+ pollId: poll.id,+ kind: "participant_confirmation",+ recipients: 1,+ }).pipe(Effect.provide(EmailBudgetStore.layer)),+ );+ if (allowed === 0) {+ return;+ }+ await sendNewParticipantConfirmationEmail({to: email,locale,branding: poll.space@@ -213,8 +232,8 @@ export const addParticipantAction = anyUserActionClientgetPollInvitePath({ pollId: poll.id, token: editToken }),),},- }),- );+ });+ });}after(() =>diff --git a/apps/web/src/features/poll/participant/mutations.ts b/apps/web/src/features/poll/participant/mutations.tsindex 4f329a772..d06f4db2d 100644--- a/apps/web/src/features/poll/participant/mutations.ts+++ b/apps/web/src/features/poll/participant/mutations.ts@@ -110,6 +110,7 @@ export type AddParticipantResult =};poll: {id: string;+ userId: string | null;title: string;allowTentativeVotes: boolean;space: {@@ -214,6 +215,7 @@ export async function addParticipant({poll: {select: {id: true,+ userId: true,title: true,allowTentativeVotes: true,space: {diff --git a/apps/web/src/lib/rate-limit/index.test.ts b/apps/web/src/lib/rate-limit/index.test.tsnew file mode 100644index 000000000..c3a01cb72--- /dev/null+++ b/apps/web/src/lib/rate-limit/index.test.ts@@ -0,0 +1,23 @@+import { describe, expect, it, vi } from "vitest";++vi.mock("@/env", () => ({ env: {} }));++import { createBudget } from "./index";++describe("createBudget", () => {+ it("grants what is left and nothing past it", async () => {+ const budget = createBudget(10, "24 h");+ expect(await budget?.reserve("a", 7)).toBe(7);+ expect(await budget?.reserve("a", 5)).toBe(3);+ expect(await budget?.reserve("a", 1)).toBe(0);+ expect(await budget?.reserve("b", 4)).toBe(4);+ });++ it("never grants more than the allowance to concurrent reservations", async () => {+ const budget = createBudget(10, "24 h");+ const grants = await Promise.all(+ Array.from({ length: 8 }, () => budget?.reserve("a", 3)),+ );+ expect(grants.reduce((sum = 0, n = 0) => sum + n, 0)).toBe(10);+ });+});diff --git a/apps/web/src/lib/rate-limit/index.ts b/apps/web/src/lib/rate-limit/index.tsindex af5c6902e..329d3adeb 100644--- a/apps/web/src/lib/rate-limit/index.ts+++ b/apps/web/src/lib/rate-limit/index.ts@@ -104,3 +104,62 @@ export function createSharedRate(requests: number, duration: Duration) {},};}++// Adds the points and starts the window on the first reservation, in one+// step, so concurrent reservations cannot both read the same balance.+const RESERVE_SCRIPT = `+local used = redis.call("INCRBY", KEYS[1], ARGV[1])+if used == tonumber(ARGV[1]) then+ redis.call("PEXPIRE", KEYS[1], ARGV[2])+end+return used+`;++/**+ * A fixed-window allowance of points per key. `reserve` atomically takes up+ * to `points` and resolves how many it got, so a caller can do part of a+ * batch when the allowance runs out midway. Returns null with rate limiting+ * off, meaning no allowance applies.+ */+export function createBudget(points: number, duration: Duration) {+ if (!isRateLimitEnabled) {+ return null;+ }++ const windowMs = parseDurationMs(duration);+ const granted = (requested: number, usedAfter: number) =>+ Math.max(0, Math.min(requested, points - (usedAfter - requested)));++ if (redis) {+ const client = redis;+ return {+ async reserve(key: string, requested: number) {+ const usedAfter = await client.eval<[number, number], number>(+ RESERVE_SCRIPT,+ [key],+ [requested, windowMs],+ );+ return granted(requested, usedAfter);+ },+ };+ }++ const windows = new Map<string, { used: number; resetAt: number }>();+ return {+ async reserve(key: string, requested: number) {+ const now = Date.now();+ let entry = windows.get(key);+ if (!entry || now >= entry.resetAt) {+ const fresh = { used: 0, resetAt: now + windowMs };+ entry = fresh;+ windows.set(key, fresh);+ // A late timer must not evict the window that replaced its own.+ setTimeout(() => {+ if (windows.get(key) === fresh) windows.delete(key);+ }, windowMs).unref?.();+ }+ entry.used += requested;+ return granted(requested, entry.used);+ },+ };+}diff --git a/apps/web/src/trpc/routers/polls.ts b/apps/web/src/trpc/routers/polls.tsindex 8f1ce8286..ed1f0b7fe 100644--- a/apps/web/src/trpc/routers/polls.ts+++ b/apps/web/src/trpc/routers/polls.ts@@ -4,6 +4,7 @@ import { sendNewPollEmail } from "@rallly/emails/templates/new-poll";import { absoluteUrl, shortUrl } from "@rallly/utils/absolute-url";import { nanoid } from "@rallly/utils/nanoid";import { TRPCError } from "@trpc/server";+import { Effect } from "effect";import { after } from "next/server";import * as z from "zod";import { getInstanceBranding } from "@/emails/branding";@@ -27,6 +28,8 @@ import {getConferencingUri,moderatedLinkText,} from "@/features/conferencing/utils";+import { chargeEmailBudget } from "@/features/email-budget/mutations";+import { EmailBudgetStore } from "@/features/email-budget/service";import { queueEmails } from "@/features/email-queue/mutations";import { getInstancePolicy } from "@/features/instance-policy/data";import { moderateContent } from "@/features/moderation/mutations";@@ -53,6 +56,7 @@ import {wallTimeDiffInMinutes,wallTimeToInstant,} from "@/lib/datetime/wall-time";+import { runtime } from "@/lib/effect/runtime";import { AppError } from "@/lib/errors/app-error";import { identifyGroup, track } from "@/lib/posthog";import { createIcsEvent } from "@/lib/utils/ics";@@ -1330,10 +1334,27 @@ export const polls = router({});}const inviteData = Array.from(invitesByEmail.values());- const notifyInviteUids = Array.from(invitesByEmail)+ const selectedInviteUids = Array.from(invitesByEmail).filter(([key]) => notifyEmails.has(key)).map(([, invite]) => invite.uid);+ // Spent before the event is written, so the queue only ever holds+ // emails the budget allowed. The schedule goes ahead either way; the+ // host is told who was not emailed.+ const budget = await runtime.runPromise(+ chargeEmailBudget({+ ownerId: poll.user.id,+ tier: getPollTier({+ space: poll.space,+ activeSpaceTier: ctx.space.tier,+ }),+ pollId: poll.id,+ kind: "scheduled_event_invite",+ recipients: selectedInviteUids.length,+ }).pipe(Effect.provide(EmailBudgetStore.layer)),+ );+ const notifyInviteUids = selectedInviteUids.slice(0, budget.allowed);+const scheduledEvent = await prisma.$transaction(async (tx) => {// create scheduled eventconst event = await tx.scheduledEvent.create({@@ -1463,6 +1484,7 @@ export const polls = router({(Date.now() - poll.createdAt.getTime()) / 86_400_000,),participant_count: poll.participants.length,+ notify_budget_skipped_count: budget.skipped,...summarizeNotifySelection({participants: poll.participants,optionId: input.optionId,@@ -1480,7 +1502,7 @@ export const polls = router({participantCount: poll.participants.length,});- return { reviewSite };+ return { reviewSite, skippedNotifications: budget.skipped };}}),reopen: privateProcedure