mirror of
https://github.com/koel/koel
synced 2024-12-24 19:43:06 +00:00
412 lines
12 KiB
JavaScript
412 lines
12 KiB
JavaScript
import Vue from 'vue';
|
|
import _ from 'lodash';
|
|
|
|
import http from '../services/http';
|
|
import utils from '../services/utils';
|
|
import stub from '../stubs/song';
|
|
import favoriteStore from './favorite';
|
|
import sharedStore from './shared';
|
|
import userStore from './user';
|
|
import albumStore from './album';
|
|
import artistStore from './artist';
|
|
import ls from '../services/ls';
|
|
|
|
export default {
|
|
stub,
|
|
albums: [],
|
|
cache: {},
|
|
|
|
state: {
|
|
/**
|
|
* All songs in the store
|
|
*
|
|
* @type {Array}
|
|
*/
|
|
songs: [stub],
|
|
|
|
/**
|
|
* The recently played songs **in the current session**
|
|
*
|
|
* @type {Array}
|
|
*/
|
|
recent: [],
|
|
},
|
|
|
|
/**
|
|
* Init the store.
|
|
*
|
|
* @param {Array.<Object>} albums The array of albums to extract our songs from
|
|
*/
|
|
init(albums) {
|
|
// Iterate through the albums. With each, add its songs into our master song list.
|
|
this.state.songs = albums.reduce((songs, album) => {
|
|
// While doing so, we populate some other information into the songs as well.
|
|
album.songs.forEach(song => {
|
|
song.fmtLength = utils.secondsToHis(song.length);
|
|
|
|
// Manually set these additional properties to be reactive
|
|
Vue.set(song, 'playCount', 0);
|
|
Vue.set(song, 'album', album);
|
|
Vue.set(song, 'liked', false);
|
|
Vue.set(song, 'lyrics', null);
|
|
Vue.set(song, 'playbackState', 'stopped');
|
|
|
|
// Cache the song, so that byId() is faster
|
|
this.cache[song.id] = song;
|
|
});
|
|
|
|
return songs.concat(album.songs);
|
|
}, []);
|
|
},
|
|
|
|
/**
|
|
* Initializes the interaction (like/play count) information.
|
|
*
|
|
* @param {Array.<Object>} interactions The array of interactions of the current user
|
|
*/
|
|
initInteractions(interactions) {
|
|
favoriteStore.clear();
|
|
|
|
interactions.forEach(interaction => {
|
|
var song = this.byId(interaction.song_id);
|
|
|
|
if (!song) {
|
|
return;
|
|
}
|
|
|
|
song.liked = interaction.liked;
|
|
song.playCount = interaction.play_count;
|
|
song.album.playCount += song.playCount;
|
|
song.album.artist.playCount += song.playCount;
|
|
|
|
if (song.liked) {
|
|
favoriteStore.add(song);
|
|
}
|
|
});
|
|
},
|
|
|
|
/**
|
|
* Get the total duration of some songs.
|
|
*
|
|
* @param {Array.<Object>} songs
|
|
* @param {Boolean} toHis Whether to convert the duration into H:i:s format
|
|
*
|
|
* @return {Float|String}
|
|
*/
|
|
getLength(songs, toHis) {
|
|
var duration = songs.reduce((length, song) => length + song.length, 0);
|
|
|
|
if (toHis) {
|
|
return utils.secondsToHis(duration);
|
|
}
|
|
|
|
return duration;
|
|
},
|
|
|
|
/**
|
|
* Get all songs.
|
|
*
|
|
* @return {Array.<Object>}
|
|
*/
|
|
all() {
|
|
return this.state.songs;
|
|
},
|
|
|
|
/**
|
|
* Get a song by its ID.
|
|
*
|
|
* @param {String} id
|
|
*
|
|
* @return {Object}
|
|
*/
|
|
byId(id) {
|
|
return this.cache[id];
|
|
},
|
|
|
|
/**
|
|
* Get songs by their IDs.
|
|
*
|
|
* @param {Array.<String>} ids
|
|
*
|
|
* @return {Array.<Object>}
|
|
*/
|
|
byIds(ids) {
|
|
return ids.map(id => this.byId(id));
|
|
},
|
|
|
|
/**
|
|
* 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}
|
|
*/
|
|
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);
|
|
},
|
|
|
|
/**
|
|
* Get extra song information (lyrics, artist info, album info).
|
|
*
|
|
* @param {Object} song
|
|
* @param {?Function} cb
|
|
*/
|
|
getInfo(song, cb = null) {
|
|
// Check if the song's info has been retrieved before.
|
|
if (song.infoRetrieved) {
|
|
if (cb) {
|
|
cb();
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
http.get(`${song.id}/info`, response => {
|
|
var data = response.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) {
|
|
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;
|
|
|
|
// 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;
|
|
}
|
|
|
|
song.infoRetrieved = true;
|
|
|
|
if (cb) {
|
|
cb();
|
|
}
|
|
});
|
|
},
|
|
|
|
/**
|
|
* Scrobble a song (using Last.fm).
|
|
*
|
|
* @param {Object} song
|
|
* @param {?Function} cb
|
|
*/
|
|
scrobble(song, cb = null) {
|
|
if (!window.useLastfm || !userStore.current().preferences.lastfm_session_key) {
|
|
return;
|
|
}
|
|
|
|
http.post(`${song.id}/scrobble/${song.playStartTime}`, () => {
|
|
if (cb) {
|
|
cb();
|
|
}
|
|
});
|
|
},
|
|
|
|
/**
|
|
* Update song data.
|
|
*
|
|
* @param {Array.<Object>} songs An array of song
|
|
* @param {Object} data
|
|
* @param {?Function} successCb
|
|
* @param {?Function} errorCb
|
|
*/
|
|
update(songs, data, successCb = null, errorCb = null) {
|
|
if (!userStore.current().is_admin) {
|
|
return;
|
|
}
|
|
|
|
http.put('songs', {
|
|
data,
|
|
songs: _.pluck(songs, 'id'),
|
|
}, response => {
|
|
response.data.forEach(song => {
|
|
this.syncUpdatedSong(song);
|
|
});
|
|
|
|
if (successCb) {
|
|
successCb();
|
|
}
|
|
}, () => {
|
|
if (errorCb) {
|
|
errorCb();
|
|
}
|
|
});
|
|
},
|
|
|
|
/**
|
|
* Sync an updated song into our current library.
|
|
*
|
|
* This is one of the most ugly functions I've written, if not the worst itself.
|
|
* Sorry, future me.
|
|
* Sorry guys.
|
|
* Forgive me.
|
|
*
|
|
* @param {Object} updatedSong The updated song, with albums and whatnot.
|
|
*
|
|
* @return {?Object} The updated song.
|
|
*/
|
|
syncUpdatedSong(updatedSong) {
|
|
// Cases:
|
|
// 1. Album doesn't change (and then, artist doesn't either)
|
|
// 2. Album changes (note that a new album might have been created) and
|
|
// 2.a. Artist remains the same.
|
|
// 2.b. Artist changes as well. Note that an artist might have been created.
|
|
|
|
// Find the original song,
|
|
var originalSong = this.byId(updatedSong.id);
|
|
|
|
if (!originalSong) {
|
|
return;
|
|
}
|
|
|
|
// and keep track of original album/artist.
|
|
var originalAlbumId = originalSong.album.id;
|
|
var originalArtistId = originalSong.album.artist.id;
|
|
|
|
// First, we update the title and the lyrics
|
|
originalSong.title = updatedSong.title;
|
|
originalSong.lyrics = updatedSong.lyrics;
|
|
|
|
if (updatedSong.album.id === originalAlbumId) { // case 1
|
|
// Nothing to do
|
|
} else { // case 2
|
|
// First, remove it from its old album
|
|
albumStore.removeSongsFromAlbum(originalSong.album, originalSong);
|
|
|
|
var existingAlbum = albumStore.byId(updatedSong.album.id);
|
|
var newAlbumCreated = !existingAlbum;
|
|
|
|
if (!newAlbumCreated) {
|
|
// The song changed to an existing album. We now add it to such album.
|
|
albumStore.addSongsIntoAlbum(existingAlbum, originalSong);
|
|
} else {
|
|
// A new album was created. We:
|
|
// - Add the new album into our collection
|
|
// - Add the song into it
|
|
albumStore.addSongsIntoAlbum(updatedSong.album, originalSong);
|
|
albumStore.append(updatedSong.album);
|
|
}
|
|
|
|
if (updatedSong.album.artist.id === originalArtistId) { // case 2.a
|
|
// Same artist, but what if the album is new?
|
|
if (newAlbumCreated) {
|
|
artistStore.addAlbumsIntoArtist(artistStore.byId(originalArtistId), updatedSong.album);
|
|
}
|
|
} else { // case 2.b
|
|
// The artist changes.
|
|
var existingArtist = artistStore.byId(updatedSong.album.artist.id);
|
|
|
|
if (!existingArtist) {
|
|
// New artist created. We:
|
|
// - Add the album into it, because now it MUST BE a new album
|
|
// (there's no "new artist with existing album" in our system).
|
|
// - Add the new artist into our collection
|
|
artistStore.addAlbumsIntoArtist(updatedSong.album.artist, updatedSong.album);
|
|
artistStore.append(updatedSong.album.artist);
|
|
}
|
|
}
|
|
|
|
// As a last step, we purify our library of empty albums/artists.
|
|
if (albumStore.isAlbumEmpty(albumStore.byId(originalAlbumId))) {
|
|
albumStore.remove(albumStore.byId(originalAlbumId));
|
|
}
|
|
|
|
if (artistStore.isArtistEmpty(artistStore.byId(originalArtistId))) {
|
|
artistStore.remove(artistStore.byId(originalArtistId));
|
|
}
|
|
|
|
// Now we make sure the next call to info() get the refreshed, correct info.
|
|
originalSong.infoRetrieved = false;
|
|
}
|
|
|
|
return originalSong;
|
|
},
|
|
|
|
/**
|
|
* Get a song's playable source URL.
|
|
*
|
|
* @param {Object} song
|
|
*
|
|
* @return {string} The source URL, with JWT token appended.
|
|
*/
|
|
getSourceUrl(song) {
|
|
return `${sharedStore.state.cdnUrl}api/${song.id}/play?jwt-token=${ls.get('jwt-token')}`;
|
|
},
|
|
|
|
/**
|
|
* 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;
|
|
},
|
|
|
|
/**
|
|
* Called when the application is tore down.
|
|
* Reset stuff.
|
|
*/
|
|
teardown() {
|
|
this.state.recent = [];
|
|
},
|
|
};
|