Skip to content

Commit

Permalink
Output more tags/genres in /ws/2/release (MBS-9400, MBS-9899) (#748)
Browse files Browse the repository at this point in the history
When a release is requested, tags and genres are now outputted for the
following associated entities:

 * the release group
 * recordings
 * release artists
 * release group artists which don't appear in the release AC
 * track artists which don't appear in the release AC
 * recording artists which don't appear in the release or track AC

Based on an initial patch by Nicolás Tamargo (reosarevok)
  • Loading branch information
reosarevok authored and mwiencek committed Feb 13, 2019
1 parent 55911ec commit 6734109
Show file tree
Hide file tree
Showing 14 changed files with 489 additions and 46 deletions.
22 changes: 11 additions & 11 deletions lib/MusicBrainz/Script/JSONDump/Constants.pm
Expand Up @@ -16,37 +16,37 @@ my @inc_rels = map { ($_ =~ s/_/-/gr) . '-rels' } @RELATABLE_ENTITIES;

Readonly our %DUMPED_ENTITY_TYPES => (
area => {
inc => WebServiceInc->new(inc => [qw( aliases annotation tags ), @inc_rels]),
inc => WebServiceInc->new(inc => [qw( aliases annotation tags genres ), @inc_rels]),
},
artist => {
inc => WebServiceInc->new(inc => [qw( aliases annotation ratings tags ), @inc_rels]),
inc => WebServiceInc->new(inc => [qw( aliases annotation ratings tags genres ), @inc_rels]),
},
event => {
inc => WebServiceInc->new(inc => [qw( aliases annotation ratings tags ), @inc_rels]),
inc => WebServiceInc->new(inc => [qw( aliases annotation ratings tags genres ), @inc_rels]),
},
instrument => {
inc => WebServiceInc->new(inc => [qw( aliases annotation tags ), @inc_rels]),
inc => WebServiceInc->new(inc => [qw( aliases annotation tags genres ), @inc_rels]),
},
label => {
inc => WebServiceInc->new(inc => [qw( aliases annotation ratings tags ), @inc_rels]),
inc => WebServiceInc->new(inc => [qw( aliases annotation ratings tags genres ), @inc_rels]),
},
place => {
inc => WebServiceInc->new(inc => [qw( aliases annotation tags ), @inc_rels]),
inc => WebServiceInc->new(inc => [qw( aliases annotation tags genres ), @inc_rels]),
},
recording => {
inc => WebServiceInc->new(inc => [qw( aliases annotation artists artist-credits isrcs ratings tags ), @inc_rels]),
inc => WebServiceInc->new(inc => [qw( aliases annotation artists artist-credits isrcs ratings tags genres ), @inc_rels]),
},
release => {
inc => WebServiceInc->new(inc => [qw( aliases annotation artists artist-credits discids isrcs labels media recording-level-rels recordings release-groups tags ), @inc_rels]),
inc => WebServiceInc->new(inc => [qw( aliases annotation artists artist-credits discids isrcs labels media recording-level-rels recordings release-groups tags genres ), @inc_rels]),
},
release_group => {
inc => WebServiceInc->new(inc => [qw( aliases annotation artists artist-credits ratings tags ), @inc_rels]),
inc => WebServiceInc->new(inc => [qw( aliases annotation artists artist-credits ratings tags genres ), @inc_rels]),
},
series => {
inc => WebServiceInc->new(inc => [qw( aliases annotation tags ), @inc_rels]),
inc => WebServiceInc->new(inc => [qw( aliases annotation tags genres ), @inc_rels]),
},
work => {
inc => WebServiceInc->new(inc => [qw( aliases annotation ratings tags ), @inc_rels]),
inc => WebServiceInc->new(inc => [qw( aliases annotation ratings tags genres ), @inc_rels]),
},
);

Expand Down
2 changes: 1 addition & 1 deletion lib/MusicBrainz/Server/Controller/WS/2/Release.pm
Expand Up @@ -138,7 +138,7 @@ sub release_toplevel {
$c->model('ArtistCredit')->load(@tracks);
my @acns = map { $_->artist_credit->all_names } @tracks;
$c->model('Artist')->load(@acns);
$self->_aliases($c, 'Artist', [uniq_by { $_->id } map { $_->artist } @acns], $stash);
$self->linked_artists($c, $stash, [uniq_by { $_->id } map { $_->artist } @acns]);
}

my @recordings = $c->model('Recording')->load(@tracks);
Expand Down
8 changes: 8 additions & 0 deletions lib/MusicBrainz/Server/ControllerBase/WS/2.pm
Expand Up @@ -437,6 +437,14 @@ sub linked_release_groups
if ($c->stash->{inc}->artist_credits)
{
$c->model('ArtistCredit')->load(@$release_groups);

my @acns = map { $_->artist_credit->all_names } @$release_groups;
$c->model('Artist')->load(@acns);

$self->linked_artists(
$c, $stash,
[uniq_by { $_->id } map { $_->artist } @acns],
);
}

$self->_tags_and_ratings($c, 'ReleaseGroup', $release_groups, $stash);
Expand Down
6 changes: 4 additions & 2 deletions lib/MusicBrainz/Server/WebService/Serializer/JSON/2/Medium.pm
Expand Up @@ -65,8 +65,10 @@ sub serialize_track {
title => $entity->name,
);

$track_output{recording} = serialize_entity($entity->recording, $inc, $stash)
if $inc->recordings;
if ($inc->recordings) {
local $stash->{track_artist_credit} = $entity->artist_credit;
$track_output{recording} = serialize_entity($entity->recording, $inc, $stash);
}

$track_output{"artist-credit"} = serialize_entity($entity->artist_credit, $inc, $stash)
if $inc->artist_credits;
Expand Down
Expand Up @@ -20,9 +20,10 @@ sub serialize
$body{length} = number($entity->length);
$body{video} = boolean($entity->video);

$body{"artist-credit"} = serialize_entity($entity->artist_credit)
if ($entity->artist_credit &&
($toplevel || ($inc && $inc->artist_credits)));
if ($entity->artist_credit && ($toplevel || ($inc && $inc->artist_credits))) {
local $MusicBrainz::Server::WebService::Serializer::JSON::2::Utils::hide_aliases = 1;
$body{"artist-credit"} = serialize_entity($entity->artist_credit, $inc, $stash);
}

$body{releases} = list_of($entity, $inc, $stash, "releases")
if ($toplevel && $inc && $inc->releases);
Expand Down
Expand Up @@ -88,6 +88,10 @@ sub serialize
# behaviour of the XML serializer). So we override a package variable to
# force the ratings to be serialized despite not being top-level.
local $MusicBrainz::Server::WebService::Serializer::JSON::2::Utils::force_ratings = 1;
# Used in `JSON::2::Utils::serialize_tags` to hide tags/genres for
# release group artists which already appear in the release artist
# credit.
local $stash->{release_artist_credit} = $entity->artist_credit;
$body{"release-group"} = serialize_entity($entity->release_group, $inc, $stash);
}

