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: Add basic spam filtering to Guestbook #558

Merged
merged 6 commits into from
Dec 18, 2024
Merged
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
1 change: 1 addition & 0 deletions db/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const Guestbook = defineTable({
url: column.text({ optional: true }),
timestamp: column.date({ default: NOW }),
theme: column.number(),
isSpam: column.boolean({ optional: true }),
},
});

Expand Down
22 changes: 19 additions & 3 deletions src/pages/guestbook.astro
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
---
import { Guestbook as GuestbookDB, count, db, desc } from "astro:db";
import {
Guestbook as GuestbookDB,
count,
db,
desc,
eq,
isNull,
or,
} from "astro:db";

import { Icon } from "src/components/Icon/Icon";
import Center from "../components/Center.astro";
import Notecard from "../components/Notecard/Notecard.astro";
Expand All @@ -22,7 +31,10 @@ const baseEntriesPerPage = 24;
const entriesPerPage =
currentPage === 1 ? baseEntriesPerPage - 1 : baseEntriesPerPage;

const entryCount = await db.select({ count: count() }).from(GuestbookDB);
const entryCount = await db
.select({ count: count() })
.from(GuestbookDB)
.where(or(eq(GuestbookDB.isSpam, false), isNull(GuestbookDB.isSpam)));
const totalEntries = entryCount[0].count;
// Calculate total pages accounting for one less entry on first page
const remainingEntries = Math.max(0, totalEntries - 1); // Subtract one for composer
Expand Down Expand Up @@ -57,9 +69,12 @@ if (Astro.request.method === "POST") {
typeof url === "string" &&
typeof theme === "string"
) {
// Extremely basic spam filtering to protect against bots
const isSpam = content.includes("<a href") || content.includes("href=");

await db
.insert(GuestbookDB)
.values({ author, content, url, theme: Number.parseInt(theme) });
.values({ author, content, url, theme: Number.parseInt(theme), isSpam });

return Astro.redirect("/guestbook");
}
Expand All @@ -68,6 +83,7 @@ if (Astro.request.method === "POST") {
const guestbook = await db
.select()
.from(GuestbookDB)
.where(or(eq(GuestbookDB.isSpam, false), isNull(GuestbookDB.isSpam)))
.orderBy(desc(GuestbookDB.timestamp))
.limit(entriesPerPage)
.offset(offset);
Expand Down
Loading