koel/resources/assets/js/stores/song.js

266 lines
6.8 KiB
JavaScript
Raw Normal View History

2016-02-09 04:57:08 +00:00
import Vue from 'vue';
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: [],
2016-01-16 05:55:16 +00:00
cache: {},
2015-12-13 04:42:28 +00:00
state: {
/**
* All songs in the store
*
* @type {Array}
*/
2015-12-13 04:42:28 +00:00
songs: [stub],
/**
* The recently played songs **in the current session**
*
* @type {Array}
*/
recent: [],
2015-12-13 04:42:28 +00:00
},
/**
* Init the store.
*
2016-01-17 14:26:24 +00:00
* @param {Array.<Object>} 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.
2016-02-13 16:59:57 +00:00
this.state.songs = albums.reduce((songs, album) => {
2015-12-13 04:42:28 +00:00
// While doing so, we populate some other information into the songs as well.
2016-02-13 16:59:57 +00:00
album.songs.forEach(song => {
2015-12-13 04:42:28 +00:00
song.fmtLength = utils.secondsToHis(song.length);
2016-02-10 16:54:55 +00:00
// Manually set these additional properties to be reactive
Vue.set(song, 'playCount', 0);
Vue.set(song, 'album', album);
Vue.set(song, 'liked', false);
2016-02-08 13:14:51 +00:00
Vue.set(song, 'lyrics', null);
Vue.set(song, 'playbackState', 'stopped');
2016-01-16 05:55:16 +00:00
// Cache the song, so that byId() is faster
this.cache[song.id] = song;
2015-12-13 04:42:28 +00:00
});
2015-12-13 04:42:28 +00:00
return songs.concat(album.songs);
}, []);
},
2015-12-22 17:46:54 +00:00
/**
* Initializes the interaction (like/play count) information.
*
2016-01-17 14:26:24 +00:00
* @param {Array.<Object>} interactions The array of interactions of the current user
2015-12-22 17:46:54 +00:00
*/
initInteractions(interactions) {
2015-12-30 04:14:47 +00:00
favoriteStore.clear();
2016-02-13 16:59:57 +00:00
interactions.forEach(interaction => {
2015-12-22 17:46:54 +00:00
var song = this.byId(interaction.song_id);
2015-12-22 17:46:54 +00:00
if (!song) {
return;
}
song.liked = interaction.liked;
song.playCount = interaction.play_count;
song.album.playCount += song.playCount;
song.album.artist.playCount += song.playCount;
2015-12-22 17:46:54 +00:00
if (song.liked) {
favoriteStore.add(song);
}
});
},
2016-01-14 08:02:59 +00:00
/**
* Get the total duration of some songs.
*
2016-01-17 14:26:24 +00:00
* @param {Array.<Object>} songs
2016-01-19 11:00:23 +00:00
* @param {Boolean} toHis Whether to convert the duration into H:i:s format
*
2016-01-17 14:26:24 +00:00
* @return {Float|String}
2016-01-14 08:02:59 +00:00
*/
getLength(songs, toHis) {
2016-02-13 16:59:57 +00:00
var duration = songs.reduce((length, song) => length + song.length, 0);
2016-01-14 08:02:59 +00:00
if (toHis) {
return utils.secondsToHis(duration);
}
return duration;
},
2015-12-13 04:42:28 +00:00
/**
* Get all songs.
2015-12-22 17:46:54 +00:00
*
2016-01-17 14:26:24 +00:00
* @return {Array.<Object>}
2015-12-13 04:42:28 +00:00
*/
all() {
return this.state.songs;
},
/**
2016-01-07 09:03:38 +00:00
* Get a song by its ID.
*
2016-01-17 14:26:24 +00:00
* @param {String} id
*
2015-12-22 17:46:54 +00:00
* @return {Object}
2015-12-13 04:42:28 +00:00
*/
byId(id) {
2016-01-16 05:55:16 +00:00
return this.cache[id];
2015-12-13 04:42:28 +00:00
},
/**
2016-01-07 09:03:38 +00:00
* Get songs by their IDs.
*
2016-01-17 14:26:24 +00:00
* @param {Array.<String>} ids
*
2016-01-17 14:26:24 +00:00
* @return {Array.<Object>}
2015-12-13 04:42:28 +00:00
*/
byIds(ids) {
2016-02-13 16:59:57 +00:00
return ids.map(id => this.byId(id));
2015-12-13 04:42:28 +00:00
},
/**
* Increase a play count for a song.
*
* @param {Object} song
* @param {?Function} cb
*/
registerPlay(song, cb = null) {
var oldCount = song.playCount;
http.post('interaction/play', { song: song.id }, response => {
// Use the data from the server to make sure we don't miss a play from another device.
song.playCount = response.data.play_count;
song.album.playCount += song.playCount - oldCount;
song.album.artist.playCount += song.playCount - oldCount;
if (cb) {
cb();
}
});
},
/**
* Add a song into the "recently played" list.
*
* @param {Object}
2015-12-13 04:42:28 +00:00
*/
addRecent(song) {
// First we make sure that there's no duplicate.
this.state.recent = _.without(this.state.recent, song);
// Then we prepend the song into the list.
this.state.recent.unshift(song);
2015-12-13 04:42:28 +00:00
},
/**
* Get extra song information (lyrics, artist info, album info).
*
* @param {Object} song
* @param {?Function} cb
2015-12-13 04:42:28 +00:00
*/
getInfo(song, cb = null) {
2016-02-08 13:14:51 +00:00
// Check if the song's info has been retrieved before.
if (song.lyrics !== null) {
2015-12-13 04:42:28 +00:00
if (cb) {
cb();
}
2015-12-13 04:42:28 +00:00
return;
}
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;
// Set the artist image on the client side to the retrieved image from server.
if (data.artist_info.image) {
song.album.artist.image = data.artist_info.image;
}
// Convert the duration into i:s
if (data.album_info && data.album_info.tracks) {
2016-02-13 16:59:57 +00:00
data.album_info.tracks.forEach(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
// Set the album on the client side to the retrieved image from server.
if (data.album_info.cover) {
song.album.cover = data.album_info.cover;
}
2015-12-13 04:42:28 +00:00
if (cb) {
cb();
}
});
},
2015-12-20 12:17:35 +00:00
/**
2016-01-07 09:03:38 +00:00
* Scrobble a song (using Last.fm).
*
2016-01-07 09:03:38 +00:00
* @param {Object} song
* @param {?Function} cb
2015-12-20 12:17:35 +00:00
*/
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();
}
});
},
/**
* Get the last n recently played songs.
*
* @param {Number} n
*
* @return {Array.<Object>}
*/
getRecent(n = 10) {
return _.take(this.state.recent, n);
},
/**
* Get top n most-played songs.
*
* @param {Number} n
*
* @return {Array.<Object>}
*/
getMostPlayed(n = 10) {
var songs = _.take(_.sortByOrder(this.state.songs, 'playCount', 'desc'), n);
// Remove those with playCount=0
_.remove(songs, song => !song.playCount);
return songs;
},
2015-12-13 04:42:28 +00:00
};