coreHandler.js 5.0 KB

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