Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] MBS-13561 / MBS-13571 / MBS-13818 / MBS-13580: Show primary aliases in more places #3410

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
7 changes: 7 additions & 0 deletions lib/MusicBrainz/Server.pm
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,12 @@ sub handle_unicode_encoding_exception {
$self->res->status(HTTP_BAD_REQUEST);
}

has current_language => (
is => 'rw',
isa => 'Str',
default => 'en',
);

# Set and unset translation language
sub with_translations {
my ($c, $code) = @_;
Expand All @@ -355,6 +361,7 @@ sub with_translations {
my $cookie_lang = Translation->instance->language_from_cookie($c->request->cookies->{lang});
$c->set_language_cookie($c->request->cookies->{lang}->value) if defined $c->request->cookies->{lang};
my $lang = Translation->instance->set_language($cookie_lang);
$c->current_language($lang);
my $html_lang = $lang =~ s/_([A-Z0-9]{2,})/-\L$1/r;

$c->stash(
Expand Down
3 changes: 1 addition & 2 deletions lib/MusicBrainz/Server/Controller/Artist.pm
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ after 'load' => sub
}
}

my $lang = $c->stash->{current_language} // 'en';
$c->model('Artist')->load_related_info([$artist], $lang);
$c->model('Artist')->load_related_info([$artist], $c->current_language);
$c->model('ArtistType')->load(map { $_->target } @{ $artist->relationships_by_type('artist') });
$c->model('Area')->load_containment($artist->area, $artist->begin_area, $artist->end_area);
};
Expand Down
3 changes: 1 addition & 2 deletions lib/MusicBrainz/Server/Controller/Collection.pm
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,7 @@ sub show : Chained('load') PathPart('') {

if ($model->can('load_related_info')) {
if ($entity_type eq 'artist') {
my $lang = $c->stash->{current_language} // 'en';
$model->load_related_info($entities, $lang);
$model->load_related_info($entities, $c->current_language);
} else {
$model->load_related_info(@$entities);
}
Expand Down
7 changes: 2 additions & 5 deletions lib/MusicBrainz/Server/Controller/Search.pm
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ sub direct : Private
my @entities = map { $_->entity } @$results;

if ($type eq 'artist') {
my $lang = $c->stash->{current_language} // 'en';
$c->model('Artist')->load_related_info(\@entities, $lang);
$c->model('Artist')->load_related_info(\@entities, $c->current_language);
}
elsif ($type eq 'editor') {
$c->model('Editor')->load_preferences(@entities);
Expand Down Expand Up @@ -245,15 +244,13 @@ sub do_external_search {
my $query = $opts{query};
my $type = $opts{type};

my $lang = $c->stash->{current_language} // 'en';

my $search = $c->model('Search');
my $ret = $search->external_search($type,
$query,
$limit,
$page,
$advanced,
$lang);
$c->current_language);

if (exists $ret->{error})
{
Expand Down
25 changes: 19 additions & 6 deletions lib/MusicBrainz/Server/Controller/Tag.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use HTTP::Status qw( :constants );

extends 'MusicBrainz::Server::Controller';

use MusicBrainz::Server::Data::Utils qw( boolean_to_json type_to_model );
use MusicBrainz::Server::Data::Utils qw( boolean_to_json );
use MusicBrainz::Server::Constants qw( %ENTITIES entities_with );
use MusicBrainz::Server::ControllerUtils::JSON qw( serialize_pager );
use MusicBrainz::Server::Entity::Util::JSON qw( to_json_object );
Expand Down Expand Up @@ -90,17 +90,27 @@ sub show : Chained('load') PathPart('')
tag => $tag->TO_JSON,
taggedEntities => {
map {
my ($entities, $total) = $c->model(type_to_model($_))->tags->find_entities(
my $entity_properties = $ENTITIES{$_};
my $model = $c->model($entity_properties->{model});

my ($entity_tags, $total) = $model->tags->find_entities(
$tag->id, 10, 0);
$c->model('ArtistCredit')->load(map { $_->entity } @$entities);

my @entities = map { $_->entity } @$entity_tags;
$model->load_aliases(\@entities, $c->current_language)
if $model->can('load_aliases');
$c->model('ArtistCredit')->load(@entities);

$c->model('ArtistCredit')->load(@entities)
if $entity_properties->{artist_credits};

("$_" => {
count => $total,
tags => [map +{
count => $_->{count},
entity => $_->{entity}->TO_JSON,
entity_id => $_->{entity_id},
}, @$entities],
}, @$entity_tags],
})
} entities_with('tags'),
},
Expand All @@ -116,11 +126,14 @@ for my $entity_type (entities_with('tags')) {
my $method = sub {
my ($self, $c) = @_;

my $model = $c->model($entity_properties->{model});
my $entity_tags = $self->_load_paged($c, sub {
$c->model($entity_properties->{model})->tags->find_entities($c->stash->{tag}->id, shift, shift);
$model->tags->find_entities($c->stash->{tag}->id, shift, shift);
});
my @entities = map { $_->entity } @$entity_tags;

$c->model('ArtistCredit')->load(map { $_->entity } @$entity_tags) if $entity_properties->{artist_credits};
$model->load_aliases(\@entities, $c->current_language) if $model->can('load_aliases');
$c->model('ArtistCredit')->load(@entities) if $entity_properties->{artist_credits};
$c->stash(
current_view => 'Node',
component_path => 'tag/EntityList',
Expand Down
6 changes: 5 additions & 1 deletion lib/MusicBrainz/Server/Controller/User/SubscriptionsRole.pm
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@ role {
unless $user->preferences->public_subscriptions;
}

my $model = $c->model(type_to_model($type));
my $lang = $c->stash->{current_language} // 'en';
my $entities = $self->_load_paged($c, sub {
$c->model(type_to_model($type))->find_by_subscribed_editor($user->id, shift, shift);
$model->find_by_subscribed_editor($user->id, shift, shift);
});
$model->load_aliases($entities, $lang) if $model->can('load_aliases');

my %extra_props;

if ($type eq 'collection') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ role {
return map +{
entity => $_,
aliases => $aliases->{$_->id},
current_language => $c->stash->{current_language} // 'en',
current_language => $c->current_language,
}, $self->$orig($c, @entities);
};
};
Expand Down
17 changes: 1 addition & 16 deletions lib/MusicBrainz/Server/Data/Artist.pm
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use MusicBrainz::Server::Data::Utils qw(
add_partial_date_to_row
conditional_merge_column_query
contains_string
find_best_primary_alias
hash_to_row
load_subobjects
merge_table_attributes
Expand Down Expand Up @@ -492,24 +491,10 @@ sub load_related_info {

my $c = $self->c;
my @artists = @{$artists_ref};
$self->load_aliases($artists_ref, $user_lang);
$c->model('ArtistType')->load(@artists);
$c->model('Gender')->load(@artists);
$c->model('Area')->load(@artists);

# Load and save aliases so Controller::Artist::show can use them later.
my $artist_aliases = $c->model('Artist')->alias->find_by_entity_ids(
map { $_->id } @artists,
);
my @all_aliases = map { @$_ } values %$artist_aliases;
$c->model('Artist')->alias_type->load(@all_aliases);
for my $artist (@artists) {
my @aliases = @{ $artist_aliases->{$artist->id} };
$artist->aliases(\@aliases);
if (defined $user_lang) {
my $best_alias = find_best_primary_alias(\@aliases, $user_lang);
$artist->primary_alias($best_alias->name) if defined $best_alias;
}
}
}

sub load_meta
Expand Down
3 changes: 3 additions & 0 deletions lib/MusicBrainz/Server/Data/ArtistCredit.pm
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ sub load
{
my ($self, @objs) = @_;
load_subobjects($self, 'artist_credit', @objs);
my $lang = $self->c->stash->{current_language} // 'en';
my @artists = map { map { $_->artist } $_->artist_credit->all_names} @objs;
$self->c->model('Artist')->load_aliases(\@artists, $lang);
}

sub find_by_ids
Expand Down
9 changes: 9 additions & 0 deletions lib/MusicBrainz/Server/Data/Relationship.pm
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,15 @@ sub load_entities

my @series = values %{$data_by_type{'series'}};
$self->c->model('SeriesType')->load(@series);

my $lang = $self->c->stash->{current_language} // 'en';

for my $type (keys %data_by_type) {
my $model = $self->c->model(type_to_model($type));
next unless $model->can('load_aliases');
my @entities = values %{$data_by_type{$type}};
$model->load_aliases(\@entities, $lang);
}
}

sub _load_subset {
Expand Down
24 changes: 23 additions & 1 deletion lib/MusicBrainz/Server/Data/Role/Alias.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use MooseX::Role::Parameterized;
use namespace::autoclean;

use MusicBrainz::Server::Data::Alias;
use MusicBrainz::Server::Data::Utils qw( type_to_model );
use MusicBrainz::Server::Data::Utils qw(
find_best_primary_alias
type_to_model
);
use Moose::Util qw( ensure_all_roles );

parameter 'type' => (
Expand Down Expand Up @@ -81,6 +84,25 @@ role

return \%ret;
};

method 'load_aliases' => sub {
my ($self, $entities_ref, $user_lang) = @_;

my @entities = @{$entities_ref};
my $entity_aliases = $self->alias->find_by_entity_ids(
map { $_->id } @entities,
);
my @all_aliases = map { @$_ } values %$entity_aliases;
$self->alias_type->load(@all_aliases);
for my $entity (@entities) {
my @aliases = @{ $entity_aliases->{$entity->id} };
$entity->aliases(\@aliases);
if (defined $user_lang) {
my $best_alias = find_best_primary_alias(\@aliases, $user_lang);
$entity->primary_alias($best_alias->name) if defined $best_alias;
}
}
};
};


Expand Down
3 changes: 2 additions & 1 deletion lib/MusicBrainz/Server/Entity/Area.pm
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use MusicBrainz::Server::Entity::Util::JSON qw( to_json_array );
use List::AllUtils qw( first );

extends 'MusicBrainz::Server::Entity';
with 'MusicBrainz::Server::Entity::Role::Annotation',
with 'MusicBrainz::Server::Entity::Role::Alias',
'MusicBrainz::Server::Entity::Role::Annotation',
'MusicBrainz::Server::Entity::Role::Comment',
'MusicBrainz::Server::Entity::Role::DatePeriod',
'MusicBrainz::Server::Entity::Role::Relatable',
Expand Down
15 changes: 2 additions & 13 deletions lib/MusicBrainz/Server/Entity/Artist.pm
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use MusicBrainz::Server::Entity::Types;
use MusicBrainz::Server::Entity::Util::JSON qw( to_json_object );

extends 'MusicBrainz::Server::Entity';
with 'MusicBrainz::Server::Entity::Role::Annotation',
with 'MusicBrainz::Server::Entity::Role::Alias',
'MusicBrainz::Server::Entity::Role::Annotation',
'MusicBrainz::Server::Entity::Role::Area',
'MusicBrainz::Server::Entity::Role::Comment',
'MusicBrainz::Server::Entity::Role::DatePeriod',
Expand All @@ -26,17 +27,6 @@ has 'sort_name' => (
isa => 'Str',
);

has 'aliases' => (
is => 'rw',
isa => 'ArrayRef[MusicBrainz::Server::Entity::Alias]',
default => sub { [] },
);

has 'primary_alias' => (
is => 'rw',
isa => 'Str',
);

has 'gender_id' => (
is => 'rw',
isa => 'Int',
Expand Down Expand Up @@ -94,7 +84,6 @@ around TO_JSON => sub {
begin_area => to_json_object($self->begin_area),
end_area => to_json_object($self->end_area),
gender => to_json_object($self->gender),
$self->primary_alias ? (primaryAlias => $self->primary_alias) : (),
begin_area_id => defined $self->begin_area_id ? (0 + $self->begin_area_id) : undef,
end_area_id => defined $self->end_area_id ? (0 + $self->end_area_id) : undef,
gender_id => defined $self->gender_id ? (0 + $self->gender_id) : undef,
Expand Down
3 changes: 2 additions & 1 deletion lib/MusicBrainz/Server/Entity/Event.pm
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use MusicBrainz::Server::Entity::PartialDate;
use MusicBrainz::Server::Entity::Types;

extends 'MusicBrainz::Server::Entity';
with 'MusicBrainz::Server::Entity::Role::Annotation',
with 'MusicBrainz::Server::Entity::Role::Alias',
'MusicBrainz::Server::Entity::Role::Annotation',
'MusicBrainz::Server::Entity::Role::Comment',
'MusicBrainz::Server::Entity::Role::DatePeriod',
'MusicBrainz::Server::Entity::Role::Rating',
Expand Down
3 changes: 2 additions & 1 deletion lib/MusicBrainz/Server/Entity/Genre.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use Moose;
use MusicBrainz::Server::Entity::Types;

extends 'MusicBrainz::Server::Entity';
with 'MusicBrainz::Server::Entity::Role::Annotation',
with 'MusicBrainz::Server::Entity::Role::Alias',
'MusicBrainz::Server::Entity::Role::Annotation',
'MusicBrainz::Server::Entity::Role::Comment',
'MusicBrainz::Server::Entity::Role::Relatable';

Expand Down
3 changes: 2 additions & 1 deletion lib/MusicBrainz/Server/Entity/Instrument.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use MusicBrainz::Server::Translation::Instruments;
use MusicBrainz::Server::Translation::InstrumentDescriptions;

extends 'MusicBrainz::Server::Entity';
with 'MusicBrainz::Server::Entity::Role::Annotation',
with 'MusicBrainz::Server::Entity::Role::Alias',
'MusicBrainz::Server::Entity::Role::Annotation',
'MusicBrainz::Server::Entity::Role::Comment',
'MusicBrainz::Server::Entity::Role::Relatable',
'MusicBrainz::Server::Entity::Role::Taggable',
Expand Down
3 changes: 2 additions & 1 deletion lib/MusicBrainz/Server/Entity/Label.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use MusicBrainz::Server::Entity::PartialDate;
use MusicBrainz::Server::Entity::Types;

extends 'MusicBrainz::Server::Entity';
with 'MusicBrainz::Server::Entity::Role::Annotation',
with 'MusicBrainz::Server::Entity::Role::Alias',
'MusicBrainz::Server::Entity::Role::Annotation',
'MusicBrainz::Server::Entity::Role::Area',
'MusicBrainz::Server::Entity::Role::Comment',
'MusicBrainz::Server::Entity::Role::DatePeriod',
Expand Down
3 changes: 2 additions & 1 deletion lib/MusicBrainz/Server/Entity/Place.pm
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use MusicBrainz::Server::Entity::PartialDate;
use MusicBrainz::Server::Entity::Types;

extends 'MusicBrainz::Server::Entity';
with 'MusicBrainz::Server::Entity::Role::Annotation',
with 'MusicBrainz::Server::Entity::Role::Alias',
'MusicBrainz::Server::Entity::Role::Annotation',
'MusicBrainz::Server::Entity::Role::Area',
'MusicBrainz::Server::Entity::Role::Comment',
'MusicBrainz::Server::Entity::Role::DatePeriod',
Expand Down
3 changes: 2 additions & 1 deletion lib/MusicBrainz/Server/Entity/Recording.pm
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ use MusicBrainz::Server::Entity::Util::JSON qw(
use List::AllUtils qw( uniq_by );

extends 'MusicBrainz::Server::Entity';
with 'MusicBrainz::Server::Entity::Role::Annotation',
with 'MusicBrainz::Server::Entity::Role::Alias',
'MusicBrainz::Server::Entity::Role::Annotation',
'MusicBrainz::Server::Entity::Role::ArtistCredit',
'MusicBrainz::Server::Entity::Role::Comment',
'MusicBrainz::Server::Entity::Role::Rating',
Expand Down
3 changes: 2 additions & 1 deletion lib/MusicBrainz/Server/Entity/Release.pm
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ use MusicBrainz::Server::Entity::Util::MediumFormat qw( combined_medium_format_n
use MusicBrainz::Server::Entity::Util::JSON qw( to_json_array to_json_object );

extends 'MusicBrainz::Server::Entity';
with 'MusicBrainz::Server::Entity::Role::Annotation',
with 'MusicBrainz::Server::Entity::Role::Alias',
'MusicBrainz::Server::Entity::Role::Annotation',
'MusicBrainz::Server::Entity::Role::ArtistCredit',
'MusicBrainz::Server::Entity::Role::Comment',
'MusicBrainz::Server::Entity::Role::Relatable',
Expand Down
3 changes: 2 additions & 1 deletion lib/MusicBrainz/Server/Entity/ReleaseGroup.pm
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use MusicBrainz::Server::Entity::Types;
use MusicBrainz::Server::Entity::Util::JSON qw( to_json_object );

extends 'MusicBrainz::Server::Entity';
with 'MusicBrainz::Server::Entity::Role::Annotation',
with 'MusicBrainz::Server::Entity::Role::Alias',
'MusicBrainz::Server::Entity::Role::Annotation',
'MusicBrainz::Server::Entity::Role::ArtistCredit',
'MusicBrainz::Server::Entity::Role::Comment',
'MusicBrainz::Server::Entity::Role::Rating',
Expand Down
Loading