backend: implement /tags and /tag/:tag_id_or_name

This commit is contained in:
Lucas 2023-03-08 20:44:54 +00:00
parent ff017f924b
commit 02c7c3c0db
4 changed files with 71 additions and 0 deletions

View File

@ -21,6 +21,9 @@ sub startup ($self)
my $r = $self->routes; my $r = $self->routes;
$r->get("/")->to("gallery#index"); $r->get("/")->to("gallery#index");
$r->get("/tags")->to("tags#list");
$r->get("/tag/:tag_id_or_name")->to("tags#show");
} }
1; 1;

View File

@ -5,6 +5,7 @@ use warnings;
use Mojo::Base "Mojolicious::Controller"; use Mojo::Base "Mojolicious::Controller";
use Mojo::JSON qw(decode_json); use Mojo::JSON qw(decode_json);
use SeaweedFS::FID;
sub index ($self) sub index ($self)
{ {

View File

@ -0,0 +1,60 @@
package PoorBooru::Backend::Controller::Tags;
use v5.36;
use strict;
use warnings;
use Mojo::Base "Mojolicious::Controller";
use Mojo::JSON qw(decode_json);
use SeaweedFS::FID;
sub list ($self)
{
$self->title("tags");
my $v = $self->validation;
my $page = $v->optional("page")->num(1, undef)->param // 1;
return $self->render(status => 404) if $v->has_error;
my $poorbooru_api = $self->config("poorbooru_api");
my $ua = Mojo::UserAgent->new;
my $res = $ua->get("$poorbooru_api/tags?page=$page")->result;
return $self->render(status => $res->code) if $res->is_error;
my @tags = map +{
name => $_->{name},
count => $_->{count},
uri => $self->url_for("/tag/$_->{name}"),
}, @{$res->json->{tags}};
return $self->render(template => "tags", tags => \@tags);
};
sub show ($self)
{
$self->title("tags");
my $v = $self->validation;
my $page = $v->optional("page")->num(1, undef)->param // 1;
return $self->render(status => 404) if $v->has_error;
my $tag_id_or_name = $self->stash("tag_id_or_name");
my $poorbooru_api = $self->config("poorbooru_api");
my $ua = Mojo::UserAgent->new;
my $res = $ua->get("$poorbooru_api/tag/$tag_id_or_name?page=$page")
->result;
return $self->render(status => $res->code) if $res->is_error;
my @media = map +{
media_src => $self->seaweed->url_for(
SeaweedFS::FID->from_string($_->{seaweedfs_fid})
),
}, @{$res->json->{media}};
return $self->render(template => "gallery", media => \@media);
};
1;

7
templates/tags.html.ep Normal file
View File

@ -0,0 +1,7 @@
% layout "main";
<p class="text-center">
% for my $t (@$tags) {
<%= link_to "$t->{name} ($t->{count})" => $t->{uri} %>
% }
</p>