Implement is_spam column

This commit is contained in:
swag 2022-01-02 01:31:33 -05:00
parent 2845fd7499
commit 26aa2ba19f
5 changed files with 35 additions and 16 deletions

View File

@ -51,4 +51,4 @@ Add the `-v` option for more verbose output
1. Input validation 1. Input validation
1. Filter URLs out of message body 1. Filter URLs out of message body
1. Add column to tag posts as spam or hidden 1. Set date in the DB rather than the model

View File

@ -67,14 +67,14 @@ any [qw{GET POST}], '/sign' => sub ($c) {
my $name = $c->param('name') || 'Anonymous'; my $name = $c->param('name') || 'Anonymous';
my $url = $c->param('url'); my $url = $c->param('url');
my $message = $c->param('message'); my $message = $c->param('message');
my $answer = $c->param('answer'); my $spam = $c->param('answer') ? 0 : 1;
if ($message && $answer) { if ($message) {
$c->message->create_post($name, $message, $url); $c->message->create_post($name, $message, $url, $spam);
$c->redirect_to('index'); $c->redirect_to('index');
} }
else { else {
$c->flash(error => 'Uh-oh!! Please try again.'); $c->flash(error => 'Message cannot be blank');
$c->redirect_to('sign'); $c->redirect_to('sign');
} }
} }
@ -106,7 +106,7 @@ app->secrets(app->config->{'secrets'}) || die $@;
app->message->max_posts(app->config->{'max_posts'}) app->message->max_posts(app->config->{'max_posts'})
if app->config->{'max_posts'}; if app->config->{'max_posts'};
app->pg->migrations->from_dir('migrations')->migrate(4); app->pg->migrations->from_dir('migrations')->migrate(5);
app->asset->store->paths(['assets']); app->asset->store->paths(['assets']);
app->asset->process('swagg.css', 'css/swagg.css'); app->asset->process('swagg.css', 'css/swagg.css');

View File

@ -20,43 +20,46 @@ sub get_posts($self, $this_page = undef) {
return $self->pg->db return $self->pg->db
->query(<<~'END_SQL', $row_count, $offset)->arrays(); ->query(<<~'END_SQL', $row_count, $offset)->arrays();
SELECT to_char(message_date, 'Dy Mon DD HH:MI:SS AM TZ YYYY'), SELECT TO_CHAR(message_date, 'Dy Mon DD HH:MI:SS AM TZ YYYY'),
visitor_name, visitor_name,
message, message,
homepage_url homepage_url
FROM messages FROM messages
WHERE NOT is_spam
ORDER BY message_date DESC ORDER BY message_date DESC
LIMIT ? OFFSET ?; LIMIT ? OFFSET ?;
END_SQL END_SQL
} }
else { else {
return $self->pg->db->query(<<~'END_SQL')->arrays() return $self->pg->db->query(<<~'END_SQL')->arrays()
SELECT to_char(message_date, 'Dy Mon DD HH:MI:SS AM TZ YYYY'), SELECT TO_CHAR(message_date, 'Dy Mon DD HH:MI:SS AM TZ YYYY'),
visitor_name, visitor_name,
message, message,
homepage_url homepage_url
FROM messages FROM messages
WHERE NOT is_spam
ORDER BY message_date DESC; ORDER BY message_date DESC;
END_SQL END_SQL
} }
} }
sub create_post($self, $name, $message, $url = undef) { sub create_post($self, $name, $message, $url = undef, $spam = 1) {
if ($url) { if ($url) {
$self->pg->db->query(<<~'END_SQL', $name, $message, $url) $self->pg->db->query(<<~'END_SQL', $name, $message, $url, $spam)
INSERT INTO messages ( INSERT INTO messages (
message_date, message_date,
visitor_name, visitor_name,
message, message,
homepage_url homepage_url,
is_spam
) )
VALUES (NOW(), ?, ?, ?); VALUES (NOW(), ?, ?, ?, ?);
END_SQL END_SQL
} }
else { else {
$self->pg->db->query(<<~'END_SQL', $name, $message) $self->pg->db->query(<<~'END_SQL', $name, $message, $spam)
INSERT INTO messages (message_date, visitor_name, message) INSERT INTO messages (message_date, visitor_name, message, is_spam)
VALUES (NOW(), ?, ?); VALUES (NOW(), ?, ?, ?);
END_SQL END_SQL
} }
@ -76,7 +79,11 @@ sub get_last_page($self) {
} }
sub get_post_count($self) { sub get_post_count($self) {
return $self->pg->db->query('SELECT count(*) FROM messages;')->text() return $self->pg->db->query(<<~'END_SQL')->text()
SELECT COUNT(*)
FROM messages
WHERE NOT is_spam;
END_SQL
} }
1; 1;

2
migrations/5/down.sql Normal file
View File

@ -0,0 +1,2 @@
ALTER TABLE messages
DROP COLUMN is_spam;

10
migrations/5/up.sql Normal file
View File

@ -0,0 +1,10 @@
ALTER TABLE messages
ADD is_spam BOOLEAN;
UPDATE messages
SET is_spam = FALSE
WHERE is_spam IS NULL;
ALTER TABLE messages
ALTER COLUMN is_spam
SET NOT NULL;