2016-02-09 04:57:08 +00:00
|
|
|
import Vue from 'vue';
|
2016-03-31 07:44:36 +00:00
|
|
|
import {
|
|
|
|
reduce,
|
|
|
|
each,
|
|
|
|
find,
|
|
|
|
union,
|
|
|
|
difference,
|
|
|
|
take,
|
|
|
|
filter,
|
2016-04-17 15:38:06 +00:00
|
|
|
orderBy,
|
2016-03-31 07:44:36 +00:00
|
|
|
} from 'lodash';
|
2015-12-13 04:42:28 +00:00
|
|
|
|
|
|
|
import config from '../config';
|
2016-01-15 07:27:25 +00:00
|
|
|
import stub from '../stubs/artist';
|
2015-12-13 04:42:28 +00:00
|
|
|
import albumStore from './album';
|
|
|
|
|
2016-04-17 15:38:06 +00:00
|
|
|
const UNKNOWN_ARTIST_ID = 1;
|
|
|
|
const VARIOUS_ARTISTS_ID = 2;
|
|
|
|
|
2015-12-13 04:42:28 +00:00
|
|
|
export default {
|
2016-01-15 07:27:25 +00:00
|
|
|
stub,
|
2016-02-08 12:21:24 +00:00
|
|
|
|
2015-12-13 04:42:28 +00:00
|
|
|
state: {
|
|
|
|
artists: [],
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Init the store.
|
2016-02-08 12:21:24 +00:00
|
|
|
*
|
2016-01-17 14:26:24 +00:00
|
|
|
* @param {Array.<Object>} artists The array of artists we got from the server.
|
2015-12-13 04:42:28 +00:00
|
|
|
*/
|
2015-12-22 17:46:54 +00:00
|
|
|
init(artists) {
|
2016-04-05 07:38:10 +00:00
|
|
|
this.all = artists;
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2016-04-17 15:38:06 +00:00
|
|
|
albumStore.init(this.all);
|
|
|
|
|
2015-12-13 04:42:28 +00:00
|
|
|
// Traverse through artists array to get the cover and number of songs for each.
|
2016-04-05 07:38:10 +00:00
|
|
|
each(this.all, artist => {
|
2016-03-05 09:01:12 +00:00
|
|
|
this.setupArtist(artist);
|
2015-12-13 04:42:28 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2016-04-17 15:38:06 +00:00
|
|
|
/**
|
|
|
|
* Set up the (reactive) properties of an artist.
|
|
|
|
*
|
|
|
|
* @param {Object} artist
|
|
|
|
*/
|
2016-03-05 09:01:12 +00:00
|
|
|
setupArtist(artist) {
|
|
|
|
this.getImage(artist);
|
|
|
|
Vue.set(artist, 'playCount', 0);
|
2016-04-17 15:38:06 +00:00
|
|
|
|
|
|
|
// Here we build a list of songs performed by the artist, so that we don't need to traverse
|
|
|
|
// down the "artist > albums > items" route later.
|
|
|
|
// This also makes sure songs in compilation albums are counted as well.
|
|
|
|
Vue.set(artist, 'songs', reduce(artist.albums, (songs, album) => {
|
|
|
|
// If the album is compilation, we cater for the songs contributed by this artist only.
|
|
|
|
if (album.is_compilation) {
|
|
|
|
return songs.concat(filter(album.songs, { contributing_artist_id: artist.id }));
|
|
|
|
}
|
|
|
|
|
2016-04-24 04:37:04 +00:00
|
|
|
// Otherwise, just use all songs in the album.
|
2016-04-17 15:38:06 +00:00
|
|
|
return songs.concat(album.songs);
|
|
|
|
}, []));
|
|
|
|
|
|
|
|
Vue.set(artist, 'songCount', artist.songs.length);
|
|
|
|
|
2016-03-05 09:01:12 +00:00
|
|
|
Vue.set(artist, 'info', null);
|
|
|
|
|
|
|
|
return artist;
|
|
|
|
},
|
|
|
|
|
2016-04-05 07:38:10 +00:00
|
|
|
/**
|
|
|
|
* Get all artists.
|
|
|
|
*
|
|
|
|
* @return {Array.<Object>}
|
|
|
|
*/
|
2016-03-18 04:45:12 +00:00
|
|
|
get all() {
|
2015-12-13 04:42:28 +00:00
|
|
|
return this.state.artists;
|
|
|
|
},
|
|
|
|
|
2016-04-05 07:38:10 +00:00
|
|
|
/**
|
|
|
|
* Set all artists.
|
|
|
|
*
|
|
|
|
* @param {Array.<Object>} value
|
|
|
|
*/
|
|
|
|
set all(value) {
|
|
|
|
this.state.artists = value;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an artist object by its ID.
|
|
|
|
*
|
|
|
|
* @param {Number} id
|
|
|
|
*/
|
2016-03-05 09:01:12 +00:00
|
|
|
byId(id) {
|
2016-03-31 08:58:46 +00:00
|
|
|
return find(this.all, { id });
|
2016-03-05 09:01:12 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2016-04-05 07:38:10 +00:00
|
|
|
* Adds an artist/artists into the current collection.
|
2016-03-05 09:01:12 +00:00
|
|
|
*
|
2016-04-05 07:38:10 +00:00
|
|
|
* @param {Array.<Object>|Object} artists
|
|
|
|
*/
|
|
|
|
add(artists) {
|
|
|
|
artists = [].concat(artists);
|
|
|
|
each(artists, a => this.setupArtist(a));
|
|
|
|
|
|
|
|
this.all = union(this.all, artists);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove artist(s) from the store.
|
|
|
|
*
|
|
|
|
* @param {Array.<Object>|Object} artists
|
2016-03-05 09:01:12 +00:00
|
|
|
*/
|
2016-04-05 07:38:10 +00:00
|
|
|
remove(artists) {
|
|
|
|
this.all = difference(this.all, [].concat(artists));
|
2016-03-05 09:01:12 +00:00
|
|
|
},
|
|
|
|
|
2016-04-05 07:38:10 +00:00
|
|
|
/**
|
|
|
|
* Add album(s) into an artist.
|
|
|
|
*
|
|
|
|
* @param {Object} artist
|
|
|
|
* @param {Array.<Object>|Object} albums
|
|
|
|
*
|
|
|
|
*/
|
2016-03-05 09:01:12 +00:00
|
|
|
addAlbumsIntoArtist(artist, albums) {
|
|
|
|
albums = [].concat(albums);
|
|
|
|
|
2016-03-31 07:44:36 +00:00
|
|
|
artist.albums = union(artist.albums ? artist.albums : [], albums);
|
2016-03-05 09:01:12 +00:00
|
|
|
|
2016-04-05 07:38:10 +00:00
|
|
|
each(albums, album => {
|
2016-03-05 09:01:12 +00:00
|
|
|
album.artist_id = artist.id;
|
|
|
|
album.artist = artist;
|
2016-04-24 04:37:04 +00:00
|
|
|
artist.playCount += album.playCount;
|
2016-03-05 09:01:12 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove album(s) from an artist.
|
|
|
|
*
|
|
|
|
* @param {Object} artist
|
|
|
|
* @param {Array.<Object>|Object} albums
|
|
|
|
*/
|
|
|
|
removeAlbumsFromArtist(artist, albums) {
|
2016-04-24 04:37:04 +00:00
|
|
|
albums = [].concat(albums);
|
|
|
|
artist.albums = difference(artist.albums, albums);
|
|
|
|
each(albums, album => artist.playCount -= album.playCount);
|
2016-03-05 09:01:12 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if an artist is empty.
|
|
|
|
*
|
|
|
|
* @param {Object} artist
|
|
|
|
*
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
isArtistEmpty(artist) {
|
|
|
|
return !artist.albums.length;
|
|
|
|
},
|
|
|
|
|
2016-04-17 15:38:06 +00:00
|
|
|
/**
|
|
|
|
* Determine if the artist is the special "Various Artists".
|
|
|
|
*
|
|
|
|
* @param {Object} artist
|
|
|
|
*
|
|
|
|
* @return {Boolean}
|
|
|
|
*/
|
|
|
|
isVariousArtists(artist) {
|
|
|
|
return artist.id === VARIOUS_ARTISTS_ID;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if the artist is the special "Unknown Artist".
|
|
|
|
*
|
|
|
|
* @param {Object} artist [description]
|
|
|
|
*
|
|
|
|
* @return {Boolean}
|
|
|
|
*/
|
|
|
|
isUnknownArtist(artist) {
|
|
|
|
return artist.id === UNKNOWN_ARTIST_ID;
|
|
|
|
},
|
|
|
|
|
2015-12-13 04:42:28 +00:00
|
|
|
/**
|
|
|
|
* Get all songs performed by an artist.
|
|
|
|
*
|
2015-12-22 09:53:03 +00:00
|
|
|
* @param {Object} artist
|
2015-12-13 04:42:28 +00:00
|
|
|
*
|
2016-01-17 14:26:24 +00:00
|
|
|
* @return {Array.<Object>}
|
2015-12-13 04:42:28 +00:00
|
|
|
*/
|
|
|
|
getSongsByArtist(artist) {
|
|
|
|
return artist.songs;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2015-12-22 09:53:03 +00:00
|
|
|
* Get the artist's image.
|
2015-12-13 04:42:28 +00:00
|
|
|
*
|
2015-12-22 09:53:03 +00:00
|
|
|
* @param {Object} artist
|
2015-12-13 04:42:28 +00:00
|
|
|
*
|
2016-01-17 14:26:24 +00:00
|
|
|
* @return {String}
|
2015-12-13 04:42:28 +00:00
|
|
|
*/
|
2015-12-22 09:53:03 +00:00
|
|
|
getImage(artist) {
|
2016-04-05 07:38:10 +00:00
|
|
|
if (!artist.image) {
|
|
|
|
// Try to get an image from one of the albums.
|
|
|
|
artist.image = config.unknownCover;
|
|
|
|
|
|
|
|
artist.albums.every(album => {
|
|
|
|
// If there's a "real" cover, use it.
|
|
|
|
if (album.image !== config.unknownCover) {
|
|
|
|
artist.image = album.cover;
|
|
|
|
|
|
|
|
// I want to break free.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
2015-12-22 09:53:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return artist.image;
|
2015-12-13 04:42:28 +00:00
|
|
|
},
|
2016-02-08 12:21:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get top n most-played artists.
|
|
|
|
*
|
|
|
|
* @param {Number} n
|
|
|
|
*
|
|
|
|
* @return {Array.<Object>}
|
|
|
|
*/
|
|
|
|
getMostPlayed(n = 6) {
|
2016-03-06 09:06:28 +00:00
|
|
|
// Only non-unknown artists with actually play count are applicable.
|
2016-04-17 15:38:06 +00:00
|
|
|
// Also, "Various Artists" doesn't count.
|
2016-04-05 07:38:10 +00:00
|
|
|
const applicable = filter(this.all, artist => {
|
2016-04-17 15:38:06 +00:00
|
|
|
return artist.playCount
|
|
|
|
&& !this.isUnknownArtist(artist)
|
|
|
|
&& !this.isVariousArtists(artist);
|
2016-03-06 09:06:28 +00:00
|
|
|
});
|
2016-02-08 12:21:24 +00:00
|
|
|
|
2016-03-31 08:58:46 +00:00
|
|
|
return take(orderBy(applicable, 'playCount', 'desc'), n);
|
2016-02-08 12:21:24 +00:00
|
|
|
},
|
2015-12-13 04:42:28 +00:00
|
|
|
};
|