Bläddra i källkod

Worked on community stations.

KrisVos130 8 år sedan
förälder
incheckning
d857096cc0

+ 11 - 4
backend/logic/actions/stations.js

@@ -194,7 +194,8 @@ module.exports = {
 
 			if (station) {
 				notifications.unschedule(`stations.nextSong?id=${stationId}`);
-				notifications.schedule(`stations.nextSong?id=${stationId}`, 100);
+				//notifications.schedule(`stations.nextSong?id=${stationId}`, 100);
+				stations.skipStation(stationId)();
 			}
 			else {
 				cb({ status: 'failure', message: `That station doesn't exist` });
@@ -290,6 +291,7 @@ module.exports = {
 				if (station.paused) {
 					station.paused = false;
 					station.timePaused += (Date.now() - station.pausedAt);
+					console.log("&&&", station.timePaused, station.pausedAt, Date.now(), station.timePaused);
 					db.models.station.update({_id: stationId}, {$set: {paused: false}, $inc: {timePaused: Date.now() - station.pausedAt}}, () => {
 						stations.updateStation(stationId, (err, station) => {
 							cache.pub('station.resume', stationId);
@@ -391,16 +393,21 @@ module.exports = {
 			if (station.type === 'community') {
 				let has = false;
 				station.queue.forEach((queueSong) => {
-					if (queueSong.songId === songId) {
+					if (queueSong._id === songId) {
 						has = true;
 					}
 				});
 				if (has) return cb({'status': 'failure', 'message': 'That song has already been added to the queue.'});
-				db.models.update({_id: stationId}, {$push: {queue: {_id: songId, title: "Title", duration: 100, requestedBy: userId}}}, (err) => {
+				db.models.station.update({_id: stationId}, {$push: {queue: {_id: songId, title: "Title", duration: 100, requestedBy: userId}}}, (err) => {
+					console.log(err);
 					if (err) return cb({'status': 'failure', 'message': 'Something went wrong.'});
 					stations.updateStation(stationId, (err, station) => {
 						if (err) return cb(err);
+						if (station.currentSong === null || station.currentSong._id === undefined) {
+							notifications.schedule(`stations.nextSong?id=${stationId}`, 1);
+						}
 						cache.pub('station.queueUpdate', stationId);
+						cb({'status': 'success', 'message': 'Added that song to the queue.'});
 					});
 				});
 			} else cb({'status': 'failure', 'message': 'That station is not a community station.'});
@@ -418,7 +425,7 @@ module.exports = {
 					}
 				});
 				if (!has) return cb({'status': 'failure', 'message': 'That song is not in the queue.'});
-				db.models.update({_id: stationId}, {$pull: {queue: {_id: songId}}}, (err) => {
+				db.models.update({_id: stationId}, {$pull: {queue: {songId: songId}}}, (err) => {
 					if (err) return cb({'status': 'failure', 'message': 'Something went wrong.'});
 					stations.updateStation(stationId, (err, station) => {
 						if (err) return cb(err);

+ 1 - 1
backend/logic/actions/users.js

@@ -45,7 +45,7 @@ module.exports = {
 						});
 					}
 					else {
-						next(null, { status: 'failure', message: 'User not found' });
+						next(null, { status: 'failure', message: 'Incorrect password' });
 					}
 				});
 			}

+ 3 - 1
backend/logic/db/schemas/station.js

@@ -23,7 +23,9 @@ module.exports = {
 	privacy: { type: String, enum: ["public", "unlisted", "private"], default: "private" },
 	locked: { type: Boolean, default: false },
 	queue: [{
-		songId: { type: String, required: true },
+		_id: { type: String, required: true },
+		title: { type: String },
+		duration: { type: Number },
 		requestedBy: { type: String, required: true }
 	}]
 };

+ 192 - 174
backend/logic/stations.js

@@ -48,181 +48,10 @@ module.exports = {
 		let _this = this;
 		_this.getStation(stationId, (err, station) => {
 			if (!err) {
+				console.log("###");
 				if (station) {
-					let notification = notifications.subscribe(`stations.nextSong?id=${station._id}`, () => {
-						console.log("NOTIFICATION!!!");
-						_this.getStation(stationId, (err, station) => {
-							if (station) {
-								// notify all the sockets on this station to go to the next song
-								async.waterfall([
-
-									(next) => {
-										if (station.type === "official") {
-											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) {
-																let $set = {};
-
-																$set.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
-																};
-																$set.startedAt = Date.now();
-																$set.timePaused = 0;
-																next(null, $set);
-															} else {
-																db.models.station.update({_id: station._id}, {$inc: {currentSongIndex: 1}}, (err) => {
-																	_this.updateStation(station._id, () => {
-																		func();
-																	});
-																});
-															}
-														});
-													} else {
-														db.models.station.update({_id: station._id}, {$set: {currentSongIndex: 0}}, (err) => {
-															_this.updateStation(station._id, (err, station) => {
-																console.log(12345678, err, station);
-																_this.calculateSongForStation(station, (err, newPlaylist) => {
-																	console.log('New playlist: ', newPlaylist);
-																	if (!err) {
-																		songs.getSong(newPlaylist[0], (err, song) => {
-																			let $set = {};
-																			if (song) {
-																				$set.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 {
-																				$set.currentSong = _this.defaultSong;
-																			}
-																			$set.startedAt = Date.now();
-																			$set.timePaused = 0;
-																			next(null, $set);
-																		});
-																	} else {
-																		let $set = {};
-																		$set.currentSong = _this.defaultSong;
-																		$set.startedAt = Date.now();
-																		$set.timePaused = 0;
-																		next(null, $set);
-																	}
-																})
-															});
-														});
-													}
-												}
-
-												func();
-											} else {
-												_this.calculateSongForStation(station, (err, playlist) => {
-													if (!err && playlist.length === 0) {
-														let $set = {};
-														$set.currentSongIndex = 0;
-														$set.currentSong = _this.defaultSong;
-														$set.startedAt = Date.now();
-														$set.timePaused = 0;
-														next(null, $set);
-													} else {
-														songs.getSong(playlist[0], (err, song) => {
-															let $set = {};
-															if (!err) {
-																$set.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 {
-																$set.currentSong = _this.defaultSong;
-															}
-															$set.currentSongIndex = 0;
-															$set.startedAt = Date.now();
-															$set.timePaused = 0;
-															next(null, $set);
-														});
-													}
-												});
-											}
-										} else {
-											if (station.queue.length > 0) {
-												db.models.station.update({_id: stationId}, {$pull: {_id: station.queue[0]._id}}, (err) => {
-													if (err) return next(err);
-													let $set = {};
-													$set.currentSong = {
-														_id: station.queue[0]._id,
-														title: station.queue[0].title,
-														duration: station.queue[0].duration
-													};
-													$set.startedAt = Date.now();
-													$set.timePaused = 0;
-													next(null, $set);
-												});
-
-												func();
-											} else {
-												next(null, {currentSong: null});
-											}
-										}
-									},
-
-									($set, next) => {
-										db.models.station.update({_id: station._id}, {$set}, (err) => {
-											_this.updateStation(station._id, (err, station) => {
-												next(null, station);
-											});
-										});
-									},
-
-
-								], (err, station) => {
-									console.log(err);
-									if (!err) {
-										io.io.to(`station.${station._id}`).emit("event:songs.next", {
-											currentSong: station.currentSong,
-											startedAt: station.startedAt,
-											paused: station.paused,
-											timePaused: 0
-										});
-										if (station.currentSong !== null && station.currentSong._id !== undefined) {
-											utils.socketsJoinSongRoom(io.io.to(`station.${station._id}`).sockets, `song.${station.currentSong._id}`);
-											console.log("NEXT SONG!!!", station.currentSong);
-											if (!station.paused) {
-												notifications.schedule(`stations.nextSong?id=${station._id}`, station.currentSong.duration * 1000);
-											}
-										} else {
-											console.log("22", !!(station.currentSong));
-											utils.socketsLeaveSongRooms(io.io.to(`station.${station._id}`).sockets, `song.${station.currentSong._id}`);
-										}
-									}
-								});
-							}
-							// the station doesn't exist anymore, unsubscribe from it
-							else {
-								console.log(112233445566, "REMOVE NOTIFICATION");
-								notifications.remove(notification);
-							}
-						});
-					}, true);
+					console.log("###1");
+					let notification = notifications.subscribe(`stations.nextSong?id=${station._id}`, _this.skipStation(station._id), true);
 					if (!station.paused ) {
 						/*if (!station.startedAt) {
 							station.startedAt = Date.now();
@@ -344,6 +173,195 @@ module.exports = {
 		});
 	},
 
+	skipStation: function(stationId) {
+		let _this = this;
+		return () => {
+			console.log("###2");
+			console.log("NOTIFICATION!!!");
+			_this.getStation(stationId, (err, station) => {
+				console.log("###3");
+				if (station) {
+					console.log("###4");
+					// notify all the sockets on this station to go to the next song
+					async.waterfall([
+
+						(next) => {
+							console.log("###5");
+							if (station.type === "official") {
+								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) {
+													let $set = {};
+
+													$set.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
+													};
+													$set.startedAt = Date.now();
+													$set.timePaused = 0;
+													next(null, $set);
+												} else {
+													db.models.station.update({_id: station._id}, {$inc: {currentSongIndex: 1}}, (err) => {
+														_this.updateStation(station._id, () => {
+															func();
+														});
+													});
+												}
+											});
+										} else {
+											db.models.station.update({_id: station._id}, {$set: {currentSongIndex: 0}}, (err) => {
+												_this.updateStation(station._id, (err, station) => {
+													console.log(12345678, err, station);
+													_this.calculateSongForStation(station, (err, newPlaylist) => {
+														console.log('New playlist: ', newPlaylist);
+														if (!err) {
+															songs.getSong(newPlaylist[0], (err, song) => {
+																let $set = {};
+																if (song) {
+																	$set.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 {
+																	$set.currentSong = _this.defaultSong;
+																}
+																$set.startedAt = Date.now();
+																$set.timePaused = 0;
+																next(null, $set);
+															});
+														} else {
+															let $set = {};
+															$set.currentSong = _this.defaultSong;
+															$set.startedAt = Date.now();
+															$set.timePaused = 0;
+															next(null, $set);
+														}
+													})
+												});
+											});
+										}
+									}
+
+									func();
+								} else {
+									_this.calculateSongForStation(station, (err, playlist) => {
+										if (!err && playlist.length === 0) {
+											let $set = {};
+											$set.currentSongIndex = 0;
+											$set.currentSong = _this.defaultSong;
+											$set.startedAt = Date.now();
+											$set.timePaused = 0;
+											next(null, $set);
+										} else {
+											songs.getSong(playlist[0], (err, song) => {
+												let $set = {};
+												if (!err) {
+													$set.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 {
+													$set.currentSong = _this.defaultSong;
+												}
+												$set.currentSongIndex = 0;
+												$set.startedAt = Date.now();
+												$set.timePaused = 0;
+												next(null, $set);
+											});
+										}
+									});
+								}
+							} else {
+								if (station.queue.length > 0) {
+									console.log("##");
+									db.models.station.update({_id: stationId}, {$pull: {queue: {songId: station.queue[0]._id}}}, (err) => {
+										console.log("##1", err);
+										if (err) return next(err);
+										let $set = {};
+										$set.currentSong = {
+											_id: station.queue[0]._id,
+											title: station.queue[0].title,
+											duration: station.queue[0].duration
+										};
+										$set.startedAt = Date.now();
+										$set.timePaused = 0;
+										if (station.paused) {
+											$set.pausedAt = Date.now();
+										}
+										next(null, $set);
+									});
+								} else {
+									console.log("##2");
+									next(null, {currentSong: null});
+								}
+							}
+						},
+
+						($set, next) => {
+							console.log("$set", $set);
+							db.models.station.update({_id: station._id}, {$set}, (err) => {
+								console.log("##2.5", err);
+								_this.updateStation(station._id, (err, station) => {
+									console.log("##2.6", err);
+									next(null, station);
+								});
+							});
+						},
+
+
+					], (err, station) => {
+						console.log("##3", err);
+						if (!err) {
+							io.io.to(`station.${station._id}`).emit("event:songs.next", {
+								currentSong: station.currentSong,
+								startedAt: station.startedAt,
+								paused: station.paused,
+								timePaused: 0
+							});
+							if (station.currentSong !== null && station.currentSong._id !== undefined) {
+								utils.socketsJoinSongRoom(io.io.to(`station.${station._id}`).sockets, `song.${station.currentSong._id}`);
+								console.log("NEXT SONG!!!", station.currentSong);
+								if (!station.paused) {
+									notifications.schedule(`stations.nextSong?id=${station._id}`, station.currentSong.duration * 1000);
+								}
+							} else {
+								console.log("22", !!(station.currentSong));
+								utils.socketsLeaveSongRooms(io.io.to(`station.${station._id}`).sockets, `song.${station.currentSong._id}`);
+							}
+						}
+					});
+				}
+				// the station doesn't exist anymore, unsubscribe from it
+				else {
+					console.log(112233445566, "REMOVE NOTIFICATION");
+					notifications.remove(notification);
+				}
+			});
+		}
+	},
+
 	defaultSong: {
 		_id: '60ItHLz5WEA',
 		title: 'Faded',

+ 2 - 2
frontend/components/Modals/AddSongToQueue.vue

@@ -44,13 +44,13 @@
 		data() {
 			return {
 				querySearch: '',
-				queryResults: [],
+				queryResults: []
 			}
 		},
 		methods: {
 			addSongToQueue: function (songId) {
 				let _this = this;
-				_this.socket.emit('queueSongs.add', songId, data => {
+				_this.socket.emit('stations.addToQueue', _this.$parent.stationId, songId, data => {
 					if (data.status !== 'success') {
 						Toast.methods.addToast(`Error: ${data.message}`, 8000);
 					} else {

+ 25 - 5
frontend/components/Station/Station.vue

@@ -8,8 +8,8 @@
 	<users-sidebar v-if='sidebars.users'></users-sidebar>
 	
 	<div class="station">
-		<h1 v-if="noSong" class="noSong">No song is currently playing.</h1>
-		<div class="columns is-mobile" v-if="!noSong">
+		<h1 v-show="noSong" class="noSong">No song is currently playing.</h1>
+		<div class="columns is-mobile" v-show="!noSong">
 			<div class="column is-8-desktop is-offset-2-desktop is-12-mobile">
 				<div class="video-container">
 					<div id="player"></div>
@@ -19,7 +19,7 @@
 				</div>
 			</div>
 		</div>
-		<div class="columns is-mobile" v-if="!noSong">
+		<div class="columns is-mobile" v-show="!noSong">
 			<div class="column is-8-desktop is-offset-2-desktop is-12-mobile">
 				<div class="columns is-mobile">
 					<div class="column is-8-desktop is-12-mobile">
@@ -86,7 +86,8 @@
 					users: false,
 					playlist: false
 				},
-				noSong: false
+				noSong: false,
+				simpleSong: false
 			}
 		},
 		methods: {
@@ -95,6 +96,7 @@
 			},
 			youtubeReady: function() {
 				let local = this;
+				console.log("@@5", local.currentSong._id);
 				local.player = new YT.Player("player", {
 					height: 270,
 					width: 480,
@@ -102,11 +104,13 @@
 					playerVars: { controls: 0, iv_load_policy: 3, rel: 0, showinfo: 0 },
 					events: {
 						'onReady': function(event) {
+							console.log("@@6");
 							local.playerReady = true;
 							let volume = parseInt(localStorage.getItem("volume"));
 							volume = (typeof volume === "number") ? volume : 20;
 							local.player.setVolume(volume);
 							if (volume > 0) local.player.unMute();
+							console.log("@@7");
 							local.playVideo();
 						},
 						'onStateChange': function(event) {
@@ -115,6 +119,9 @@
 								local.player.seekTo(local.getTimeElapsed() / 1000, true);
 								if (local.paused) local.player.pauseVideo();
 							}
+						},
+						'onError': function(err) {
+							console.log("@@@@", err, local.currentSong._id);
 						}
 					}
 				});
@@ -129,7 +136,9 @@
 			},
 			playVideo: function() {
 				let local = this;
+				console.log("@@9");
 				if (local.playerReady) {
+					console.log("@@@1");
 					local.videoLoading = true;
 					local.player.loadVideoById(local.currentSong._id);
 
@@ -156,12 +165,14 @@
 				let currentTime = Date.now();
 
 				if (local.currentTime !== undefined && local.paused) {
+					console.log("123");
 					local.timePaused += (Date.now() - local.currentTime);
 					local.currentTime = undefined;
 				}
 
 				let duration = (Date.now() - local.startedAt - local.timePaused) / 1000;
 				let songDuration = local.currentSong.duration;
+				//console.log(duration, currentTime, local.startedAt, local.timePaused, local.startedAt - local.timePaused, Date.now() - local.startedAt - local.timePaused, Date.now() - local.startedAt);
 				if (songDuration <= duration) local.player.pauseVideo();
 				if ((!local.paused) && duration <= songDuration) local.timeElapsed = local.formatTime(duration);
 			},
@@ -262,6 +273,8 @@
 							_this.paused = data.paused;
 							_this.timePaused = data.timePaused;
 							if (data.currentSong) {
+								_this.noSong = false;
+								_this.simpleSong = (data.currentSong.likes === -1 && data.currentSong.dislikes === -1);
 								_this.youtubeReady();
 								_this.playVideo();
 								_this.socket.emit('songs.getOwnSongRatings', data.currentSong._id, data => {
@@ -271,6 +284,8 @@
 									}
 								});
 							} else {
+								if (_this.playerReady) _this.player.pauseVideo();
+								console.log("NO SONG TRUE1", data);
 								_this.noSong = true;
 							}
 						} else {
@@ -283,10 +298,13 @@
 						_this.paused = data.paused;
 						_this.timePaused = data.timePaused;
 						if (data.currentSong) {
+							_this.noSong = false;
+							_this.simpleSong = (data.currentSong.likes === -1 && data.currentSong.dislikes === -1);
 							if (!_this.playerReady) {
 								_this.youtubeReady();
+							} else {
+								_this.playVideo();
 							}
-							_this.playVideo();
 							_this.socket.emit('songs.getOwnSongRatings', data.currentSong._id, (data) => {
 								console.log(data);
 								if (_this.currentSong._id === data.songId) {
@@ -295,6 +313,8 @@
 								}
 							});
 						} else {
+							if (_this.playerReady) _this.player.pauseVideo();
+							console.log("NO SONG TRUE2", data);
 							_this.noSong = true;
 						}
 					});