Expand Down Expand Up @@ -118,6 +122,10 @@ sub serialize
# a package variable to force the ratings to be serialized despite not
# being top-level.
local $MusicBrainz::Server::WebService::Serializer::JSON::2::Utils::force_ratings = 1;
# Used in `JSON::2::Utils::serialize_tags` to hide tags/genres for
# track/recording artists which already appear in the release artist
# credit.
local $stash->{release_artist_credit} = $entity->artist_credit;
$body{media} = [
map { serialize_entity($_, $inc, $stash) }
$entity->all_mediums ];
Expand Down
Expand Up @@ -21,7 +21,7 @@ sub serialize
$body{"first-release-date"} = $entity->first_release_date->format;
$body{disambiguation} = $entity->comment // "";

$body{"artist-credit"} = serialize_entity($entity->artist_credit)
$body{"artist-credit"} = serialize_entity($entity->artist_credit, $inc, $stash)
if $inc && ($inc->artist_credits || $inc->artists);

$body{releases} = list_of($entity, $inc, $stash, "releases")
Expand Down
24 changes: 20 additions & 4 deletions lib/MusicBrainz/Server/WebService/Serializer/JSON/2/Utils.pm
Expand Up @@ -20,6 +20,8 @@ our @EXPORT_OK = qw(
serializer
);

