diff --git a/.env.example b/.env.example
index 827826b..490ab12 100644
--- a/.env.example
+++ b/.env.example
@@ -75,6 +75,9 @@ WEB_PUSH_EMAIL=
# Email options
FEEDBACK_EMAIL=
+# Bearer token for the internal admin API (/api/admin), at least 32 characters. Off when empty.
+ADMIN_API_TOKEN=
+
# Discord webhook for error notifications
DISCORD_WEBHOOK_URL=
diff --git a/mobile/src/app/(app)/feedback.tsx b/mobile/src/app/(app)/feedback.tsx
index 29b6014..125eba2 100644
--- a/mobile/src/app/(app)/feedback.tsx
+++ b/mobile/src/app/(app)/feedback.tsx
@@ -1,34 +1,53 @@
-import React, { useState } from 'react';
+import Constants from 'expo-constants';
+import * as Crypto from 'expo-crypto';
+import React, { useRef, useState } from 'react';
import { useTranslation } from 'react-i18next';
-import { ScrollView, StyleSheet } from 'react-native';
+import { Platform, ScrollView, StyleSheet } from 'react-native';
import { Input, ModalHeader, Section, icons, toast } from '@/components/ui';
import { api } from '@/lib/api';
+import { type DraftKey, keyForDraft } from '@/lib/draftKey';
import { goBackOr, useConfirmDiscard } from '@/lib/navigation';
import { colors, spacing } from '@/theme/tokens';
-/** The server's `submitFeedback` minimum. */
+/** The server's `submitFeedback` limits. */
const MIN_LENGTH = 10;
+const MAX_LENGTH = 5000;
/** Port of the web's src/components/Account/SubmitFeedback.tsx, as a modal form. */
const Feedback = () => {
const { t } = useTranslation();
const submitFeedback = api.user.submitFeedback.useMutation();
const [feedback, setFeedback] = useState('');
+ // Resending the same text (a double tap, a retry after an error) reuses its key, so it's saved
+ // once; edited text gets a new one
+ const draftKey = useRef(null);
+ // Covers taps before the header re-renders with the spinner
+ const submitting = useRef(false);
const trimmed = feedback.trim();
+ const tooLong = MAX_LENGTH < trimmed.length;
const allowClose = useConfirmDiscard(0 < trimmed.length && !submitFeedback.isLoading);
const submit = async () => {
- if (MIN_LENGTH > trimmed.length) {
+ if (MIN_LENGTH > trimmed.length || tooLong || submitting.current) {
return;
}
+ submitting.current = true;
+ draftKey.current = keyForDraft(draftKey.current, trimmed, () => Crypto.randomUUID());
try {
- await submitFeedback.mutateAsync({ feedback: trimmed });
+ await submitFeedback.mutateAsync({
+ feedback: trimmed,
+ clientKey: draftKey.current.key,
+ source: 'ios' === Platform.OS ? 'ios' : 'android',
+ appVersion: Constants.expoConfig?.version,
+ });
} catch (error) {
console.warn(error);
toast.error(t('account.feedback_failed'));
return;
+ } finally {
+ submitting.current = false;
}
toast.success(t('account.feedback_sent'));
@@ -47,7 +66,9 @@ const Feedback = () => {
keyboardShouldPersistTaps="handled"
keyboardDismissMode="interactive"
>
-
+
{
confirm={{
label: t('account.feedback_submit'),
icon: icons.sendFill,
- disabled: MIN_LENGTH > trimmed.length,
+ disabled: MIN_LENGTH > trimmed.length || tooLong,
loading: submitFeedback.isLoading,
onPress: () => void submit(),
}}
diff --git a/mobile/src/i18n/locales/en.json b/mobile/src/i18n/locales/en.json
index a741be4..6d7008e 100644
--- a/mobile/src/i18n/locales/en.json
+++ b/mobile/src/i18n/locales/en.json
@@ -81,6 +81,7 @@
"feedback": "Send feedback",
"feedback_placeholder": "Enter your feedback",
"feedback_too_short": "Feedback should be at least 10 characters",
+ "feedback_too_long": "Feedback should be at most 5000 characters",
"feedback_submit": "Submit",
"feedback_sent": "Feedback submitted",
"feedback_failed": "Failed to submit feedback",
diff --git a/mobile/src/lib/draftKey.test.ts b/mobile/src/lib/draftKey.test.ts
new file mode 100644
index 0000000..46aea6e
--- /dev/null
+++ b/mobile/src/lib/draftKey.test.ts
@@ -0,0 +1,15 @@
+import assert from 'node:assert/strict';
+import { describe, it } from 'node:test';
+
+import { keyForDraft } from './draftKey';
+
+describe('keyForDraft', () => {
+ it('should keep the key for retries and change it when the text is edited', () => {
+ let count = 0;
+ const newKey = () => `key-${++count}`;
+
+ const first = keyForDraft(null, 'Some feedback', newKey);
+ assert.equal(keyForDraft(first, 'Some feedback', newKey).key, first.key);
+ assert.notEqual(keyForDraft(first, 'Some feedback, edited', newKey).key, first.key);
+ });
+});
diff --git a/mobile/src/lib/draftKey.ts b/mobile/src/lib/draftKey.ts
new file mode 100644
index 0000000..18378c4
--- /dev/null
+++ b/mobile/src/lib/draftKey.ts
@@ -0,0 +1,12 @@
+/**
+ * The idempotency key a feedback draft is sent with.
+ */
+export type DraftKey = { text: string; key: string };
+
+/**
+ * The key to send `text` with: the previous one while the text is unchanged, so retries of the
+ * same draft are saved once, and a new one once it's edited, so the server never mistakes the
+ * edited text for the earlier attempt.
+ */
+export const keyForDraft = (previous: DraftKey | null, text: string, newKey: () => string) =>
+ previous && previous.text === text ? previous : { text, key: newKey() };
diff --git a/prisma/migrations/20261110120000_add_feedback/migration.sql b/prisma/migrations/20261110120000_add_feedback/migration.sql
new file mode 100644
index 0000000..05a4244
--- /dev/null
+++ b/prisma/migrations/20261110120000_add_feedback/migration.sql
@@ -0,0 +1,64 @@
+-- CreateEnum
+CREATE TYPE "FeedbackStatus" AS ENUM ('OPEN', 'REPLIED', 'CLOSED');
+
+-- CreateEnum
+CREATE TYPE "FeedbackReplyStatus" AS ENUM ('SENDING', 'SENT', 'FAILED');
+
+-- CreateTable
+CREATE TABLE "Feedback" (
+ "id" TEXT NOT NULL,
+ "userId" INTEGER,
+ "email" TEXT NOT NULL,
+ "name" TEXT,
+ "message" TEXT NOT NULL,
+ "source" TEXT NOT NULL,
+ "appVersion" TEXT,
+ "clientKey" TEXT,
+ "externalId" TEXT,
+ "status" "FeedbackStatus" NOT NULL DEFAULT 'OPEN',
+ "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
+ "updatedAt" TIMESTAMP(3) NOT NULL,
+
+ CONSTRAINT "Feedback_pkey" PRIMARY KEY ("id")
+);
+
+-- CreateTable
+CREATE TABLE "FeedbackReply" (
+ "id" TEXT NOT NULL,
+ "feedbackId" TEXT NOT NULL,
+ "message" TEXT NOT NULL,
+ "idempotencyKey" TEXT NOT NULL,
+ "deliveryStatus" "FeedbackReplyStatus" NOT NULL DEFAULT 'SENDING',
+ "attemptId" TEXT,
+ "attemptStartedAt" TIMESTAMP(3),
+ "sentAt" TIMESTAMP(3),
+ "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
+ "updatedAt" TIMESTAMP(3) NOT NULL,
+
+ CONSTRAINT "FeedbackReply_pkey" PRIMARY KEY ("id")
+);
+
+-- CreateIndex
+CREATE UNIQUE INDEX "Feedback_clientKey_key" ON "Feedback"("clientKey");
+
+-- CreateIndex
+CREATE UNIQUE INDEX "Feedback_externalId_key" ON "Feedback"("externalId");
+
+-- CreateIndex
+CREATE INDEX "Feedback_status_createdAt_idx" ON "Feedback"("status", "createdAt");
+
+-- CreateIndex
+CREATE INDEX "Feedback_userId_createdAt_idx" ON "Feedback"("userId", "createdAt");
+
+-- CreateIndex
+CREATE UNIQUE INDEX "FeedbackReply_idempotencyKey_key" ON "FeedbackReply"("idempotencyKey");
+
+-- CreateIndex
+CREATE INDEX "FeedbackReply_feedbackId_idx" ON "FeedbackReply"("feedbackId");
+
+-- AddForeignKey
+ALTER TABLE "Feedback" ADD CONSTRAINT "Feedback_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
+
+-- AddForeignKey
+ALTER TABLE "FeedbackReply" ADD CONSTRAINT "FeedbackReply_feedbackId_fkey" FOREIGN KEY ("feedbackId") REFERENCES "Feedback"("id") ON DELETE CASCADE ON UPDATE CASCADE;
+
diff --git a/prisma/schema.prisma b/prisma/schema.prisma
index 696907b..a1bc809 100644
--- a/prisma/schema.prisma
+++ b/prisma/schema.prisma
@@ -72,6 +72,7 @@ model User {
friendOf Friendship[] @relation("FriendFriendships")
recurringExpenses RecurringExpense[]
devicePushTokens DevicePushToken[]
+ feedback Feedback[]
}
model VerificationToken {
@@ -381,3 +382,60 @@ model QuickSplitExpense {
@@index([splitId, createdAt])
}
+
+enum FeedbackStatus {
+ OPEN
+ REPLIED
+ CLOSED
+}
+
+/// Feedback sent from the app. Deleted with the account.
+model Feedback {
+ id String @id @default(cuid())
+ userId Int?
+ /// The sender's email and name when they sent it
+ email String
+ name String?
+ message String
+ /// `web`, `ios`, `android`, `mobile` (older app builds that don't say which) or `import`
+ source String
+ appVersion String?
+ /// Sent by the app once per draft, so a retried submit doesn't save it twice
+ clientKey String? @unique
+ /// Only set on feedback imported from the old feedback emails
+ externalId String? @unique
+ status FeedbackStatus @default(OPEN)
+ createdAt DateTime @default(now())
+ updatedAt DateTime @updatedAt
+ user User? @relation(fields: [userId], references: [id], onDelete: Cascade)
+ replies FeedbackReply[]
+
+ @@index([status, createdAt])
+ @@index([userId, createdAt])
+}
+
+enum FeedbackReplyStatus {
+ /// Reserved, the email is being sent, or it's unknown whether it went
+ SENDING
+ SENT
+ /// Certainly not sent, so it's safe to send again
+ FAILED
+}
+
+model FeedbackReply {
+ id String @id @default(cuid())
+ feedbackId String
+ message String
+ idempotencyKey String @unique
+ deliveryStatus FeedbackReplyStatus @default(SENDING)
+ /// The send attempt that holds it while SENDING: only that attempt may complete it
+ attemptId String?
+ attemptStartedAt DateTime?
+ /// When the email was accepted for delivery
+ sentAt DateTime?
+ createdAt DateTime @default(now())
+ updatedAt DateTime @updatedAt
+ feedback Feedback @relation(fields: [feedbackId], references: [id], onDelete: Cascade)
+
+ @@index([feedbackId])
+}
diff --git a/src/components/Account/SubmitFeedback.tsx b/src/components/Account/SubmitFeedback.tsx
index 9891a8b..2d1c388 100644
--- a/src/components/Account/SubmitFeedback.tsx
+++ b/src/components/Account/SubmitFeedback.tsx
@@ -1,4 +1,4 @@
-import React, { useState } from 'react';
+import React, { useRef, useState } from 'react';
import { AppDrawer } from '../ui/drawer';
import { ChevronRight, MessageSquare } from 'lucide-react';
import { api } from '~/utils/api';
@@ -8,29 +8,52 @@ import { useForm } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import { Form, FormControl, FormField, FormItem, FormMessage } from '../ui/form';
import { toast } from 'sonner';
+import { type DraftKey, keyForDraft } from '~/utils/draftKey';
const feedbackSchema = z.object({
feedback: z
.string({ required_error: 'Feedback is required' })
- .min(10, { message: 'Feedback should be at least 10 characters' }),
+ .trim()
+ .min(10, { message: 'Feedback should be at least 10 characters' })
+ .max(5000, { message: 'Feedback should be at most 5000 characters' }),
});
export const SubmitFeedback: React.FC = () => {
const submitFeedbackMutation = api.user.submitFeedback.useMutation();
const [feedbackOpen, setFeedbackOpen] = useState(false);
+ // Resending the same text (a double tap, a retry) reuses its key, so it's saved once
+ const draftKey = useRef(null);
+ // Covers taps before the disabled button re-renders
+ const submitting = useRef(false);
const feedbackForm = useForm>({
resolver: zodResolver(feedbackSchema),
});
async function onGroupSubmit(values: z.infer) {
+ if (submitting.current) {
+ return;
+ }
+
+ submitting.current = true;
+ draftKey.current = keyForDraft(draftKey.current, values.feedback, () => crypto.randomUUID());
try {
- await submitFeedbackMutation.mutateAsync({ feedback: values.feedback });
- feedbackForm.reset();
- toast.success('Feedback submitted', { duration: 1500 });
+ await submitFeedbackMutation.mutateAsync({
+ feedback: values.feedback,
+ clientKey: draftKey.current.key,
+ source: 'web',
+ });
} catch (e) {
- toast.error('Failed to submit feedback');
+ // Keep the drawer and the text, so it can be sent again as it is
+ toast.error('Failed to submit feedback, please try again');
+ return;
+ } finally {
+ submitting.current = false;
}
+
+ feedbackForm.reset({ feedback: '' });
+ draftKey.current = null;
+ toast.success('Feedback submitted', { duration: 1500 });
setFeedbackOpen(false);
}
@@ -53,6 +76,7 @@ export const SubmitFeedback: React.FC = () => {
className="h-[70vh]"
shouldCloseOnAction={false}
actionTitle="Submit"
+ actionDisabled={submitFeedbackMutation.isLoading}
actionOnClick={async () => {
await feedbackForm.handleSubmit(onGroupSubmit)();
}}
diff --git a/src/env.js b/src/env.js
index 1dab66d..8cc8e98 100644
--- a/src/env.js
+++ b/src/env.js
@@ -70,6 +70,8 @@ export const env = createEnv({
/** Access token for Expo's enhanced push security, needed once it's turned on for the project. */
EXPO_ACCESS_TOKEN: z.string().optional(),
FEEDBACK_EMAIL: z.string().optional(),
+ /** Bearer token for the internal admin API under /api/admin. The API is off when unset. */
+ ADMIN_API_TOKEN: z.string().min(32).optional(),
DISCORD_WEBHOOK_URL: z.string().optional(),
ADSENSE_CLIENT: z.string().startsWith('ca-pub-').optional(),
ADSENSE_FEED_SLOT: z.string().optional(),
@@ -135,6 +137,7 @@ export const env = createEnv({
WEB_PUSH_PUBLIC_KEY: process.env.WEB_PUSH_PUBLIC_KEY,
EXPO_ACCESS_TOKEN: process.env.EXPO_ACCESS_TOKEN,
FEEDBACK_EMAIL: process.env.FEEDBACK_EMAIL,
+ ADMIN_API_TOKEN: process.env.ADMIN_API_TOKEN,
DISCORD_WEBHOOK_URL: process.env.DISCORD_WEBHOOK_URL,
ADSENSE_CLIENT: process.env.ADSENSE_CLIENT,
ADSENSE_FEED_SLOT: process.env.ADSENSE_FEED_SLOT,
diff --git a/src/pages/api/admin/feedback/[id]/index.ts b/src/pages/api/admin/feedback/[id]/index.ts
new file mode 100644
index 0000000..f5a7b79
--- /dev/null
+++ b/src/pages/api/admin/feedback/[id]/index.ts
@@ -0,0 +1,26 @@
+import { AdminApiError, adminApiRoute, queryParam } from '~/server/adminApi';
+import {
+ FeedbackNotFoundError,
+ setFeedbackStatus,
+ statusBody,
+} from '~/server/api/services/feedbackAdmin';
+
+// Read by `adminApiRoute`, once the request is authorised
+export const config = { api: { bodyParser: false } };
+
+/**
+ * PATCH /api/admin/feedback/{id} with `{ "status": "OPEN" | "REPLIED" | "CLOSED" }`
+ */
+export default adminApiRoute(['PATCH'], async (req, res, body) => {
+ const id = queryParam(req.query.id) ?? '';
+ const { status } = statusBody.parse(body);
+
+ try {
+ res.status(200).json(await setFeedbackStatus(id, status));
+ } catch (error) {
+ if (error instanceof FeedbackNotFoundError) {
+ throw new AdminApiError(404, error.message);
+ }
+ throw error;
+ }
+});
diff --git a/src/pages/api/admin/feedback/[id]/reply/[replyId].ts b/src/pages/api/admin/feedback/[id]/reply/[replyId].ts
new file mode 100644
index 0000000..d49ad8c
--- /dev/null
+++ b/src/pages/api/admin/feedback/[id]/reply/[replyId].ts
@@ -0,0 +1,36 @@
+import { AdminApiError, adminApiRoute, queryParam } from '~/server/adminApi';
+import {
+ ReplyNotFoundError,
+ ReplyNotSendingError,
+ ReplyStillSendingError,
+ resolveReply,
+ resolveReplyBody,
+} from '~/server/api/services/feedbackAdmin';
+
+// Read by `adminApiRoute`, once the request is authorised
+export const config = { api: { bodyParser: false } };
+
+/**
+ * PATCH /api/admin/feedback/{id}/reply/{replyId} with `{ "deliveryStatus": "SENT" | "FAILED" }`
+ * Settles a reply stuck in SENDING. Only do this after checking whether the email arrived: SENT
+ * records it as sent; FAILED lets the same key and message be sent again. Allowed only from
+ * SENDING, once the send attempt started at least 10 minutes ago and is treated as abandoned;
+ * before that, 409 "Reply is still sending; try again later".
+ */
+export default adminApiRoute(['PATCH'], async (req, res, body) => {
+ const feedbackId = queryParam(req.query.id) ?? '';
+ const replyId = queryParam(req.query.replyId) ?? '';
+ const input = resolveReplyBody.parse(body);
+
+ try {
+ res.status(200).json({ reply: await resolveReply(feedbackId, replyId, input) });
+ } catch (error) {
+ if (error instanceof ReplyNotFoundError) {
+ throw new AdminApiError(404, error.message);
+ }
+ if (error instanceof ReplyNotSendingError || error instanceof ReplyStillSendingError) {
+ throw new AdminApiError(409, error.message);
+ }
+ throw error;
+ }
+});
diff --git a/src/pages/api/admin/feedback/[id]/reply/index.ts b/src/pages/api/admin/feedback/[id]/reply/index.ts
new file mode 100644
index 0000000..9d2569d
--- /dev/null
+++ b/src/pages/api/admin/feedback/[id]/reply/index.ts
@@ -0,0 +1,51 @@
+import { AdminApiError, adminApiRoute, queryParam } from '~/server/adminApi';
+import {
+ FeedbackNotFoundError,
+ IdempotencyConflictError,
+ ReplyInProgressError,
+ replyBody,
+ replyToFeedback,
+} from '~/server/api/services/feedbackAdmin';
+import { MailNotSentError } from '~/server/mailer';
+
+// Read by `adminApiRoute`, once the request is authorised
+export const config = { api: { bodyParser: false } };
+
+/**
+ * POST /api/admin/feedback/{id}/reply with `{ "message": string, "idempotencyKey": string }`
+ * Emails the reply and records it. A key stands for one message: send an edited reply with a new
+ * key. 201 when sent now, 200 when that key and message were already sent, 409 when the key was
+ * used for other feedback or another message, or the reply is being sent or it's unknown whether
+ * it went (resolve it with PATCH .../reply/{replyId}), 502 when the email wasn't sent (retry with
+ * the same key and message) or it's unknown whether it went (not retried).
+ */
+export default adminApiRoute(['POST'], async (req, res, body) => {
+ const id = queryParam(req.query.id) ?? '';
+ const input = replyBody.parse(body);
+
+ try {
+ const { reply, created } = await replyToFeedback(id, input);
+ res.status(created ? 201 : 200).json({ reply, created });
+ } catch (error) {
+ if (error instanceof FeedbackNotFoundError) {
+ throw new AdminApiError(404, error.message);
+ }
+ if (error instanceof IdempotencyConflictError || error instanceof ReplyInProgressError) {
+ throw new AdminApiError(409, error.message);
+ }
+ if (error instanceof MailNotSentError) {
+ // Only codes: the error message can contain the recipient or the server's reply
+ console.error('Feedback reply email not sent', {
+ feedbackId: id,
+ code: error.code,
+ responseCode: error.responseCode,
+ definite: error.definite,
+ });
+ throw new AdminApiError(
+ 502,
+ error.definite ? 'Email not sent' : 'Email delivery uncertain; not resending',
+ );
+ }
+ throw error;
+ }
+});
diff --git a/src/pages/api/admin/feedback/import.ts b/src/pages/api/admin/feedback/import.ts
new file mode 100644
index 0000000..df08915
--- /dev/null
+++ b/src/pages/api/admin/feedback/import.ts
@@ -0,0 +1,20 @@
+import { adminApiRoute } from '~/server/adminApi';
+import { importBody, importFeedback } from '~/server/api/services/feedbackAdmin';
+
+// Read by `adminApiRoute`, once the request is authorised
+export const config = { api: { bodyParser: false } };
+
+/**
+ * POST /api/admin/feedback/import with `{ "items": [...] }`, at most 500 items.
+ * Only feedback from existing accounts is stored; the rest is counted as `unmatched`. Safe to run
+ * again: items already imported (by `externalId`) are skipped.
+ */
+export default adminApiRoute(
+ ['POST'],
+ async (_req, res, body) => {
+ const input = importBody.parse(body);
+ res.status(200).json(await importFeedback(input));
+ },
+ // A full batch of long messages is past the default 1mb
+ { maxBodyBytes: 10 * 1024 * 1024 },
+);
diff --git a/src/pages/api/admin/feedback/index.ts b/src/pages/api/admin/feedback/index.ts
new file mode 100644
index 0000000..2535e27
--- /dev/null
+++ b/src/pages/api/admin/feedback/index.ts
@@ -0,0 +1,14 @@
+import { adminApiRoute } from '~/server/adminApi';
+import { listFeedback, listFeedbackQuery } from '~/server/api/services/feedbackAdmin';
+
+// Read by `adminApiRoute`, once the request is authorised
+export const config = { api: { bodyParser: false } };
+
+/**
+ * GET /api/admin/feedback?since=&status=&limit=<1-200>&cursor=
+ * Feedback newest first, with `nextCursor` for the next page.
+ */
+export default adminApiRoute(['GET'], async (req, res) => {
+ const query = listFeedbackQuery.parse(req.query);
+ res.status(200).json(await listFeedback(query));
+});
diff --git a/src/pages/delete-account.tsx b/src/pages/delete-account.tsx
index bb27316..48727ae 100644
--- a/src/pages/delete-account.tsx
+++ b/src/pages/delete-account.tsx
@@ -44,7 +44,7 @@ const DeleteAccount: NextPage<{ feedbackEmail: string }> = ({ feedbackEmail }) =
What gets deleted
Your name, email address, profile photo, sign-in methods (Google, Apple, email), sessions,
- notification settings and your friends list are deleted straight away.
+ notification settings, your friends list and feedback you sent us are deleted straight away.
What is kept
diff --git a/src/pages/privacy.tsx b/src/pages/privacy.tsx
index d0ea07f..5bfc580 100644
--- a/src/pages/privacy.tsx
+++ b/src/pages/privacy.tsx
@@ -36,6 +36,11 @@ const Privacy: NextPage<{ feedbackEmail: string; adsEnabled: boolean }> = ({
We automatically collect information about how you interact with the Service, such as
pages visited and features used.
+
Feedback
+
+ When you send us feedback, we store it with your account, name and email address so we can
+ reply. It is deleted when you delete your account.
+
Quick splits
When you use Quick split without an account, we store the split's name, the names you
diff --git a/src/server/adminApi.ts b/src/server/adminApi.ts
new file mode 100644
index 0000000..c7cca7c
--- /dev/null
+++ b/src/server/adminApi.ts
@@ -0,0 +1,106 @@
+import { type NextApiRequest, type NextApiResponse } from 'next';
+import { ZodError } from 'zod';
+
+import { env } from '~/env';
+import { isAdminAuthorized } from '~/server/adminAuth';
+import { isJsonContentType } from '~/server/mobileAuthTokens';
+
+export class AdminApiError extends Error {
+ constructor(
+ public status: number,
+ message: string,
+ ) {
+ super(message);
+ }
+}
+
+const DEFAULT_MAX_BODY_BYTES = 1024 * 1024;
+
+type Handler = (req: NextApiRequest, res: NextApiResponse, body: unknown) => Promise;
+
+/**
+ * Reads and parses a JSON body of at most `maxBytes`.
+ */
+const readJsonBody = async (req: NextApiRequest, maxBytes: number): Promise => {
+ const chunks: Buffer[] = [];
+ let size = 0;
+
+ for await (const chunk of req) {
+ const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk as string);
+ size += buffer.length;
+ if (size > maxBytes) {
+ throw new AdminApiError(413, 'Body too large');
+ }
+ chunks.push(buffer);
+ }
+
+ const text = Buffer.concat(chunks).toString('utf8');
+ if ('' === text.trim()) {
+ return undefined;
+ }
+
+ try {
+ return JSON.parse(text) as unknown;
+ } catch {
+ throw new AdminApiError(400, 'Invalid JSON');
+ }
+};
+
+/**
+ * Only an error's name and Prisma or Node code: messages can contain request data.
+ */
+const safeErrorDetails = (error: unknown) => {
+ if (!(error instanceof Error)) {
+ return { name: typeof error };
+ }
+ const { code } = error as { code?: unknown };
+ return { name: error.name, code: 'string' === typeof code ? code : undefined };
+};
+
+/**
+ * An internal admin API route: answers 404 unless ADMIN_API_TOKEN is set, then needs
+ * `Authorization: Bearer `. Only `methods` are allowed, bodies must be JSON (read
+ * only once the request is authorised, so the route needs `bodyParser: false` in its config) and every response is
+ * JSON. `AdminApiError` and zod errors become their status codes.
+ */
+export const adminApiRoute =
+ (methods: string[], handler: Handler, { maxBodyBytes = DEFAULT_MAX_BODY_BYTES } = {}) =>
+ async (req: NextApiRequest, res: NextApiResponse) => {
+ res.setHeader('Cache-Control', 'no-store');
+
+ if (!env.ADMIN_API_TOKEN) {
+ return res.status(404).json({ error: 'Not found' });
+ }
+
+ if (!isAdminAuthorized(req.headers.authorization, env.ADMIN_API_TOKEN)) {
+ return res.status(401).json({ error: 'Unauthorized' });
+ }
+
+ if (!req.method || !methods.includes(req.method)) {
+ res.setHeader('Allow', methods.join(', '));
+ return res.status(405).json({ error: 'Method not allowed' });
+ }
+
+ const hasBody = 'GET' !== req.method;
+ if (hasBody && !isJsonContentType(req.headers['content-type'])) {
+ return res.status(415).json({ error: 'Send a JSON body' });
+ }
+
+ try {
+ const body = hasBody ? await readJsonBody(req, maxBodyBytes) : undefined;
+ await handler(req, res, body);
+ } catch (error) {
+ if (error instanceof AdminApiError) {
+ return res.status(error.status).json({ error: error.message });
+ }
+ if (error instanceof ZodError) {
+ return res.status(400).json({ error: 'Invalid request', issues: error.issues });
+ }
+ console.error('Admin API error', safeErrorDetails(error));
+ return res.status(500).json({ error: 'Internal server error' });
+ }
+ };
+
+/** A single string route or query param. */
+export const queryParam = (value: string | string[] | undefined) =>
+ Array.isArray(value) ? value[0] : value;
diff --git a/src/server/adminAuth.ts b/src/server/adminAuth.ts
new file mode 100644
index 0000000..12e6e76
--- /dev/null
+++ b/src/server/adminAuth.ts
@@ -0,0 +1,30 @@
+import { createHash, timingSafeEqual } from 'crypto';
+
+import { getBearerToken } from '~/server/mobileAuthTokens';
+
+/**
+ * Pure helpers for the internal admin API. Kept free of db and env imports so they can be unit
+ * tested.
+ */
+
+const digest = (value: string) => createHash('sha256').update(value).digest();
+
+/**
+ * Whether `authorization` is `Bearer `. Compares digests in constant time, so neither the
+ * token nor its length leaks through timing. Never true without an expected token.
+ */
+export const isAdminAuthorized = (
+ authorization: string | undefined,
+ expected: string | undefined,
+) => {
+ if (!expected) {
+ return false;
+ }
+
+ const token = getBearerToken(authorization);
+ if (!token) {
+ return false;
+ }
+
+ return timingSafeEqual(digest(token), digest(expected));
+};
diff --git a/src/server/api/routers/user.ts b/src/server/api/routers/user.ts
index 513a93d..1c0977e 100644
--- a/src/server/api/routers/user.ts
+++ b/src/server/api/routers/user.ts
@@ -21,13 +21,14 @@ import { TRPCError } from '@trpc/server';
import { randomUUID } from 'crypto';
import { getDocumentUploadUrl } from '~/server/storage';
import { FILE_SIZE_LIMIT } from '~/lib/constants';
-import { sendFeedbackEmail, sendInviteEmail } from '~/server/mailer';
+import { sendInviteEmail } from '~/server/mailer';
import { pushNotification } from '~/server/notification';
import { DEVICE_PLATFORMS, isValidExpoPushToken } from '~/utils/expoPush';
import { toFixedNumber, toUIString } from '~/utils/numbers';
import { SplitwiseGroupSchema, SplitwiseUserSchema } from '~/types';
import { env } from '~/env';
import { runBalanceTransaction } from '../services/balanceTransaction';
+import { submitFeedback, submitFeedbackInput } from '../services/feedbackService';
import { getBalanceBreakdownWithFriend } from '../services/balanceBreakdown';
import { sendRecurringStatusPushNotification } from '../services/notificationService';
import { pauseRulesWith, recurrenceInputSchema } from '../services/recurringService';
@@ -451,11 +452,14 @@ export const userRouter = createTRPCRouter({
await deleteExpense(input.expenseId, ctx.session.user.id);
}),
- submitFeedback: protectedProcedure
- .input(z.object({ feedback: z.string().min(10) }))
- .mutation(async ({ input, ctx }) => {
- await sendFeedbackEmail(input.feedback, ctx.session.user);
- }),
+ submitFeedback: protectedProcedure.input(submitFeedbackInput).mutation(async ({ input, ctx }) => {
+ const { id } = await submitFeedback(
+ ctx.session.user,
+ input,
+ ctx.mobileSessionId ? 'mobile' : 'web',
+ );
+ return { id };
+ }),
getFriend: protectedProcedure
.input(z.object({ friendId: z.number() }))
diff --git a/src/server/api/services/accountDeletion.ts b/src/server/api/services/accountDeletion.ts
index e71eba6..a8eb410 100644
--- a/src/server/api/services/accountDeletion.ts
+++ b/src/server/api/services/accountDeletion.ts
@@ -344,8 +344,8 @@ const leaveSettledGroups = async (tx: Prisma.TransactionClient, userId: number)
};
/**
- * Removes the person, not what they share: their name, email, sign-in methods, sessions, devices
- * and friendships go. Expenses, balances and notes stay for the people they share them with,
+ * Removes the person, not what they share: their name, email, sign-in methods, sessions, devices,
+ * friendships and feedback go. Expenses, balances and notes stay for the people they share them with,
* against an anonymous user row.
*/
const anonymiseAccount = async (
@@ -366,6 +366,8 @@ const anonymiseAccount = async (
await tx.devicePushToken.deleteMany({ where: { userId } });
await tx.session.deleteMany({ where: { userId } });
await tx.pushNotification.deleteMany({ where: { userId } });
+ // The row stays, so its cascade doesn't remove these
+ await tx.feedback.deleteMany({ where: { userId } });
await tx.verificationToken.deleteMany({
where: {
OR: [
diff --git a/src/server/api/services/feedbackAdmin.ts b/src/server/api/services/feedbackAdmin.ts
new file mode 100644
index 0000000..9357b1f
--- /dev/null
+++ b/src/server/api/services/feedbackAdmin.ts
@@ -0,0 +1,469 @@
+import {
+ type FeedbackReply,
+ FeedbackReplyStatus,
+ FeedbackStatus,
+ Prisma,
+ type PrismaClient,
+} from '@prisma/client';
+import { randomUUID } from 'crypto';
+import { z } from 'zod';
+
+import { db as defaultDb } from '~/server/db';
+import { MailNotSentError, sendFeedbackReplyEmail } from '~/server/mailer';
+
+import { isUniqueViolation } from './feedbackService';
+
+/**
+ * Reviewing and replying to feedback, for the internal admin API.
+ */
+
+export const FEEDBACK_LIST_MAX = 200;
+export const FEEDBACK_IMPORT_MAX = 500;
+const IMPORTED_MESSAGE_MAX = 50_000;
+const REPLY_MAX = 20_000;
+/** Reserving a reply retries this often when another request changes it meanwhile. */
+const RESERVE_ATTEMPTS = 3;
+
+export class FeedbackNotFoundError extends Error {}
+export class IdempotencyConflictError extends Error {}
+/** A reply with this key is being sent, or it's unknown whether it went: not sending it again. */
+export class ReplyInProgressError extends Error {}
+export class ReplyNotFoundError extends Error {}
+/** Only a SENDING reply can be resolved by hand. */
+export class ReplyNotSendingError extends Error {}
+/** Its send attempt started less than `RECONCILE_AFTER_MS` ago, so it can't be resolved yet. */
+export class ReplyStillSendingError extends Error {}
+
+/**
+ * A reply stuck in SENDING this long after its attempt started is treated as abandoned, and an
+ * operator may resolve it. This doesn't prove the send has stopped (the SMTP socket timeout is for
+ * inactivity), so resolving stays a manual step, taken only after checking whether the email
+ * arrived.
+ */
+export const RECONCILE_AFTER_MS = 10 * 60 * 1000;
+
+/** A new attempt to send a reply: only the attempt holding a SENDING reply may complete it. */
+const newAttempt = () => ({ attemptId: randomUUID(), attemptStartedAt: new Date() });
+
+const statusSchema = z.nativeEnum(FeedbackStatus);
+
+export const listFeedbackQuery = z.object({
+ since: z.string().datetime({ offset: true }).optional(),
+ status: statusSchema.optional(),
+ limit: z.coerce.number().int().min(1).max(FEEDBACK_LIST_MAX).default(50),
+ cursor: z.string().min(1).optional(),
+});
+
+export const replyBody = z.object({
+ message: z.string().trim().min(1).max(REPLY_MAX),
+ idempotencyKey: z.string().trim().min(1).max(200),
+});
+
+export const statusBody = z.object({ status: statusSchema });
+
+export const resolveReplyBody = z.object({
+ deliveryStatus: z.enum([FeedbackReplyStatus.SENT, FeedbackReplyStatus.FAILED]),
+});
+
+export const importBody = z.object({
+ items: z
+ .array(
+ z.object({
+ externalId: z.string().trim().min(1).max(500),
+ email: z.string().trim().email(),
+ message: z.string().trim().min(1).max(IMPORTED_MESSAGE_MAX),
+ createdAt: z.string().datetime({ offset: true }),
+ source: z.literal('import').optional(),
+ replies: z
+ .array(
+ z.object({
+ message: z.string().trim().min(1).max(REPLY_MAX),
+ sentAt: z.string().datetime({ offset: true }),
+ idempotencyKey: z.string().trim().min(1).max(200),
+ }),
+ )
+ .optional(),
+ }),
+ )
+ .max(FEEDBACK_IMPORT_MAX),
+});
+
+const replySelect = {
+ id: true,
+ message: true,
+ idempotencyKey: true,
+ deliveryStatus: true,
+ sentAt: true,
+ createdAt: true,
+} satisfies Prisma.FeedbackReplySelect;
+
+const feedbackInclude = {
+ user: { select: { id: true, name: true, email: true, deletedAt: true } },
+ replies: { select: replySelect, orderBy: { createdAt: 'asc' } },
+} satisfies Prisma.FeedbackInclude;
+
+type FeedbackWithDetails = Prisma.FeedbackGetPayload<{ include: typeof feedbackInclude }>;
+type ReplyDetails = Pick;
+
+const toReplyJson = (reply: ReplyDetails) => ({
+ id: reply.id,
+ message: reply.message,
+ idempotencyKey: reply.idempotencyKey,
+ deliveryStatus: reply.deliveryStatus,
+ sentAt: reply.sentAt?.toISOString() ?? null,
+ createdAt: reply.createdAt.toISOString(),
+});
+
+const toFeedbackJson = (feedback: FeedbackWithDetails) => ({
+ id: feedback.id,
+ createdAt: feedback.createdAt.toISOString(),
+ updatedAt: feedback.updatedAt.toISOString(),
+ status: feedback.status,
+ source: feedback.source,
+ appVersion: feedback.appVersion,
+ message: feedback.message,
+ email: feedback.email,
+ name: feedback.name,
+ user: feedback.user
+ ? {
+ id: feedback.user.id,
+ name: feedback.user.name,
+ email: feedback.user.email,
+ deletedAt: feedback.user.deletedAt?.toISOString() ?? null,
+ }
+ : null,
+ replies: feedback.replies.map(toReplyJson),
+});
+
+/**
+ * Newest first. Pass the previous page's `nextCursor` as `cursor` for the next page.
+ */
+export const listFeedback = async (
+ query: z.infer,
+ db: PrismaClient = defaultDb,
+) => {
+ const rows = await db.feedback.findMany({
+ where: {
+ ...(query.status ? { status: query.status } : {}),
+ ...(query.since ? { createdAt: { gte: new Date(query.since) } } : {}),
+ },
+ include: feedbackInclude,
+ orderBy: [{ createdAt: 'desc' }, { id: 'desc' }],
+ take: query.limit + 1,
+ ...(query.cursor ? { cursor: { id: query.cursor }, skip: 1 } : {}),
+ });
+
+ const items = rows.slice(0, query.limit);
+ return {
+ items: items.map(toFeedbackJson),
+ nextCursor: rows.length > query.limit ? items.at(-1)?.id ?? null : null,
+ };
+};
+
+export const setFeedbackStatus = async (
+ id: string,
+ status: FeedbackStatus,
+ db: PrismaClient = defaultDb,
+) => {
+ try {
+ const feedback = await db.feedback.update({
+ where: { id },
+ data: { status },
+ include: feedbackInclude,
+ });
+ return toFeedbackJson(feedback);
+ } catch (error) {
+ if (error instanceof Prisma.PrismaClientKnownRequestError && 'P2025' === error.code) {
+ throw new FeedbackNotFoundError('Feedback not found');
+ }
+ throw error;
+ }
+};
+
+type Reservation =
+ | { send: false; reply: ReplyDetails }
+ | { send: true; reply: ReplyDetails; attemptId: string; to: string; original: string };
+
+/**
+ * Claims the reply with this key for sending, or finds that it was sent. Each step is a single
+ * atomic write (the unique key on insert, a conditional update from FAILED), so only one attempt
+ * ever holds a reply in SENDING, and nothing stays locked while the email goes out. The claim
+ * gets a new `attemptId`.
+ */
+const reserveReply = async (
+ db: PrismaClient,
+ id: string,
+ message: string,
+ idempotencyKey: string,
+ attempt = 1,
+): Promise => {
+ const retry = () => {
+ if (RESERVE_ATTEMPTS <= attempt) {
+ throw new ReplyInProgressError('Delivery in progress or uncertain; not resending');
+ }
+ return reserveReply(db, id, message, idempotencyKey, attempt + 1);
+ };
+
+ const feedback = await db.feedback.findUnique({
+ where: { id },
+ select: { message: true, email: true, user: { select: { email: true } } },
+ });
+ if (!feedback) {
+ throw new FeedbackNotFoundError('Feedback not found');
+ }
+ const target = { to: feedback.user?.email ?? feedback.email, original: feedback.message };
+
+ const existing = await db.feedbackReply.findUnique({
+ where: { idempotencyKey },
+ select: { ...replySelect, feedbackId: true },
+ });
+
+ if (!existing) {
+ try {
+ const claim = newAttempt();
+ const reply = await db.feedbackReply.create({
+ data: { feedbackId: id, message, idempotencyKey, ...claim },
+ select: replySelect,
+ });
+ return { send: true, reply, attemptId: claim.attemptId, ...target };
+ } catch (error) {
+ // Another request took the key first: look again at what it did
+ if (isUniqueViolation(error, 'idempotencyKey')) {
+ return retry();
+ }
+ throw error;
+ }
+ }
+
+ if (existing.feedbackId !== id) {
+ throw new IdempotencyConflictError('idempotencyKey was used for other feedback');
+ }
+ // A key stands for one message: an edited reply needs a new key, also after a failure
+ if (existing.message !== message) {
+ throw new IdempotencyConflictError('idempotencyKey was used for a different message');
+ }
+ if (FeedbackReplyStatus.SENT === existing.deliveryStatus) {
+ return { send: false, reply: existing };
+ }
+ if (FeedbackReplyStatus.SENDING === existing.deliveryStatus) {
+ throw new ReplyInProgressError('Delivery in progress or uncertain; not resending');
+ }
+
+ // FAILED certainly wasn't sent: claim it again, unless another retry just did
+ const claim = newAttempt();
+ const { count } = await db.feedbackReply.updateMany({
+ where: { id: existing.id, deliveryStatus: FeedbackReplyStatus.FAILED },
+ data: { deliveryStatus: FeedbackReplyStatus.SENDING, ...claim },
+ });
+ if (0 === count) {
+ return retry();
+ }
+ return {
+ send: true,
+ reply: { ...existing, deliveryStatus: FeedbackReplyStatus.SENDING },
+ attemptId: claim.attemptId,
+ ...target,
+ };
+};
+
+type ReplyDeps = { db: PrismaClient; send: typeof sendFeedbackReplyEmail };
+
+/**
+ * Emails a reply to whoever sent the feedback and records it, at most once per `idempotencyKey`.
+ *
+ * The reply is reserved as SENDING first, then the email is sent outside any transaction. When it
+ * certainly failed, the reply is marked FAILED and the same key can be retried; when it's uncertain
+ * (e.g. the connection dropped mid-send) it stays SENDING and isn't sent again. Throws
+ * `MailNotSentError` in both cases, and `ReplyInProgressError` for a key that's SENDING.
+ */
+export const replyToFeedback = async (
+ id: string,
+ { message, idempotencyKey }: z.infer,
+ { db, send }: ReplyDeps = { db: defaultDb, send: sendFeedbackReplyEmail },
+) => {
+ const reservation = await reserveReply(db, id, message, idempotencyKey);
+ if (!reservation.send) {
+ return { reply: toReplyJson(reservation.reply), created: false };
+ }
+
+ // Completing only ever touches the reply while this attempt still holds it: once an operator
+ // resolved it, or another attempt claimed it since, this one's outcome is no longer its to record.
+ const owned = {
+ id: reservation.reply.id,
+ deliveryStatus: FeedbackReplyStatus.SENDING,
+ attemptId: reservation.attemptId,
+ };
+ const lostOwnership = () =>
+ console.warn('Feedback reply attempt no longer holds the reply', reservation.reply.id);
+
+ try {
+ await send({ to: reservation.to, reply: message, original: reservation.original });
+ } catch (error) {
+ if (error instanceof MailNotSentError && error.definite) {
+ const { count } = await db.feedbackReply.updateMany({
+ where: owned,
+ data: { deliveryStatus: FeedbackReplyStatus.FAILED },
+ });
+ if (0 === count) {
+ lostOwnership();
+ }
+ }
+ throw error;
+ }
+
+ const recorded = await db.$transaction(async (tx) => {
+ const { count } = await tx.feedbackReply.updateMany({
+ where: owned,
+ data: { deliveryStatus: FeedbackReplyStatus.SENT, sentAt: new Date() },
+ });
+ if (1 === count) {
+ await tx.feedback.updateMany({ where: { id }, data: { status: FeedbackStatus.REPLIED } });
+ }
+ const reply = await tx.feedbackReply.findUniqueOrThrow({
+ where: { id: reservation.reply.id },
+ select: replySelect,
+ });
+ return { reply, created: 1 === count };
+ });
+ if (!recorded.created) {
+ lostOwnership();
+ }
+ return { reply: toReplyJson(recorded.reply), created: recorded.created };
+};
+
+/**
+ * Settles a reply stuck in SENDING, e.g. after the connection dropped mid-send. An operator action,
+ * to take only after checking whether the email arrived: SENT records it as sent, FAILED lets the
+ * same key and message be sent again. Never done automatically, and only once its attempt started
+ * at least `RECONCILE_AFTER_MS` ago and is treated as abandoned; that attempt can't complete it
+ * afterwards.
+ */
+export const resolveReply = async (
+ feedbackId: string,
+ replyId: string,
+ { deliveryStatus }: z.infer,
+ db: PrismaClient = defaultDb,
+ now = new Date(),
+) =>
+ db.$transaction(async (tx) => {
+ const sent = FeedbackReplyStatus.SENT === deliveryStatus;
+ const { count } = await tx.feedbackReply.updateMany({
+ where: {
+ id: replyId,
+ feedbackId,
+ deliveryStatus: FeedbackReplyStatus.SENDING,
+ OR: [
+ { attemptStartedAt: null },
+ { attemptStartedAt: { lt: new Date(now.getTime() - RECONCILE_AFTER_MS) } },
+ ],
+ },
+ data: {
+ deliveryStatus,
+ attemptId: null,
+ ...(sent ? { sentAt: new Date() } : {}),
+ },
+ });
+
+ const reply = await tx.feedbackReply.findFirst({
+ where: { id: replyId, feedbackId },
+ select: replySelect,
+ });
+ if (!reply) {
+ throw new ReplyNotFoundError('Reply not found');
+ }
+ if (0 === count) {
+ if (FeedbackReplyStatus.SENDING === reply.deliveryStatus) {
+ throw new ReplyStillSendingError('Reply is still sending; try again later');
+ }
+ throw new ReplyNotSendingError(
+ `Reply is ${reply.deliveryStatus}; only SENDING replies can be resolved`,
+ );
+ }
+
+ if (sent) {
+ await tx.feedback.update({
+ where: { id: feedbackId },
+ data: { status: FeedbackStatus.REPLIED },
+ });
+ }
+ return toReplyJson(reply);
+ });
+
+type ImportItem = z.infer['items'][number];
+type ImportOutcome = 'created' | 'skipped' | 'unmatched';
+
+/**
+ * Imports one item for a live account with its email, in a transaction that holds that account
+ * against deletion until the feedback is saved.
+ */
+const importItem = async (db: PrismaClient, item: ImportItem): Promise => {
+ try {
+ return await db.$transaction(async (tx) => {
+ const [user] = await tx.$queryRaw<{ id: number }[]>`
+ SELECT id FROM "User"
+ WHERE lower(email) = lower(${item.email}) AND "deletedAt" IS NULL
+ ORDER BY id LIMIT 1 FOR SHARE`;
+ if (!user) {
+ return 'unmatched';
+ }
+
+ const existing = await tx.feedback.findUnique({
+ where: { externalId: item.externalId },
+ select: { id: true },
+ });
+ if (existing) {
+ return 'skipped';
+ }
+
+ const replies = item.replies ?? [];
+ await tx.feedback.create({
+ data: {
+ externalId: item.externalId,
+ userId: user.id,
+ email: item.email,
+ message: item.message,
+ source: 'import',
+ status: 0 < replies.length ? FeedbackStatus.REPLIED : FeedbackStatus.OPEN,
+ createdAt: new Date(item.createdAt),
+ replies: {
+ create: replies.map((reply) => ({
+ message: reply.message,
+ idempotencyKey: reply.idempotencyKey,
+ deliveryStatus: FeedbackReplyStatus.SENT,
+ sentAt: new Date(reply.sentAt),
+ createdAt: new Date(reply.sentAt),
+ })),
+ },
+ },
+ });
+ return 'created';
+ });
+ } catch (error) {
+ // Imported meanwhile by another run
+ if (isUniqueViolation(error, 'externalId')) {
+ return 'skipped';
+ }
+ throw error;
+ }
+};
+
+/**
+ * Adds feedback from before it was saved here, e.g. from the feedback emails. Only feedback from
+ * an existing, not deleted, account is imported: anything else is counted as `unmatched` and not
+ * stored, so feedback of deleted accounts doesn't come back. Items already imported (by
+ * `externalId`) are skipped, so it's safe to run again.
+ */
+export const importFeedback = async (
+ { items }: z.infer,
+ db: PrismaClient = defaultDb,
+) => {
+ const counts: Record = { created: 0, skipped: 0, unmatched: 0 };
+
+ // One at a time keeps the counts exact when the same externalId appears twice in a batch.
+ await items.reduce(async (previous, item) => {
+ await previous;
+ counts[await importItem(db, item)] += 1;
+ }, Promise.resolve());
+
+ return counts;
+};
diff --git a/src/server/api/services/feedbackService.ts b/src/server/api/services/feedbackService.ts
new file mode 100644
index 0000000..34845d0
--- /dev/null
+++ b/src/server/api/services/feedbackService.ts
@@ -0,0 +1,165 @@
+import { type Feedback, Prisma, type PrismaClient } from '@prisma/client';
+import { TRPCError } from '@trpc/server';
+import { z } from 'zod';
+
+import { db as defaultDb } from '~/server/db';
+import { type FeedbackEmailDetails, sendFeedbackEmail } from '~/server/mailer';
+import { safeMailErrorDetails } from '~/server/mailErrors';
+
+const HOUR_MS = 60 * 60 * 1000;
+
+export const FEEDBACK_MIN_LENGTH = 10;
+export const FEEDBACK_MAX_LENGTH = 5000;
+export const FEEDBACK_SOURCES = ['web', 'ios', 'android'] as const;
+
+/** The same message from the same person within this window is a resend, not new feedback. */
+export const DUPLICATE_WINDOW_MS = 24 * HOUR_MS;
+export const FEEDBACK_RATE_LIMIT = { limit: 10, windowMs: HOUR_MS };
+
+export const submitFeedbackInput = z.object({
+ feedback: z.string().trim().min(FEEDBACK_MIN_LENGTH).max(FEEDBACK_MAX_LENGTH),
+ /** One per draft, so retrying the same draft never saves it twice. */
+ clientKey: z.string().uuid().optional(),
+ source: z.enum(FEEDBACK_SOURCES).optional(),
+ appVersion: z.string().trim().max(50).optional(),
+});
+
+export type SubmitFeedbackInput = z.infer;
+
+/** Only the id is used: the email and name are read from the account when it's saved. */
+export type FeedbackSender = { id: number };
+
+type Deps = {
+ db: PrismaClient;
+ notify: (feedback: FeedbackEmailDetails) => Promise;
+};
+
+const defaultDeps: Deps = { db: defaultDb, notify: sendFeedbackEmail };
+
+/**
+ * Whether more feedback from someone who sent `recentCount` in the last rate limit window would
+ * be too many.
+ */
+export const isOverFeedbackLimit = (recentCount: number) =>
+ recentCount >= FEEDBACK_RATE_LIMIT.limit;
+
+export const isUniqueViolation = (error: unknown, field: string) =>
+ error instanceof Prisma.PrismaClientKnownRequestError &&
+ 'P2002' === error.code &&
+ JSON.stringify(error.meta?.target ?? '').includes(field);
+
+/**
+ * The feedback this submission repeats, if any: the same draft (`clientKey`) with the same text, or
+ * the same message from the same person within a day, which covers older apps that don't send a
+ * key.
+ */
+const findExisting = async (
+ tx: Prisma.TransactionClient,
+ userId: number,
+ message: string,
+ clientKey: string | undefined,
+ now: Date,
+) => {
+ if (clientKey) {
+ const byKey = await tx.feedback.findUnique({ where: { clientKey } });
+ if (byKey) {
+ // Another person's key, or the same draft edited after a failed send: never report text
+ // as saved when it wasn't
+ if (byKey.userId !== userId || byKey.message !== message) {
+ throw new TRPCError({ code: 'CONFLICT', message: 'Feedback key already used' });
+ }
+ return byKey;
+ }
+ }
+
+ return tx.feedback.findFirst({
+ where: { userId, message, createdAt: { gte: new Date(now.getTime() - DUPLICATE_WINDOW_MS) } },
+ orderBy: { createdAt: 'desc' },
+ });
+};
+
+/**
+ * Saves feedback, then emails us about it without waiting. Resubmitting returns the saved
+ * feedback instead of saving or emailing it again.
+ */
+export const submitFeedback = async (
+ user: FeedbackSender,
+ input: SubmitFeedbackInput,
+ /** Where it came from when the client doesn't say: older app builds only send the message. */
+ fallbackSource: 'web' | 'mobile',
+ { db, notify }: Deps = defaultDeps,
+): Promise<{ id: string; created: boolean }> => {
+ const message = input.feedback.trim();
+ const now = new Date();
+
+ let result: { feedback: Feedback; created: boolean };
+ try {
+ // Locking the sender's row makes their submissions one at a time, so two quick resends can't
+ // both miss each other in the checks below. It also waits for an account deletion that's under
+ // way, and the email and name come from the locked row, not the possibly stale session.
+ result = await db.$transaction(async (tx) => {
+ const [sender] = await tx.$queryRaw<{ email: string | null; name: string | null }[]>`
+ SELECT email, name FROM "User" WHERE id = ${user.id} AND "deletedAt" IS NULL FOR UPDATE`;
+ if (!sender) {
+ throw new TRPCError({ code: 'UNAUTHORIZED', message: 'Account not found' });
+ }
+ if (!sender.email) {
+ throw new TRPCError({
+ code: 'BAD_REQUEST',
+ message: 'Add an email address to send feedback',
+ });
+ }
+
+ const existing = await findExisting(tx, user.id, message, input.clientKey, now);
+ if (existing) {
+ return { feedback: existing, created: false };
+ }
+
+ const recentCount = await tx.feedback.count({
+ where: {
+ userId: user.id,
+ createdAt: { gte: new Date(now.getTime() - FEEDBACK_RATE_LIMIT.windowMs) },
+ },
+ });
+ if (isOverFeedbackLimit(recentCount)) {
+ throw new TRPCError({
+ code: 'TOO_MANY_REQUESTS',
+ message: 'Too much feedback at once. Try again in an hour.',
+ });
+ }
+
+ const feedback = await tx.feedback.create({
+ data: {
+ userId: user.id,
+ email: sender.email,
+ name: sender.name,
+ message,
+ source: input.source ?? fallbackSource,
+ // An empty version is no version
+ appVersion: input.appVersion ? input.appVersion : null,
+ clientKey: input.clientKey ?? null,
+ },
+ });
+ return { feedback, created: true };
+ });
+ } catch (error) {
+ if (!input.clientKey || !isUniqueViolation(error, 'clientKey')) {
+ throw error;
+ }
+
+ const existing = await db.feedback.findUnique({ where: { clientKey: input.clientKey } });
+ if (existing?.userId !== user.id || existing.message !== message) {
+ throw new TRPCError({ code: 'CONFLICT', message: 'Feedback key already used' });
+ }
+ result = { feedback: existing, created: false };
+ }
+
+ const { feedback, created } = result;
+ if (created) {
+ notify(feedback).catch((error) =>
+ console.error('Failed to email feedback', feedback.id, safeMailErrorDetails(error)),
+ );
+ }
+
+ return { id: feedback.id, created };
+};
diff --git a/src/server/mailErrors.ts b/src/server/mailErrors.ts
new file mode 100644
index 0000000..4ba7cee
--- /dev/null
+++ b/src/server/mailErrors.ts
@@ -0,0 +1,82 @@
+/**
+ * Pure helpers for SMTP send errors. Kept free of db and env imports so they can be unit tested.
+ */
+
+/** Our own code for "SMTP isn't configured", which never reaches a server. */
+export const MAIL_NOT_CONFIGURED = 'ENOTCONFIGURED';
+
+/** Failures that happen before the server could have accepted the message. */
+const DEFINITE_CODES = new Set([
+ MAIL_NOT_CONFIGURED,
+ 'EAUTH',
+ 'ENOAUTH',
+ 'EOAUTH2',
+ 'EENVELOPE',
+ 'EMESSAGE',
+ 'EDNS',
+ 'ETLS',
+ 'EREQUIRETLS',
+ 'ECONFIG',
+]);
+/** Timeouts while still connecting, before anything was sent. */
+const SETUP_TIMEOUTS = new Set(['CONNECT_TIMEOUT', 'GREETING_TIMEOUT', 'UPGRADE_TIMEOUT']);
+/** Socket errors that only happen while opening the connection. */
+const CONNECT_ERRORS = new Set([
+ 'ECONNREFUSED',
+ 'ENOTFOUND',
+ 'EHOSTUNREACH',
+ 'ENETUNREACH',
+ 'EAI_AGAIN',
+]);
+
+type SmtpError = {
+ code?: unknown;
+ originalCode?: unknown;
+ responseCode?: unknown;
+ timeoutType?: unknown;
+};
+
+const asSmtpError = (error: unknown): SmtpError =>
+ error && 'object' === typeof error ? (error as SmtpError) : {};
+
+const ERROR_CODE = /^E[A-Z0-9_]{1,30}$/;
+
+/**
+ * The parts of a send error that are safe to log or keep: nodemailer's error code and the SMTP
+ * reply code. Never the message, which can contain addresses or the server's reply text.
+ */
+export const safeMailErrorDetails = (error: unknown) => {
+ const { code, responseCode } = asSmtpError(error);
+ return {
+ code: 'string' === typeof code && ERROR_CODE.test(code) ? code : undefined,
+ responseCode:
+ 'number' === typeof responseCode && Number.isInteger(responseCode) ? responseCode : undefined,
+ };
+};
+
+/**
+ * Whether a send error means the message certainly wasn't accepted, so sending it again can't
+ * deliver it twice. Dropped connections and timeouts once connected are uncertain: the server may
+ * have accepted the message before the connection went.
+ */
+export const isDefiniteSendFailure = (error: unknown) => {
+ const { code, originalCode, responseCode, timeoutType } = asSmtpError(error);
+
+ // The server answered, and refused
+ if ('number' === typeof responseCode && 400 <= responseCode) {
+ return true;
+ }
+ if ('string' !== typeof code) {
+ return false;
+ }
+ if (DEFINITE_CODES.has(code)) {
+ return true;
+ }
+ if ('ETIMEDOUT' === code) {
+ return 'string' === typeof timeoutType && SETUP_TIMEOUTS.has(timeoutType);
+ }
+ if ('ESOCKET' === code) {
+ return 'string' === typeof originalCode && CONNECT_ERRORS.has(originalCode);
+ }
+ return false;
+};
diff --git a/src/server/mailer.ts b/src/server/mailer.ts
index 9d0d6c1..aa5db6c 100644
--- a/src/server/mailer.ts
+++ b/src/server/mailer.ts
@@ -1,13 +1,21 @@
-import { type User } from 'next-auth';
import nodemailer, { type Transporter } from 'nodemailer';
import { env } from '~/env';
+import { MAIL_NOT_CONFIGURED, isDefiniteSendFailure, safeMailErrorDetails } from './mailErrors';
import { sendToDiscord } from './service-notification';
let transporter: Transporter;
+let boundedTransporter: Transporter;
-const getTransporter = () => {
- if (transporter) return transporter;
+/**
+ * For sends a request waits on: gives up instead of hanging on a slow or silent server.
+ */
+const BOUNDED_TIMEOUTS = {
+ connectionTimeout: 10_000,
+ greetingTimeout: 10_000,
+ socketTimeout: 15_000,
+};
+const createTransporter = (timeouts?: typeof BOUNDED_TIMEOUTS) => {
const host = env.EMAIL_SERVER_HOST;
const port = parseInt(env.EMAIL_SERVER_PORT ?? '');
const user = env.EMAIL_SERVER_USER;
@@ -28,10 +36,30 @@ const getTransporter = () => {
tls: {
rejectUnauthorized: env.NODE_ENV !== 'development',
},
+ ...timeouts,
};
- transporter = nodemailer.createTransport(transport);
- return transporter;
+ return nodemailer.createTransport(transport);
+};
+
+const getTransporter = () => {
+ if (transporter) return transporter;
+
+ const created = createTransporter();
+ if (created) {
+ transporter = created;
+ }
+ return created;
+};
+
+const getBoundedTransporter = () => {
+ if (boundedTransporter) return boundedTransporter;
+
+ const created = createTransporter(BOUNDED_TIMEOUTS);
+ if (created) {
+ boundedTransporter = created;
+ }
+ return created;
};
export async function sendSignUpEmail(email: string, token: string, url: string) {
@@ -100,15 +128,116 @@ export async function sendInviteEmail(email: string, name: string) {
await sendMail(email, subject, text, html);
}
-export async function sendFeedbackEmail(feedback: string, user: User) {
- console.log('Received feedback from: ', user.email, 'Feedback: ', feedback);
+export type FeedbackEmailDetails = {
+ id: string;
+ message: string;
+ email: string;
+ name: string | null;
+ source: string;
+ appVersion: string | null;
+};
- if (!env.FEEDBACK_EMAIL) return;
+/**
+ * Tells us about new feedback. The subject keeps the `Feedback received on SplitPro` prefix that
+ * existing inbox filters match. Best effort: returns whether it was sent, and on failure logs and
+ * reports only the feedback id and error codes, never the recipient or the server's reply.
+ */
+export async function sendFeedbackEmail(
+ feedback: FeedbackEmailDetails,
+ deliver: typeof deliverMail = deliverMail,
+) {
+ if (!env.FEEDBACK_EMAIL) return false;
- const subject = `Feedback received on SplitPro from ${user.name}`;
- const text = `Feedback created by ${user.name} :\n\nFeedback: ${feedback}\n\nemail: ${user.email}`;
+ const sender = feedback.name ?? feedback.email;
+ const subject = `Feedback received on SplitPro from ${sender}`;
+ const version = feedback.appVersion ? ` ${feedback.appVersion}` : '';
+ const text = `Feedback created by ${sender} :\n\nFeedback: ${feedback.message}\n\nemail: ${feedback.email}\nsource: ${feedback.source}${version}\nfeedback id: ${feedback.id}`;
+ const html = `