Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions API.pm
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,36 @@ sub getUserFavorites {
});
}

sub getUserFavoriteIds {
my ($self, $cb, $force) = @_;

$self->_get('favorite/getUserFavoriteIds', sub {
my ($favoriteIds) = @_;

$cb->($favoriteIds);
},{
_ttl => QOBUZ_USER_DATA_EXPIRY,
_user_cache => 1,
_use_token => 1,
_wipecache => $force,
});
}

sub getUserFavoriteStatus {
my ($self, $cb, $args, $force) = @_;

$args->{_ttl} = QOBUZ_USER_DATA_EXPIRY;
$args->{_user_cache} = 1;
$args->{_use_token} = 1;
$args->{_wipecache} => $force;

$self->_get('favorite/status', sub {
my ($favoriteStatus) = @_;

$cb->($favoriteStatus);
}, $args);
}

sub createFavorite {
my ($self, $cb, $args) = @_;

Expand All @@ -408,7 +438,11 @@ sub createFavorite {

$self->_get('favorite/create', sub {
$cb->(shift);
$self->getUserFavorites(sub{}, 'refresh')
$self->getUserFavorites(sub{}, 'refresh');
$self->getUserFavoriteIds(sub{}, 'refresh');
$self->getUserFavoriteStatus(sub{}, { type => 'album', item_id => $args->{album_ids} }, 'refresh') if $args->{album_ids};
$self->getUserFavoriteStatus(sub{}, { type => 'artist', item_id => $args->{artist_ids} }, 'refresh') if $args->{artist_ids};
$self->getUserFavoriteStatus(sub{}, { type => 'track', item_id => $args->{track_ids} }, 'refresh') if $args->{track_ids};
Comment on lines +441 to +445
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is doing more work than before. Is it worth it? Wouldn't the first call have all the information the subsequent two would get us?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're just clearing caches here, after an add or delete favourite. Maybe we should do it within the add/delete api routines. At least we would know which of the 3 types to clear in the case of the status cache.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not only wiping caches, you're doing calls to eg. favorite/status, too, right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, sorry I meant wipe/recreate cache. We could delay the recreation of the cache entry until it is requested again, so just do the clear at that stage.

}, $args);
}

Expand All @@ -420,7 +454,11 @@ sub deleteFavorite {

$self->_get('favorite/delete', sub {
$cb->(shift);
$self->getUserFavorites(sub{}, 'refresh')
$self->getUserFavorites(sub{}, 'refresh');
$self->getUserFavoriteIds(sub{}, 'refresh');
$self->getUserFavoriteStatus(sub{}, { type => 'album', item_id => $args->{album_ids} }, 'refresh') if $args->{album_ids};
$self->getUserFavoriteStatus(sub{}, { type => 'artist', item_id => $args->{artist_ids} }, 'refresh') if $args->{artist_ids};
$self->getUserFavoriteStatus(sub{}, { type => 'track', item_id => $args->{track_ids} }, 'refresh') if $args->{track_ids};
}, $args);
}

Expand Down
35 changes: 20 additions & 15 deletions Plugin.pm
Original file line number Diff line number Diff line change
Expand Up @@ -754,10 +754,8 @@ sub QobuzArtist {
}],
};

