Enhance spam detection logic in contact form API

- Improved spam detection by integrating Gemini AI and heuristic checks, allowing for more accurate identification of spam messages.
- Updated logging to specify the source of spam detection (Gemini or heuristic) for better debugging and monitoring.
- Maintained existing functionality while enhancing the overall user experience in the contact form submission process.
This commit is contained in:
becarta
2025-06-12 23:51:58 +02:00
parent 5ceb3491c7
commit 954eaddc81

View File

@@ -211,16 +211,20 @@ export const POST: APIRoute = async ({ request, clientAddress }) => {
errors.disclaimer = 'Please check the required consent box before submitting';
}
// Check for spam
if (isSpam(message, name, email)) {
errors.spam = 'Your message was flagged as potential spam. Please revise your message and try again.';
}
// Gemini AI spam detection
// Gemini AI spam detection FIRST
let spamDetected = false;
let spamReason = '';
if (await isSpamWithGemini(message)) {
spamDetected = true;
spamReason = 'Gemini';
} else if (isSpam(message, name, email)) {
spamDetected = true;
spamReason = 'heuristic';
}
if (spamDetected) {
const token = jwt.sign({ email, message }, MANUAL_REVIEW_SECRET, { expiresIn: '1h' });
console.warn(
`[SPAM DETECTED by Gemini]`,
`[SPAM DETECTED by ${spamReason === 'Gemini' ? 'Gemini' : 'heuristic'}]`,
{ name, email, message, ip: request.headers.get('x-forwarded-for') }
);
return new Response(