coreHandler.js 5.0 KB

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