diff --git a/lib/PoorBooru/Backend.pm b/lib/PoorBooru/Backend.pm index 63ba9a9..5719a27 100644 --- a/lib/PoorBooru/Backend.pm +++ b/lib/PoorBooru/Backend.pm @@ -21,6 +21,9 @@ sub startup ($self) my $r = $self->routes; $r->get("/")->to("gallery#index"); + + $r->get("/tags")->to("tags#list"); + $r->get("/tag/:tag_id_or_name")->to("tags#show"); } 1; diff --git a/lib/PoorBooru/Backend/Controller/Gallery.pm b/lib/PoorBooru/Backend/Controller/Gallery.pm index 09cdb54..30bc92a 100644 --- a/lib/PoorBooru/Backend/Controller/Gallery.pm +++ b/lib/PoorBooru/Backend/Controller/Gallery.pm @@ -5,6 +5,7 @@ use warnings; use Mojo::Base "Mojolicious::Controller"; use Mojo::JSON qw(decode_json); +use SeaweedFS::FID; sub index ($self) { diff --git a/lib/PoorBooru/Backend/Controller/Tags.pm b/lib/PoorBooru/Backend/Controller/Tags.pm new file mode 100644 index 0000000..ef3da51 --- /dev/null +++ b/lib/PoorBooru/Backend/Controller/Tags.pm @@ -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; diff --git a/templates/tags.html.ep b/templates/tags.html.ep new file mode 100644 index 0000000..9526cb4 --- /dev/null +++ b/templates/tags.html.ep @@ -0,0 +1,7 @@ +% layout "main"; + +
+% for my $t (@$tags) { +<%= link_to "$t->{name} ($t->{count})" => $t->{uri} %> +% } +