coreHandler.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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': (username, email, password, recaptcha, cb) => {
  45. console.log(username, email, password, recaptcha);
  46. waterfall([
  47. // verify the request with google recaptcha
  48. (next) => {
  49. request({
  50. url: 'https://www.google.com/recaptcha/api/siteverify',
  51. method: 'POST',
  52. form: {
  53. 'secret': config.get("apis.recaptcha.secret"),
  54. 'response': recaptcha
  55. }
  56. }, next);
  57. },
  58. // check if the response from Google recaptcha is successful
  59. // if it is, we check if a user with the requested username already exists
  60. (response, body, next) => {
  61. let json = JSON.parse(body);
  62. console.log(json);
  63. if (json.success !== true) return next('Response from recaptcha was not successful');
  64. globals.db.models.user.findOne({ 'username': username }, next);
  65. },
  66. // if the user already exists, respond with that
  67. // otherwise check if a user with the requested email already exists
  68. (user, next) => {
  69. if (user) return next(true, { status: 'failure', message: 'A user with that username already exists' });
  70. globals.db.models.user.findOne({ 'email.address': email }, next);
  71. },
  72. // if the user already exists, respond with that
  73. // otherwise, generate a salt to use with hashing the new users password
  74. (user, next) => {
  75. if (user) return next(true, { status: 'failure', message: 'A user with that email already exists' });
  76. bcrypt.genSalt(10, next);
  77. },
  78. // hash the password
  79. (salt, next) => {
  80. bcrypt.hash(password, salt, next)
  81. },
  82. // save the new user to the database
  83. (hash, next) => {
  84. globals.db.models.user.create({
  85. username: username,
  86. email: {
  87. address: email,
  88. verificationToken: globals.utils.generateRandomString(64)
  89. },
  90. services: {
  91. password: {
  92. password: hash
  93. }
  94. }
  95. }, next);
  96. },
  97. // respond with the new user
  98. (newUser, next) => {
  99. next(null, { status: 'success', user: newUser })
  100. }
  101. ], (err, payload) => {
  102. // log this error somewhere
  103. if (err && err !== true) {
  104. console.error(err);
  105. return cb({ status: 'error', message: 'An error occurred while registering a new user' });
  106. }
  107. // respond with the payload that was passed to us earlier
  108. cb(payload);
  109. });
  110. },
  111. '/stations': cb => {
  112. cb(stations.getStations().map(station => {
  113. return {
  114. id: station.id,
  115. playlist: station.playlist,
  116. displayName: station.displayName,
  117. description: station.description,
  118. currentSongIndex: station.currentSongIndex,
  119. users: station.users
  120. }
  121. }));
  122. },
  123. '/stations/join/:id': (id, cb) => {
  124. stations.getStation(id).users = stations.getStation(id).users + 1;
  125. cb(stations.getStation(id).users);
  126. },
  127. '/stations/leave/:id': (id, cb) => {
  128. if (stations.getStation(id)) {
  129. stations.getStation(id).users = stations.getStation(id).users - 1;
  130. if (cb) cb(stations.getStation(id).users);
  131. }
  132. },
  133. '/youtube/getVideo/:query': (query, cb) => {
  134. const params = [
  135. 'part=snippet',
  136. `q=${encodeURIComponent(query)}`,
  137. `key=${config.get('apis.youtube.key')}`,
  138. 'type=video',
  139. 'maxResults=15'
  140. ].join('&');
  141. request(`https://www.googleapis.com/youtube/v3/search?${params}`, (err, res, body) => {
  142. cb(body);
  143. });
  144. },
  145. '/stations/add/:song': (station, song, user, cb) => {
  146. const params = [
  147. 'part=snippet,contentDetails,statistics,status',
  148. `id=${encodeURIComponent(song.id)}`,
  149. `key=${config.get('apis.youtube.key')}`
  150. ].join('&');
  151. // if (user.logged_in) {
  152. request(`https://www.googleapis.com/youtube/v3/videos?${params}`, (err, res, body) => {
  153. // TODO: Get data from Wikipedia and Spotify
  154. body = JSON.parse(body);
  155. const newSong = new globals.db.models.song({
  156. id: body.items[0].id,
  157. title: body.items[0].snippet.title,
  158. duration: globals.utils.convertTime(body.items[0].contentDetails.duration),
  159. thumbnail: body.items[0].snippet.thumbnails.high.url
  160. });
  161. console.log(newSong);
  162. newSong.save(err => {
  163. if (err) throw err;
  164. });
  165. stations.getStation(station).playlist.push(newSong);
  166. cb(stations.getStation(station.playlist));
  167. });
  168. //}
  169. },
  170. '/songs': cb => {
  171. globals.db.models.song.find({}, (err, songs) => {
  172. if (err) throw err;
  173. cb(songs);
  174. });
  175. },
  176. '/songs/:song/update': (song, cb) => {
  177. globals.db.models.song.findOneAndUpdate({ id: song.id }, song, { upsert: true }, (err, updatedSong) => {
  178. if (err) throw err;
  179. cb(updatedSong);
  180. });
  181. },
  182. '/songs/:song/remove': (song, cb) => {
  183. globals.db.models.song.find({ id: song.id }).remove().exec();
  184. }
  185. };