| 
					
				 | 
			
			
				@@ -3,21 +3,22 @@ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 const db = require('../db'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 const utils = require('../utils'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 const notifications = require('../notifications'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+const cache = require('../cache'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 const async = require('async'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 const config = require('config'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 const request = require('request'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-notifications.subscribe("queue.newSong", function(songId) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	io.to('admin.queue').emit("event:song.new", { songId }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+notifications.subscribe('queue.newSong', songId => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	io.to('admin.queue').emit('event:song.new', { songId }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-notifications.subscribe("queue.removedSong", function(songId) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	io.to('admin.queue').emit("event:song.removed", { songId }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+notifications.subscribe('queue.removedSong', songId => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	io.to('admin.queue').emit('event:song.removed', { songId }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-notifications.subscribe("queue.updatedSong", function(songId) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+notifications.subscribe('queue.updatedSong', songId => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	//TODO Retrieve new Song object 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	io.to('admin.queue').emit("event:song.updated", { songId }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	io.to('admin.queue').emit('event:song.updated', { songId }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 module.exports = { 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -35,30 +36,31 @@ module.exports = { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		//TODO Check if id and updatedSong is valid 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		db.models.queueSong.findOne({ id }, function(err, queueSong) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 			if (err) throw err; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-			//List of properties that are allowed to be changed 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-			const updatableProperties = ["id", "title", "artists", "genres", "thumbnail", "explicit", "duration", "skipDuration"]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			// List of properties that are allowed to be changed 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			const updatableProperties = ['id', 'title', 'artists', 'genres', 'thumbnail', 'explicit', 'duration', 'skipDuration']; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 			//TODO Check if new id, if any, is already in use in queue or on rotation 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 			let updated = false; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 			for (let prop in queueSong) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-				if (updatableProperties.indexOf(prop) !== -1 && updatedSong.hasOwnProperty("prop") && updatedSong[prop] !== queueSong[prop]) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				if (updatableProperties.indexOf(prop) !== -1 && updatedSong.hasOwnProperty('prop') && updatedSong[prop] !== queueSong[prop]) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 					queueSong[prop] = updatedSong[prop]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 					updated = true; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 				} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 			} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-			if (!updated) return cb({ status: 'failure', message: 'No properties changed.' }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			if (!updated) return cb({ status: 'failure', message: 'No properties changed' }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 			queueSong.save((err) => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-				if (err) return cb({ status: 'failure', message: 'Couldn\'t save to Database.' }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				if (err) return cb({ status: 'failure', message: 'Couldn\'t save to Database' }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-				return cb({ status: 'success', message: 'Successfully updated the queueSong object.' }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				return cb({ status: 'success', message: 'Successfully updated the queueSong object' }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 			}); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		}); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	remove: (session, id, cb) => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		//TODO Require admin/login 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		db.models.queueSong.find({ id }).remove().exec(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	remove: (session, _id, cb) => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		// TODO Require admin/login 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		db.models.queueSong.find({ _id }).remove().exec(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		cb({ status: 'success', message: 'Song was removed successfully' }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	add: (session, id, cb) => { 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -72,7 +74,6 @@ module.exports = { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		async.waterfall([ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 			// Get YouTube data from id 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 			(next) => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-				console.log(111, id); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 				const youtubeParams = [ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 					'part=snippet,contentDetails,statistics,status', 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 					`id=${encodeURIComponent(id)}`, 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -89,24 +90,23 @@ module.exports = { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 					body = JSON.parse(body); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 					//TODO Clean up duration converter 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-					console.log(body); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 					let dur = body.items[0].contentDetails.duration; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-					dur = dur.replace("PT", ""); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+					dur = dur.replace('PT', ''); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 					let durInSec = 0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 					dur = dur.replace(/([\d]*)H/, function(v, v2) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 						v2 = Number(v2); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 						durInSec = (v2 * 60 * 60) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-						return ""; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+						return ''; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 					}); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 					dur = dur.replace(/([\d]*)M/, function(v, v2) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 						v2 = Number(v2); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 						durInSec = (v2 * 60) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-						return ""; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+						return ''; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 					}); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 					dur = dur.replace(/([\d]*)S/, function(v, v2) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 						v2 = Number(v2); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 						durInSec += v2; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-						return ""; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+						return ''; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 					}); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 					let newSong = { 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -126,7 +126,6 @@ module.exports = { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 				}); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 			}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 			(newSong, next) => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-				console.log(222); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 				const spotifyParams = [ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 					`q=${encodeURIComponent(newSong.title)}`, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 					`type=track` 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -145,6 +144,7 @@ module.exports = { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 					for (let i in body) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 						let items = body[i].items; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 						for (let j in items) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 							let item = items[j]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 							let hasArtist = false; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 							for (let k = 0; k < item.artists.length; k++) { 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -155,7 +155,7 @@ module.exports = { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 							} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 							if (hasArtist && newSong.title.indexOf(item.name) !== -1) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 								newSong.duration = item.duration_ms / 1000; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-								newSong.artists = item.map(artist => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+								newSong.artists = item.artists.map(artist => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 									return artist.name; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 								}); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 								newSong.title = item.name; 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -163,6 +163,7 @@ module.exports = { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 								newSong.thumbnail = item.album.images[1].url; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 								break durationArtistLoop; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 							} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 						} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 					} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -170,31 +171,24 @@ module.exports = { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 				}); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 			}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 			(newSong, next) => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-				console.log(333); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 				const song = new db.models.queueSong(newSong); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 				song.save(err => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 					if (err) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 						console.error(err); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-						return next('Failed to add song to database.'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+						return next('Failed to add song to database'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 					} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 					//stations.getStation(station).playlist.push(newSong); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 					next(null, newSong); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 				}); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 			} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		], 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		(err, newSong) => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-			console.log(444, err); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-			if (err) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-				return cb({ status: 'failure', message: err }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-			} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-			//TODO Emit to Redis 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-			notifications.emit("queue.newSong", newSong._id); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-			return cb({ status: 'success', message: 'Successfully added that song to the queue.' }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			if (err) return cb({ status: 'error', message: err }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			cache.pub('queue.newSong', newSong._id); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			return cb({ status: 'success', message: 'Successfully added that song to the queue' }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		}); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 |