From 7357b9c7918fb056f87906710f9a2d0cbb8625f0 Mon Sep 17 00:00:00 2001 From: swag Date: Fri, 2 Jun 2023 14:46:58 -0400 Subject: [PATCH] Add routes for mods to view hidden posts --- lib/PostText.pm | 8 +++++ lib/PostText/Controller/Moderator.pm | 24 ++++++++++++++ lib/PostText/Model/Moderator.pm | 35 +++++++++++++++++++++ templates/moderator/remark_by_id.html.ep | 35 +++++++++++++++++++++ templates/moderator/thread_by_id.html.ep | 40 ++++++++++++++++++++++++ 5 files changed, 142 insertions(+) create mode 100644 templates/moderator/remark_by_id.html.ep create mode 100644 templates/moderator/thread_by_id.html.ep diff --git a/lib/PostText.pm b/lib/PostText.pm index 20e66b1..91993da 100644 --- a/lib/PostText.pm +++ b/lib/PostText.pm @@ -206,6 +206,10 @@ sub startup($self) { ->to('moderator#unhide_thread') ->name('unhide_thread'); + $mod_thread->get('/single/:thread_id', [thread_id => qr/\d+/]) + ->to('moderator#thread_by_id') + ->name('hidden_thread'); + my $mod_remark = $moderator->any('/remark'); $mod_remark->get('/unflag/:remark_id', [remark_id => qr/\d+/]) @@ -220,6 +224,10 @@ sub startup($self) { ->to('moderator#unhide_remark') ->name('unhide_remark'); + $mod_remark->get('/single/:remark_id', [remark_id => qr/\d+/]) + ->to('moderator#remark_by_id') + ->name('hidden_remark'); + # Admin my $mod_admin = $moderator->under('/admin')->to('moderator#admin_check'); diff --git a/lib/PostText/Controller/Moderator.pm b/lib/PostText/Controller/Moderator.pm index 5d152f6..7db0c10 100644 --- a/lib/PostText/Controller/Moderator.pm +++ b/lib/PostText/Controller/Moderator.pm @@ -330,4 +330,28 @@ sub admin_check($self) { return $self->redirect_to('mod_login'), undef; } +sub thread_by_id($self) { + my $thread_id = $self->param('thread_id'); + my $thread = $self->thread->by_id($thread_id); + + $self->stash(thread => $thread); + + $self->stash(status => 404, error => 'Thread not found 🤷') + unless keys %{$thread}; + + $self->render; +} + +sub remark_by_id($self) { + my $remark_id = $self->param('remark_id'); + my $remark = $self->remark->by_id($remark_id); + + $self->stash(remark => $remark); + + $self->stash(error => 'Remark not found 🤷') + unless keys %{$remark}; + + $self->render; +} + 1; diff --git a/lib/PostText/Model/Moderator.pm b/lib/PostText/Model/Moderator.pm index 7dc6b05..ace354e 100644 --- a/lib/PostText/Model/Moderator.pm +++ b/lib/PostText/Model/Moderator.pm @@ -4,6 +4,8 @@ use Mojo::Base -base, -signatures; has [qw{pg authenticator}]; +has date_format => 'Dy, FMDD Mon YYYY HH24:MI:SS TZHTZM'; + sub create($self, $name, $email, $password) { my $password_hash = $self->authenticator->hash_password($password); @@ -224,4 +226,37 @@ sub demote($self, $email) { END_SQL } +sub thread_by_id($self, $thread_id) { + my $date_format = $self->date_format; + + $self->pg->db->query(<<~'END_SQL', $date_format, $thread_id)->hash; + SELECT t.thread_id AS id, + TO_CHAR(t.thread_date, ?) AS date, + t.thread_author AS author, + t.thread_title AS title, + t.thread_body AS body, + COUNT(r.*) AS remark_tally, + t.bump_tally AS bump_tally + FROM threads AS t + LEFT JOIN remarks AS r + ON t.thread_id = r.thread_id + WHERE t.thread_id = ? + GROUP BY t.thread_id; + END_SQL +} + +sub remark_by_id($self, $remark_id) { + my $date_format = $self->date_format; + + $self->pg->db->query(<<~'END_SQL', $date_format, $remark_id)->hash; + SELECT remark_id AS id, + TO_CHAR(remark_date, ?) AS date, + remark_author AS author, + remark_body AS body, + thread_id + FROM remarks + WHERE remark_id = ?; + END_SQL +} + 1; diff --git a/templates/moderator/remark_by_id.html.ep b/templates/moderator/remark_by_id.html.ep new file mode 100644 index 0000000..a213941 --- /dev/null +++ b/templates/moderator/remark_by_id.html.ep @@ -0,0 +1,35 @@ +% layout 'default'; +% title my $remark_id = $remark->{'id'} ? "Remark #$remark_id" : '?'; +

<%= title %>

+<% if (keys %{$remark}) { =%> +
+
+

+ <%= $remark->{'date'} %> + #<%= $remark->{'id'} %> +

+ +
+ <%== markdown $remark->{'body'} =%> +
+ + <% if (is_mod) { =%> + + <% } =%> +
+
+<% } =%> diff --git a/templates/moderator/thread_by_id.html.ep b/templates/moderator/thread_by_id.html.ep new file mode 100644 index 0000000..6ea7359 --- /dev/null +++ b/templates/moderator/thread_by_id.html.ep @@ -0,0 +1,40 @@ +% layout 'default'; +% title my $thread_id = $thread->{'id'} ? "Thread #$thread_id" : '?'; +

<%= title %>

+<% if (keys %{$thread}) { =%> +
+
+

+ <%= $thread->{'title'} %> + #<%= $thread->{'id'} %> +

+ + +
+ <%== markdown $thread->{'body'} =%> +
+ + <% if (is_mod) { =%> + + <% } =%> +
+
+<% } =%>