coreHandler.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. '/station/:id/join': (stationId, socketId, cb) => {
  132. const station = stations.getStation(stationId);
  133. if (station) {
  134. var response = station.handleUserJoin(socketId);
  135. return cb(response);
  136. }
  137. else {
  138. return cb({ status: 'error', message: 'Room with that ID does not exists' });
  139. }
  140. },
  141. '/station/:id/skip': (stationId, socketId, cb) => {
  142. const station = stations.getStation(stationId);
  143. if (station) {
  144. var response = station.handleUserJoin(socketId);
  145. return cb(response);
  146. }
  147. else {
  148. return cb({ status: 'error', message: 'Room with that ID does not exists' });
  149. }
  150. },
  151. /*'/stations/search/:query': (query, cb) => {
  152. const params = [
  153. 'part=snippet',
  154. `q=${encodeURIComponent(query)}`,
  155. `key=${config.get('apis.youtube.key')}`,
  156. 'type=video',
  157. 'maxResults=25'
  158. ].join('&');
  159. request(`https://www.googleapis.com/youtube/v3/search?${params}`, (err, res, body) => {
  160. if (err) {
  161. return cb({ status: 'error', message: 'Failed to make request' });
  162. }
  163. else {
  164. try {
  165. return cb({ status: 'success', body: JSON.parse(body) });
  166. }
  167. catch (e) {
  168. return cb({ status: 'error', message: 'Non JSON response' });
  169. }
  170. }
  171. });
  172. },*/
  173. '/song/:id/toggleLike': (songId, userId, cb) => {
  174. var user = global.db.user.findOne(userId);
  175. var song = global.db.song.findOne(songId);
  176. if (user !== undefined) {
  177. if (song !== undefined) {
  178. var liked = false;
  179. if (song.likes.indexOf(userId) === -1) {
  180. liked = true;
  181. // Add like
  182. } else {
  183. // Remove like
  184. }
  185. if (song.dislikes.indexOf(userId) !== -1) {
  186. // Remove dislike
  187. }
  188. // Emit to all sockets with this user that their likes/dislikes updated.
  189. // Emit to all sockets in the room that the likes/dislikes has updated
  190. cb({liked: liked, disliked: false});
  191. } else {
  192. cb({err: "Song not found."});
  193. }
  194. } else {
  195. cb({err: "User not found."});
  196. }
  197. },
  198. '/user/:id/ratings': (userId, cb) => {
  199. var user = global.db.user.findOne(userId);
  200. if (user !== undefined) {
  201. cb({likes: user.likes, dislikes: user.dislikes});
  202. } else {
  203. cb({err: "User not found."});
  204. }
  205. }
  206. };