Browse Source

fix: liking/disliking was broken if a song was already liked/disliked, and error handling wasn't working correctly for liking/disliking/unliking/undisliking

Kristian Vos 3 years ago
parent
commit
b2b1fcd4a9
1 changed files with 46 additions and 34 deletions
  1. 46 34
      backend/logic/actions/songs.js

+ 46 - 34
backend/logic/actions/songs.js

@@ -851,38 +851,40 @@ export default {
 							{
 								session,
 								namespace: "playlists",
-								action: "addSongToPlaylist",
-								args: [false, youtubeId, user.likedSongsPlaylist]
+								action: "removeSongFromPlaylist",
+								args: [youtubeId, user.dislikedSongsPlaylist]
 							},
 							this
 						)
 						.then(res => {
-							if (res.status === "error") {
-								if (res.message === "That song is already in the playlist")
-									return next("You have already liked this song.");
-								return next("Unable to add song to the 'Liked Songs' playlist.");
-							}
-
-							return next(null, song, user.dislikedSongsPlaylist);
+							if (res.status === "error")
+								return next("Unable to remove song from the 'Disliked Songs' playlist.");
+							return next(null, song, user.likedSongsPlaylist);
 						})
 						.catch(err => next(err));
+
+					
 				},
 
-				(song, dislikedSongsPlaylist, next) => {
-					this.module
+				(song, likedSongsPlaylist, next) => {
+					return this.module
 						.runJob(
 							"RUN_ACTION2",
 							{
 								session,
 								namespace: "playlists",
-								action: "removeSongFromPlaylist",
-								args: [youtubeId, dislikedSongsPlaylist]
+								action: "addSongToPlaylist",
+								args: [false, youtubeId, likedSongsPlaylist]
 							},
 							this
 						)
 						.then(res => {
-							if (res.status === "error")
-								return next("Unable to remove song from the 'Disliked Songs' playlist.");
+							if (res.status === "error") {
+								if (res.message === "That song is already in the playlist")
+									return next("You have already liked this song.");
+								return next("Unable to add song to the 'Liked Songs' playlist.");
+							}
+
 							return next(null, song);
 						})
 						.catch(err => next(err));
@@ -894,7 +896,7 @@ export default {
 						.catch(err => next(err));
 				}
 			],
-			async (err, song, { likes, dislikes }) => {
+			async (err, song, ratings) => {
 				if (err) {
 					err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
 					this.log(
@@ -905,6 +907,8 @@ export default {
 					return cb({ status: "error", message: err });
 				}
 
+				const { likes, dislikes } = ratings;
+
 				SongsModule.runJob("UPDATE_SONG", { songId: song._id });
 
 				CacheModule.runJob("PUB", {
@@ -968,38 +972,40 @@ export default {
 							{
 								session,
 								namespace: "playlists",
-								action: "addSongToPlaylist",
-								args: [false, youtubeId, user.dislikedSongsPlaylist]
+								action: "removeSongFromPlaylist",
+								args: [youtubeId, user.likedSongsPlaylist]
 							},
 							this
 						)
 						.then(res => {
-							if (res.status === "error") {
-								if (res.message === "That song is already in the playlist")
-									return next("You have already disliked this song.");
-								return next("Unable to add song to the 'Disliked Songs' playlist.");
-							}
-
-							return next(null, song, user.likedSongsPlaylist);
+							if (res.status === "error")
+								return next("Unable to remove song from the 'Liked Songs' playlist.");
+							return next(null, song, user.dislikedSongsPlaylist);
 						})
 						.catch(err => next(err));
+
+					
 				},
 
-				(song, likedSongsPlaylist, next) => {
-					this.module
+				(song, dislikedSongsPlaylist, next) => {
+					return this.module
 						.runJob(
 							"RUN_ACTION2",
 							{
 								session,
 								namespace: "playlists",
-								action: "removeSongFromPlaylist",
-								args: [youtubeId, likedSongsPlaylist]
+								action: "addSongToPlaylist",
+								args: [false, youtubeId, dislikedSongsPlaylist]
 							},
 							this
 						)
 						.then(res => {
-							if (res.status === "error")
-								return next("Unable to remove song from the 'Liked Songs' playlist.");
+							if (res.status === "error") {
+								if (res.message === "That song is already in the playlist")
+									return next("You have already disliked this song.");
+								return next("Unable to add song to the 'Disliked Songs' playlist.");
+							}
+
 							return next(null, song);
 						})
 						.catch(err => next(err));
@@ -1011,7 +1017,7 @@ export default {
 						.catch(err => next(err));
 				}
 			],
-			async (err, song, { likes, dislikes }) => {
+			async (err, song, ratings) => {
 				if (err) {
 					err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
 					this.log(
@@ -1022,6 +1028,8 @@ export default {
 					return cb({ status: "error", message: err });
 				}
 
+				const { likes, dislikes } = ratings;
+
 				SongsModule.runJob("UPDATE_SONG", { songId: song._id });
 
 				CacheModule.runJob("PUB", {
@@ -1124,7 +1132,7 @@ export default {
 						.catch(err => next(err));
 				}
 			],
-			async (err, song, { likes, dislikes }) => {
+			async (err, song, ratings) => {
 				if (err) {
 					err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
 					this.log(
@@ -1135,6 +1143,8 @@ export default {
 					return cb({ status: "error", message: err });
 				}
 
+				const { likes, dislikes } = ratings;
+
 				SongsModule.runJob("UPDATE_SONG", { songId: song._id });
 
 				CacheModule.runJob("PUB", {
@@ -1239,7 +1249,7 @@ export default {
 						.catch(err => next(err));
 				}
 			],
-			async (err, song, { likes, dislikes }) => {
+			async (err, song, ratings) => {
 				if (err) {
 					err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
 					this.log(
@@ -1249,6 +1259,8 @@ export default {
 					);
 					return cb({ status: "error", message: err });
 				}
+				
+				const { likes, dislikes } = ratings;
 
 				SongsModule.runJob("UPDATE_SONG", { songId: song._id });