| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432 | 'use strict';const async   = require('async'),	  request = require('request'),	  config  = require('config');const io = require('../io');const db = require('../db');const cache = require('../cache');const notifications = require('../notifications');const utils = require('../utils');const stations = require('../stations');const songs = require('../songs');const hooks = require('./hooks');cache.sub('station.locked', stationId => {	io.io.to(`station.${stationId}`).emit("event:stations.locked");});cache.sub('station.unlocked', stationId => {	io.io.to(`station.${stationId}`).emit("event:stations.unlocked");});cache.sub('station.pause', stationId => {	io.io.to(`station.${stationId}`).emit("event:stations.pause");});cache.sub('station.resume', stationId => {	stations.getStation(stationId, (err, station) => {		io.io.to(`station.${stationId}`).emit("event:stations.resume", {timePaused: station.timePaused});	});});cache.sub('station.queueUpdate', stationId => {	stations.getStation(stationId, (err, station) => {		if (!err) {			io.io.to(`station.${stationId}`).emit("event:queue.update", station.queue);		}	});});cache.sub('station.create', stationId => {	stations.initializeStation(stationId, (err, station) => {		//TODO Emit to admin station page		io.io.to('home').emit("event:stations.created", station);	});});module.exports = {	/**	 * Get a list of all the stations	 *	 * @param session	 * @param cb	 * @return {{ status: String, stations: Array }}	 */	index: (session, cb) => {		// TODO: the logic should be a bit more personalized to the users preferred genres		// and it should probably just a different cache table then 'stations'		cache.hgetall('stations', (err, stations) => {			if (err && err !== true) {				return cb({					status: 'error',					message: 'An error occurred while obtaining the stations'				});			}			let arr = [];			for (let prop in stations) {				arr.push(stations[prop]);			}			cb({ status: 'success', stations: arr });		});	},	getPlaylist: (session, stationId, cb) => {		let playlist = [];		stations.getStation(stationId, (err, station) => {			for (let s = 1; s < station.playlist.length; s++) {				songs.getSong(station.playlist[s], (err, song) => {					playlist.push(song);				});			}		});		cb({ status: 'success', data: playlist })	},	/**	 * Joins the station by its id	 *	 * @param session	 * @param stationId - the station id	 * @param cb	 * @return {{ status: String, userCount: Integer }}	 */	join: (session, stationId, cb) => {		stations.getStation(stationId, (err, station) => {			if (err && err !== true) {				return cb({ status: 'error', message: 'An error occurred while joining the station' });			}			if (station) {				//TODO Loop through all sockets, see if socket with same session exists, and if so leave all other station rooms and join this stationRoom				/*cache.client.hincrby('station.userCounts', stationId, 1, (err, userCount) => {					if (err) return cb({ status: 'error', message: 'An error occurred while joining the station' });*/				utils.socketJoinRoom(session.socketId, `station.${stationId}`);				if (station.currentSong) {					utils.socketJoinSongRoom(session.socketId, `song.${station.currentSong._id}`);					//TODO Emit to cache, listen on cache					songs.getSong(station.currentSong._id, (err, song) => {						if (!err && song) {							station.currentSong.likes = song.likes;							station.currentSong.dislikes = song.dislikes;						} else {							station.currentSong.likes = -1;							station.currentSong.dislikes = -1;						}						cb({							status: 'success',							currentSong: station.currentSong,							startedAt: station.startedAt,							paused: station.paused,							timePaused: station.timePaused						});					});				} else {					cb({						status: 'success',						currentSong: null,						startedAt: station.startedAt,						paused: station.paused,						timePaused: station.timePaused					});				}				//});			}			else {				cb({ status: 'failure', message: `That station doesn't exist` });			}		});	},	/**	 * Skips the users current station	 *	 * @param session	 * @param stationId - the station id	 * @param cb	 * @return {{ status: String, skipCount: Integer }}	 */	/*skip: (session, stationId, cb) => {		if (!session) return cb({ status: 'failure', message: 'You must be logged in to skip a song!' });		stations.getStation(stationId, (err, station) => {						if (err && err !== true) {				return cb({ status: 'error', message: 'An error occurred while skipping the station' });			}			if (station) {				cache.client.hincrby('station.skipCounts', session.stationId, 1, (err, skipCount) => {					session.skippedSong = true;					if (err) return cb({ status: 'error', message: 'An error occurred while skipping the station' });					cache.hget('station.userCounts', session.stationId, (err, userCount) => {						cb({ status: 'success', skipCount });					});				});			}			else {				cb({ status: 'failure', message: `That station doesn't exist` });			}		});	},*/	forceSkip: hooks.adminRequired((session, stationId, cb) => {		stations.getStation(stationId, (err, station) => {			if (err && err !== true) {				return cb({ status: 'error', message: 'An error occurred while skipping the station' });			}			if (station) {				notifications.unschedule(`stations.nextSong?id=${stationId}`);				notifications.schedule(`stations.nextSong?id=${stationId}`, 100);			}			else {				cb({ status: 'failure', message: `That station doesn't exist` });			}		});	}),	/**	 * Leaves the users current station	 *	 * @param session	 * @param stationId	 * @param cb	 * @return {{ status: String, userCount: Integer }}	 */	leave: (session, stationId, cb) => {		stations.getStation(stationId, (err, station) => {			if (err && err !== true) {				return cb({ status: 'error', message: 'An error occurred while leaving the station' });			}			if (session) session.stationId = null;			else if (station) {				cache.client.hincrby('station.userCounts', stationId, -1, (err, userCount) => {					if (err) return cb({ status: 'error', message: 'An error occurred while leaving the station' });					utils.socketLeaveRooms(session);					cb({ status: 'success', userCount });				});			} else {				cb({ status: 'failure', message: `That station doesn't exist, it may have been deleted` });			}		});	},	lock: hooks.adminRequired((session, stationId, cb) => {		stations.getStation(stationId, (err, station) => {			if (err && err !== true) {				return cb({ status: 'error', message: 'An error occurred while locking the station' });			} else if (station) {				// Add code to update Mongo and Redis				cb({ status: 'success' });			} else {				cb({ status: 'failure', message: `That station doesn't exist, it may have been deleted` });			}		});	}),	unlock: hooks.adminRequired((session, stationId, cb) => {		stations.getStation(stationId, (err, station) => {			if (err && err !== true) {				return cb({ status: 'error', message: 'An error occurred while unlocking the station' });			} else if (station) {				// Add code to update Mongo and Redis				cb({ status: 'success' });			} else {				cb({ status: 'failure', message: `That station doesn't exist, it may have been deleted` });			}		});	}),	pause: hooks.adminRequired((session, stationId, cb) => {		stations.getStation(stationId, (err, station) => {			if (err && err !== true) {				return cb({ status: 'error', message: 'An error occurred while pausing the station' });			} else if (station) {				if (!station.paused) {					station.paused = true;					station.pausedAt = Date.now();					db.models.station.update({_id: stationId}, {$set: {paused: true, pausedAt: Date.now()}}, () => {						if (err) return cb({ status: 'failure', message: 'An error occurred while pausing the station.' });						stations.updateStation(stationId, () => {							cache.pub('station.pause', stationId);							notifications.unschedule(`stations.nextSong?id=${stationId}`);							cb({ status: 'success' });						});					});				} else {					cb({ status: 'failure', message: 'That station was already paused.' });				}				cb({ status: 'success' });			} else {				cb({ status: 'failure', message: `That station doesn't exist, it may have been deleted` });			}		});	}),	resume: hooks.adminRequired((session, stationId, cb) => {		stations.getStation(stationId, (err, station) => {			if (err && err !== true) {				return cb({ status: 'error', message: 'An error occurred while resuming the station' });			} else if (station) {				if (station.paused) {					station.paused = false;					station.timePaused += (Date.now() - station.pausedAt);					db.models.station.update({_id: stationId}, {$set: {paused: false}, $inc: {timePaused: Date.now() - station.pausedAt}}, () => {						stations.updateStation(stationId, (err, station) => {							cache.pub('station.resume', stationId);							cb({ status: 'success' });						});					});				} else {					cb({ status: 'failure', message: 'That station is not paused.' });				}			} else {				cb({ status: 'failure', message: `That station doesn't exist, it may have been deleted` });			}		});	}),	remove: hooks.adminRequired((session, stationId, cb) => {		db.models.station.remove({ _id: stationId });		cache.hdel('stations', stationId, () => {			return cb({ status: 'success', message: 'Station successfully removed' });		});	}),	create: hooks.adminRequired((session, data, cb) => {		async.waterfall([			(next) => {				return (data) ? next() : cb({ 'status': 'failure', 'message': 'Invalid data' });			},			// check the cache for the station			(next) => cache.hget('stations', data._id, next),			// if the cached version exist			(station, next) => {				if (station) return next({ 'status': 'failure', 'message': 'A station with that id already exists' });				db.models.station.findOne({ _id: data._id }, next);			},			(station, next) => {				if (station) return next({ 'status': 'failure', 'message': 'A station with that id already exists' });				const { _id, displayName, description, genres, playlist } = data;				db.models.station.create({					_id,					displayName,					description,					type: "official",					playlist,					genres,					currentSong: stations.defaultSong				}, next);			}		], (err, station) => {			if (err) {console.log(err); return cb({ 'status': 'failure', 'message': 'Something went wrong.'});}			cache.pub('station.create', data._id);			return cb(null, { 'status': 'success', 'message': 'Successfully created station.' });		});	}),	createCommunity: hooks.loginRequired((session, data, cb) => {		async.waterfall([			(next) => {				return (data) ? next() : cb({ 'status': 'failure', 'message': 'Invalid data' });			},			// check the cache for the station			(next) => cache.hget('stations', data._id, next),			// if the cached version exist			(station, next) => {				if (station) return next({ 'status': 'failure', 'message': 'A station with that id already exists' });				db.models.station.findOne({ _id: data._id }, next);			},			(station, next) => {				if (station) return next({ 'status': 'failure', 'message': 'A station with that id already exists' });				const { _id, displayName, description } = data;				db.models.station.create({					_id,					displayName,					description,					type: "community",					queue: [],					currentSong: null				}, next);			}		], (err, station) => {			if (err) {console.log(err); throw err;}			cache.pub('station.create', data._id);			return cb({ 'status': 'success', 'message': 'Successfully created station.' });		});	}),	addToQueue: hooks.loginRequired((session, stationId, songId, cb, userId) => {		stations.getStation(stationId, (err, station) => {			if (err) return cb(err);			if (station.type === 'community') {				let has = false;				station.queue.forEach((queueSong) => {					if (queueSong.songId === songId) {						has = true;					}				});				if (has) return cb({'status': 'failure', 'message': 'That song has already been added to the queue.'});				db.models.update({_id: stationId}, {$push: {queue: {_id: songId, title: "Title", duration: 100, requestedBy: userId}}}, (err) => {					if (err) return cb({'status': 'failure', 'message': 'Something went wrong.'});					stations.updateStation(stationId, (err, station) => {						if (err) return cb(err);						cache.pub('station.queueUpdate', stationId);					});				});			} else cb({'status': 'failure', 'message': 'That station is not a community station.'});		});	}),	removeFromQueue: hooks.adminRequired((session, stationId, songId, cb, userId) => {		stations.getStation(stationId, (err, station) => {			if (err) return cb(err);			if (station.type === 'community') {				let has = false;				station.queue.forEach((queueSong) => {					if (queueSong._id === songId) {						has = true;					}				});				if (!has) return cb({'status': 'failure', 'message': 'That song is not in the queue.'});				db.models.update({_id: stationId}, {$pull: {queue: {_id: songId}}}, (err) => {					if (err) return cb({'status': 'failure', 'message': 'Something went wrong.'});					stations.updateStation(stationId, (err, station) => {						if (err) return cb(err);						cache.pub('station.queueUpdate', stationId);					});				});			} else cb({'status': 'failure', 'message': 'That station is not a community station.'});		});	}),};
 |