mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-16 15:23:29 +00:00
feat(notifications): resolve a tap target per event and offer mood quick answers
This commit is contained in:
1 parent
9aadb29876
commit
f19dfc4572
21 files changed
+859
-21
No files matched your search
@@ -0,0 +1,152 @@
|
||||
/**
|
||||
* `POST /api/notifications/action/<token>` — a quick answer tapped inside a
|
||||
* notification.
|
||||
*
|
||||
* The caller is a notification app (ntfy's `http` action), not a browser
|
||||
* with a session, so the signed path segment is the whole credential. What
|
||||
* the token proves and how it is kept single-use is in
|
||||
* `src/lib/notifications/action-token.ts`; this route only applies it:
|
||||
*
|
||||
* 1. Rate-limit per client address BEFORE looking at the token, so the
|
||||
* route is no oracle for guessing one (the anonymous-surface limiter
|
||||
* collapses a broken proxy trust chain into one tight bucket).
|
||||
* 2. Verify the signature (constant-time) and the expiry.
|
||||
* 3. Re-check what may have changed since the reminder went out: the
|
||||
* account still exists and its mood module is still on.
|
||||
* 4. Write the mood entry under the reminder's nonce. The unique
|
||||
* `(userId, source, externalId)` key makes the first answer the only
|
||||
* one: a repeat tap, or a different answer on the same reminder, is
|
||||
* refused with 409 and writes nothing.
|
||||
*
|
||||
* The token travels in the path, so it is scrubbed from every log line by
|
||||
* the `PATH_SECRET_PATHS` entry for this prefix, and the route sits on the
|
||||
* URL-credential inventory in `session-surface-guard.test.ts`.
|
||||
*/
|
||||
import { NextRequest } from "next/server";
|
||||
|
||||
import { Prisma } from "@/generated/prisma/client";
|
||||
import { apiHandler } from "@/lib/api-handler";
|
||||
import { apiError, apiSuccess } from "@/lib/api-response";
|
||||
import { prisma } from "@/lib/db";
|
||||
import { annotate, getEvent } from "@/lib/logging/context";
|
||||
import { isModuleEnabled } from "@/lib/modules/gate";
|
||||
import { logTelegramMood } from "@/lib/mood/create-from-telegram";
|
||||
import {
|
||||
MOOD_QUICK_ACTIONS,
|
||||
quickActionExternalId,
|
||||
verifyActionToken,
|
||||
} from "@/lib/notifications/action-token";
|
||||
import { checkAuthSurfaceRateLimit, rateLimitHeaders } from "@/lib/rate-limit";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
/** Generous for one person tapping, tight for anyone enumerating. */
|
||||
const RATE_LIMIT = 20;
|
||||
const RATE_WINDOW_MS = 10 * 60 * 1000;
|
||||
|
||||
interface RouteContext {
|
||||
params: Promise<{ token: string }>;
|
||||
}
|
||||
|
||||
function isUniqueViolation(err: unknown): boolean {
|
||||
return (
|
||||
err instanceof Prisma.PrismaClientKnownRequestError && err.code === "P2002"
|
||||
);
|
||||
}
|
||||
|
||||
export const POST = apiHandler(
|
||||
async (request: NextRequest, context: RouteContext) => {
|
||||
annotate({ action: { name: "notification.action.invoke" } });
|
||||
|
||||
const rl = await checkAuthSurfaceRateLimit(
|
||||
request,
|
||||
"notification-action",
|
||||
RATE_LIMIT,
|
||||
RATE_WINDOW_MS,
|
||||
);
|
||||
if (!rl.allowed) {
|
||||
const response = apiError("Too many requests", 429);
|
||||
for (const [k, v] of Object.entries(rateLimitHeaders(rl))) {
|
||||
response.headers.set(k, v);
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
const { token } = await context.params;
|
||||
const verdict = verifyActionToken(token);
|
||||
if (!verdict.ok) {
|
||||
annotate({
|
||||
action: { name: "notification.action.refused" },
|
||||
meta: { reason: verdict.reason },
|
||||
});
|
||||
return verdict.reason === "expired"
|
||||
? apiError("This answer has expired", 410, {
|
||||
errorCode: "notification_action.expired",
|
||||
})
|
||||
: apiError("Invalid action", 401, {
|
||||
errorCode: "notification_action.invalid",
|
||||
});
|
||||
}
|
||||
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { id: verdict.userId },
|
||||
select: { id: true, timezone: true },
|
||||
});
|
||||
if (!user) {
|
||||
annotate({
|
||||
action: { name: "notification.action.refused" },
|
||||
meta: { reason: "unknown_user" },
|
||||
});
|
||||
return apiError("Invalid action", 401, {
|
||||
errorCode: "notification_action.invalid",
|
||||
});
|
||||
}
|
||||
getEvent()?.setAuth({
|
||||
user_id: user.id,
|
||||
auth_method: "notification_action_token",
|
||||
});
|
||||
|
||||
if (!(await isModuleEnabled(user.id, "mood"))) {
|
||||
annotate({
|
||||
action: { name: "notification.action.refused" },
|
||||
meta: { reason: "module_disabled" },
|
||||
});
|
||||
return apiError("Mood tracking is turned off", 403, {
|
||||
errorCode: "notification_action.module_disabled",
|
||||
});
|
||||
}
|
||||
|
||||
const score = MOOD_QUICK_ACTIONS[verdict.action];
|
||||
let created: boolean;
|
||||
try {
|
||||
const result = await logTelegramMood({
|
||||
userId: user.id,
|
||||
score,
|
||||
tz: user.timezone,
|
||||
externalId: quickActionExternalId(verdict.nonce),
|
||||
source: "NOTIFICATION",
|
||||
});
|
||||
created = result.created;
|
||||
} catch (err) {
|
||||
// Two taps raced past the existence check; the unique key let one in.
|
||||
if (!isUniqueViolation(err)) throw err;
|
||||
created = false;
|
||||
}
|
||||
|
||||
if (!created) {
|
||||
annotate({
|
||||
action: { name: "notification.action.refused" },
|
||||
meta: { reason: "already_used" },
|
||||
});
|
||||
return apiError("This reminder was already answered", 409, {
|
||||
errorCode: "notification_action.already_used",
|
||||
});
|
||||
}
|
||||
|
||||
annotate({
|
||||
action: { name: "notification.action.mood_logged" },
|
||||
meta: { action: verdict.action },
|
||||
});
|
||||
return apiSuccess({ logged: true, action: verdict.action });
|
||||
},
|
||||
);
|
||||
Reference in new issue
Block a user