| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 | 
							- 'use strict';
 
- // nodejs modules
 
- const path   = require('path'),
 
-       fs     = require('fs'),
 
-       os     = require('os'),
 
-       events = require('events');
 
- // npm modules
 
- const config    = require('config'),
 
-       request   = require('request'),
 
-       waterfall = require('async/waterfall'),
 
-       bcrypt = require('bcrypt'),
 
- 	  passport  = require('passport');
 
- // custom modules
 
- const global   = require('./global'),
 
- 	  stations = require('./stations');
 
- var eventEmitter = new events.EventEmitter();
 
- const edmStation = new stations.Station("edm", {
 
- 	"genres": ["edm"],
 
- 	playlist: [
 
- 		'LHCob76kigA',
 
- 		'lEi_XBg2Fpk'
 
- 	],
 
- 	currentSongIndex: 0,
 
- 	paused: false,
 
- 	displayName: "EDM",
 
- 	description: "EDM Music"
 
- });
 
- const chillStation = new stations.Station("chill", {
 
- 	"genres": ["chill"],
 
- 	playlist: [
 
- 		'lEi_XBg2Fpk'
 
- 	],
 
- 	currentSongIndex: 0,
 
- 	paused: false,
 
- 	displayName: "Chill",
 
- 	description: "Chill Music"
 
- });
 
- stations.addStation(edmStation);
 
- stations.addStation(chillStation);
 
- module.exports = {
 
- 	// module functions
 
- 	on: (name, cb) => {
 
- 		eventEmitter.on(name, cb);
 
- 	},
 
- 	emit: (name, data) => {
 
- 		eventEmitter.emit(name, data);
 
- 	},
 
- 	// core route handlers
 
- 	'/users/register': (username, email, password, recaptcha, cb) => {
 
- 		console.log(username, password);
 
- 		request({
 
- 			url: 'https://www.google.com/recaptcha/api/siteverify',
 
- 			method: 'POST',
 
- 			form: {
 
- 				'secret': config.get("apis.recapthca.secret"),
 
- 				'response': recaptcha
 
- 			}
 
- 		}, function (error, response, body) {
 
- 			if (error === null && JSON.parse(body).success === true) {
 
- 				body = JSON.parse(body);
 
- 				global.db.user.findOne({'username': username}, function (err, user) {
 
- 					console.log(err, user);
 
- 					if (err) return cb(err);
 
- 					if (user) return cb("username");
 
- 					else {
 
- 						global.db.user.findOne({'email.address': email}, function (err, user) {
 
- 							console.log(err, user);
 
- 							if (err) return cb(err);
 
- 							if (user) return cb("email");
 
- 							else {
 
- 								// TODO: Email verification code, send email
 
- 								bcrypt.genSalt(10, function (err, salt) {
 
- 									if (err) {
 
- 										return cb(err);
 
- 									} else {
 
- 										bcrypt.hash(password, salt, function (err, hash) {
 
- 											if (err) {
 
- 												return cb(err);
 
- 											} else {
 
- 												let newUser = new global.db.user({
 
- 													username: username,
 
- 													email: {
 
- 														address: email,
 
- 														verificationToken: global.generateRandomString("64")
 
- 													},
 
- 													services: {
 
- 														password: {
 
- 															password: hash
 
- 														}
 
- 													}
 
- 												});
 
- 												newUser.save(err => {
 
- 													if (err) throw err;
 
- 													return cb(null, newUser);
 
- 												});
 
- 											}
 
- 										});
 
- 									}
 
- 								});
 
- 							}
 
- 						});
 
- 					}
 
- 				});
 
- 			} else {
 
- 				cb("Recaptcha failed");
 
- 			}
 
- 		});
 
- 	},
 
- 	'/stations': cb => {
 
- 		cb(stations.getStations().map(station => {
 
- 			return {
 
- 				id: station.id,
 
- 				playlist: station.playlist,
 
- 				displayName: station.displayName,
 
- 				description: station.description,
 
- 				currentSongIndex: station.currentSongIndex,
 
- 				users: station.users
 
- 			}
 
- 		}));
 
- 	},
 
- 	'/stations/join/:id': (id, cb) => {
 
- 		stations.getStation(id).users = stations.getStation(id).users + 1;
 
- 		cb(stations.getStation(id).users);
 
- 	},
 
- 	'/stations/leave/:id': (id, cb) => {
 
- 		if (stations.getStation(id)) {
 
- 			stations.getStation(id).users = stations.getStation(id).users - 1;
 
- 			if (cb) cb(stations.getStation(id).users);
 
- 		}
 
- 	},
 
- 	'/youtube/getVideo/:query': (query, cb) => {
 
- 		const params = [
 
- 			'part=snippet',
 
- 			`q=${encodeURIComponent(query)}`,
 
- 			`key=${config.get('apis.youtube.key')}`,
 
- 			'type=video',
 
- 			'maxResults=15'
 
- 		].join('&');
 
- 		request(`https://www.googleapis.com/youtube/v3/search?${params}`, (err, res, body) => {
 
- 			cb(body);
 
- 		});
 
- 	},
 
- 	'/stations/add/:song': (station, song, user, cb) => {
 
- 		const params = [
 
- 			'part=snippet,contentDetails,statistics,status',
 
- 			`id=${encodeURIComponent(song.id)}`,
 
- 			`key=${config.get('apis.youtube.key')}`
 
- 		].join('&');
 
- 		// if (user.logged_in) {
 
- 			request(`https://www.googleapis.com/youtube/v3/videos?${params}`, (err, res, body) => {
 
- 				// TODO: Get data from Wikipedia and Spotify
 
- 				body = JSON.parse(body);
 
- 				const newSong = new global.db.song({
 
- 					id: body.items[0].id,
 
- 					title: body.items[0].snippet.title,
 
- 					duration: global.convertTime(body.items[0].contentDetails.duration),
 
- 					thumbnail: body.items[0].snippet.thumbnails.high.url
 
- 				});
 
- 				console.log(newSong);
 
- 				newSong.save(err => {
 
- 					if (err) throw err;
 
- 				});
 
- 				stations.getStation(station).playlist.push(newSong);
 
- 				cb(stations.getStation(station.playlist));
 
- 			});
 
- 		//}
 
- 	},
 
- 	'/songs': cb => {
 
- 		// let songs = [];
 
- 		// cb(stations.getStations().map(station => {
 
- 		// 	station.playlist.forEach(song => {
 
- 		// 		songs.push(song);
 
- 		// 	});
 
- 		// 	return songs;
 
- 		// }));
 
- 		global.db.song.find({}, (err, songs) => {
 
- 			if (err) throw err;
 
- 			cb(songs);
 
- 		});
 
- 	},
 
- 	'/songs/update': (songs, cb) => {
 
- 		console.log(songs);
 
- 		cb(stations.getStations());
 
- 	}
 
- };
 
 
  |