Explorar o código

fix: Removed debug console.logs and replaced mixed async waterfalls

Owen Diffey hai 1 ano
pai
achega
0c804a73c4

+ 20 - 11
backend/logic/actions/apis.js

@@ -139,7 +139,7 @@ export default {
 						next();
 					},
 
-					async () => {
+					next => {
 						this.keepLongJob();
 						this.publishProgress({
 							status: "started",
@@ -147,7 +147,6 @@ export default {
 							message: "Starting up",
 							id: this.toString()
 						});
-						console.log("KRIS@4", this.toString());
 						// await CacheModule.runJob(
 						// 	"RPUSH",
 						// 	{ key: `longJobs.${session.userId}`, value: this.toString() },
@@ -156,9 +155,14 @@ export default {
 
 						SpotifyModule.runJob(
 							"GET_ALTERNATIVE_MEDIA_SOURCES_FOR_TRACKS",
-							{ mediaSources, collectAlternativeMediaSourcesOrigins },
+							{
+								mediaSources,
+								collectAlternativeMediaSourcesOrigins
+							},
 							this
-						);
+						)
+							.then(() => next())
+							.catch(next);
 					}
 				],
 				async err => {
@@ -205,7 +209,7 @@ export default {
 						next();
 					},
 
-					async () => {
+					next => {
 						this.keepLongJob();
 						this.publishProgress({
 							status: "started",
@@ -213,7 +217,6 @@ export default {
 							message: "Starting up",
 							id: this.toString()
 						});
-						console.log("KRIS@4", this.toString());
 						// await CacheModule.runJob(
 						// 	"RPUSH",
 						// 	{ key: `longJobs.${session.userId}`, value: this.toString() },
@@ -224,7 +227,9 @@ export default {
 							"GET_ALTERNATIVE_ALBUM_SOURCES_FOR_ALBUMS",
 							{ albumIds, collectAlternativeAlbumSourcesOrigins },
 							this
-						);
+						)
+							.then(() => next())
+							.catch(next);
 					}
 				],
 				async err => {
@@ -273,7 +278,7 @@ export default {
 						next();
 					},
 
-					async () => {
+					next => {
 						this.keepLongJob();
 						this.publishProgress({
 							status: "started",
@@ -281,7 +286,6 @@ export default {
 							message: "Starting up",
 							id: this.toString()
 						});
-						console.log("KRIS@4", this.toString());
 						// await CacheModule.runJob(
 						// 	"RPUSH",
 						// 	{ key: `longJobs.${session.userId}`, value: this.toString() },
@@ -290,9 +294,14 @@ export default {
 
 						SpotifyModule.runJob(
 							"GET_ALTERNATIVE_ARTIST_SOURCES_FOR_ARTISTS",
-							{ artistIds, collectAlternativeArtistSourcesOrigins },
+							{
+								artistIds,
+								collectAlternativeArtistSourcesOrigins
+							},
 							this
-						);
+						)
+							.then(() => next())
+							.catch(next);
 					}
 				],
 				async err => {

+ 21 - 10
backend/logic/actions/playlists.js

@@ -1588,7 +1588,7 @@ export default {
 						.catch(next);
 				},
 
-				async () => {
+				next => {
 					this.keepLongJob();
 					this.publishProgress({
 						status: "started",
@@ -1596,19 +1596,30 @@ export default {
 						message: "Adding songs to playlist.",
 						id: this.toString()
 					});
-					await CacheModule.runJob(
+					CacheModule.runJob(
 						"RPUSH",
-						{ key: `longJobs.${session.userId}`, value: this.toString() },
-						this
-					);
-					await CacheModule.runJob(
-						"PUB",
 						{
-							channel: "longJob.added",
-							value: { jobId: this.toString(), userId: session.userId }
+							key: `longJobs.${session.userId}`,
+							value: this.toString()
 						},
 						this
-					);
+					)
+						.then(() => {
+							CacheModule.runJob(
+								"PUB",
+								{
+									channel: "longJob.added",
+									value: {
+										jobId: this.toString(),
+										userId: session.userId
+									}
+								},
+								this
+							)
+								.then(() => next())
+								.catch(next);
+						})
+						.catch(next);
 				},
 
 				(nothing, next) => {

+ 23 - 2
backend/logic/actions/soundcloud.js

@@ -9,6 +9,7 @@ import moduleManager from "../../index";
 const DBModule = moduleManager.modules.db;
 const UtilsModule = moduleManager.modules.utils;
 const SoundcloudModule = moduleManager.modules.soundcloud;
+const CacheModule = moduleManager.modules.cache;
 
 export default {
 	/**
@@ -16,7 +17,7 @@ export default {
 	 *
 	 * @returns {{status: string, data: object}}
 	 */
-	fetchNewApiKey: useHasPermission("soundcloud.fetchNewApiKey", function fetchNewApiKey(session, cb) {
+	fetchNewApiKey: useHasPermission("soundcloud.fetchNewApiKey", async function fetchNewApiKey(session, cb) {
 		this.keepLongJob();
 		this.publishProgress({
 			status: "started",
@@ -24,6 +25,16 @@ export default {
 			message: "Fetching new SoundCloud API key.",
 			id: this.toString()
 		});
+		await CacheModule.runJob("RPUSH", { key: `longJobs.${session.userId}`, value: this.toString() }, this);
+		await CacheModule.runJob(
+			"PUB",
+			{
+				channel: "longJob.added",
+				value: { jobId: this.toString(), userId: session.userId }
+			},
+			this
+		);
+
 		SoundcloudModule.runJob("GENERATE_SOUNDCLOUD_API_KEY", {}, this)
 			.then(response => {
 				this.log("SUCCESS", "SOUNDCLOUD_FETCH_NEW_API_KEY", `Fetching new API key was successful.`);
@@ -52,7 +63,7 @@ export default {
 	 *
 	 * @returns {{status: string, data: object}}
 	 */
-	testApiKey: useHasPermission("soundcloud.testApiKey", function testApiKey(session, cb) {
+	testApiKey: useHasPermission("soundcloud.testApiKey", async function testApiKey(session, cb) {
 		this.keepLongJob();
 		this.publishProgress({
 			status: "started",
@@ -60,6 +71,16 @@ export default {
 			message: "Testing SoundCloud API key.",
 			id: this.toString()
 		});
+		await CacheModule.runJob("RPUSH", { key: `longJobs.${session.userId}`, value: this.toString() }, this);
+		await CacheModule.runJob(
+			"PUB",
+			{
+				channel: "longJob.added",
+				value: { jobId: this.toString(), userId: session.userId }
+			},
+			this
+		);
+
 		SoundcloudModule.runJob("TEST_SOUNDCLOUD_API_KEY", {}, this)
 			.then(response => {
 				this.log(

+ 5 - 5
backend/logic/actions/stations.js

@@ -1036,13 +1036,13 @@ export default {
 						.catch(err => next(err));
 				},
 
-				async () => {
-					const response = await StationsModule.stationHistoryModel
+				next => {
+					StationsModule.stationHistoryModel
 						.find({ stationId }, { documentVersion: 0, __v: 0 })
 						.sort({ "payload.skippedAt": -1 })
-						.limit(250);
-
-					return response;
+						.limit(250)
+						.then(response => next(null, response))
+						.catch(next);
 				}
 			],
 			async (err, history) => {

+ 22 - 2
backend/logic/actions/youtube.js

@@ -524,7 +524,7 @@ export default {
 	 *
 	 * @returns {{status: string, data: object}}
 	 */
-	getMissingVideos: useHasPermission("youtube.getMissingVideos", function getMissingVideos(session, cb) {
+	getMissingVideos: useHasPermission("youtube.getMissingVideos", async function getMissingVideos(session, cb) {
 		this.keepLongJob();
 		this.publishProgress({
 			status: "started",
@@ -532,6 +532,16 @@ export default {
 			message: "Fetching missing YouTube videos.",
 			id: this.toString()
 		});
+		await CacheModule.runJob("RPUSH", { key: `longJobs.${session.userId}`, value: this.toString() }, this);
+		await CacheModule.runJob(
+			"PUB",
+			{
+				channel: "longJob.added",
+				value: { jobId: this.toString(), userId: session.userId }
+			},
+			this
+		);
+
 		return YouTubeModule.runJob("GET_MISSING_VIDEOS", {}, this)
 			.then(response => {
 				this.log("SUCCESS", "YOUTUBE_GET_MISSING_VIDEOS", `Getting missing videos was successful.`);
@@ -557,7 +567,7 @@ export default {
 	 *
 	 * @returns {{status: string, data: object}}
 	 */
-	updateVideosV1ToV2: useHasPermission("youtube.updateVideosV1ToV2", function updateVideosV1ToV2(session, cb) {
+	updateVideosV1ToV2: useHasPermission("youtube.updateVideosV1ToV2", async function updateVideosV1ToV2(session, cb) {
 		this.keepLongJob();
 		this.publishProgress({
 			status: "started",
@@ -565,6 +575,16 @@ export default {
 			message: "Updating YouTube videos from v1 to v2.",
 			id: this.toString()
 		});
+		await CacheModule.runJob("RPUSH", { key: `longJobs.${session.userId}`, value: this.toString() }, this);
+		await CacheModule.runJob(
+			"PUB",
+			{
+				channel: "longJob.added",
+				value: { jobId: this.toString(), userId: session.userId }
+			},
+			this
+		);
+
 		return YouTubeModule.runJob("UPDATE_VIDEOS_V1_TO_V2", {}, this)
 			.then(response => {
 				this.log("SUCCESS", "YOUTUBE_UPDATE_VIDEOS_V1_TO_V2", `Updating v1 videos to v2 was successful.`);

+ 0 - 2
backend/logic/media.js

@@ -375,8 +375,6 @@ class _MediaModule extends CoreClass {
 					(song, next) => {
 						if (song && song.duration > 0) return next(true, song);
 
-						console.log(123, payload);
-
 						if (payload.mediaSource.startsWith("youtube:")) {
 							const youtubeId = payload.mediaSource.split(":")[1];
 

+ 1 - 12
backend/logic/soundcloud.js

@@ -625,8 +625,6 @@ class _SoundCloudModule extends CoreClass {
 
 			const jobResponses = await Promise.all(jobsToRun);
 
-			console.log(jobResponses.map(jobResponse => jobResponse.response.data));
-
 			return jobResponses
 				.map(jobResponse => jobResponse.response.data)
 				.map(artist => ({
@@ -639,17 +637,10 @@ class _SoundCloudModule extends CoreClass {
 		};
 
 		const { userPermalinks } = payload;
-		console.log(userPermalinks);
-
-		// const existingArtists = (
-		// 	await SoundcloudModule.soundcloudArtistsModel.find({ userPermalink: userPermalinks })
-		// ).map(artists => artists._doc);
-		// console.log(existingArtists);
 		const existingArtists = [];
 
 		const existingUserPermalinks = existingArtists.map(existingArtists => existingArtists.userPermalink);
-		const existingArtistsObjectIds = existingArtists.map(existingArtists => existingArtists._id.toString());
-		console.log(existingUserPermalinks, existingArtistsObjectIds);
+		// const existingArtistsObjectIds = existingArtists.map(existingArtists => existingArtists._id.toString());
 
 		if (userPermalinks.length === existingArtists.length) return { artists: existingArtists };
 
@@ -657,8 +648,6 @@ class _SoundCloudModule extends CoreClass {
 			userPermalink => existingUserPermalinks.indexOf(userPermalink) === -1
 		);
 
-		console.log(missingUserPermalinks);
-
 		if (missingUserPermalinks.length === 0) return { videos: existingArtists };
 
 		const newArtists = await getArtists(missingUserPermalinks);

+ 16 - 54
backend/logic/spotify.js

@@ -333,7 +333,6 @@ class _SpotifyModule extends CoreClass {
 							}
 						})
 						.catch(err => {
-							console.log(4443311, err);
 							reject(err);
 						});
 				})
@@ -524,21 +523,15 @@ class _SpotifyModule extends CoreClass {
 	async GET_ALBUMS_FROM_IDS(payload) {
 		const { albumIds } = payload;
 
-		console.log(albumIds);
-
 		const existingAlbums = (await SpotifyModule.spotifyAlbumModel.find({ albumId: albumIds })).map(
 			album => album._doc
 		);
 		const existingAlbumIds = existingAlbums.map(existingAlbum => existingAlbum.albumId);
 
-		console.log(existingAlbums);
-
 		const missingAlbumIds = albumIds.filter(albumId => existingAlbumIds.indexOf(albumId) === -1);
 
 		if (missingAlbumIds.length === 0) return existingAlbums;
 
-		console.log(missingAlbumIds);
-
 		const jobsToRun = [];
 
 		const chunkSize = 2;
@@ -550,8 +543,6 @@ class _SpotifyModule extends CoreClass {
 
 		const jobResponses = await Promise.all(jobsToRun);
 
-		console.log(jobResponses);
-
 		const newAlbums = jobResponses
 			.map(jobResponse => jobResponse.response.data.albums)
 			.flat()
@@ -560,8 +551,6 @@ class _SpotifyModule extends CoreClass {
 				rawData: album
 			}));
 
-		console.log(newAlbums);
-
 		await SpotifyModule.runJob("CREATE_ALBUMS", { spotifyAlbums: newAlbums }, this);
 
 		return existingAlbums.concat(newAlbums);
@@ -577,21 +566,15 @@ class _SpotifyModule extends CoreClass {
 	async GET_ARTISTS_FROM_IDS(payload) {
 		const { artistIds } = payload;
 
-		console.log(artistIds);
-
 		const existingArtists = (await SpotifyModule.spotifyArtistModel.find({ artistId: artistIds })).map(
 			artist => artist._doc
 		);
 		const existingArtistIds = existingArtists.map(existingArtist => existingArtist.artistId);
 
-		console.log(existingArtists);
-
 		const missingArtistIds = artistIds.filter(artistId => existingArtistIds.indexOf(artistId) === -1);
 
 		if (missingArtistIds.length === 0) return existingArtists;
 
-		console.log(missingArtistIds);
-
 		const jobsToRun = [];
 
 		const chunkSize = 50;
@@ -603,8 +586,6 @@ class _SpotifyModule extends CoreClass {
 
 		const jobResponses = await Promise.all(jobsToRun);
 
-		console.log(jobResponses);
-
 		const newArtists = jobResponses
 			.map(jobResponse => jobResponse.response.data.artists)
 			.flat()
@@ -613,8 +594,6 @@ class _SpotifyModule extends CoreClass {
 				rawData: artist
 			}));
 
-		console.log(newArtists);
-
 		await SpotifyModule.runJob("CREATE_ARTISTS", { spotifyArtists: newArtists }, this);
 
 		return existingArtists.concat(newArtists);
@@ -821,7 +800,6 @@ class _SpotifyModule extends CoreClass {
 					}
 				});
 			} catch (err) {
-				console.log("ERROR", err);
 				this.publishProgress({
 					status: "working",
 					message: `Failed to get alternative artist source for ${artistId}`,
@@ -833,8 +811,6 @@ class _SpotifyModule extends CoreClass {
 			}
 		});
 
-		console.log("Done!");
-
 		this.publishProgress({
 			status: "finished",
 			message: `Finished getting alternative artist sources`
@@ -876,25 +852,21 @@ class _SpotifyModule extends CoreClass {
 			)
 		);
 
-		const soundcloudIds = Array.from(
-			new Set(
-				wikiDataResponse.results.bindings
-					.filter(binding => !!binding.SoundCloud_ID)
-					.map(binding => binding.SoundCloud_ID.value)
-			)
-		);
-
-		const musicbrainzArtistIds = Array.from(
-			new Set(
-				wikiDataResponse.results.bindings
-					.filter(binding => !!binding.MusicBrainz_artist_ID)
-					.map(binding => binding.MusicBrainz_artist_ID.value)
-			)
-		);
+		// const soundcloudIds = Array.from(
+		// 	new Set(
+		// 		wikiDataResponse.results.bindings
+		// 			.filter(binding => !!binding.SoundCloud_ID)
+		// 			.map(binding => binding.SoundCloud_ID.value)
+		// 	)
+		// );
 
-		console.log("Youtube channel ids", youtubeChannelIds);
-		console.log("Soundcloud ids", soundcloudIds);
-		console.log("Musicbrainz artist ids", musicbrainzArtistIds);
+		// const musicbrainzArtistIds = Array.from(
+		// 	new Set(
+		// 		wikiDataResponse.results.bindings
+		// 			.filter(binding => !!binding.MusicBrainz_artist_ID)
+		// 			.map(binding => binding.MusicBrainz_artist_ID.value)
+		// 	)
+		// );
 
 		return youtubeChannelIds;
 	}
@@ -927,7 +899,6 @@ class _SpotifyModule extends CoreClass {
 					}
 				});
 			} catch (err) {
-				console.log("ERROR", err);
 				this.publishProgress({
 					status: "working",
 					message: `Failed to get alternative album source for ${albumId}`,
@@ -939,8 +910,6 @@ class _SpotifyModule extends CoreClass {
 			}
 		});
 
-		console.log("Done!");
-
 		this.publishProgress({
 			status: "finished",
 			message: `Finished getting alternative album sources`
@@ -996,10 +965,6 @@ class _SpotifyModule extends CoreClass {
 	async GET_ALTERNATIVE_MEDIA_SOURCES_FOR_TRACKS(payload) {
 		const { mediaSources, collectAlternativeMediaSourcesOrigins } = payload;
 
-		// console.log("KR*S94955", mediaSources);
-
-		// this.pub
-
 		await async.eachLimit(mediaSources, 1, async mediaSource => {
 			try {
 				const result = await SpotifyModule.runJob(
@@ -1017,7 +982,6 @@ class _SpotifyModule extends CoreClass {
 					}
 				});
 			} catch (err) {
-				console.log("ERROR", err);
 				this.publishProgress({
 					status: "working",
 					message: `Failed to get alternative media for ${mediaSource}`,
@@ -1029,8 +993,6 @@ class _SpotifyModule extends CoreClass {
 			}
 		});
 
-		console.log("Done!");
-
 		this.publishProgress({
 			status: "finished",
 			message: `Finished getting alternative media`
@@ -1332,7 +1294,7 @@ class _SpotifyModule extends CoreClass {
 									Promise.allSettled(promisesToRun2).then(resolve);
 								})
 								.catch(err => {
-									console.log("KRISWORKERR", err);
+									console.log(err);
 									resolve();
 								});
 						});
@@ -1546,7 +1508,7 @@ class _SpotifyModule extends CoreClass {
 							Promise.allSettled(promisesToRun2).then(resolve);
 						})
 						.catch(err => {
-							console.log("KRISWORKERR", err);
+							console.log(err);
 							resolve();
 						});
 				});

+ 0 - 3
backend/logic/stations.js

@@ -617,8 +617,6 @@ class _StationsModule extends CoreClass {
 								.map(playlistSong => playlistSong.mediaSource)
 								.filter(mediaSource => currentMediaSources.indexOf(mediaSource) === -1);
 
-							console.log(4343, getYoutubeIds);
-
 							const { songs } = await SongsModule.runJob("GET_SONGS", { mediaSources: getYoutubeIds });
 
 							const weightRegex = new RegExp(`${weightTagName}\\[(\\d+)\\]`);
@@ -1954,7 +1952,6 @@ class _StationsModule extends CoreClass {
 	ADD_TO_QUEUE(payload) {
 		return new Promise((resolve, reject) => {
 			const { stationId, mediaSource, requestUser, requestType } = payload;
-			console.log(45436546, mediaSource);
 			async.waterfall(
 				[
 					next => {

+ 3 - 27
backend/logic/youtube.js

@@ -811,11 +811,6 @@ class _YouTubeModule extends CoreClass {
 			const channelCustomUrl = splitQuery[3];
 			const channelUsernameOrCustomUrl = splitQuery[4];
 
-			console.log(`Channel id: ${channelId}`);
-			console.log(`Channel username: ${channelUsername}`);
-			console.log(`Channel custom URL: ${channelCustomUrl}`);
-			console.log(`Channel username or custom URL: ${channelUsernameOrCustomUrl}`);
-
 			const disableSearch = payload.disableSearch || false;
 
 			async.waterfall(
@@ -1401,8 +1396,6 @@ class _YouTubeModule extends CoreClass {
 
 			const jobResponses = await Promise.all(jobsToRun);
 
-			console.log(jobResponses);
-
 			return jobResponses
 				.map(jobResponse => jobResponse.response.data.items)
 				.flat()
@@ -1447,20 +1440,16 @@ class _YouTubeModule extends CoreClass {
 		};
 
 		const { identifiers, createMissing, replaceExisting } = payload;
-		console.log(identifiers, createMissing, replaceExisting);
 
 		const youtubeIds = identifiers.filter(identifier => !mongoose.Types.ObjectId.isValid(identifier));
 		const objectIds = identifiers.filter(identifier => mongoose.Types.ObjectId.isValid(identifier));
-		console.log(youtubeIds, objectIds);
 
 		const existingVideos = (await YouTubeModule.youtubeVideoModel.find({ youtubeId: youtubeIds }))
 			.concat(await YouTubeModule.youtubeVideoModel.find({ _id: objectIds }))
 			.map(video => video._doc);
-		console.log(existingVideos);
 
 		const existingYoutubeIds = existingVideos.map(existingVideo => existingVideo.youtubeId);
-		const existingYoutubeObjectIds = existingVideos.map(existingVideo => existingVideo._id.toString());
-		console.log(existingYoutubeIds, existingYoutubeObjectIds);
+		// const existingYoutubeObjectIds = existingVideos.map(existingVideo => existingVideo._id.toString());
 
 		if (!replaceExisting) {
 			if (!createMissing) return { videos: existingVideos };
@@ -1469,13 +1458,11 @@ class _YouTubeModule extends CoreClass {
 
 			const missingYoutubeIds = youtubeIds.filter(youtubeId => existingYoutubeIds.indexOf(youtubeId) === -1);
 
-			console.log(missingYoutubeIds);
-
 			if (missingYoutubeIds.length === 0) return { videos: existingVideos };
 
 			const newVideos = await getVideosFromYoutubeIds(missingYoutubeIds);
 
-			console.dir(newVideos, { depth: 5 });
+			// console.dir(newVideos, { depth: 5 });
 
 			await YouTubeModule.runJob("CREATE_VIDEOS", { youtubeVideos: newVideos }, this);
 
@@ -1531,8 +1518,6 @@ class _YouTubeModule extends CoreClass {
 
 			const jobResponses = await Promise.all(jobsToRun);
 
-			console.log(jobResponses);
-
 			return jobResponses
 				.map(jobResponse => jobResponse.response.data.items)
 				.flat()
@@ -1549,23 +1534,18 @@ class _YouTubeModule extends CoreClass {
 		};
 
 		const { channelIds } = payload;
-		console.log(channelIds);
 
 		const existingChannels = (await YouTubeModule.youtubeChannelModel.find({ channelId: channelIds })).map(
 			channel => channel._doc
 		);
-		console.log(existingChannels);
 
 		const existingChannelIds = existingChannels.map(existingChannel => existingChannel.channelId);
-		const existingChannelObjectIds = existingChannels.map(existingChannel => existingChannel._id.toString());
-		console.log(existingChannelIds, existingChannelObjectIds);
+		// const existingChannelObjectIds = existingChannels.map(existingChannel => existingChannel._id.toString());
 
 		if (channelIds.length === existingChannels.length) return { channels: existingChannels };
 
 		const missingChannelIds = channelIds.filter(channelId => existingChannelIds.indexOf(channelId) === -1);
 
-		console.log(missingChannelIds);
-
 		if (missingChannelIds.length === 0) return { videos: existingChannels };
 
 		const newChannels = await getChannels(missingChannelIds);
@@ -1878,10 +1858,6 @@ class _YouTubeModule extends CoreClass {
 
 		const missingChannelIds = videoChannelIds.filter(channelId => currentChannelIds.indexOf(channelId) === -1);
 
-		console.log(currentChannelIds);
-		console.log(videoChannelIds);
-		console.log(currentChannelIds.length, videoChannelIds.length);
-
 		const res = await YouTubeModule.runJob("GET_CHANNELS_FROM_IDS", { channelIds: missingChannelIds }, this);
 
 		const gotChannels = res.channels;