fix: detect spam
This commit is contained in:
parent
f395b7cdbc
commit
3743ae8f5d
5 changed files with 953 additions and 2 deletions
|
|
@ -15,7 +15,8 @@
|
|||
"serve": "cross-env NODE_OPTIONS=--no-deprecation next start",
|
||||
"storybook": "storybook dev -p 6006",
|
||||
"build-storybook": "storybook build",
|
||||
"chromatic": "npx chromatic --project-token=chpt_70d6a2e05af185a"
|
||||
"chromatic": "npx chromatic --project-token=chpt_70d6a2e05af185a",
|
||||
"test": "vitest"
|
||||
},
|
||||
"dependencies": {
|
||||
"@payloadcms/db-postgres": "^3.22.0",
|
||||
|
|
@ -25,6 +26,7 @@
|
|||
"classnames": "^2.5.1",
|
||||
"cross-env": "^7.0.3",
|
||||
"graphql": "^16.8.1",
|
||||
"languagedetect": "^2.0.0",
|
||||
"moment": "^2.30.1",
|
||||
"next": "15.1.2",
|
||||
"payload": "^3.22.0",
|
||||
|
|
@ -56,7 +58,8 @@
|
|||
"eslint-config-prettier": "^9.1.0",
|
||||
"eslint-plugin-storybook": "^0.8.0",
|
||||
"storybook": "^8.2.9",
|
||||
"typescript": "5.5.4"
|
||||
"typescript": "5.5.4",
|
||||
"vitest": "^3.1.1"
|
||||
},
|
||||
"engines": {
|
||||
"node": "^18.20.2 || >=20.9.0"
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
import { z } from 'zod'
|
||||
import { Resend } from 'resend'
|
||||
import { isSpam } from '@/utils/detectSpam'
|
||||
|
||||
/**
|
||||
* Send email through Resend API
|
||||
|
|
@ -32,6 +33,12 @@ export async function send(prevState: any, formData: FormData) {
|
|||
}
|
||||
}
|
||||
|
||||
if (isSpam(validatedFields.data.message)) {
|
||||
return {
|
||||
message: "Ihre Nachricht wurde als unerwünschte Werbung eingestuft und nicht gesendet."
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const resend = new Resend(process.env.RESEND_API_KEY);
|
||||
const resp = await resend.emails.send({
|
||||
|
|
|
|||
28
src/utils/detectSpam.test.ts
Normal file
28
src/utils/detectSpam.test.ts
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
import { isSpam } from './detectSpam';
|
||||
import { describe, it, expect } from 'vitest'
|
||||
|
||||
describe('isSpam function', () => {
|
||||
it('should classify a message containing a URL as spam', () => {
|
||||
const spamMessage = "Check out this site for amazing deals https://www.spamsite.com";
|
||||
const result = isSpam(spamMessage);
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
|
||||
it('should not classify a German message without a URL as spam', () => {
|
||||
const nonSpamMessage = "Das ist eine Nachricht ohne URL";
|
||||
const result = isSpam(nonSpamMessage);
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
|
||||
it('should not classify a English message without a URL as spam', () => {
|
||||
const nonSpamMessage = "Hi, this is some message";
|
||||
const result = isSpam(nonSpamMessage);
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
|
||||
it('should not classify a German message with a URL as spam', () => {
|
||||
const spamMessage = "Schauen Sie sich diese Website für tolle Angebote an https://www.spamsite.com";
|
||||
const result = isSpam(spamMessage);
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
});
|
||||
21
src/utils/detectSpam.ts
Normal file
21
src/utils/detectSpam.ts
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
import LanguageDetect from 'languagedetect'
|
||||
|
||||
/**
|
||||
* This function determines whether a given message can be classified as spam.
|
||||
*
|
||||
* A message is classified as spam based on the following criteria:
|
||||
* 1. The message contains a URL i.e., it includes `https://`.
|
||||
* 2. The message is not written in the German language.
|
||||
*
|
||||
* Returns a boolean value indicating whether the message is spam (`true`) or not (`false`).
|
||||
*/
|
||||
export const isSpam = (message: string): boolean => {
|
||||
|
||||
if (message.includes('https://')) {
|
||||
const lngDetector = new LanguageDetect();
|
||||
const language = lngDetector.detect(message)[0][0];
|
||||
return language !== 'german';
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
Loading…
Reference in a new issue