From aa5e4ffe29207473aa2226d348c75513f1ecc4ae Mon Sep 17 00:00:00 2001 From: Lucas Date: Fri, 17 Mar 2023 19:58:12 +0000 Subject: [PATCH] api: return tags for /media/:media_id --- lib/PoorBooru/API/V0/Controller/Media.pm | 21 +++++++++++++++++++++ lib/PoorBooru/API/V0/Controller/Tags.pm | 3 +++ 2 files changed, 24 insertions(+) diff --git a/lib/PoorBooru/API/V0/Controller/Media.pm b/lib/PoorBooru/API/V0/Controller/Media.pm index 20e2124..57577d2 100644 --- a/lib/PoorBooru/API/V0/Controller/Media.pm +++ b/lib/PoorBooru/API/V0/Controller/Media.pm @@ -9,6 +9,13 @@ my $MEDIA_SEARCH_OPTS = { order_by => { -desc => "media_id" }, rows => 5, }; +my $TAGS_SEARCH_OPTS = { + rows => 5, +}; +my $TAGS_COUNT_VIEW_SEARCH_OPTS = { + order_by => { -desc => "count" }, + rows => 5, +}; sub list ($self) { @@ -35,6 +42,14 @@ sub list ($self) sub show ($self) { + my $v = $self->validation; + my $page = $v->optional("page")->num(1, undef)->param // 1; + + return $self->render( + json => {error => "Invalid page number"}, + status => 400, + ) if $v->has_error; + my $media_id = $self->stash("media_id"); my $media = $self->schema->resultset("Media") ->single({ media_id => $media_id }); @@ -44,11 +59,17 @@ sub show ($self) status => 404, ) if !defined($media); + my $paged_media_tags = $self->schema->resultset("TaggedMediaView") + ->search({ media_id => $media_id }, { $TAGS_SEARCH_OPTS->%* }) + ->page($page); + my @tags = map { $_->tag_name } $paged_media_tags->all; + return $self->render(json => { id => $media->media_id, name => $media->filename, download => $media->seaweedfs_fid, content_type => $media->content_type, + tags => [ @tags ], }); }; diff --git a/lib/PoorBooru/API/V0/Controller/Tags.pm b/lib/PoorBooru/API/V0/Controller/Tags.pm index 2354682..4340140 100644 --- a/lib/PoorBooru/API/V0/Controller/Tags.pm +++ b/lib/PoorBooru/API/V0/Controller/Tags.pm @@ -9,6 +9,9 @@ my $MEDIA_SEARCH_OPTS = { order_by => { -desc => "media_id" }, rows => 5, }; +my $TAGS_SEARCH_OPTS = { + rows => 5, +}; my $TAGS_COUNT_VIEW_SEARCH_OPTS = { order_by => { -desc => "count" }, rows => 5,