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-03-31 08:58:46 +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';
|
|
|
|
|
|
|
|
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) {
|
|
|
|
this.state.artists = artists;
|
2015-12-13 04:42:28 +00:00
|
|
|
|
|
|
|
// Traverse through artists array to get the cover and number of songs for each.
|
2016-03-31 07:44:36 +00:00
|
|
|
each(this.state.artists, artist => {
|
2016-03-05 09:01:12 +00:00
|
|
|
this.setupArtist(artist);
|
2015-12-13 04:42:28 +00:00
|
|
|
});
|
2015-12-22 17:46:54 +00:00
|
|
|
|
|
|
|
albumStore.init(this.state.artists);
|
2015-12-13 04:42:28 +00:00
|
|
|
},
|
|
|
|
|
2016-03-05 09:01:12 +00:00
|
|
|
setupArtist(artist) {
|
|
|
|
this.getImage(artist);
|
|
|
|
Vue.set(artist, 'playCount', 0);
|
2016-03-31 07:44:36 +00:00
|
|
|
Vue.set(artist, 'songCount', reduce(artist.albums, (count, album) => count + album.songs.length, 0));
|
2016-03-05 09:01:12 +00:00
|
|
|
Vue.set(artist, 'info', null);
|
|
|
|
|
|
|
|
return artist;
|
|
|
|
},
|
|
|
|
|
2016-03-18 04:45:12 +00:00
|
|
|
get all() {
|
2015-12-13 04:42:28 +00:00
|
|
|
return this.state.artists;
|
|
|
|
},
|
|
|
|
|
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
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Appends a new artist into the current collection.
|
|
|
|
*
|
|
|
|
* @param {Object} artist
|
|
|
|
*/
|
|
|
|
append(artist) {
|
|
|
|
this.state.artists.push(this.setupArtist(artist));
|
|
|
|
},
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
albums.forEach(album => {
|
|
|
|
album.artist_id = artist.id;
|
|
|
|
album.artist = artist;
|
|
|
|
});
|
|
|
|
|
2016-03-31 07:44:36 +00:00
|
|
|
artist.playCount = reduce(artist.albums, (count, album) => count + album.playCount, 0);
|
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-03-31 07:44:36 +00:00
|
|
|
artist.albums = difference(artist.albums, [].concat(albums));
|
|
|
|
artist.playCount = reduce(artist.albums, (count, album) => count + album.playCount, 0);
|
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;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove artist(s) from the store.
|
|
|
|
*
|
|
|
|
* @param {Array.<Object>|Object} artists
|
|
|
|
*/
|
|
|
|
remove(artists) {
|
2016-03-31 07:44:36 +00:00
|
|
|
this.state.artists = difference(this.state.artists, [].concat(artists));
|
2016-03-05 09:01:12 +00:00
|
|
|
},
|
|
|
|
|
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) {
|
|
|
|
if (!artist.songs) {
|
2016-03-31 07:44:36 +00:00
|
|
|
artist.songs = reduce(artist.albums, (songs, album) => songs.concat(album.songs), []);
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
// If the artist already has a proper image, just return it.
|
|
|
|
if (artist.image) {
|
|
|
|
return artist.image;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we try to get an image from one of their albums.
|
|
|
|
artist.image = config.unknownCover;
|
2015-12-13 04:42:28 +00:00
|
|
|
|
|
|
|
artist.albums.every(album => {
|
|
|
|
// If there's a "real" cover, use it.
|
2016-03-13 17:00:32 +00:00
|
|
|
if (album.image !== config.unknownCover) {
|
2015-12-22 09:53:03 +00:00
|
|
|
artist.image = album.cover;
|
2016-02-08 12:21:24 +00:00
|
|
|
|
2015-12-13 04:42:28 +00:00
|
|
|
// 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-03-31 07:44:36 +00:00
|
|
|
const applicable = filter(this.state.artists, artist => {
|
2016-03-06 09:06:28 +00:00
|
|
|
return artist.playCount && artist.id !== 1;
|
|
|
|
});
|
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
|
|
|
};
|