Browse Source

Added back ratings and fixed more issues with io.

KrisVos130 8 years ago
parent
commit
158b3e2b4f
4 changed files with 68 additions and 63 deletions
  1. 55 55
      backend/logic/actions/songs.js
  2. 12 6
      frontend/components/Station/Station.vue
  3. 0 1
      frontend/io.js
  4. 1 1
      frontend/main.js

+ 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.' });
 			});
 		});
 	}),

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

@@ -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;
+						}
 					}
 				});
 

+ 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');