From 6eae4e0fe44d72114e00fad1345463e478c6c130 Mon Sep 17 00:00:00 2001 From: Lucas Date: Sat, 18 Feb 2023 19:10:33 +0000 Subject: [PATCH] backend: implement main section --- lib/PoorBooru.pm | 38 ++++++++++++++++++++++++++++++++++++-- views/gallery.tt | 16 ++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/lib/PoorBooru.pm b/lib/PoorBooru.pm index fafbd30..b496ee0 100644 --- a/lib/PoorBooru.pm +++ b/lib/PoorBooru.pm @@ -21,6 +21,25 @@ sub http_tiny () ); } +sub gallery_nav_pager ($$) +{ + my ($p, $uri) = @_; + + my (%pager, %res); + %pager = %$p; + $res{first} = $pager{first} != $pager{cur} ? + uri_for($uri, { page => $pager{first} }) : undef; + $res{prev} = defined($pager{prev}) && $pager{prev} != $pager{first} ? + uri_for($uri, { page => $pager{prev} }) : undef; + $res{cur} = $pager{cur}; + $res{next} = defined($pager{next}) && $pager{next} != $pager{last} ? + uri_for($uri, { page => $pager{next} }) : undef; + $res{last} = $pager{last} != $pager{cur} ? + uri_for($uri, { page => $pager{last} }) : undef; + + return \%res; +} + hook before_template_render => sub { my $tokens = shift; @@ -32,8 +51,23 @@ hook before_template_render => sub { }; get "/" => sub { - template "index" => { - title => "main", + my $page = query_parameters->get("page") // 1; + send_error("Invalid page number", 400) if $page !~ /^[1-9][0-9]*$/; + + my $res = http_tiny()->get("$POORBOORU_API/media?page=$page"); + send_error("API error", 500) if !$res->{success}; + + my $data = decode_json($res->{content}); + my @media = map +( { + # XXX point to a cache + image_src => "$POORBOORU_API/download/$_", + view_uri => uri_for("/view/$_"), + } ), @{$data->{media}}; + + template "gallery" => { + title => "PoorBooru", + media => \@media, + pager => gallery_nav_pager($data->{pager}, "/"), }; }; diff --git a/views/gallery.tt b/views/gallery.tt index ef34cbb..3930d49 100644 --- a/views/gallery.tt +++ b/views/gallery.tt @@ -7,3 +7,19 @@ [% END -%] + +