Skip to content

Commit

Permalink
MBS-8700: List genres in JSON-LD
Browse files Browse the repository at this point in the history
This uses only the genres on top_tags (so, the ones that would be
displayed on the sidebar). This sounds sensible enough to me,
since if someone has added 100 genres to an artist we probably don't
want to show them all.

https://schema.org/genre specifically allows only Text or URL for genre,
so I went with URL to at least be able to pass an MBID for it. Ideally
we would serialize a genre object with ID and name, but that doesn't
seem to be supported.
  • Loading branch information
reosarevok committed Feb 8, 2021
1 parent a25e1a4 commit c9689b1
Show file tree
Hide file tree
Showing 18 changed files with 75 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/MusicBrainz/Server/Controller/Area.pm
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ with 'MusicBrainz::Server::Controller::Role::WikipediaExtract';
with 'MusicBrainz::Server::Controller::Role::CommonsImage';
with 'MusicBrainz::Server::Controller::Role::EditRelationships';
with 'MusicBrainz::Server::Controller::Role::JSONLD' => {
endpoints => {show => {}, aliases => {copy_stash => ['aliases']}}
endpoints => {show => {copy_stash => ['top_tags']}, aliases => {copy_stash => ['aliases']}}
};
with 'MusicBrainz::Server::Controller::Role::Collection' => {
entity_type => 'area'
Expand Down
3 changes: 2 additions & 1 deletion lib/MusicBrainz/Server/Controller/Artist.pm
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ with 'MusicBrainz::Server::Controller::Role::JSONLD' => {
{from => 'recordings_jsonld', to => 'recordings'},
{from => 'identities', to => 'identities'},
{from => 'legal_name', to => 'legal_name'},
{from => 'other_identities', to => 'other_identities'}]},
{from => 'other_identities', to => 'other_identities'},
'top_tags']},
recordings => {copy_stash => [{from => 'recordings_jsonld', to => 'recordings'}]},
relationships => {},
aliases => {copy_stash => ['aliases']}}
Expand Down
2 changes: 1 addition & 1 deletion lib/MusicBrainz/Server/Controller/Label.pm
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ with 'MusicBrainz::Server::Controller::Role::WikipediaExtract';
with 'MusicBrainz::Server::Controller::Role::CommonsImage';
with 'MusicBrainz::Server::Controller::Role::EditRelationships';
with 'MusicBrainz::Server::Controller::Role::JSONLD' => {
endpoints => {show => {copy_stash => [{from => 'releases_jsonld', to => 'releases'}]},
endpoints => {show => {copy_stash => [{from => 'releases_jsonld', to => 'releases'}, 'top_tags']},
aliases => {copy_stash => ['aliases']}}
};
with 'MusicBrainz::Server::Controller::Role::Collection' => {
Expand Down
2 changes: 1 addition & 1 deletion lib/MusicBrainz/Server/Controller/Place.pm
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ with 'MusicBrainz::Server::Controller::Role::WikipediaExtract';
with 'MusicBrainz::Server::Controller::Role::CommonsImage';
with 'MusicBrainz::Server::Controller::Role::EditRelationships';
with 'MusicBrainz::Server::Controller::Role::JSONLD' => {
endpoints => {show => {}, aliases => {copy_stash => ['aliases']}}
endpoints => {show => {copy_stash => ['top_tags']}, aliases => {copy_stash => ['aliases']}}
};
with 'MusicBrainz::Server::Controller::Role::Collection' => {
entity_type => 'place'
Expand Down
2 changes: 1 addition & 1 deletion lib/MusicBrainz/Server/Controller/Recording.pm
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ with 'MusicBrainz::Server::Controller::Role::Tag';
with 'MusicBrainz::Server::Controller::Role::EditListing';
with 'MusicBrainz::Server::Controller::Role::EditRelationships';
with 'MusicBrainz::Server::Controller::Role::JSONLD' => {
endpoints => {show => {}, aliases => {copy_stash => ['aliases']}}
endpoints => {show => {copy_stash => ['top_tags']}, aliases => {copy_stash => ['aliases']}}
};
with 'MusicBrainz::Server::Controller::Role::Collection' => {
entity_type => 'recording'
Expand Down
2 changes: 1 addition & 1 deletion lib/MusicBrainz/Server/Controller/Release.pm
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ with 'MusicBrainz::Server::Controller::Role::EditListing';
with 'MusicBrainz::Server::Controller::Role::Tag';
with 'MusicBrainz::Server::Controller::Role::JSONLD' => {
endpoints => {
show => {copy_stash => ['release_artwork']},
show => {copy_stash => ['release_artwork', 'top_tags']},
aliases => {copy_stash => ['aliases']},
cover_art => {copy_stash => ['cover_art']},
},
Expand Down
3 changes: 2 additions & 1 deletion lib/MusicBrainz/Server/Controller/ReleaseGroup.pm
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ with 'MusicBrainz::Server::Controller::Role::WikipediaExtract';
with 'MusicBrainz::Server::Controller::Role::Cleanup';
with 'MusicBrainz::Server::Controller::Role::EditRelationships';
with 'MusicBrainz::Server::Controller::Role::JSONLD' => {
endpoints => {show => {copy_stash => [{from => 'releases_jsonld', to => 'releases'}]}, aliases => {copy_stash => ['aliases']}}
endpoints => {show => {copy_stash => [{from => 'releases_jsonld', to => 'releases'}, 'top_tags']},
aliases => {copy_stash => ['aliases']}}
};
with 'MusicBrainz::Server::Controller::Role::Collection' => {
entity_name => 'rg',
Expand Down
2 changes: 1 addition & 1 deletion lib/MusicBrainz/Server/Controller/Work.pm
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ with 'MusicBrainz::Server::Controller::Role::WikipediaExtract';
with 'MusicBrainz::Server::Controller::Role::CommonsImage';
with 'MusicBrainz::Server::Controller::Role::EditRelationships';
with 'MusicBrainz::Server::Controller::Role::JSONLD' => {
endpoints => {show => {}, aliases => {copy_stash => ['aliases']}}
endpoints => {show => {copy_stash => ['top_tags']}, aliases => {copy_stash => ['aliases']}}
};
with 'MusicBrainz::Server::Controller::Role::Collection' => {
entity_type => 'work'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use MusicBrainz::Server::WebService::Serializer::JSON::LD::Utils qw( serialize_e
use MusicBrainz::Server::Constants qw( $AREA_TYPE_COUNTRY $AREA_TYPE_CITY );

extends 'MusicBrainz::Server::WebService::Serializer::JSON::LD';
with 'MusicBrainz::Server::WebService::Serializer::JSON::LD::Role::Genre';
with 'MusicBrainz::Server::WebService::Serializer::JSON::LD::Role::GID';
with 'MusicBrainz::Server::WebService::Serializer::JSON::LD::Role::Name';
# Role::LifeSpan is not included here because schema.org does not have
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use MusicBrainz::Server::Data::Utils qw( non_empty );
use List::MoreUtils qw( uniq );

extends 'MusicBrainz::Server::WebService::Serializer::JSON::LD';
with 'MusicBrainz::Server::WebService::Serializer::JSON::LD::Role::Genre';
with 'MusicBrainz::Server::WebService::Serializer::JSON::LD::Role::GID';
with 'MusicBrainz::Server::WebService::Serializer::JSON::LD::Role::Name';
with 'MusicBrainz::Server::WebService::Serializer::JSON::LD::Role::LifeSpan' =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use Moose;
use MusicBrainz::Server::WebService::Serializer::JSON::LD::Utils qw( serialize_entity list_or_single format_date );

extends 'MusicBrainz::Server::WebService::Serializer::JSON::LD';
with 'MusicBrainz::Server::WebService::Serializer::JSON::LD::Role::Genre';
with 'MusicBrainz::Server::WebService::Serializer::JSON::LD::Role::GID';
with 'MusicBrainz::Server::WebService::Serializer::JSON::LD::Role::Name';
with 'MusicBrainz::Server::WebService::Serializer::JSON::LD::Role::LifeSpan';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use Moose;
use MusicBrainz::Server::WebService::Serializer::JSON::LD::Utils qw( serialize_entity );

extends 'MusicBrainz::Server::WebService::Serializer::JSON::LD';
with 'MusicBrainz::Server::WebService::Serializer::JSON::LD::Role::Genre';
with 'MusicBrainz::Server::WebService::Serializer::JSON::LD::Role::GID';
with 'MusicBrainz::Server::WebService::Serializer::JSON::LD::Role::Name';
with 'MusicBrainz::Server::WebService::Serializer::JSON::LD::Role::LifeSpan';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use MusicBrainz::Server::Data::Utils qw( non_empty );
use List::MoreUtils qw( uniq );

extends 'MusicBrainz::Server::WebService::Serializer::JSON::LD';
with 'MusicBrainz::Server::WebService::Serializer::JSON::LD::Role::Genre';
with 'MusicBrainz::Server::WebService::Serializer::JSON::LD::Role::GID';
with 'MusicBrainz::Server::WebService::Serializer::JSON::LD::Role::Name';
with 'MusicBrainz::Server::WebService::Serializer::JSON::LD::Role::Length';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use List::AllUtils qw( uniq );
use List::UtilsBy qw( uniq_by );

extends 'MusicBrainz::Server::WebService::Serializer::JSON::LD';
with 'MusicBrainz::Server::WebService::Serializer::JSON::LD::Role::Genre';
with 'MusicBrainz::Server::WebService::Serializer::JSON::LD::Role::GID';
with 'MusicBrainz::Server::WebService::Serializer::JSON::LD::Role::Name';
with 'MusicBrainz::Server::WebService::Serializer::JSON::LD::Role::Length';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use MusicBrainz::Server::WebService::Serializer::JSON::LD::Utils qw( serialize_e
use List::AllUtils qw( uniq );

extends 'MusicBrainz::Server::WebService::Serializer::JSON::LD';
with 'MusicBrainz::Server::WebService::Serializer::JSON::LD::Role::Genre';
with 'MusicBrainz::Server::WebService::Serializer::JSON::LD::Role::GID';
with 'MusicBrainz::Server::WebService::Serializer::JSON::LD::Role::Name';
with 'MusicBrainz::Server::WebService::Serializer::JSON::LD::Role::Aliases';
Expand Down
30 changes: 30 additions & 0 deletions lib/MusicBrainz/Server/WebService/Serializer/JSON/LD/Role/Genre.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package MusicBrainz::Server::WebService::Serializer::JSON::LD::Role::Genre;
use Moose::Role;
use MusicBrainz::Server::WebService::Serializer::JSON::LD::Utils qw( list_or_single serialize_entity );

around serialize => sub {
my ($orig, $self, $entity, $inc, $stash, $toplevel) = @_;
my $ret = $self->$orig($entity, $inc, $stash, $toplevel);

my $tags = $stash->store($entity)->{top_tags};
my @genres = map { $_->{tag}->genre } grep { $_->{tag}->genre } @{ $tags };

if (@genres) {
$ret->{genre} = list_or_single(map { DBDefs->CANONICAL_SERVER . '/genre/' . $_->gid } @genres);
}

return $ret;
};

no Moose::Role;
1;

=head1 COPYRIGHT AND LICENSE
Copyright (C) 2020 MetaBrainz Foundation
This file is part of MusicBrainz, the open internet music database,
and is licensed under the GPL version 2, or (at your option) any
later version: http://www.gnu.org/licenses/gpl-2.0.txt
=cut
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use Moose;
use MusicBrainz::Server::WebService::Serializer::JSON::LD::Utils qw( list_or_single serialize_entity );

extends 'MusicBrainz::Server::WebService::Serializer::JSON::LD';
with 'MusicBrainz::Server::WebService::Serializer::JSON::LD::Role::Genre';
with 'MusicBrainz::Server::WebService::Serializer::JSON::LD::Role::GID';
with 'MusicBrainz::Server::WebService::Serializer::JSON::LD::Role::Name';
with 'MusicBrainz::Server::WebService::Serializer::JSON::LD::Role::Aliases';
Expand Down
27 changes: 27 additions & 0 deletions t/lib/t/MusicBrainz/Server/Controller/Artist/Show.pm
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,33 @@ EOSQL
};
};

test 'Embedded JSON-LD `genre` property' => sub {
my $test = shift;
my $mech = $test->mech;
my $c = $test->c;

$c->sql->do(<<'EOSQL');
INSERT INTO artist (id, gid, name, sort_name)
VALUES (1, 'dcb48a49-b17d-49b9-aee5-4f168d8004d9', 'Group', 'Group');
INSERT INTO tag (id, name, ref_count) VALUES (30, 'dubstep', 347);
INSERT INTO genre (id, gid, name) VALUES (1, '1b50083b-1afa-4778-82c8-548b309af783', 'dubstep');
INSERT INTO artist_tag (artist, count, last_updated, tag) VALUES (1, 3, '2011-01-18 15:21:33.71184+00', 30);
EOSQL

$mech->get_ok('/artist/dcb48a49-b17d-49b9-aee5-4f168d8004d9');
page_test_jsonld $mech => {
'name' => 'Group',
'@type' => 'MusicGroup',
'@id' => 'https://musicbrainz.org/artist/dcb48a49-b17d-49b9-aee5-4f168d8004d9',
'genre' => 'https://musicbrainz.org/genre/1b50083b-1afa-4778-82c8-548b309af783',
'@context' => 'http://schema.org'
};
};

test 'Embedded JSON-LD sameAs & performsAs' => sub {
my $test = shift;
my $mech = $test->mech;
Expand Down

0 comments on commit c9689b1

Please sign in to comment.