| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470 | 'use strict';const db = require('../db');const io = require('../io');const songs = require('../songs');const cache = require('../cache');const async = require('async');const utils = require('../utils');const logger = require('../logger');const hooks = require('./hooks');const queueSongs = require('./queueSongs');cache.sub('song.removed', songId => {	utils.emitToRoom('admin.songs', 'event:admin.song.removed', songId);});cache.sub('song.added', songId => {	db.models.song.findOne({songId}, (err, song) => {		utils.emitToRoom('admin.songs', 'event:admin.song.added', song);	});});cache.sub('song.updated', songId => {	db.models.song.findOne({songId}, (err, song) => {		utils.emitToRoom('admin.songs', 'event:admin.song.updated', song);	});});cache.sub('song.like', (data) => {	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});		});	});});cache.sub('song.dislike', (data) => {	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});		});	});});cache.sub('song.unlike', (data) => {	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});		});	});});cache.sub('song.undislike', (data) => {	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});		});	});});module.exports = {	/**	 * Returns the length of the songs list	 *	 * @param session	 * @param cb	 */	length: hooks.adminRequired((session, cb) => {		async.waterfall([			(next) => {				db.models.song.count({}, next);			}		], (err, count) => {			if (err) {				err = utils.getError(err);				logger.error("SONGS_LENGTH", `Failed to get length from songs. "${err}"`);				return cb({'status': 'failure', 'message': err});			}			logger.success("SONGS_LENGTH", `Got length from songs successfully.`);			cb(count);		});	}),	/**	 * Gets a set of songs	 *	 * @param session	 * @param set - the set number to return	 * @param cb	 */	getSet: hooks.adminRequired((session, set, cb) => {		async.waterfall([			(next) => {				db.models.song.find({}).limit(15 * set).exec(next);			}		], (err, songs) => {			if (err) {				err = utils.getError(err);				logger.error("SONGS_GET_SET", `Failed to get set from songs. "${err}"`);				return cb({'status': 'failure', 'message': err});			}			logger.success("SONGS_GET_SET", `Got set from songs successfully.`);			logger.stationIssue(songs.length, true);			logger.stationIssue(Math.max(songs.length - 15, 0), true);			cb(songs.splice(Math.max(songs.length - 15, 0)));		});	}),	/**	 * Updates a song	 *	 * @param session	 * @param songId - the song id	 * @param song - the updated song object	 * @param cb	 */	update: hooks.adminRequired((session, songId, song, cb) => {		async.waterfall([			(next) => {				db.models.song.updateOne({_id: songId}, song, {runValidators: true}, next);			},			(res, next) => {				songs.updateSong(songId, next);			}		], (err, song) => {			if (err) {				err = utils.getError(err);				logger.error("SONGS_UPDATE", `Failed to update song "${songId}". "${err}"`);				return cb({'status': 'failure', 'message': err});			}			logger.success("SONGS_UPDATE", `Successfully updated song "${songId}".`);			cache.pub('song.updated', song.songId);			cb({ status: 'success', message: 'Song has been successfully updated', data: song });		});	}),	/**	 * Removes a song	 *	 * @param session	 * @param songId - the song id	 * @param cb	 */	remove: hooks.adminRequired((session, songId, cb) => {		async.waterfall([			(next) => {				db.models.song.deleteOne({_id: songId}, next);			},			(res, next) => {//TODO Check if res gets returned from above				cache.hdel('songs', songId, next);			}		], (err) => {			if (err) {				err = utils.getError(err);				logger.error("SONGS_UPDATE", `Failed to remove song "${songId}". "${err}"`);				return cb({'status': 'failure', 'message': err});			}			logger.success("SONGS_UPDATE", `Successfully remove song "${songId}".`);			cache.pub('song.removed', songId);			cb({status: 'success', message: 'Song has been successfully updated'});		});	}),	/**	 * Adds a song	 *	 * @param session	 * @param song - the song object	 * @param cb	 * @param userId	 */	add: hooks.adminRequired((session, song, cb, userId) => {		async.waterfall([			(next) => {				db.models.song.findOne({songId: song.songId}, next);			},			(existingSong, next) => {				if (existingSong) return next('Song is already in rotation.');				next();			},			(next) => {				const newSong = new db.models.song(song);				newSong.acceptedBy = userId;				newSong.acceptedAt = Date.now();				newSong.save(next);			},			(res, next) => {				queueSongs.remove(session, song._id, () => {					next();				});			},		], (err) => {			if (err) {				err = utils.getError(err);				logger.error("SONGS_ADD", `User "${userId}" failed to add song. "${err}"`);				return cb({'status': 'failure', 'message': err});			}			logger.success("SONGS_ADD", `User "${userId}" successfully added song "${song.songId}".`);			cache.pub('song.added', song.songId);			cb({status: 'success', message: 'Song has been moved from the queue successfully.'});		});		//TODO Check if video is in queue and Add the song to the appropriate stations	}),	/**	 * Likes a song	 *	 * @param session	 * @param songId - the song id	 * @param cb	 * @param userId	 */	like: hooks.loginRequired((session, songId, cb, userId) => {		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.updateOne({_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) => {								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: 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.' });				});			});		});	}),	/**	 * Dislikes a song	 *	 * @param session	 * @param songId - the song id	 * @param cb	 * @param userId	 */	dislike: hooks.loginRequired((session, songId, cb, userId) => {		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.updateOne({_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) => {								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, 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.' });				});			});		});	}),	/**	 * Undislikes a song	 *	 * @param session	 * @param songId - the song id	 * @param cb	 * @param userId	 */	undislike: hooks.loginRequired((session, songId, cb, userId) => {		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.updateOne({_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.'});				});			});		});	}),	/**	 * Unlikes a song	 *	 * @param session	 * @param songId - the song id	 * @param cb	 * @param userId	 */	unlike: hooks.loginRequired((session, songId, cb, userId) => {		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.updateOne({_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.updateOne({_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.' });				});			});		});	}),	/**	 * Gets user's own song ratings	 *	 * @param session	 * @param songId - the song id	 * @param cb	 * @param userId	 */	getOwnSongRatings: hooks.loginRequired((session, songId, cb, userId) => {		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)					});				}			});		});	})};
 |