diff --git a/lib/PostText.pm b/lib/PostText.pm index 17145e0..29e656c 100644 --- a/lib/PostText.pm +++ b/lib/PostText.pm @@ -145,14 +145,25 @@ sub startup($self) { my $moderator = $r->under('/moderator', sub ($c) { return 1 if $c->is_mod; - $c->redirect_to('mod_login'); # Return false otherwise a body is rendered with the redirect... - return undef; + return $c->redirect_to('mod_login'), undef; }); $moderator->get('/flagged') ->to('moderator#flagged') ->name('flagged_list'); + + $moderator->get('/unflag/:thread_id', [thread_id => qr/\d+/]) + ->to('moderator#unflag') + ->name('unflag_thread'); + + $moderator->get('/hide/:thread_id', [thread_id => qr/\d+/]) + ->to('moderator#hide') + ->name('hide_thread'); + + $moderator->get('/unhide/:thread_id', [thread_id => qr/\d+/]) + ->to('moderator#unhide') + ->name('unhide_thread'); } 1; diff --git a/lib/PostText/Controller/Moderator.pm b/lib/PostText/Controller/Moderator.pm index b7c987e..7977cea 100644 --- a/lib/PostText/Controller/Moderator.pm +++ b/lib/PostText/Controller/Moderator.pm @@ -57,4 +57,35 @@ sub logout($self) { $self->redirect_to('threads_list'); } +sub unflag($self) { + my $thread_id = $self->param('thread_id'); + my $redirect_url = $self->url_for('threads_list')->fragment('info')->to_abs; + + $self->moderator->unflag($thread_id); + $self->flash(info => "Thread #$thread_id has been unflagged. ◀️"); + + $self->redirect_to($redirect_url); +} + +sub hide($self) { + my $thread_id = $self->param('thread_id'); + my $redirect_url = $self->url_for(single_thread => thread_id => $thread_id) + ->fragment('info')->to_abs; + + $self->moderator->hide($thread_id); + $self->flash(info => "Thread #$thread_id has been hidden. 🫥"); + + $self->redirect_to($redirect_url); +} + +sub unhide($self) { + my $thread_id = $self->param('thread_id'); + my $redirect_url = $self->url_for('threads_list')->fragment('info')->to_abs; + + $self->moderator->unhide($thread_id); + $self->flash(info => "Thread #$thread_id has been unhidden. ⏪"); + + $self->redirect_to($redirect_url); +} + 1; diff --git a/lib/PostText/Model/Moderator.pm b/lib/PostText/Model/Moderator.pm index 63c7cb4..a221a29 100644 --- a/lib/PostText/Model/Moderator.pm +++ b/lib/PostText/Model/Moderator.pm @@ -35,4 +35,29 @@ sub get_name($self, $mod_id) { END_SQL } +sub unflag($self, $thread_id) { + $self->pg->db->query(<<~'END_SQL', $thread_id) + UPDATE threads + SET flagged_status = FALSE + WHERE thread_id = ?; + END_SQL +} + +sub hide($self, $thread_id) { + $self->pg->db->query(<<~'END_SQL', $thread_id) + UPDATE threads + SET hidden_status = TRUE, + flagged_status = FALSE + WHERE thread_id = ?; + END_SQL +} + +sub unhide($self, $thread_id) { + $self->pg->db->query(<<~'END_SQL', $thread_id) + UPDATE threads + SET hidden_status = FALSE + WHERE thread_id = ?; + END_SQL +} + 1; diff --git a/lib/PostText/Model/Thread.pm b/lib/PostText/Model/Thread.pm index db33d6e..d8b6598 100644 --- a/lib/PostText/Model/Thread.pm +++ b/lib/PostText/Model/Thread.pm @@ -95,12 +95,4 @@ sub flag($self, $thread_id) { END_SQL } -sub unflag($self, $thread_id) { - $self->pg->db->query(<<~'END_SQL', $thread_id) - UPDATE threads - SET flagged_status = FALSE - WHERE thread_id = ?; - END_SQL -} - 1; diff --git a/t/moderator.t b/t/moderator.t index a4b639b..532c9f0 100644 --- a/t/moderator.t +++ b/t/moderator.t @@ -39,6 +39,23 @@ subtest Login => sub { ->status_is(302) ->header_like(Location => qr{moderator/flagged}); + # Do these subs while logged in + subtest Flag => sub { + $t->get_ok('/moderator/unflag/1') + ->status_is(302) + ->header_like(Location => qr{thread/list}); + }; + + subtest Hide => sub { + $t->get_ok('/moderator/hide/1') + ->status_is(302) + ->header_like(Location => qr{thread/single/1}); + + $t->get_ok('/moderator/unhide/1') + ->status_is(302) + ->header_like(Location => qr{thread/list}); + }; + $t->get_ok('/logout') ->status_is(302) ->header_like(Location => qr{thread/list});