Browse Source

Fixed some issues with likes/dislikes.

KrisVos130 8 years ago
parent
commit
4b41981e3b
2 changed files with 180 additions and 81 deletions
  1. 179 69
      backend/logic/actions/songs.js
  2. 1 12
      frontend/components/Station/Station.vue

+ 179 - 69
backend/logic/actions/songs.js

@@ -219,23 +219,41 @@ module.exports = {
 	 * @param userId
 	 */
 	like: hooks.loginRequired((session, songId, cb, userId) => {
-		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.' });
-			db.models.user.update({_id: userId}, {$push: {liked: songId}, $pull: {disliked: songId}}, err => {
-				if (!err) {
-					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) => {
+		async.waterfall([
+			(next) => {
+				db.models.song.findOne({songId}, next);
+			},
+
+			(song, next) => {
+				if (!song) return next('No song found with that id.');
+				next(null, song);
+			}
+		], (err, song) => {
+			if (err) {
+				err = utils.getError(err);
+				logger.error("SONGS_LIKE", `User "${userId}" failed to like song ${songId}. "${err}"`);
+				return cb({'status': 'failure', 'message': err});
+			}
+			let oldSongId = songId;
+			songId = song._id;
+			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.' });
+				db.models.user.update({_id: userId}, {$push: {liked: songId}, $pull: {disliked: songId}}, err => {
+					if (!err) {
+						db.models.user.count({"liked": songId}, (err, likes) => {
 							if (err) return cb({ status: 'failure', message: 'Something went wrong while liking this song.' });
-							db.models.song.update({songId}, {$set: {likes: likes, dislikes: dislikes}}, (err) => {
+							db.models.user.count({"disliked": songId}, (err, dislikes) => {
 								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.' });
+								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: oldSongId, 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.' });
+				});
 			});
 		});
 	}),
@@ -249,23 +267,41 @@ module.exports = {
 	 * @param userId
 	 */
 	dislike: hooks.loginRequired((session, songId, cb, userId) => {
-		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.' });
-			db.models.user.update({_id: userId}, {$push: {disliked: songId}, $pull: {liked: songId}}, err => {
-				if (!err) {
-					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) => {
+		async.waterfall([
+			(next) => {
+				db.models.song.findOne({songId}, next);
+			},
+
+			(song, next) => {
+				if (!song) return next('No song found with that id.');
+				next(null, song);
+			}
+		], (err, song) => {
+			if (err) {
+				err = utils.getError(err);
+				logger.error("SONGS_DISLIKE", `User "${userId}" failed to like song ${songId}. "${err}"`);
+				return cb({'status': 'failure', 'message': err});
+			}
+			let oldSongId = songId;
+			songId = song._id;
+			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.' });
+				db.models.user.update({_id: userId}, {$push: {disliked: songId}, $pull: {liked: songId}}, err => {
+					if (!err) {
+						db.models.user.count({"liked": songId}, (err, likes) => {
 							if (err) return cb({ status: 'failure', message: 'Something went wrong while disliking this song.' });
-							db.models.song.update({songId}, {$set: {likes: likes, dislikes: dislikes}}, (err) => {
+							db.models.user.count({"disliked": songId}, (err, dislikes) => {
 								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.' });
+								db.models.song.update({_id: songId}, {$set: {likes: likes, dislikes: dislikes}}, (err, res) => {
+									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: oldSongId, 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.' });
+				});
 			});
 		});
 	}),
@@ -279,23 +315,62 @@ module.exports = {
 	 * @param userId
 	 */
 	undislike: hooks.loginRequired((session, songId, cb, userId) => {
-		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.user.update({_id: userId}, {$pull: {liked: songId, disliked: songId}}, err => {
-				if (!err) {
-					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({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.' });
+		async.waterfall([
+			(next) => {
+				db.models.song.findOne({songId}, next);
+			},
+
+			(song, next) => {
+				if (!song) return next('No song found with that id.');
+				next(null, song);
+			}
+		], (err, song) => {
+			if (err) {
+				err = utils.getError(err);
+				logger.error("SONGS_UNDISLIKE", `User "${userId}" failed to like song ${songId}. "${err}"`);
+				return cb({'status': 'failure', 'message': err});
+			}
+			let oldSongId = songId;
+			songId = song._id;
+			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.user.update({_id: userId}, {$pull: {liked: songId, disliked: songId}}, err => {
+					if (!err) {
+						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: oldSongId,
+										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.'});
+				});
 			});
 		});
 	}),
@@ -309,23 +384,41 @@ module.exports = {
 	 * @param userId
 	 */
 	unlike: hooks.loginRequired((session, songId, cb, userId) => {
-		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.user.update({_id: userId}, {$pull: {liked: songId, disliked: songId}}, err => {
-				if (!err) {
-					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({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.' });
+		async.waterfall([
+			(next) => {
+				db.models.song.findOne({songId}, next);
+			},
+
+			(song, next) => {
+				if (!song) return next('No song found with that id.');
+				next(null, song);
+			}
+		], (err, song) => {
+			if (err) {
+				err = utils.getError(err);
+				logger.error("SONGS_UNLIKE", `User "${userId}" failed to like song ${songId}. "${err}"`);
+				return cb({'status': 'failure', 'message': err});
+			}
+			let oldSongId = songId;
+			songId = song._id;
+			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.user.update({_id: userId}, {$pull: {liked: songId, disliked: songId}}, err => {
+					if (!err) {
+						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: oldSongId, 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.' });
+				});
 			});
 		});
 	}),
@@ -339,20 +432,37 @@ module.exports = {
 	 * @param userId
 	 */
 	getOwnSongRatings: hooks.loginRequired((session, songId, cb, userId) => {
-		db.models.user.findOne({_id: userId}, (err, user) => {
-			if (!err && user) {
-				return cb({
-					status: 'success',
-					songId: songId,
-					liked: (user.liked.indexOf(songId) !== -1),
-					disliked: (user.disliked.indexOf(songId) !== -1)
-				});
-			} else {
-				return cb({
-					status: 'failure',
-					message: utils.getError(err)
-				});
+		async.waterfall([
+			(next) => {
+				db.models.song.findOne({songId}, next);
+			},
+
+			(song, next) => {
+				if (!song) return next('No song found with that id.');
+				next(null, song);
 			}
+		], (err, song) => {
+			if (err) {
+				err = utils.getError(err);
+				logger.error("SONGS_GET_OWN_RATINGS", `User "${userId}" failed to get ratings for ${songId}. "${err}"`);
+				return cb({'status': 'failure', 'message': err});
+			}
+			let newSongId = song._id;
+			db.models.user.findOne({_id: userId}, (err, user) => {
+				if (!err && user) {
+					return cb({
+						status: 'success',
+						songId: songId,
+						liked: (user.liked.indexOf(newSongId) !== -1),
+						disliked: (user.disliked.indexOf(newSongId) !== -1)
+					});
+				} else {
+					return cb({
+						status: 'failure',
+						message: utils.getError(err)
+					});
+				}
+			});
 		});
 	})
 };

+ 1 - 12
frontend/components/Station/Station.vue

@@ -379,7 +379,6 @@
 
 						_this.socket.emit('playlists.getFirstSong', _this.privatePlaylistQueueSelected, data => {
 							if (data.status === 'success') {
-								console.log(data.song);
 								let songId = data.song._id;
 								_this.automaticallyRequestedSongId = data.song.songId;
 								_this.socket.emit('stations.addToQueue', _this.station._id, data.song.songId, data2 => {
@@ -395,12 +394,9 @@
 				}
 			},
 			joinStation: function () {
-				console.log("JOINSTATION");
 				let _this = this;
 				_this.socket.emit('stations.join', _this.stationName, res => {
-					console.log("SOCKET STATIONS JOIN");
 					if (res.status === 'success') {
-						console.log("SOCKET STATIONS JOIN SUCCESS");
 						_this.station = {
 							_id: res.data._id,
 							name: _this.stationName,
@@ -419,7 +415,6 @@
 						_this.userCount = res.data.userCount;
 						_this.users = res.data.users;
 						if (res.data.currentSong) {
-							console.log(">> HAS CURRENT SONG");
 							_this.noSong = false;
 							_this.simpleSong = (res.data.currentSong.likes === -1 && res.data.currentSong.dislikes === -1);
 							if (_this.simpleSong) {
@@ -427,14 +422,13 @@
 							}
 							_this.youtubeReady();
 							_this.playVideo();
-							_this.socket.emit('songs.getOwnSongRatings', res.data.currentSong._id, data => {
+							_this.socket.emit('songs.getOwnSongRatings', res.data.currentSong.songId, data => {
 								if (_this.currentSong.songId === data.songId) {
 									_this.liked = data.liked;
 									_this.disliked = data.disliked;
 								}
 							});
 						} else {
-							console.log(">> HAS NO CURRENT SONG");
 							if (_this.playerReady) _this.player.pauseVideo();
 							_this.noSong = true;
 						}
@@ -474,7 +468,6 @@
 			}
 		},
 		ready: function() {
-			console.log("READY");
 			let _this = this;
 
 			Date.currently = () => {
@@ -486,13 +479,11 @@
 			window.stationInterval = 0;
 
 			io.getSocket(socket => {
-				console.log("SOCKET");
 				_this.socket = socket;
 
 				io.removeAllListeners();
 				if (_this.socket.connected) _this.joinStation();
 				io.onConnect(() => {
-					console.log("IO CONNECT");
 					_this.joinStation();
 				});
 				_this.socket.emit('stations.findByName', _this.stationName, res => {
@@ -504,7 +495,6 @@
 					}
 				});
 				_this.socket.on('event:songs.next', data => {
-					console.log("NEXT SONG EVENT");
 					_this.previousSong = (_this.currentSong.songId) ? _this.currentSong : null;
 					_this.currentSong = (data.currentSong) ? data.currentSong : {};
 					_this.startedAt = data.startedAt;
@@ -616,7 +606,6 @@
 				});
 
 				_this.socket.on('event:newOfficialPlaylist', (playlist) => {
-					console.log(playlist);
 					if (this.type === 'official') {
 						this.songsList = playlist;
 					}