Procházet zdrojové kódy

Merge pull request #20 from Musare/staging

v1 final changes/fixes
Jonathan před 8 roky
rodič
revize
084c1c6538

+ 55 - 55
backend/logic/actions/songs.js

@@ -19,7 +19,7 @@ cache.sub('song.added', songId => {
 });
 
 cache.sub('song.like', (data) => {
-	utils.emitToRoom(`song.${data.songId}`, 'event:song.like', {songId: data.songId, undisliked: data.undisliked});
+	utils.emitToRoom(`song.${data.songId}`, 'event:song.like', {songId: data.songId, likes: data.likes, dislikes: data.dislikes});
 	utils.socketsFromUser(data.userId, (sockets) => {
 		sockets.forEach((socket) => {
 			socket.emit('event:song.newRatings', {songId: data.songId, liked: true, disliked: false});
@@ -28,7 +28,7 @@ cache.sub('song.like', (data) => {
 });
 
 cache.sub('song.dislike', (data) => {
-	utils.emitToRoom(`song.${data.songId}`, 'event:song.dislike', {songId: data.songId, unliked: data.unliked});
+	utils.emitToRoom(`song.${data.songId}`, 'event:song.dislike', {songId: data.songId, likes: data.likes, dislikes: data.dislikes});
 	utils.socketsFromUser(data.userId, (sockets) => {
 		sockets.forEach((socket) => {
 			socket.emit('event:song.newRatings', {songId: data.songId, liked: false, disliked: true});
@@ -37,7 +37,7 @@ cache.sub('song.dislike', (data) => {
 });
 
 cache.sub('song.unlike', (data) => {
-	utils.emitToRoom(`song.${data.songId}`, 'event:song.unlike', {songId: data.songId});
+	utils.emitToRoom(`song.${data.songId}`, 'event:song.unlike', {songId: data.songId, likes: data.likes, dislikes: data.dislikes});
 	utils.socketsFromUser(data.userId, (sockets) => {
 		sockets.forEach((socket) => {
 			socket.emit('event:song.newRatings', {songId: data.songId, liked: false, disliked: false});
@@ -46,7 +46,7 @@ cache.sub('song.unlike', (data) => {
 });
 
 cache.sub('song.undislike', (data) => {
-	utils.emitToRoom(`song.${data.songId}`, 'event:song.undislike', {songId: data.songId});
+	utils.emitToRoom(`song.${data.songId}`, 'event:song.undislike', {songId: data.songId, likes: data.likes, dislikes: data.dislikes});
 	utils.socketsFromUser(data.userId, (sockets) => {
 		sockets.forEach((socket) => {
 			socket.emit('event:song.newRatings', {songId: data.songId, liked: false, disliked: false});
@@ -106,89 +106,89 @@ module.exports = {
 	}),
 
 	like: hooks.loginRequired((session, songId, cb, userId) => {
-		return cb({ status: 'failure', message: 'Ratings are currently disabled.' });
 		db.models.user.findOne({ _id: userId }, (err, user) => {
 			if (user.liked.indexOf(songId) !== -1) return cb({ status: 'failure', message: 'You have already liked this song.' });
-			let dislikes = 0;
-			if (user.disliked.indexOf(songId) !== -1) dislikes = -1;
-			db.models.song.update({ _id: songId }, { $inc: { likes: 1, dislikes: dislikes } }, err => {
+			db.models.user.update({_id: userId}, {$push: {liked: songId}, $pull: {disliked: songId}}, err => {
 				if (!err) {
-					db.models.user.update({_id: userId}, {$push: {liked: songId}, $pull: {disliked: songId}}, err => {
-						if (!err) {
-							songs.updateSong(songId, (err, song) => {});
-							cache.pub('song.like', JSON.stringify({ songId, userId: session.userId, undisliked: (dislikes === -1) }));
-						} else db.models.song.update({ _id: songId }, { $inc: { likes: -1, dislikes: -dislikes } }, err => {
-							return cb({ status: 'failure', message: 'Something went wrong while liking this song.' });
+					db.models.user.count({"liked": songId}, (err, likes) => {
+						if (err) return cb({ status: 'failure', message: 'Something went wrong while liking this song.' });
+						db.models.user.count({"disliked": songId}, (err, dislikes) => {
+							if (err) return cb({ status: 'failure', message: 'Something went wrong while liking this song.' });
+							db.models.song.update({_id: songId}, {$set: {likes: likes, dislikes: dislikes}}, (err) => {
+								if (err) return cb({ status: 'failure', message: 'Something went wrong while liking this song.' });
+								songs.updateSong(songId, (err, song) => {});
+								cache.pub('song.like', JSON.stringify({ songId, userId: session.userId, likes: likes, dislikes: dislikes }));
+								return cb({ status: 'success', message: 'You have successfully liked this song.' });
+							});
 						});
 					});
-				} else {
-					return cb({ status: 'failure', message: 'Something went wrong while liking this song.' });
-				}
+				} else return cb({ status: 'failure', message: 'Something went wrong while liking this song.' });
 			});
 		});
 	}),
 
 	dislike: hooks.loginRequired((session, songId, cb, userId) => {
-		return cb({ status: 'failure', message: 'Ratings are currently disabled.' });
-		db.models.user.findOne({_id: userId}, (err, user) => {
+		db.models.user.findOne({ _id: userId }, (err, user) => {
 			if (user.disliked.indexOf(songId) !== -1) return cb({ status: 'failure', message: 'You have already disliked this song.' });
-			let likes = 0;
-			if (user.liked.indexOf(songId) !== -1) likes = -1;
-			db.models.song.update({_id: songId}, {$inc: {likes: likes, dislikes: 1}}, (err) => {
+			db.models.user.update({_id: userId}, {$push: {disliked: songId}, $pull: {liked: songId}}, err => {
 				if (!err) {
-					db.models.user.update({_id: userId}, {$push: {disliked: songId}, $pull: {liked: songId}}, (err) => {
-						if (!err) {
-							songs.updateSong(songId, (err, song) => {});
-							cache.pub('song.dislike', JSON.stringify({songId, userId: userId, unliked: (likes === -1)}));
-						} else db.models.song.update({_id: songId}, {$inc: {likes: -likes, dislikes: -1}}, (err) => {
-							return cb({ status: 'failure', message: 'Something went wrong while disliking this song.' });
+					db.models.user.count({"liked": songId}, (err, likes) => {
+						if (err) return cb({ status: 'failure', message: 'Something went wrong while disliking this song.' });
+						db.models.user.count({"disliked": songId}, (err, dislikes) => {
+							if (err) return cb({ status: 'failure', message: 'Something went wrong while disliking this song.' });
+							db.models.song.update({_id: songId}, {$set: {likes: likes, dislikes: dislikes}}, (err) => {
+								if (err) return cb({ status: 'failure', message: 'Something went wrong while disliking this song.' });
+								songs.updateSong(songId, (err, song) => {});
+								cache.pub('song.dislike', JSON.stringify({ songId, userId: session.userId, likes: likes, dislikes: dislikes }));
+								return cb({ status: 'success', message: 'You have successfully disliked this song.' });
+							});
 						});
 					});
-				} else {
-					return cb({ status: 'failure', message: 'Something went wrong while disliking this song.' });
-				}
+				} else return cb({ status: 'failure', message: 'Something went wrong while disliking this song.' });
 			});
 		});
 	}),
 
 	undislike: hooks.loginRequired((session, songId, cb, userId) => {
-		return cb({ status: 'failure', message: 'Ratings are currently disabled.' });
-		db.models.user.findOne({_id: userId}, (err, user) => {
+		db.models.user.findOne({ _id: userId }, (err, user) => {
 			if (user.disliked.indexOf(songId) === -1) return cb({ status: 'failure', message: 'You have not disliked this song.' });
-			db.models.song.update({_id: songId}, {$inc: {dislikes: -1}}, (err) => {
+			db.models.user.update({_id: userId}, {$pull: {liked: songId, disliked: songId}}, err => {
 				if (!err) {
-					db.models.user.update({_id: userId}, {$pull: {disliked: songId}}, (err) => {
-						if (!err) {
-							songs.updateSong(songId, (err, song) => {});
-							cache.pub('song.undislike', JSON.stringify({songId, userId: userId}));
-						} else db.models.song.update({_id: songId}, {$inc: {dislikes: 1}}, (err) => {
-							return cb({ status: 'failure', message: 'Something went wrong while undisliking this song.' });
+					db.models.user.count({"liked": songId}, (err, likes) => {
+						if (err) return cb({ status: 'failure', message: 'Something went wrong while undisliking this song.' });
+						db.models.user.count({"disliked": songId}, (err, dislikes) => {
+							if (err) return cb({ status: 'failure', message: 'Something went wrong while undisliking this song.' });
+							db.models.song.update({_id: songId}, {$set: {likes: likes, dislikes: dislikes}}, (err) => {
+								if (err) return cb({ status: 'failure', message: 'Something went wrong while undisliking this song.' });
+								songs.updateSong(songId, (err, song) => {});
+								cache.pub('song.undislike', JSON.stringify({ songId, userId: session.userId, likes: likes, dislikes: dislikes }));
+								return cb({ status: 'success', message: 'You have successfully undisliked this song.' });
+							});
 						});
 					});
-				} else {
-					return cb({ status: 'failure', message: 'Something went wrong while undisliking this song.' });
-				}
+				} else return cb({ status: 'failure', message: 'Something went wrong while undisliking this song.' });
 			});
 		});
 	}),
 
 	unlike: hooks.loginRequired((session, songId, cb, userId) => {
-		return cb({ status: 'failure', message: 'Ratings are currently disabled.' });
-		db.models.user.findOne({_id: userId}, (err, user) => {
+		db.models.user.findOne({ _id: userId }, (err, user) => {
 			if (user.liked.indexOf(songId) === -1) return cb({ status: 'failure', message: 'You have not liked this song.' });
-			db.models.song.update({_id: songId}, {$inc: {likes: -1}}, (err) => {
+			db.models.user.update({_id: userId}, {$pull: {liked: songId, disliked: songId}}, err => {
 				if (!err) {
-					db.models.user.update({_id: userId}, {$pull: {liked: songId}}, (err) => {
-						if (!err) {
-							songs.updateSong(songId, (err, song) => {});
-							cache.pub('song.unlike', JSON.stringify({songId, userId: userId}));
-						} else db.models.song.update({_id: songId}, {$inc: {likes: 1}}, (err) => {
-							return cb({ status: 'failure', message: 'Something went wrong while unliking this song.' });
+					db.models.user.count({"liked": songId}, (err, likes) => {
+						if (err) return cb({ status: 'failure', message: 'Something went wrong while unliking this song.' });
+						db.models.user.count({"disliked": songId}, (err, dislikes) => {
+							if (err) return cb({ status: 'failure', message: 'Something went wrong while undiking this song.' });
+							db.models.song.update({_id: songId}, {$set: {likes: likes, dislikes: dislikes}}, (err) => {
+								if (err) return cb({ status: 'failure', message: 'Something went wrong while unliking this song.' });
+								songs.updateSong(songId, (err, song) => {});
+								cache.pub('song.unlike', JSON.stringify({ songId, userId: session.userId, likes: likes, dislikes: dislikes }));
+								return cb({ status: 'success', message: 'You have successfully unliked this song.' });
+							});
 						});
 					});
-				} else {
-					return cb({ status: 'failure', message: 'Something went wrong while unliking this song.' });
-				}
+				} else return cb({ status: 'failure', message: 'Something went wrong while unliking this song.' });
 			});
 		});
 	}),

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

@@ -536,7 +536,8 @@ module.exports = {
 				db.models.playlist.findOne({ _id: playlistId }, (err, playlist) => {
 					if (err) return cb(err);
 					if (playlist) {
-						db.models.station.update({_id: stationId}, { $set: { privatePlaylist: playlistId, currentSongIndex: 0 } }, (err) => {
+						let currentSongIndex = (playlist.songs.length > 0) ? playlist.songs.length - 1 : 0;
+						db.models.station.update({_id: stationId}, { $set: { privatePlaylist: playlistId, currentSongIndex: currentSongIndex } }, (err) => {
 							if (err) return cb(err);
 							stations.updateStation(stationId, (err, station) => {
 								if (err) return cb(err);

+ 15 - 9
frontend/components/Station/Station.vue

@@ -163,7 +163,7 @@
 									local.player.seekTo(local.timeBeforePause / 1000, true);
 									local.player.pauseVideo();
 								}
-								if (event.data === 2 && !local.paused && !local.noSong && local.getTimeElapsed() < local.currentSong.duration) {
+								if (event.data === 2 && !local.paused && !local.noSong && (local.getTimeElapsed() / 1000) < local.currentSong.duration) {
 									local.player.seekTo(local.getTimeElapsed() / 1000 + local.currentSong.skipDuration, true);
 									local.player.playVideo();
 								}
@@ -426,8 +426,8 @@
 				_this.socket.on('event:song.like', data => {
 					if (!this.noSong) {
 						if (data.songId === _this.currentSong._id) {
-							_this.currentSong.likes++;
-							if (data.undisliked) _this.currentSong.dislikes--;
+							_this.currentSong.dislikes = data.dislikes;
+							_this.currentSong.likes = data.likes;
 						}
 					}
 				});
@@ -435,21 +435,27 @@
 				_this.socket.on('event:song.dislike', data => {
 					if (!this.noSong) {
 						if (data.songId === _this.currentSong._id) {
-							_this.currentSong.dislikes++;
-							if (data.unliked) _this.currentSong.likes--;
+							_this.currentSong.dislikes = data.dislikes;
+							_this.currentSong.likes = data.likes;
 						}
 					}
 				});
 
 				_this.socket.on('event:song.unlike', data => {
 					if (!this.noSong) {
-						if (data.songId === _this.currentSong._id) _this.currentSong.likes--;
+						if (data.songId === _this.currentSong._id) {
+							_this.currentSong.dislikes = data.dislikes;
+							_this.currentSong.likes = data.likes;
+						}
 					}
 				});
 
 				_this.socket.on('event:song.undislike', data => {
 					if (!this.noSong) {
-						if (data.songId === _this.currentSong._id) _this.currentSong.dislikes--;
+						if (data.songId === _this.currentSong._id) {
+							_this.currentSong.dislikes = data.dislikes;
+							_this.currentSong.likes = data.likes;
+						}
 					}
 				});
 
@@ -490,9 +496,9 @@
 				});
 			});
 
-			
+
 			let volume = parseInt(localStorage.getItem("volume"));
-			volume = (typeof volume === "number") ? volume : 20;
+			volume = (typeof volume === "number" && !isNaN(volume)) ? volume : 20;
 			localStorage.setItem("volume", volume);
 			$("#volumeSlider").val(volume);
 		},

+ 0 - 3
frontend/components/pages/Home.vue

@@ -18,9 +18,6 @@
 							<div v-if="station.privacy !== 'public'" title="This station is not visible to other users." class="station-status">
 								<i class='material-icons'>lock</i>
 							</div>
-							<div v-if="isOwner(station)" title="This is your station." class="station-status">
-								<i class='material-icons'>home</i>
-							</div>
 						</div>
 					</div>
 

+ 0 - 1
frontend/io.js

@@ -58,7 +58,6 @@ export default {
 	init: function (url) {
 		this.socket = window.socket = io(url);
 		this.socket.on('connect', () => {
-			console.log("IO: SOCKET CONNECTED");
 			onConnectCallbacks.forEach((cb) => {
 				cb();
 			});

+ 1 - 1
frontend/main.js

@@ -42,8 +42,8 @@ router.beforeEach(transition => {
 	}
 	if (window.socket) {
 		io.removeAllListeners();
-		io.clear();
 	}
+	io.clear();
 	if (transition.to.loginRequired || transition.to.adminRequired) {
 		auth.getStatus((authenticated, role) => {
 			if (transition.to.loginRequired && !authenticated) transition.redirect('/login');