2015-12-13 04:42:28 +00:00
|
|
|
import _ from 'lodash';
|
|
|
|
|
|
|
|
import http from '../services/http';
|
|
|
|
import utils from '../services/utils';
|
|
|
|
import stub from '../stubs/song';
|
|
|
|
import favoriteStore from './favorite';
|
2015-12-22 17:14:47 +00:00
|
|
|
import userStore from './user';
|
2015-12-13 04:42:28 +00:00
|
|
|
|
|
|
|
export default {
|
|
|
|
stub,
|
|
|
|
albums: [],
|
|
|
|
|
|
|
|
state: {
|
|
|
|
songs: [stub],
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Init the store.
|
|
|
|
*
|
2015-12-22 17:46:54 +00:00
|
|
|
* @param {Array} albums The array of albums to extract our songs from
|
2015-12-13 04:42:28 +00:00
|
|
|
*/
|
2015-12-22 17:46:54 +00:00
|
|
|
init(albums) {
|
2015-12-13 04:42:28 +00:00
|
|
|
// Iterate through the albums. With each, add its songs into our master song list.
|
|
|
|
this.state.songs = _.reduce(albums, (songs, album) => {
|
|
|
|
// While doing so, we populate some other information into the songs as well.
|
|
|
|
_.each(album.songs, song => {
|
|
|
|
song.fmtLength = utils.secondsToHis(song.length);
|
|
|
|
|
|
|
|
// Keep a back reference to the album
|
|
|
|
song.album = album;
|
|
|
|
});
|
|
|
|
|
|
|
|
return songs.concat(album.songs);
|
|
|
|
}, []);
|
|
|
|
},
|
|
|
|
|
2015-12-22 17:46:54 +00:00
|
|
|
/**
|
|
|
|
* Initializes the interaction (like/play count) information.
|
|
|
|
*
|
|
|
|
* @param {Array} interactions The array of interactions of the current user
|
|
|
|
*/
|
|
|
|
initInteractions(interactions) {
|
|
|
|
_.each(interactions, interaction => {
|
|
|
|
var song = this.byId(interaction.song_id);
|
|
|
|
|
|
|
|
if (!song) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
song.liked = interaction.liked;
|
|
|
|
song.playCount = interaction.play_count;
|
|
|
|
|
|
|
|
if (song.liked) {
|
|
|
|
favoriteStore.add(song);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-12-13 04:42:28 +00:00
|
|
|
/**
|
|
|
|
* Get all songs.
|
2015-12-22 17:46:54 +00:00
|
|
|
*
|
|
|
|
* @return {Array}
|
2015-12-13 04:42:28 +00:00
|
|
|
*/
|
|
|
|
all() {
|
|
|
|
return this.state.songs;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a song by its ID
|
|
|
|
*
|
2015-12-22 17:46:54 +00:00
|
|
|
* @param {String} id
|
2015-12-13 04:42:28 +00:00
|
|
|
*
|
2015-12-22 17:46:54 +00:00
|
|
|
* @return {Object}
|
2015-12-13 04:42:28 +00:00
|
|
|
*/
|
|
|
|
byId(id) {
|
|
|
|
return _.find(this.state.songs, {id});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get songs by their ID's
|
|
|
|
*
|
2015-12-22 17:46:54 +00:00
|
|
|
* @param {Array} ids
|
2015-12-13 04:42:28 +00:00
|
|
|
*
|
2015-12-22 17:46:54 +00:00
|
|
|
* @return {Array}
|
2015-12-13 04:42:28 +00:00
|
|
|
*/
|
|
|
|
byIds(ids) {
|
2015-12-14 13:13:12 +00:00
|
|
|
return _.filter(this.state.songs, song => _.contains(ids, song.id));
|
2015-12-13 04:42:28 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Increase a play count for a song.
|
|
|
|
*
|
2015-12-22 17:46:54 +00:00
|
|
|
* @param {Object} song
|
2015-12-13 04:42:28 +00:00
|
|
|
*/
|
|
|
|
registerPlay(song) {
|
|
|
|
// Increase playcount
|
2015-12-14 13:13:12 +00:00
|
|
|
http.post('interaction/play', { id: song.id }, data => song.playCount = data.play_count);
|
2015-12-13 04:42:28 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2015-12-19 16:36:44 +00:00
|
|
|
* Get extra song information (lyrics, artist info, album info).
|
2015-12-13 04:42:28 +00:00
|
|
|
*
|
2015-12-19 16:36:44 +00:00
|
|
|
* @param {Object} song
|
|
|
|
* @param {Function} cb
|
|
|
|
*
|
|
|
|
* @return {Object}
|
2015-12-13 04:42:28 +00:00
|
|
|
*/
|
2015-12-19 16:36:44 +00:00
|
|
|
getInfo(song, cb = null) {
|
2015-12-13 04:42:28 +00:00
|
|
|
if (!_.isUndefined(song.lyrics)) {
|
|
|
|
if (cb) {
|
|
|
|
cb();
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-12-19 16:36:44 +00:00
|
|
|
http.get(`${song.id}/info`, data => {
|
|
|
|
song.lyrics = data.lyrics;
|
|
|
|
|
|
|
|
// If the artist image is not in a nice form, don't use it.
|
|
|
|
if (data.artist_info && typeof data.artist_info.image !== 'string') {
|
|
|
|
data.artist_info.image = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
song.album.artist.info = data.artist_info;
|
|
|
|
|
|
|
|
// Convert the duration into i:s
|
|
|
|
if (data.album_info && data.album_info.tracks) {
|
|
|
|
_.each(data.album_info.tracks, track => track.fmtLength = utils.secondsToHis(track.length));
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the album cover is not in a nice form, don't use it.
|
|
|
|
if (data.album_info && typeof data.album_info.image !== 'string') {
|
|
|
|
data.album_info.image = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
song.album.info = data.album_info;
|
2015-12-13 04:42:28 +00:00
|
|
|
|
|
|
|
if (cb) {
|
|
|
|
cb();
|
|
|
|
}
|
|
|
|
});
|
2015-12-19 16:36:44 +00:00
|
|
|
},
|
2015-12-20 12:17:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Scrobble a song (using Last.fm)
|
|
|
|
*
|
|
|
|
* @param {Object} song
|
|
|
|
* @param {Function} cb
|
|
|
|
*/
|
|
|
|
scrobble(song, cb = null) {
|
2015-12-22 17:46:54 +00:00
|
|
|
if (!window.useLastfm || !userStore.current().preferences.lastfm_session_key) {
|
2015-12-20 12:17:35 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
http.post(`${song.id}/scrobble/${song.playStartTime}`, () => {
|
|
|
|
if (cb) {
|
|
|
|
cb();
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
});
|
|
|
|
},
|
2015-12-13 04:42:28 +00:00
|
|
|
};
|