coreHandler.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. request({
  63. url: 'https://www.google.com/recaptcha/api/siteverify',
  64. method: 'POST',
  65. form: {
  66. 'secret': config.get("apis.recapthca.secret"),
  67. 'response': recaptcha
  68. }
  69. }, function (error, response, body) {
  70. console.log(error, body, error === null, JSON.parse(body).success === true);
  71. if (error === null && JSON.parse(body).success === true) {
  72. body = JSON.parse(body);
  73. global.db.user.findOne({'username': username}, function (err, user) {
  74. console.log(err, user);
  75. if (err) return cb(err);
  76. if (user) return cb("username");
  77. else {
  78. global.db.user.findOne({'email.address': email}, function (err, user) {
  79. console.log(err, user);
  80. if (err) return cb(err);
  81. if (user) return cb("email");
  82. else {
  83. //TODO Email verification code, send email
  84. bcrypt.genSalt(10, function (err, salt) {
  85. if (err) {
  86. return cb(err);
  87. } else {
  88. bcrypt.hash(password, salt, function (err, hash) {
  89. if (err) {
  90. return cb(err);
  91. } else {
  92. let newUser = new global.db.user({
  93. username: username,
  94. email: {
  95. address: email,
  96. verificationToken: global.generateRandomString("64")
  97. },
  98. services: {
  99. password: {
  100. password: hash
  101. }
  102. }
  103. });
  104. newUser.save(function (err) {
  105. if (err) throw err;
  106. return cb(null, newUser);
  107. });
  108. }
  109. });
  110. }
  111. });
  112. }
  113. });
  114. }
  115. });
  116. } else {
  117. cb("Recaptcha failed");
  118. }
  119. });
  120. },
  121. '/stations': cb => {
  122. cb(stations.getStations().map(function (result) {
  123. return {
  124. id: result.getId(),
  125. displayName: result.getDisplayName(),
  126. description: result.getDescription(),
  127. users: result.getUsers()
  128. }
  129. }));
  130. },
  131. '/stations/join/:id': (id, user, cb) => {
  132. const station = stations.getStation(id);
  133. if (station) {
  134. user.stationId = id;
  135. /*io.sockets.emit('station-joined', {
  136. user: {
  137. id: user.id,
  138. username: user.username
  139. }
  140. });*/
  141. return cb({
  142. status: 'joined',
  143. data: {
  144. displayName: station.getDisplayName(),
  145. users: station.getUsers(),
  146. currentSong: station.getCurrentSong(),
  147. timePaused: station.getTimePaused(),
  148. paused: station.isPaused(),
  149. currentTime: Date.now()
  150. }
  151. });
  152. }
  153. else {
  154. return cb({ status: 'error', message: 'Room with that ID does not exists' });
  155. }
  156. },
  157. '/stations/search/:query': (query, cb) => {
  158. const params = [
  159. 'part=snippet',
  160. `q=${encodeURIComponent(query)}`,
  161. `key=${config.get('apis.youtube.key')}`,
  162. 'type=video',
  163. 'maxResults=25'
  164. ].join('&');
  165. request(`https://www.googleapis.com/youtube/v3/search?${params}`, (err, res, body) => {
  166. if (err) {
  167. return cb({ status: 'error', message: 'Failed to make request' });
  168. }
  169. else {
  170. try {
  171. return cb({ status: 'success', body: JSON.parse(body) });
  172. }
  173. catch (e) {
  174. return cb({ status: 'error', message: 'Non JSON response' });
  175. }
  176. }
  177. });
  178. },
  179. '/songs/:id/toggleLike': (songId, userId, cb) => {
  180. var user = global.db.user.findOne(userId);
  181. var song = global.db.song.findOne(songId);
  182. if (user !== undefined) {
  183. if (song !== undefined) {
  184. var liked = false;
  185. if (song.likes.indexOf(userId) === -1) {
  186. liked = true;
  187. // Add like
  188. } else {
  189. // Remove like
  190. }
  191. if (song.dislikes.indexOf(userId) !== -1) {
  192. // Remove dislike
  193. }
  194. // Emit to all sockets with this user that their likes/dislikes updated.
  195. // Emit to all sockets in the room that the likes/dislikes has updated
  196. cb({liked: liked, disliked: false});
  197. } else {
  198. cb({err: "Song not found."});
  199. }
  200. } else {
  201. cb({err: "User not found."});
  202. }
  203. },
  204. '/user/ratings': (userId, cb) => {
  205. var user = global.db.user.findOne(userId);
  206. if (user !== undefined) {
  207. cb({likes: user.likes, dislikes: user.dislikes});
  208. } else {
  209. cb({err: "User not found."});
  210. }
  211. }
  212. };