Browse Source

Fixed issue with empty playlist and removed songs.

KrisVos130 8 years ago
parent
commit
151e3e29d8
2 changed files with 72 additions and 40 deletions
  1. 1 12
      backend/logic/actions/stations.js
  2. 71 28
      backend/logic/stations.js

+ 1 - 12
backend/logic/actions/stations.js

@@ -11,17 +11,6 @@ const notifications = require('../notifications');
 const utils = require('../utils');
 const stations = require('../stations');
 
-const defaultSong = {
-	_id: '60ItHLz5WEA',
-	title: 'Faded',
-	artists: ['Alan Walker'],
-	duration: 212,
-	skipDuration: 0,
-	likes: 0,
-	dislikes: 0,
-	thumbnail: 'https://i.scdn.co/image/2ddde58427f632037093857ebb71a67ddbdec34b'
-};
-
 cache.sub('station.locked', stationId => {
 	io.io.to(`station.${stationId}`).emit("event:stations.locked");
 });
@@ -296,7 +285,7 @@ module.exports = {
 					type: "official",
 					playlist,
 					genres,
-					currentSong: defaultSong
+					currentSong: stations.defaultSong
 				}, next);
 			}
 

+ 71 - 28
backend/logic/stations.js

@@ -108,32 +108,12 @@ module.exports = {
 						async.waterfall([
 
 							(next) => {
-								if (station.currentSongIndex < station.playlist.length - 1) {
-									station.currentSongIndex++;
-									db.models.song.findOne({ _id: station.playlist[station.currentSongIndex] }, (err, song) => {
-										if (!err) {
-											station.currentSong = {
-												_id: song._id,
-												title: song.title,
-												artists: song.artists,
-												duration: song.duration,
-												likes: song.likes,
-												dislikes: song.dislikes,
-												skipDuration: song.skipDuration,
-												thumbnail: song.thumbnail
-											};
-											station.startedAt = Date.now();
-											station.timePaused = 0;
-											next(null, station);
-										}
-									});
-								} else {
-									station.currentSongIndex = 0;
-									_this.calculateSongForStation(station, (err, newPlaylist) => {
-										console.log('New playlist: ', newPlaylist);
-										if (!err) {
-											db.models.song.findOne({ _id: newPlaylist[0] }, (err, song) => {
-												if (song) {
+								if (station.playlist.length > 0) {
+									function func() {
+										if (station.currentSongIndex < station.playlist.length - 1) {
+											station.currentSongIndex++;
+											db.models.song.findOne({_id: station.playlist[station.currentSongIndex]}, (err, song) => {
+												if (!err && song) {
 													station.currentSong = {
 														_id: song._id,
 														title: song.title,
@@ -146,12 +126,64 @@ module.exports = {
 													};
 													station.startedAt = Date.now();
 													station.timePaused = 0;
-													station.playlist = newPlaylist;
 													next(null, station);
+												} else {
+													station.currentSongIndex++;
+													func();
 												}
 											});
+										} else {
+											station.currentSongIndex = 0;
+											_this.calculateSongForStation(station, (err, newPlaylist) => {
+												console.log('New playlist: ', newPlaylist);
+												if (!err) {
+													db.models.song.findOne({_id: newPlaylist[0]}, (err, song) => {
+														if (song) {
+															station.currentSong = {
+																_id: song._id,
+																title: song.title,
+																artists: song.artists,
+																duration: song.duration,
+																likes: song.likes,
+																dislikes: song.dislikes,
+																skipDuration: song.skipDuration,
+																thumbnail: song.thumbnail
+															};
+															station.playlist = newPlaylist;
+														} else {
+															station.currentSong = _this.defaultSong;
+														}
+														station.startedAt = Date.now();
+														station.timePaused = 0;
+														next(null, station);
+													});
+												} else {
+													station.currentSong = _this.defaultSong;
+													station.startedAt = Date.now();
+													station.timePaused = 0;
+													next(null, station);
+												}
+											})
 										}
-									})
+									}
+									func();
+								} else {
+									_this.calculateSongForStation(station, (err, playlist) => {
+										if (!err && playlist.length === 0) {
+											station.currentSongIndex = 0;
+											station.currentSong = _this.defaultSong;
+											station.startedAt = Date.now();
+											station.timePaused = 0;
+											next(null, station);
+										} else {
+											station.currentSongIndex = 0;
+											station.currentSong = playlist[0];
+											station.startedAt = Date.now();
+											station.timePaused = 0;
+											station.playlist = playlist;
+											next(null, station);
+										}
+									});
 								}
 							},
 
@@ -202,6 +234,17 @@ module.exports = {
 
 			return cb(null, station);
 		});
+	},
+
+	defaultSong: {
+		_id: '60ItHLz5WEA',
+		title: 'Faded',
+		artists: ['Alan Walker'],
+		duration: 212,
+		skipDuration: 0,
+		likes: 0,
+		dislikes: 0,
+		thumbnail: 'https://i.scdn.co/image/2ddde58427f632037093857ebb71a67ddbdec34b'
 	}
 
 };