From f4c9d251393f69434dca26c0024eb4071d976fd2 Mon Sep 17 00:00:00 2001 From: swag Date: Tue, 13 Jun 2023 20:22:07 -0400 Subject: [PATCH] Confirmation for bump_thread --- lib/PostText/Controller/Thread.pm | 21 ++++++++++++++++----- t/thread.t | 5 +++++ templates/thread/bump.html.ep | 17 +++++++++++++++++ templates/thread/flag.html.ep | 2 +- 4 files changed, 39 insertions(+), 6 deletions(-) create mode 100644 templates/thread/bump.html.ep diff --git a/lib/PostText/Controller/Thread.pm b/lib/PostText/Controller/Thread.pm index 780a5b7..61b0db7 100644 --- a/lib/PostText/Controller/Thread.pm +++ b/lib/PostText/Controller/Thread.pm @@ -140,13 +140,24 @@ sub feed($self) { sub bump($self) { my $thread_id = $self->param('thread_id'); + my $v = $self->validation; - $self->thread->bump($thread_id); - $self->flash(info => "Thread #$thread_id has been bumped. 🔝"); + $v->optional(captcha => 'trim')->size(4, 4)->like(qr/bump/i); - $self->redirect_to( - $self->url_for('threads_list')->fragment('info')->to_abs - ); + if ($v->is_valid) { + my $redirect_url = + $self->url_for('threads_list')->fragment('info')->to_abs; + + $self->thread->bump($thread_id); + $self->flash(info => "Thread #$thread_id has been bumped. 🔝"); + + return $self->redirect_to($redirect_url); + } + elsif ($v->has_error) { + $self->stash(status => 400) + } + + $self->render; } sub flag($self) { diff --git a/t/thread.t b/t/thread.t index 843ce41..844bc6c 100644 --- a/t/thread.t +++ b/t/thread.t @@ -87,6 +87,10 @@ subtest 'Bumping thread', sub { $t->get_ok('/thread/bump/1')->status_is(200) ->element_exists('p[class="stash-with-info"]') ->text_like(p => qr/Thread #1 has been bumped/); + + $t->get_ok('/thread/bump/1' )->status_is(200); + $t->get_ok('/thread/bump/65536')->status_is(404); + $t->get_ok('/thread/bump/1', form => {captcha => 'bump'})->status_is(400); }; subtest 'Flagging thread', sub { @@ -100,6 +104,7 @@ subtest 'Flagging thread', sub { $t->get_ok('/thread/flag/1' )->status_is(200); $t->get_ok('/thread/flag/65536')->status_is(404); + $t->get_ok('/thread/flag/1', form => {captcha => 'flag'})->status_is(400); }; done_testing; diff --git a/templates/thread/bump.html.ep b/templates/thread/bump.html.ep new file mode 100644 index 0000000..1b9d77e --- /dev/null +++ b/templates/thread/bump.html.ep @@ -0,0 +1,17 @@ +% layout 'default'; +% title $thread_id ? "Bump Thread #$thread_id" : '?'; +

<%= title %>

+<% if ($thread_id) { =%> +
+
+ <% if (my $error = validation->error('captcha')) { =%> +

Must be between <%= $error->[2] %> + and <%= $error->[3] %> characters.

+ <% } =%> + <%= label_for captcha => "Enter the word 'bump' to confirm:" %> + <%= text_field captcha => id => 'captcha' %> +
+ + +
+<% } =%> diff --git a/templates/thread/flag.html.ep b/templates/thread/flag.html.ep index 8c84e3a..65a0296 100644 --- a/templates/thread/flag.html.ep +++ b/templates/thread/flag.html.ep @@ -11,7 +11,7 @@ <%= label_for captcha => "Enter the word 'flag' to confirm:" %> <%= text_field captcha => id => 'captcha' %> - + <% } =%>