coreHandler.js 5.0 KB

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