| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547 | 'use strict';const async = require('async');const hooks = require('./hooks');const moduleManager = require("../../index");const db = moduleManager.modules["db"];const cache = moduleManager.modules["cache"];const utils = moduleManager.modules["utils"];const logger = moduleManager.modules["logger"];const playlists = moduleManager.modules["playlists"];const songs = moduleManager.modules["songs"];cache.sub('playlist.create', playlistId => {	playlists.getPlaylist(playlistId, (err, playlist) => {		if (!err) {			utils.socketsFromUser(playlist.createdBy, (sockets) => {				sockets.forEach(socket => {					socket.emit('event:playlist.create', playlist);				});			});		}	});});cache.sub('playlist.delete', res => {	utils.socketsFromUser(res.userId, sockets => {		sockets.forEach(socket => {			socket.emit('event:playlist.delete', res.playlistId);		});	});});cache.sub('playlist.moveSongToTop', res => {	utils.socketsFromUser(res.userId, sockets => {		sockets.forEach(socket => {			socket.emit('event:playlist.moveSongToTop', {playlistId: res.playlistId, songId: res.songId});		});	});});cache.sub('playlist.moveSongToBottom', res => {	utils.socketsFromUser(res.userId, sockets => {		sockets.forEach(socket => {			socket.emit('event:playlist.moveSongToBottom', {playlistId: res.playlistId, songId: res.songId});		});	});});cache.sub('playlist.addSong', res => {	utils.socketsFromUser(res.userId, sockets => {		sockets.forEach(socket => {			socket.emit('event:playlist.addSong', { playlistId: res.playlistId, song: res.song });		});	});});cache.sub('playlist.removeSong', res => {	utils.socketsFromUser(res.userId, sockets => {		sockets.forEach(socket => {			socket.emit('event:playlist.removeSong', { playlistId: res.playlistId, songId: res.songId });		});	});});cache.sub('playlist.updateDisplayName', res => {	utils.socketsFromUser(res.userId, sockets => {		sockets.forEach(socket => {			socket.emit('event:playlist.updateDisplayName', { playlistId: res.playlistId, displayName: res.displayName });		});	});});let lib = {	/**	 * Gets the first song from a private playlist	 *	 * @param {Object} session - the session object automatically added by socket.io	 * @param {String} playlistId - the id of the playlist we are getting the first song from	 * @param {Function} cb - gets called with the result	 */	getFirstSong: hooks.loginRequired((session, playlistId, cb) => {		async.waterfall([			(next) => {				playlists.getPlaylist(playlistId, next);			},			(playlist, next) => {				if (!playlist || playlist.createdBy !== session.userId) return next('Playlist not found.');				next(null, playlist.songs[0]);			}		], async (err, song) => {			if (err) {				err = await utils.getError(err);				logger.error("PLAYLIST_GET_FIRST_SONG", `Getting the first song of playlist "${playlistId}" failed for user "${session.userId}". "${err}"`);				return cb({ status: 'failure', message: err});			}			logger.success("PLAYLIST_GET_FIRST_SONG", `Successfully got the first song of playlist "${playlistId}" for user "${session.userId}".`);			cb({				status: 'success',				song: song			});		});	}),	/**	 * Gets all playlists for the user requesting it	 *	 * @param {Object} session - the session object automatically added by socket.io	 * @param {Function} cb - gets called with the result	 */	indexForUser: hooks.loginRequired((session, cb) => {		async.waterfall([			(next) => {				db.models.playlist.find({ createdBy: session.userId }, next);			}		], async (err, playlists) => {			if (err) {				err = await utils.getError(err);				logger.error("PLAYLIST_INDEX_FOR_USER", `Indexing playlists for user "${session.userId}" failed. "${err}"`);				return cb({ status: 'failure', message: err});			}			logger.success("PLAYLIST_INDEX_FOR_USER", `Successfully indexed playlists for user "${session.userId}".`);			cb({				status: 'success',				data: playlists			});		});	}),	/**	 * Creates a new private playlist	 *	 * @param {Object} session - the session object automatically added by socket.io	 * @param {Object} data - the data for the new private playlist	 * @param {Function} cb - gets called with the result	 */	create: hooks.loginRequired((session, data, cb) => {		async.waterfall([			(next) => {				return (data) ? next() : cb({ 'status': 'failure', 'message': 'Invalid data' });			},			(next) => {				const { displayName, songs } = data;				db.models.playlist.create({					displayName,					songs,					createdBy: session.userId,					createdAt: Date.now()				}, next);			}		], async (err, playlist) => {			if (err) {				err = await utils.getError(err);				logger.error("PLAYLIST_CREATE", `Creating private playlist failed for user "${session.userId}". "${err}"`);				return cb({ status: 'failure', message: err});			}			cache.pub('playlist.create', playlist._id);			logger.success("PLAYLIST_CREATE", `Successfully created private playlist for user "${session.userId}".`);			cb({ status: 'success', message: 'Successfully created playlist', data: {				_id: playlist._id			} });		});	}),	/**	 * Gets a playlist from id	 *	 * @param {Object} session - the session object automatically added by socket.io	 * @param {String} playlistId - the id of the playlist we are getting	 * @param {Function} cb - gets called with the result	 */	getPlaylist: hooks.loginRequired((session, playlistId, cb) => {		async.waterfall([			(next) => {				playlists.getPlaylist(playlistId, next);			},			(playlist, next) => {				if (!playlist || playlist.createdBy !== session.userId) return next('Playlist not found');				next(null, playlist);			}		], async (err, playlist) => {			if (err) {				err = await utils.getError(err);				logger.error("PLAYLIST_GET", `Getting private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`);				return cb({ status: 'failure', message: err});			}			logger.success("PLAYLIST_GET", `Successfully got private playlist "${playlistId}" for user "${session.userId}".`);			cb({				status: 'success',				data: playlist			});		});	}),	//TODO Remove this	/**	 * Updates a private playlist	 *	 * @param {Object} session - the session object automatically added by socket.io	 * @param {String} playlistId - the id of the playlist we are updating	 * @param {Object} playlist - the new private playlist object	 * @param {Function} cb - gets called with the result	 */	update: hooks.loginRequired((session, playlistId, playlist, cb) => {		async.waterfall([			(next) => {				db.models.playlist.updateOne({ _id: playlistId, createdBy: session.userId }, playlist, {runValidators: true}, next);			},			(res, next) => {				playlists.updatePlaylist(playlistId, next)			}		], async (err, playlist) => {			if (err) {				err = await utils.getError(err);				logger.error("PLAYLIST_UPDATE", `Updating private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`);				return cb({ status: 'failure', message: err});			}			logger.success("PLAYLIST_UPDATE", `Successfully updated private playlist "${playlistId}" for user "${session.userId}".`);			cb({				status: 'success',				data: playlist			});		});	}),	/**	 * Adds a song to a private playlist	 *	 * @param {Object} session - the session object automatically added by socket.io	 * @param {String} songId - the id of the song we are trying to add	 * @param {String} playlistId - the id of the playlist we are adding the song to	 * @param {Function} cb - gets called with the result	 */	addSongToPlaylist: hooks.loginRequired((session, songId, playlistId, cb) => {		async.waterfall([			(next) => {				playlists.getPlaylist(playlistId, (err, playlist) => {					if (err || !playlist || playlist.createdBy !== session.userId) return next('Something went wrong when trying to get the playlist');					async.each(playlist.songs, (song, next) => {						if (song.songId === songId) return next('That song is already in the playlist');						next();					}, next);				});			},			(next) => {				songs.getSong(songId, (err, song) => {					if (err) {						utils.getSongFromYouTube(songId, (song) => {							next(null, song);						});					} else {						next(null, {							_id: song._id,							songId: songId,							title: song.title,							duration: song.duration						});					}				});			},			(newSong, next) => {				db.models.playlist.updateOne({_id: playlistId}, {$push: {songs: newSong}}, {runValidators: true}, (err) => {					if (err) return next(err);					playlists.updatePlaylist(playlistId, (err, playlist) => {						next(err, playlist, newSong);					});				});			}		],		async (err, playlist, newSong) => {			if (err) {				err = await utils.getError(err);				logger.error("PLAYLIST_ADD_SONG", `Adding song "${songId}" to private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`);				return cb({ status: 'failure', message: err});			} else {				logger.success("PLAYLIST_ADD_SONG", `Successfully added song "${songId}" to private playlist "${playlistId}" for user "${session.userId}".`);				cache.pub('playlist.addSong', { playlistId: playlist._id, song: newSong, userId: session.userId });				return cb({ status: 'success', message: 'Song has been successfully added to the playlist', data: playlist.songs });			}		});	}),	/**	 * Adds a set of songs to a private playlist	 *	 * @param {Object} session - the session object automatically added by socket.io	 * @param {String} url - the url of the the YouTube playlist	 * @param {String} playlistId - the id of the playlist we are adding the set of songs to	 * @param {Function} cb - gets called with the result	 */	addSetToPlaylist: hooks.loginRequired((session, url, playlistId, cb) => {		async.waterfall([			(next) => {				utils.getPlaylistFromYouTube(url, songs => {					next(null, songs);				});			},			(songs, next) => {				let processed = 0;				function checkDone() {					if (processed === songs.length) next();				}				for (let s = 0; s < songs.length; s++) {					lib.addSongToPlaylist(session, songs[s].contentDetails.videoId, playlistId, () => {						processed++;						checkDone();					});				}			},			(next) => {				playlists.getPlaylist(playlistId, next);			},			(playlist, next) => {				if (!playlist || playlist.createdBy !== session.userId) return next('Playlist not found.');				next(null, playlist);			}		], async (err, playlist) => {			if (err) {				err = await utils.getError(err);				logger.error("PLAYLIST_IMPORT", `Importing a YouTube playlist to private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`);				return cb({ status: 'failure', message: err});			} else {				logger.success("PLAYLIST_IMPORT", `Successfully imported a YouTube playlist to private playlist "${playlistId}" for user "${session.userId}".`);				cb({ status: 'success', message: 'Playlist has been successfully imported.', data: playlist.songs });			}		});	}),	/**	 * Removes a song from a private playlist	 *	 * @param {Object} session - the session object automatically added by socket.io	 * @param {String} songId - the id of the song we are removing from the private playlist	 * @param {String} playlistId - the id of the playlist we are removing the song from	 * @param {Function} cb - gets called with the result	 */	removeSongFromPlaylist: hooks.loginRequired((session, songId, playlistId, cb) => {		async.waterfall([			(next) => {				if (!songId || typeof songId !== 'string') return next('Invalid song id.');				if (!playlistId  || typeof playlistId !== 'string') return next('Invalid playlist id.');				next();			},			(next) => {				playlists.getPlaylist(playlistId, next);			},			(playlist, next) => {				if (!playlist || playlist.createdBy !== session.userId) return next('Playlist not found');				db.models.playlist.updateOne({_id: playlistId}, {$pull: {songs: {songId: songId}}}, next);			},			(res, next) => {				playlists.updatePlaylist(playlistId, next);			}		], async (err, playlist) => {			if (err) {				err = await utils.getError(err);				logger.error("PLAYLIST_REMOVE_SONG", `Removing song "${songId}" from private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`);				return cb({ status: 'failure', message: err});			} else {				logger.success("PLAYLIST_REMOVE_SONG", `Successfully removed song "${songId}" from private playlist "${playlistId}" for user "${session.userId}".`);				cache.pub('playlist.removeSong', { playlistId: playlist._id, songId: songId, userId: session.userId });				return cb({ status: 'success', message: 'Song has been successfully removed from playlist', data: playlist.songs });			}		});	}),	/**	 * Updates the displayName of a private playlist	 *	 * @param {Object} session - the session object automatically added by socket.io	 * @param {String} playlistId - the id of the playlist we are updating the displayName for	 * @param {Function} cb - gets called with the result	 */	updateDisplayName: hooks.loginRequired((session, playlistId, displayName, cb) => {		async.waterfall([			(next) => {				db.models.playlist.updateOne({ _id: playlistId, createdBy: session.userId }, { $set: { displayName } }, {runValidators: true}, next);			},			(res, next) => {				playlists.updatePlaylist(playlistId, next);			}		], async (err, playlist) => {			if (err) {				err = await utils.getError(err);				logger.error("PLAYLIST_UPDATE_DISPLAY_NAME", `Updating display name to "${displayName}" for private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`);				return cb({ status: 'failure', message: err});			}			logger.success("PLAYLIST_UPDATE_DISPLAY_NAME", `Successfully updated display name to "${displayName}" for private playlist "${playlistId}" for user "${session.userId}".`);			cache.pub('playlist.updateDisplayName', {playlistId: playlistId, displayName: displayName, userId: session.userId});			return cb({ status: 'success', message: 'Playlist has been successfully updated' });		});	}),	/**	 * Moves a song to the top of the list in a private playlist	 *	 * @param {Object} session - the session object automatically added by socket.io	 * @param {String} playlistId - the id of the playlist we are moving the song to the top from	 * @param {String} songId - the id of the song we are moving to the top of the list	 * @param {Function} cb - gets called with the result	 */	moveSongToTop: hooks.loginRequired((session, playlistId, songId, cb) => {		async.waterfall([			(next) => {				playlists.getPlaylist(playlistId, next);			},			(playlist, next) => {				if (!playlist || playlist.createdBy !== session.userId) return next('Playlist not found');				async.each(playlist.songs, (song, next) => {					if (song.songId === songId) return next(song);					next();				}, (err) => {					if (err && err.songId) return next(null, err);					next('Song not found');				});			},			(song, next) => {				db.models.playlist.updateOne({_id: playlistId}, {$pull: {songs: {songId}}}, (err) => {					if (err) return next(err);					return next(null, song);				});			},			(song, next) => {				db.models.playlist.updateOne({_id: playlistId}, {					$push: {						songs: {							$each: [song],							$position: 0						}					}				}, next);			},			(res, next) => {				playlists.updatePlaylist(playlistId, next);			}		], async (err, playlist) => {			if (err) {				err = await utils.getError(err);				logger.error("PLAYLIST_MOVE_SONG_TO_TOP", `Moving song "${songId}" to the top for private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`);				return cb({ status: 'failure', message: err});			}			logger.success("PLAYLIST_MOVE_SONG_TO_TOP", `Successfully moved song "${songId}" to the top for private playlist "${playlistId}" for user "${session.userId}".`);			cache.pub('playlist.moveSongToTop', {playlistId, songId, userId: session.userId});			return cb({ status: 'success', message: 'Playlist has been successfully updated' });		});	}),	/**	 * Moves a song to the bottom of the list in a private playlist	 *	 * @param {Object} session - the session object automatically added by socket.io	 * @param {String} playlistId - the id of the playlist we are moving the song to the bottom from	 * @param {String} songId - the id of the song we are moving to the bottom of the list	 * @param {Function} cb - gets called with the result	 */	moveSongToBottom: hooks.loginRequired((session, playlistId, songId, cb) => {		async.waterfall([			(next) => {				playlists.getPlaylist(playlistId, next);			},			(playlist, next) => {				if (!playlist || playlist.createdBy !== session.userId) return next('Playlist not found');				async.each(playlist.songs, (song, next) => {					if (song.songId === songId) return next(song);					next();				}, (err) => {					if (err && err.songId) return next(null, err);					next('Song not found');				});			},			(song, next) => {				db.models.playlist.updateOne({_id: playlistId}, {$pull: {songs: {songId}}}, (err) => {					if (err) return next(err);					return next(null, song);				});			},			(song, next) => {				db.models.playlist.updateOne({_id: playlistId}, {					$push: {						songs: song					}				}, next);			},			(res, next) => {				playlists.updatePlaylist(playlistId, next);			}		], async (err, playlist) => {			if (err) {				err = await utils.getError(err);				logger.error("PLAYLIST_MOVE_SONG_TO_BOTTOM", `Moving song "${songId}" to the bottom for private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`);				return cb({ status: 'failure', message: err});			}			logger.success("PLAYLIST_MOVE_SONG_TO_BOTTOM", `Successfully moved song "${songId}" to the bottom for private playlist "${playlistId}" for user "${session.userId}".`);			cache.pub('playlist.moveSongToBottom', {playlistId, songId, userId: session.userId});			return cb({ status: 'success', message: 'Playlist has been successfully updated' });		});	}),	/**	 * Removes a private playlist	 *	 * @param {Object} session - the session object automatically added by socket.io	 * @param {String} playlistId - the id of the playlist we are moving the song to the top from	 * @param {Function} cb - gets called with the result	 */	remove: hooks.loginRequired((session, playlistId, cb) => {		async.waterfall([			(next) => {				playlists.deletePlaylist(playlistId, next);			}		], async (err) => {			if (err) {				err = await utils.getError(err);				logger.error("PLAYLIST_REMOVE", `Removing private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`);				return cb({ status: 'failure', message: err});			}			logger.success("PLAYLIST_REMOVE", `Successfully removed private playlist "${playlistId}" for user "${session.userId}".`);			cache.pub('playlist.delete', {userId: session.userId, playlistId});			return cb({ status: 'success', message: 'Playlist successfully removed' });		});	})};module.exports = lib;
 |