Report 404s better

This commit is contained in:
swagg boi 2023-05-28 02:07:09 -04:00
parent 65bb0ef5a0
commit f8bdcc9201
7 changed files with 50 additions and 20 deletions

View file

@ -58,7 +58,9 @@ Run the tests locally (against development environment):
## TODOs ## TODOs
1. Report 404s better (thread by id and remark by id) 1. Clean-up 404 stuff and variable checking (what's up with
`$last_page` checking?)
1. Don't allow non-mods to view hidden posts at all
1. "All new posts flagged" mode (require approval for new posts) 1. "All new posts flagged" mode (require approval for new posts)
1. Tests for mod-only user? 1. Tests for mod-only user?

View file

@ -6,9 +6,15 @@ sub by_id($self) {
my $remark_id = $self->param('remark_id'); my $remark_id = $self->param('remark_id');
my $remark = $self->remark->by_id($remark_id); my $remark = $self->remark->by_id($remark_id);
$self->stash(status => 404) unless $remark->{'id'}; if (my $remark_id = $remark->{'id'}) {
$self->stash(remark => $remark)
$self->stash(remark => $remark); }
else {
$self->stash(
remark => {},
error => 'Remark not found 🤷'
)
}
$self->render; $self->render;
} }

View file

@ -64,13 +64,19 @@ sub by_id($self) {
$self->stash( $self->stash(
thread => {}, thread => {},
remarks => [], remarks => [],
status => 404 status => 404,
error => 'Thread not found 🤷'
) )
} }
# Check for remarks or thread page number to make sure # Check for remarks or thread page number to make sure
# remark->by_page_for did its job # remark->by_page_for did its job
$self->stash(status => 404) unless $remarks->[0] || 1 >= $this_page; unless ((my $first_remark = $remarks->[0]) || 1 >= $this_page) {
$self->stash(
status => 404,
error => 'Page not found 🕵️'
)
}
$self->render; $self->render;
} }
@ -81,14 +87,24 @@ sub by_page($self) {
my $last_page = $self->thread->last_page; my $last_page = $self->thread->last_page;
my $threads = $self->thread->by_page($this_page); my $threads = $self->thread->by_page($this_page);
$self->stash(status => 404) unless $threads->[0]; if (my $first_thread = $threads->[0]) {
$self->stash(
$self->stash( threads => $threads,
threads => $threads, this_page => $this_page,
this_page => $this_page, last_page => $last_page,
last_page => $last_page, base_path => $base_path
base_path => $base_path )
); }
else {
$self->stash(
threads => [],
this_page => undef,
last_page => undef,
base_path => undef,
status => 404,
error => 'Page not found 🕵️'
)
}
$self->render; $self->render;
} }

View file

@ -38,7 +38,7 @@ subtest 'View single thread', sub {
->text_like(h2 => qr/Thread #1/); ->text_like(h2 => qr/Thread #1/);
$t->get_ok('/thread/single/65536')->status_is(404) $t->get_ok('/thread/single/65536')->status_is(404)
->text_like(h2 => qr/Thread #/); ->text_like(p => qr/Thread not found/);
}; };
subtest 'Threads feed', sub { subtest 'Threads feed', sub {

View file

@ -1,11 +1,12 @@
% layout 'default'; % layout 'default';
% title "Remark #$remark->{'id'}"; % title my $remark_id = $remark->{'id'} ? "Remark #$remark_id" : '?';
<h2 class="page-title"><%= title %></h2> <h2 class="page-title"><%= title %></h2>
<% if (my $remark_id = $remark->{'id'}) { =%>
<main class="pager"> <main class="pager">
<article class="post"> <article class="post">
<h4 class="post__title"> <h4 class="post__title">
<%= $remark->{'date'} %> <%= $remark->{'date'} %>
<span class="post__id">#<%= $remark->{'id'} %></span> <span class="post__id">#<%= $remark_id %></span>
</h4> </h4>
<h5 class="post__author"><%= $remark->{'author'} %></h5> <h5 class="post__author"><%= $remark->{'author'} %></h5>
<div class="post__body"> <div class="post__body">
@ -31,3 +32,4 @@
<% } =%> <% } =%>
</article> </article>
</main> </main>
<% } =%>

View file

@ -1,11 +1,12 @@
% layout 'default'; % layout 'default';
% title "Thread #$thread->{'id'}"; % title my $thread_id = $thread->{'id'} ? "Thread #$thread_id" : '?';
<h2 class="page-title"><%= title %></h2> <h2 class="page-title"><%= title %></h2>
<% if (my $thread_id = $thread->{'id'}) { =%>
<main class="pager"> <main class="pager">
<article class="post"> <article class="post">
<h3 class="post__title"> <h3 class="post__title">
<%= $thread->{'title'} %> <%= $thread->{'title'} %>
<span class="post__id">#<%= $thread->{'id'} %></span> <span class="post__id">#<%= $thread_id %></span>
</h3> </h3>
<h4 class="post__date"><%= $thread->{'date'} %></h4> <h4 class="post__date"><%= $thread->{'date'} %></h4>
<h5 class="post__author"><%= $thread->{'author'} %></h5> <h5 class="post__author"><%= $thread->{'author'} %></h5>
@ -36,6 +37,7 @@
<% } =%> <% } =%>
</article> </article>
</main> </main>
<% } =%>
<% if (my $first_remark = $remarks->[0]) { =%> <% if (my $first_remark = $remarks->[0]) { =%>
<section class="pager" id="remarks"> <section class="pager" id="remarks">
<h3 class="pager__title">Remarks</h3> <h3 class="pager__title">Remarks</h3>

View file

@ -1,6 +1,7 @@
% layout 'default'; % layout 'default';
% title 'Threads List'; % title 'Threads List';
<h2 class="page-title"><%= title %></h2> <h2 class="page-title"><%= title %></h2>
<% if (my $first_thread = $threads->[0]) { =%>
<main class="pager"> <main class="pager">
<% for my $thread (@{$threads}) { =%> <% for my $thread (@{$threads}) { =%>
<article class="post"> <article class="post">
@ -36,5 +37,6 @@
<nav class="pager__nav"> <nav class="pager__nav">
<%= pagination $this_page, $last_page, ($base_path . '/{page}') %> <%= pagination $this_page, $last_page, ($base_path . '/{page}') %>
</nav> </nav>
<% } =%> <% } =%>
</main> </main>
<% } =%>