coreHandler.js 5.0 KB

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