$api->getUserFavorites(sub {
my $favorites = shift;
my $artistId = $artist->{id};
my $isFavorite = ($favorites && $favorites->{artists}) ? grep { $_->{id} eq $artistId } @{$favorites->{artists}->{items}} : 0;
$api->getUserFavoriteStatus(sub {
my $isFavorite = shift->{status};

push @$items, {
name => cstring($client, $isFavorite ? 'PLUGIN_QOBUZ_REMOVE_FAVORITE_ARTIST' : 'PLUGIN_QOBUZ_ADD_FAVORITE_ARTIST', $artist->{name}),
Expand All @@ -772,6 +770,9 @@ sub QobuzArtist {
$cb->({
items => $items
});
}, {
type => 'artist',
item_id => $artist->{id},
});
}, $args->{artistId});
}
Expand Down Expand Up @@ -992,13 +993,13 @@ sub QobuzUserFavorites {
sub QobuzManageFavorites {
my ($client, $cb, $params, $args) = @_;

getAPIHandler($client)->getUserFavorites(sub {
getAPIHandler($client)->getUserFavoriteIds(sub {
my $favorites = shift;

my $items = [];

if ( (my $artist = $args->{artist}) && (my $artistId = $args->{artistId}) ) {
my $isFavorite = grep { $_->{id} eq $artistId } @{$favorites->{artists}->{items}};
my $isFavorite = grep { $_ eq $artistId } @{$favorites->{artists}};

push @$items, {
name => cstring($client, $isFavorite ? 'PLUGIN_QOBUZ_REMOVE_FAVORITE_ARTIST' : 'PLUGIN_QOBUZ_ADD_FAVORITE_ARTIST', $artist),
Expand All @@ -1011,7 +1012,7 @@ sub QobuzManageFavorites {
}

if ( (my $album = $args->{album}) && (my $albumId = $args->{albumId}) ) {
my $isFavorite = grep { $_->{id} eq $albumId } @{$favorites->{albums}->{items}};
my $isFavorite = grep { $_ eq $albumId } @{$favorites->{albums}};

push @$items, {
name => cstring($client, $isFavorite ? 'PLUGIN_QOBUZ_REMOVE_FAVORITE_RELEASE' : 'PLUGIN_QOBUZ_ADD_FAVORITE_RELEASE', $album),
Expand All @@ -1024,7 +1025,7 @@ sub QobuzManageFavorites {
}

if ( (my $title = $args->{title}) && (my $trackId = $args->{trackId}) ) {
my $isFavorite = grep { $_->{id} eq $trackId } @{$favorites->{tracks}->{items}};
my $isFavorite = grep { $_ eq $trackId } @{$favorites->{tracks}};

push @$items, {
name => cstring($client, $isFavorite ? 'PLUGIN_QOBUZ_REMOVE_FAVORITE_TRACK' : 'PLUGIN_QOBUZ_ADD_FAVORITE_TRACK', $title),
Expand Down Expand Up @@ -1172,9 +1173,8 @@ sub QobuzGetTracks {

if (!$album) { # the album does not exist in the Qobuz library
$log->warn("Get album ($albumId) failed");
$api->getUserFavorites(sub {
my $favorites = shift;
my $isFavorite = ($favorites && $favorites->{albums}) ? grep { $_->{id} eq $albumId } @{$favorites->{albums}->{items}} : 0;
$api->getUserFavoriteStatus(sub {
my $isFavorite = shift->{status};

push @$items, {
name => cstring($client, 'PLUGIN_QOBUZ_ALBUM_NOT_FOUND'),
Expand All @@ -1196,7 +1196,10 @@ sub QobuzGetTracks {
$cb->({
items => $items,
}, @_ );
});
}, {
type => 'album',
item_id => $albumId,
});
return;

} elsif (!$album->{streamable} && !$prefs->get('playSamples')) { # the album is not streamable
Expand Down Expand Up @@ -1448,9 +1451,8 @@ sub QobuzGetTracks {
}
}

$api->getUserFavorites(sub {
my $favorites = shift;
my $isFavorite = ($favorites && $favorites->{albums}) ? grep { $_->{id} eq $albumId } @{$favorites->{albums}->{items}} : 0;
$api->getUserFavoriteStatus(sub {
my $isFavorite = shift->{status};

push @$items, {
name => cstring($client, $isFavorite ? 'PLUGIN_QOBUZ_REMOVE_FAVORITE_RELEASE' : 'PLUGIN_QOBUZ_ADD_FAVORITE_RELEASE', $album->{title}),
Expand Down Expand Up @@ -1549,6 +1551,9 @@ sub QobuzGetTracks {
$cb->({
items => $items,
}, @_ );
}, {
type => 'album',
item_id => $albumId,
});
}, $albumId);
}
Expand Down