coreHandler.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 global = require('./global'),
  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. stations.addStation(edmStation);
  28. module.exports = {
  29. // module functions
  30. on: (name, cb) => {
  31. eventEmitter.on(name, cb);
  32. },
  33. emit: (name, data) => {
  34. eventEmitter.emit(name, data);
  35. },
  36. // core route handlers
  37. '/users/register': (username, email, password, recaptcha, cb) => {
  38. console.log(username, password);
  39. request({
  40. url: 'https://www.google.com/recaptcha/api/siteverify',
  41. method: 'POST',
  42. form: {
  43. 'secret': config.get("apis.recapthca.secret"),
  44. 'response': recaptcha
  45. }
  46. }, function (error, response, body) {
  47. if (error === null && JSON.parse(body).success === true) {
  48. body = JSON.parse(body);
  49. global.db.user.findOne({'username': username}, function (err, user) {
  50. console.log(err, user);
  51. if (err) return cb(err);
  52. if (user) return cb("username");
  53. else {
  54. global.db.user.findOne({'email.address': email}, function (err, user) {
  55. console.log(err, user);
  56. if (err) return cb(err);
  57. if (user) return cb("email");
  58. else {
  59. // TODO: Email verification code, send email
  60. bcrypt.genSalt(10, function (err, salt) {
  61. if (err) {
  62. return cb(err);
  63. } else {
  64. bcrypt.hash(password, salt, function (err, hash) {
  65. if (err) {
  66. return cb(err);
  67. } else {
  68. let newUser = new global.db.user({
  69. username: username,
  70. email: {
  71. address: email,
  72. verificationToken: global.generateRandomString("64")
  73. },
  74. services: {
  75. password: {
  76. password: hash
  77. }
  78. }
  79. });
  80. newUser.save(err => {
  81. if (err) throw err;
  82. return cb(null, newUser);
  83. });
  84. }
  85. });
  86. }
  87. });
  88. }
  89. });
  90. }
  91. });
  92. } else {
  93. cb("Recaptcha failed");
  94. }
  95. });
  96. },
  97. '/stations': cb => {
  98. cb(stations.getStations().map(station => {
  99. return {
  100. id: station.id,
  101. playlist: station.playlist,
  102. displayName: station.displayName,
  103. description: station.description,
  104. currentSongIndex: station.currentSongIndex,
  105. users: station.users
  106. }
  107. }));
  108. },
  109. '/stations/join/:id': (id, cb) => {
  110. stations.getStation(id).users = stations.getStation(id).users + 1;
  111. cb(stations.getStation(id).users);
  112. },
  113. '/stations/leave/:id': (id, cb) => {
  114. if (stations.getStation(id)) {
  115. stations.getStation(id).users = stations.getStation(id).users - 1;
  116. if (cb) cb(stations.getStation(id).users);
  117. }
  118. },
  119. '/youtube/getVideo/:query': (query, cb) => {
  120. const params = [
  121. 'part=snippet',
  122. `q=${encodeURIComponent(query)}`,
  123. `key=${config.get('apis.youtube.key')}`,
  124. 'type=video',
  125. 'maxResults=15'
  126. ].join('&');
  127. request(`https://www.googleapis.com/youtube/v3/search?${params}`, (err, res, body) => {
  128. cb(body);
  129. });
  130. },
  131. '/stations/add/:song': (station, song, user, cb) => {
  132. const params = [
  133. 'part=snippet,contentDetails,statistics,status',
  134. `id=${encodeURIComponent(song.id)}`,
  135. `key=${config.get('apis.youtube.key')}`
  136. ].join('&');
  137. // if (user.logged_in) {
  138. request(`https://www.googleapis.com/youtube/v3/videos?${params}`, (err, res, body) => {
  139. // TODO: Get data from Wikipedia and Spotify
  140. body = JSON.parse(body);
  141. const newSong = new global.db.song({
  142. id: body.items[0].id,
  143. title: body.items[0].snippet.title,
  144. duration: global.convertTime(body.items[0].contentDetails.duration),
  145. thumbnail: body.items[0].snippet.thumbnails.high.url
  146. });
  147. console.log(newSong);
  148. newSong.save(err => {
  149. if (err) throw err;
  150. });
  151. stations.getStation(station).playlist.push(newSong);
  152. cb(stations.getStation(station.playlist));
  153. });
  154. //}
  155. },
  156. '/songs': cb => {
  157. // let songs = [];
  158. // cb(stations.getStations().map(station => {
  159. // station.playlist.forEach(song => {
  160. // songs.push(song);
  161. // });
  162. // return songs;
  163. // }));
  164. global.db.song.find({}, (err, songs) => {
  165. if (err) throw err;
  166. cb(songs);
  167. });
  168. },
  169. '/songs/update': (songs, cb) => {
  170. console.log(songs);
  171. cb(stations.getStations());
  172. }
  173. };