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