Browse Source

Added station forceSkipping and fixed more station issues.

KrisVos130 8 years ago
parent
commit
32d8e645a4

+ 20 - 6
backend/logic/actions/songs.js

@@ -181,12 +181,26 @@ module.exports = {
 	getOwnSongRatings: (sessionId, songId, cb) => {
 		cache.hget('sessions', sessionId, (err, session) => {
 			cache.hget('userSessions', session.userSessionId, (err, userSession) => {
-				db.models.user.findOne({_id: userSession.userId}, (err, user) => {
-					console.log({ status: 'success', songId: songId, liked: (user.liked.indexOf(songId) !== -1), disliked: (user.disliked.indexOf(songId) !== -1) })
-					console.log(user.liked)
-					console.log(user.disliked)
-					return cb({ status: 'success', songId: songId, liked: (user.liked.indexOf(songId) !== -1), disliked: (user.disliked.indexOf(songId) !== -1) });
-				});
+				if (!err && userSession) {
+					db.models.user.findOne({_id: userSession.userId}, (err, user) => {
+						console.log({
+							status: 'success',
+							songId: songId,
+							liked: (user.liked.indexOf(songId) !== -1),
+							disliked: (user.disliked.indexOf(songId) !== -1)
+						})
+						console.log(user.liked)
+						console.log(user.disliked)
+						return cb({
+							status: 'success',
+							songId: songId,
+							liked: (user.liked.indexOf(songId) !== -1),
+							disliked: (user.disliked.indexOf(songId) !== -1)
+						});
+					});
+				} else {
+					return cb({ status: 'failure', message: 'Not logged in.' });
+				}
 			});
 		});
 	},

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

@@ -148,6 +148,25 @@ module.exports = {
 		});
 	},
 
+	forceSkip: (session, stationId, cb) => {
+		//TODO Require admin
+
+		stations.initializeAndReturnStation(stationId, (err, station) => {
+
+			if (err && err !== true) {
+				return cb({ status: 'error', message: 'An error occurred while skipping the station' });
+			}
+
+			if (station) {
+				notifications.unschedule(`stations.nextSong?id=${stationId}`);
+				notifications.schedule(`stations.nextSong?id=${stationId}`, 100);
+			}
+			else {
+				cb({ status: 'failure', message: `That station doesn't exist` });
+			}
+		});
+	},
+
 	/**
 	 * Leaves the users current station
 	 *
@@ -217,7 +236,7 @@ module.exports = {
 						if (!err) {
 							db.models.station.update({_id: stationId}, {$set: {paused: true}}, () => {
 								cache.pub('station.pause', stationId);
-								notifications.unschedule(stationId);
+								notifications.unschedule(`stations.nextSong?id=${stationId}`);
 								cb({ status: 'success' });
 							});
 						} else {

+ 2 - 2
backend/logic/notifications.js

@@ -53,8 +53,8 @@ const lib = {
 	 * @return {Object} - the subscription object
 	 */
 	subscribe: (name, cb, unique = false) => {
-		if (unique && subscriptions.find((subscription) => subscription.name == name)) return;
-		let subscription = { name: crypto.createHash('md5').update(`_notification:${name}_`).digest('hex'), cb };
+		if (unique && subscriptions.find((subscription) => subscription.originalName == name)) return;
+		let subscription = { originalName: name, name: crypto.createHash('md5').update(`_notification:${name}_`).digest('hex'), cb };
 		subscriptions.push(subscription);
 		return subscription;
 	},

+ 106 - 106
backend/logic/stations.js

@@ -106,126 +106,126 @@ 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) {
-						if (!station.paused) {
-							// notify all the sockets on this station to go to the next song
-							async.waterfall([
+						// 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;
-															}
-															station.startedAt = Date.now();
-															station.timePaused = 0;
-															next(null, station);
-														});
-													} 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.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) {
+									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;
+												}
 												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
-								});
-								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);
+						], (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!!!");
+							if (!station.paused) {
+								notifications.schedule(`stations.nextSong?id=${station._id}`, station.currentSong.duration * 1000);
+							}
+							//skipTimeout = setTimeout(skipSongTemp, station.currentSong.duration * 1000);
+						});
 					}
 					// the station doesn't exist anymore or is paused, unsubscribe from it
 					else {

+ 7 - 0
frontend/components/Station/Station.vue

@@ -208,6 +208,13 @@
 				this.paused = true;
 				if (this.playerReady) this.player.pauseVideo();
 			},
+			skipStation: function () {
+				let _this = this;
+				_this.socket.emit('stations.forceSkip', _this.stationId, res => {
+					//TODO Toasts
+					console.log(res);
+				});
+			},
 			resumeStation: function () {
 				let _this = this;
 				_this.socket.emit('stations.resume', _this.stationId, res => {

+ 6 - 1
frontend/components/Station/StationHeader.vue

@@ -16,7 +16,12 @@
 					<i class="material-icons">flag</i>
 				</span>
 			</a>
-			<a class="nav-item" href="#">
+			<a v-if="$parent.$parent.role === 'admin'" class="nav-item" href="#" @click="$parent.skipStation()">
+				<span class="icon">
+					<i class="material-icons left">skip_next</i>
+				</span>
+			</a>
+			<a v-if="$parent.$parent.role !== 'admin' && $parent.$parent.loggedIn" class="nav-item" href="#" @click="$parent.voteSkipStation()">
 				<span class="icon">
 					<i class="material-icons left">skip_next</i>
 				</span>