coreHandler.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. '/youtube/getVideos/:query': (query, cb) => {
  152. cb({
  153. type: "query",
  154. items: [
  155. {
  156. id: "39fk3490krf9",
  157. title: "Test Title",
  158. channel: "Test Channel",
  159. duration: 200,
  160. image: "https://i.ytimg.com/vi/lwg5yAuanPg/hqdefault.jpg?custom=true&w=196&h=110&stc=true&jpg444=true&jpgq=90&sp=68&sigh=DWOZl_nkv78qzj8WHPY1-53iQfA"
  161. },
  162. {
  163. id: "49iug05it",
  164. title: "Test Title 222",
  165. channel: "Test Channel 222",
  166. duration: 100,
  167. image: "https://i.ytimg.com/vi/QJwIsBoe3Lg/hqdefault.jpg?custom=true&w=196&h=110&stc=true&jpg444=true&jpgq=90&sp=68&sigh=8L20nlTyPf7xuIB8DTeBQFWW2Xw"
  168. }
  169. ]
  170. });
  171. },
  172. '/songs/queue/addSongs/:songs': (songs, user, cb) => {
  173. if (user !== null && user !== undefined && user.logged_in) {
  174. if (Array.isArray(songs)) {
  175. if (songs.length > 0) {
  176. let failed = 0;
  177. let success = 0;
  178. songs.forEach(function (song) {
  179. if (typeof song === "object" && song !== null) {
  180. let obj = {};
  181. obj.title = song.title;
  182. obj._id = song.id;
  183. obj.artists = [];
  184. obj.image = "test";
  185. obj.duration = 0;
  186. obj.genres = ["edm"];
  187. //TODO Get data from Wikipedia and Spotify
  188. obj.requestedBy = user._id;
  189. console.log(user._id);
  190. console.log(user);
  191. obj.requestedAt = Date.now();
  192. let queueSong = new global.db.queueSong(obj);
  193. queueSong.save(function(err) {
  194. console.log(err);
  195. if (err) failed++;
  196. else success++;
  197. });
  198. } else {
  199. failed++;
  200. }
  201. });
  202. cb({success, failed});
  203. } else {
  204. cb({err: "No songs supplied."});
  205. }
  206. } else {
  207. cb({err: "Not supplied an array."});
  208. }
  209. } else {
  210. cb({err: "Not logged in."});
  211. }
  212. },
  213. '/songs/queue/getSongs': (user, cb) => {
  214. if (user !== null && user !== undefined && user.logged_in) {
  215. global.db.queueSong.find({}, function(err, songs) {
  216. if (err) throw err;
  217. else cb({songs: songs});
  218. });
  219. } else {
  220. cb({err: "Not logged in."});
  221. }
  222. },
  223. '/songs/queue/updateSong/:id': (user, id, object, cb) => {
  224. if (user !== null && user !== undefined && user.logged_in) {
  225. global.db.queueSong.findOne({_id: id}, function(err, song) {
  226. if (err) throw err;
  227. else {
  228. if (song !== undefined && song !== null) {
  229. if (typeof object === "object" && object !== null) {
  230. delete object.requestedBy;
  231. delete object.requestedAt;
  232. global.db.queueSong.update({_id: id}, {$set: object}, function(err, song) {
  233. if (err) throw err;
  234. cb({success: true});
  235. });
  236. } else {
  237. cb({err: "Invalid data."});
  238. }
  239. } else {
  240. cb({err: "Song not found."});
  241. }
  242. }
  243. });
  244. } else {
  245. cb({err: "Not logged in."});
  246. }
  247. },
  248. /*'/stations/search/:query': (query, cb) => {
  249. const params = [
  250. 'part=snippet',
  251. `q=${encodeURIComponent(query)}`,
  252. `key=${config.get('apis.youtube.key')}`,
  253. 'type=video',
  254. 'maxResults=25'
  255. ].join('&');
  256. request(`https://www.googleapis.com/youtube/v3/search?${params}`, (err, res, body) => {
  257. if (err) {
  258. return cb({ status: 'error', message: 'Failed to make request' });
  259. }
  260. else {
  261. try {
  262. return cb({ status: 'success', body: JSON.parse(body) });
  263. }
  264. catch (e) {
  265. return cb({ status: 'error', message: 'Non JSON response' });
  266. }
  267. }
  268. });
  269. },*/
  270. '/song/:id/toggleLike': (songId, userId, cb) => {
  271. var user = global.db.user.findOne(userId);
  272. var song = global.db.song.findOne(songId);
  273. if (user !== undefined) {
  274. if (song !== undefined) {
  275. var liked = false;
  276. if (song.likes.indexOf(userId) === -1) {
  277. liked = true;
  278. // Add like
  279. } else {
  280. // Remove like
  281. }
  282. if (song.dislikes.indexOf(userId) !== -1) {
  283. // Remove dislike
  284. }
  285. // Emit to all sockets with this user that their likes/dislikes updated.
  286. // Emit to all sockets in the room that the likes/dislikes has updated
  287. cb({liked: liked, disliked: false});
  288. } else {
  289. cb({err: "Song not found."});
  290. }
  291. } else {
  292. cb({err: "User not found."});
  293. }
  294. },
  295. '/user/:id/ratings': (userId, cb) => {
  296. var user = global.db.user.findOne(userId);
  297. if (user !== undefined) {
  298. cb({likes: user.likes, dislikes: user.dislikes});
  299. } else {
  300. cb({err: "User not found."});
  301. }
  302. }
  303. };