our $hide_aliases = 0;
our $hide_tags_and_genres = 0;
our $force_ratings = 0;

my %serializers =
Expand Down Expand Up @@ -148,6 +150,8 @@ sub count_of
sub serialize_aliases {
my ($into, $entity, $inc, $stash) = @_;

return if $hide_aliases;

return unless defined $inc && $inc->aliases;

my $opts = $stash->store($entity);
Expand All @@ -171,9 +175,9 @@ sub serialize_aliases {
}

sub serialize_annotation {
my ($into, $entity, $inc) = @_;
my ($into, $entity, $inc, $stash, $toplevel) = @_;

return unless defined $inc && $inc->annotation;
return unless $toplevel && defined $inc && $inc->annotation;

my $annotation = $entity->latest_annotation;
$into->{annotation} = defined $annotation ?
Expand Down Expand Up @@ -249,6 +253,8 @@ sub serialize_relationships {
$inc->has_rels &&
$entity->has_loaded_relationships);

local $hide_tags_and_genres = 1;

my @relationships =
map { serialize_entity($_, $inc, $stash) }
sort_by {
Expand All @@ -265,11 +271,21 @@ sub serialize_relationships {
sub serialize_tags {
my ($into, $entity, $inc, $stash, $toplevel) = @_;

return if $hide_tags_and_genres;

return unless
($toplevel &&
defined $inc &&
(defined $inc &&
($inc->tags || $inc->user_tags || $inc->genres || $inc->user_genres));

if ($entity->isa('MusicBrainz::Server::Entity::Artist')) {
if (my $release_ac = $stash->{release_artist_credit}) {
return if (grep { $_->artist_id == $entity->id } $release_ac->all_names);
}
if (my $track_ac = $stash->{track_artist_credit}) {
return if (grep { $_->artist_id == $entity->id } $track_ac->all_names);
}
}

my $opts = $stash->store($entity);

if ($inc->tags) {
Expand Down
Expand Up @@ -32,6 +32,7 @@ ws_test 'browse release group via release',
<name>Distance</name>
<sort-name>Distance</sort-name>
<disambiguation>UK dubstep artist Greg Sanders</disambiguation>
<rating votes-count="1">5</rating>
</artist>
</name-credit>
</artist-credit>
Expand Down Expand Up @@ -60,6 +61,7 @@ ws_test 'browse release group via artist',
<name>Distance</name>
<sort-name>Distance</sort-name>
<disambiguation>UK dubstep artist Greg Sanders</disambiguation>
<rating votes-count="1">5</rating>
</artist>
</name-credit>
</artist-credit>
Expand All @@ -83,6 +85,7 @@ ws_test 'browse release group via artist',
<name>Distance</name>
<sort-name>Distance</sort-name>
<disambiguation>UK dubstep artist Greg Sanders</disambiguation>
<rating votes-count="1">5</rating>
</artist>
</name-credit>
</artist-credit>
Expand Down
Expand Up @@ -35,6 +35,8 @@ test 'browse release group via release' => sub {
name => "Distance",
"sort-name" => "Distance",
disambiguation => "UK dubstep artist Greg Sanders",
tags => [],
genres => [],
},
joinphrase => "",
}],
Expand Down Expand Up @@ -78,6 +80,8 @@ test 'browse release group via artist' => sub {
name => "Distance",
"sort-name" => "Distance",
disambiguation => "UK dubstep artist Greg Sanders",
tags => [],
genres => [],
},
joinphrase => "",
}],
Expand Down Expand Up @@ -108,6 +112,8 @@ test 'browse release group via artist' => sub {
name => "Distance",
"sort-name" => "Distance",
disambiguation => "UK dubstep artist Greg Sanders",
tags => [],
genres => [],
},
joinphrase => "",
}],
Expand Down

0 comments on commit 6734109

Please sign in to comment.