Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: identify spam submissions #76

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion apps/web/src/app/(main)/form/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ export default async function FormPage({ params }: { params: { id: string } }) {
<>
<div className="flex w-full items-center justify-between">
<span className="text-muted-foreground">
Total Submissions: {formSubmissions.length}
{formSubmissions.length} Submissions with{' '}
{
formSubmissions.filter((submission) => submission.isSpam)
.length
}{' '}
Spam
</span>
<ExportSubmissionsDropDownButton
submissions={formSubmissions as FormData[]}
Expand Down
5 changes: 5 additions & 0 deletions apps/web/src/app/(main)/form/[id]/submissions-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
TableHeader,
TableRow,
} from '@formbase/ui/primitives/table';
import { cn } from '@formbase/ui/utils/cn';
import { formatFileName } from '@formbase/utils';

import { api } from '~/lib/trpc/react';
Expand Down Expand Up @@ -314,6 +315,10 @@ export function SubmissionsTable({
<TableRow
key={row.id}
data-state={row.getIsSelected() && 'selected'}
className={cn({
'bg-red-100 hover:bg-red-200 dark:bg-red-800 dark:hover:bg-red-700':
row.original.isSpam,
})}
>
{row.getVisibleCells().map((cell) => (
<TableCell key={cell.id}>
Expand Down
12 changes: 10 additions & 2 deletions apps/web/src/app/api/s/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { assignFileOrImage, uploadFileFromBlob } from '~/lib/upload-file';
type Json = string | number | boolean | null | { [key: string]: Json } | Json[];

async function getFormData(request: Request): Promise<{
data: Record<string, Blob | string | undefined>;
data: Record<string, Blob | string | undefined> & { _gotcha?: string };
source: 'formData' | 'json';
}> {
try {
Expand Down Expand Up @@ -88,14 +88,22 @@ export async function POST(
if (source === 'formData')
await processFileUploads(formData, formData as unknown as FormData);

const formDataKeys = Object.keys(formData);
const formDataKeys = Object.keys(formData).filter(
(key) => key !== '_gotcha',
);
const formKeys = form.keys;
const updatedKeys = [...new Set([...formKeys, ...formDataKeys])];

const submissionIsSpam = !!formData._gotcha;
if (submissionIsSpam) delete formData._gotcha;

console.log('Submission is spam: ', submissionIsSpam);

await api.formData.setFormData({
data: formData as Json,
formId,
keys: updatedKeys,
isSpam: submissionIsSpam,
});

void handleEmailNotifications(form, formId);
Expand Down
1 change: 1 addition & 0 deletions packages/api/routers/formData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export const formDataRouter = createTRPCRouter({
data: input.data,
formId: input.formId,
id: generateId(15),
isSpam: input.isSpam ?? false,
createdAt: new Date(),
});

Expand Down
1 change: 1 addition & 0 deletions packages/db/drizzle/0001_blushing_dust.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "form_datas" ADD COLUMN "is_spam" boolean DEFAULT false NOT NULL;
Loading
Loading