coreHandler.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. 'use strict';
  2. // nodejs modules
  3. const path = require('path'),
  4. fs = require('fs'),
  5. os = require('os'),
  6. events = require('events');
  7. // npm modules
  8. const config = require('config'),
  9. request = require('request'),
  10. waterfall = require('async/waterfall'),
  11. bcrypt = require('bcrypt'),
  12. passport = require('passport');
  13. // custom modules
  14. const globals = require('./globals'),
  15. stations = require('./stations');
  16. var eventEmitter = new events.EventEmitter();
  17. const edmStation = new stations.Station("edm", {
  18. "genres": ["edm"],
  19. playlist: [
  20. 'gCYcHz2k5x0'
  21. ],
  22. currentSongIndex: 0,
  23. paused: false,
  24. displayName: "EDM",
  25. description: "EDM Music"
  26. });
  27. const chillStation = new stations.Station("chill", {
  28. "genres": ["chill"],
  29. playlist: [
  30. 'gCYcHz2k5x0'
  31. ],
  32. currentSongIndex: 0,
  33. paused: false,
  34. displayName: "Chill",
  35. description: "Chill Music"
  36. });
  37. stations.addStation(edmStation);
  38. stations.addStation(chillStation);
  39. module.exports = {
  40. // module functions
  41. on: (name, cb) => eventEmitter.on(name, cb),
  42. emit: (name, data) => eventEmitter.emit(name, data),
  43. // core route handlers
  44. '/users/register': (session, username, email, password, recaptcha, cb) => {
  45. waterfall([
  46. // verify the request with google recaptcha
  47. (next) => {
  48. request({
  49. url: 'https://www.google.com/recaptcha/api/siteverify',
  50. method: 'POST',
  51. form: {
  52. 'secret': config.get("apis.recaptcha.secret"),
  53. 'response': recaptcha
  54. }
  55. }, next);
  56. },
  57. // check if the response from Google recaptcha is successful
  58. // if it is, we check if a user with the requested username already exists
  59. (response, body, next) => {
  60. let json = JSON.parse(body);
  61. console.log(json);
  62. if (json.success !== true) return next('Response from recaptcha was not successful');
  63. globals.db.models.user.findOne({ 'username': username }, next);
  64. },
  65. // if the user already exists, respond with that
  66. // otherwise check if a user with the requested email already exists
  67. (user, next) => {
  68. if (user) return next(true, { status: 'failure', message: 'A user with that username already exists' });
  69. globals.db.models.user.findOne({ 'email.address': email }, next);
  70. },
  71. // if the user already exists, respond with that
  72. // otherwise, generate a salt to use with hashing the new users password
  73. (user, next) => {
  74. if (user) return next(true, { status: 'failure', message: 'A user with that email already exists' });
  75. bcrypt.genSalt(10, next);
  76. },
  77. // hash the password
  78. (salt, next) => {
  79. bcrypt.hash(password, salt, next)
  80. },
  81. // save the new user to the database
  82. (hash, next) => {
  83. globals.db.models.user.create({
  84. username: username,
  85. email: {
  86. address: email,
  87. verificationToken: globals.utils.generateRandomString(64)
  88. },
  89. services: {
  90. password: {
  91. password: hash
  92. }
  93. }
  94. }, next);
  95. },
  96. // respond with the new user
  97. (newUser, next) => {
  98. next(null, { status: 'success', user: newUser })
  99. }
  100. ], (err, payload) => {
  101. // log this error somewhere
  102. if (err && err !== true) {
  103. console.error(err);
  104. return cb({ status: 'error', message: 'An error occurred while registering for an account' });
  105. }
  106. // respond with the payload that was passed to us earlier
  107. cb(payload);
  108. });
  109. },
  110. '/users/logout': (req, cb) => {
  111. if (!req.user || !req.user.logged_in) return cb({ status: 'failure', message: `You're not currently logged in` });
  112. req.logout();
  113. cb({ status: 'success', message: `You've been successfully logged out` });
  114. },
  115. '/stations': (session, cb) => {
  116. cb(stations.getStations().map(station => {
  117. return {
  118. id: station.id,
  119. playlist: station.playlist,
  120. displayName: station.displayName,
  121. description: station.description,
  122. currentSongIndex: station.currentSongIndex,
  123. users: station.users
  124. }
  125. }));
  126. },
  127. '/stations/join/:id': (session, id, cb) => {
  128. let station = stations.getStation(id);
  129. if (!station) return cb({ status: 'error', message: `Station with id '${id}' does not exist` });
  130. session.station_id = id;
  131. station.users++;
  132. cb({ status: 'success', users: station.users });
  133. },
  134. // leaves the users current station
  135. // returns the count of users that are still in that station
  136. '/stations/leave': (session, cb) => {
  137. let station = stations.getStation(session.station_id);
  138. if (!station) return cb({ status: 'failure', message: `Not currently in a station, or station doesn't exist` });
  139. session.station_id = "";
  140. station.users--;
  141. cb({ status: 'success', users: station.users });
  142. },
  143. '/youtube/getVideo/:query': (session, query, cb) => {
  144. const params = [
  145. 'part=snippet',
  146. `q=${encodeURIComponent(query)}`,
  147. `key=${config.get('apis.youtube.key')}`,
  148. 'type=video',
  149. 'maxResults=15'
  150. ].join('&');
  151. request(`https://www.googleapis.com/youtube/v3/search?${params}`, (err, res, body) => {
  152. if (err) {
  153. console.error(err);
  154. return cb({ status: 'error', message: 'Failed to search youtube with the requested query' });
  155. }
  156. cb({ status: 'success', data: JSON.parse(body) });
  157. });
  158. },
  159. '/stations/add/:song': (session, station, song, cb) => {
  160. if (!session.logged_in) return cb({ status: 'failure', message: 'You must be logged in to add a song' });
  161. const params = [
  162. 'part=snippet,contentDetails,statistics,status',
  163. `id=${encodeURIComponent(song.id)}`,
  164. `key=${config.get('apis.youtube.key')}`
  165. ].join('&');
  166. request(`https://www.googleapis.com/youtube/v3/videos?${params}`, (err, res, body) => {
  167. // TODO: Get data from Wikipedia and Spotify
  168. if (err) {
  169. console.error(err);
  170. return cb({ status: 'error', message: 'Failed to find song from youtube' });
  171. }
  172. const newSong = new globals.db.models.song({
  173. id: json.items[0].id,
  174. title: json.items[0].snippet.title,
  175. duration: globals.utils.convertTime(json.items[0].contentDetails.duration),
  176. thumbnail: json.items[0].snippet.thumbnails.high.url
  177. });
  178. // save the song to the database
  179. newSong.save(err => {
  180. if (err) {
  181. console.error(err);
  182. return cb({ status: 'error', message: 'Failed to save song from youtube to the database' });
  183. }
  184. stations.getStation(station).playlist.push(newSong);
  185. cb({ status: 'success', data: stations.getStation(station.playlist) });
  186. });
  187. });
  188. },
  189. '/songs': (session, cb) => {
  190. globals.db.models.song.find({}, (err, songs) => {
  191. if (err) throw err;
  192. cb(songs);
  193. });
  194. },
  195. '/songs/:song/update': (session, song, cb) => {
  196. globals.db.models.song.findOneAndUpdate({ id: song.id }, song, { upsert: true }, (err, updatedSong) => {
  197. if (err) throw err;
  198. cb(updatedSong);
  199. });
  200. },
  201. '/songs/:song/remove': (session, song, cb) => {
  202. globals.db.models.song.find({ id: song.id }).remove().exec();
  203. }
  204. };