Browse Source

Fixed stations.

KrisVos130 8 years ago
parent
commit
ba6cc59abb
4 changed files with 121 additions and 109 deletions
  1. 1 0
      backend/logic/actions/stations.js
  2. 4 1
      backend/logic/notifications.js
  3. 114 106
      backend/logic/stations.js
  4. 2 2
      frontend/App.vue

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

@@ -217,6 +217,7 @@ module.exports = {
 						if (!err) {
 							db.models.station.update({_id: stationId}, {$set: {paused: true}}, () => {
 								cache.pub('station.pause', stationId);
+								notifications.unschedule(stationId);
 								cb({ status: 'success' });
 							});
 						} else {

+ 4 - 1
backend/logic/notifications.js

@@ -67,8 +67,11 @@ const lib = {
 	remove: (subscription) => {
 		let index = subscriptions.indexOf(subscription);
 		if (index) subscriptions.splice(index, 1);
-	}
+	},
 
+	unschedule: (name) => {
+		pub.del(crypto.createHash('md5').update(`_notification:${name}_`).digest('hex'));
+	},
 };
 
 module.exports = lib;

+ 114 - 106
backend/logic/stations.js

@@ -75,6 +75,7 @@ module.exports = {
 	},
 
 	initializeAndReturnStation: function(stationId, cb) {
+		console.log("INITIALIZE", stationId);
 		let _this = this;
 		async.waterfall([
 
@@ -105,130 +106,135 @@ module.exports = {
 				//TODO Recalculate songs if the last song of the station playlist is getting played
 				cache.hget('stations', station._id, (err, station) => {
 					if (station) {
-						// notify all the sockets on this station to go to the next song
-						async.waterfall([
+						if (!station.paused) {
+							// notify all the sockets on this station to go to the next song
+							async.waterfall([
 
-							(next) => {
-								if (station.playlist.length > 0) {
-									function func() {
-										if (station.currentSongIndex < station.playlist.length - 1) {
-											station.currentSongIndex++;
-											songs.getSong(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++;
-													func();
-												}
-											});
-										} else {
-											station.currentSongIndex = 0;
-											_this.calculateSongForStation(station, (err, newPlaylist) => {
-												console.log('New playlist: ', newPlaylist);
-												if (!err) {
-													songs.getSong(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;
-														}
+								(next) => {
+									if (station.playlist.length > 0) {
+										function func() {
+											if (station.currentSongIndex < station.playlist.length - 1) {
+												station.currentSongIndex++;
+												songs.getSong(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.currentSong = _this.defaultSong;
-													station.startedAt = Date.now();
-													station.timePaused = 0;
-													next(null, station);
-												}
-											})
+													} else {
+														station.currentSongIndex++;
+														func();
+													}
+												});
+											} else {
+												station.currentSongIndex = 0;
+												_this.calculateSongForStation(station, (err, newPlaylist) => {
+													console.log('New playlist: ', newPlaylist);
+													if (!err) {
+														songs.getSong(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 {
-											songs.getSong(playlist[0], (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
-													};
-												} else {
-													station.currentSong = _this.defaultSong;
-												}
+
+										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;
-												station.playlist = playlist;
 												next(null, station);
-											});
-										}
-									});
-								}
-							},
+											} else {
+												songs.getSong(playlist[0], (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
+														};
+													} else {
+														station.currentSong = _this.defaultSong;
+													}
+													station.currentSongIndex = 0;
+													station.startedAt = Date.now();
+													station.timePaused = 0;
+													station.playlist = playlist;
+													next(null, station);
+												});
+											}
+										});
+									}
+								},
 
-							(station, next) => {
-								cache.hset('stations', station._id, station, (err) => next(err, station));
-								//TODO Also save to DB
-							},
+								(station, next) => {
+									cache.hset('stations', station._id, station, (err) => next(err, station));
+									//TODO Also save to DB
+								},
 
 
-						], (err, station) => {
-							io.io.to(`station.${stationId}`).emit("event:songs.next", {
-								currentSong: station.currentSong,
-								startedAt: station.startedAt,
-								paused: station.paused,
-								timePaused: 0
+							], (err, station) => {
+								io.io.to(`station.${stationId}`).emit("event:songs.next", {
+									currentSong: station.currentSong,
+									startedAt: station.startedAt,
+									paused: station.paused,
+									timePaused: 0
+								});
+								utils.socketsJoinSongRoom(io.io.to(`station.${stationId}`).sockets, `song.${station.currentSong._id}`);
+								// schedule a notification to be dispatched when the next song ends
+								console.log("NEXT SONG!!!");
+								notifications.schedule(`stations.nextSong?id=${station._id}`, station.currentSong.duration * 1000);
+								//skipTimeout = setTimeout(skipSongTemp, station.currentSong.duration * 1000);
 							});
-							utils.socketsJoinSongRoom(io.io.to(`station.${stationId}`).sockets, `song.${station.currentSong._id}`);
-							// schedule a notification to be dispatched when the next song ends
-							notifications.schedule(`stations.nextSong?id=${station.id}`, station.currentSong.duration * 1000);
-							//skipTimeout = setTimeout(skipSongTemp, station.currentSong.duration * 1000);
-						});
+						}
 					}
-					// the station doesn't exist anymore, unsubscribe from it
+					// the station doesn't exist anymore or is paused, unsubscribe from it
 					else {
 						notifications.remove(notification);
 					}
 				});
 			}, true);
 
+			console.log(station.paused);
 			if (!station.paused) {
 				if (!station.startedAt) {
 					station.startedAt = Date.now();
@@ -239,10 +245,12 @@ module.exports = {
 				console.log(timeLeft);
 				if (station.currentSong.duration * 1000 < timeLeft || timeLeft < 0) {
 					console.log("Test");
-					notifications.schedule(`stations.nextSong?id=${station.id}`, 1);
+					notifications.schedule(`stations.nextSong?id=${station._id}`, 1);
 				} else {
-					notifications.schedule(`stations.nextSong?id=${station.id}`, timeLeft);
+					notifications.schedule(`stations.nextSong?id=${station._id}`, timeLeft);
 				}
+			} else {
+				notifications.unschedule(`stations.nextSong?id${station._id}`);
 			}
 
 			return cb(null, station);

+ 2 - 2
frontend/App.vue

@@ -58,10 +58,10 @@
 			});
 		},
 		events: {
-		    'submitOnEnter': function(cb, event) {
+			'submitOnEnter': function(cb, event){
 				if (event.which == 13) {
 					cb();
-					return false;    //<---- Add this line
+					return false;
 				}
 			},
 			'register': function () {