coreHandler.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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: 20,
  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"],
  33. duration: 20,
  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. const popStation = new stations.Station("pop", {
  45. "genres": ["pop"],
  46. playlist: [
  47. {
  48. id: "HXeYRs_zR6w",
  49. title: "Nobody But Me",
  50. artists: ["Michael Bublé"],
  51. duration: 12,
  52. thumbnail: "https://yt3.ggpht.com/-CGlBu6kDEi8/AAAAAAAAAAI/AAAAAAAAAAA/Pi679mvyyyU/s88-c-k-no-mo-rj-c0xffffff/photo.jpg",
  53. likes: 0,
  54. dislikes: 1
  55. },
  56. {
  57. id: "CR4YE7htLgI",
  58. title: "Someday ",
  59. artists: ["Michael Bublé", "Meghan Trainor"],
  60. duration: 30,
  61. thumbnail: "https://yt3.ggpht.com/-CGlBu6kDEi8/AAAAAAAAAAI/AAAAAAAAAAA/Pi679mvyyyU/s88-c-k-no-mo-rj-c0xffffff/photo.jpg",
  62. likes: 0,
  63. dislikes: 1
  64. }
  65. ],
  66. currentSongIndex: 0,
  67. paused: false,
  68. displayName: "Pop",
  69. description: "Pop Music"
  70. });
  71. stations.addStation(edmStation);
  72. stations.addStation(popStation);
  73. module.exports = {
  74. // module functions
  75. on: (name, cb) => {
  76. eventEmitter.on(name, cb);
  77. },
  78. emit: (name, data) => {
  79. eventEmitter.emit(name, data);
  80. },
  81. // core route handlers
  82. '/users/register': (username, email, password, recaptcha, cb) => {
  83. console.log(username, password);
  84. request({
  85. url: 'https://www.google.com/recaptcha/api/siteverify',
  86. method: 'POST',
  87. form: {
  88. 'secret': config.get("apis.recapthca.secret"),
  89. 'response': recaptcha
  90. }
  91. }, function (error, response, body) {
  92. if (error === null && JSON.parse(body).success === true) {
  93. body = JSON.parse(body);
  94. global.db.user.findOne({'username': username}, function (err, user) {
  95. console.log(err, user);
  96. if (err) return cb(err);
  97. if (user) return cb("username");
  98. else {
  99. global.db.user.findOne({'email.address': email}, function (err, user) {
  100. console.log(err, user);
  101. if (err) return cb(err);
  102. if (user) return cb("email");
  103. else {
  104. // TODO: Email verification code, send email
  105. bcrypt.genSalt(10, function (err, salt) {
  106. if (err) {
  107. return cb(err);
  108. } else {
  109. bcrypt.hash(password, salt, function (err, hash) {
  110. if (err) {
  111. return cb(err);
  112. } else {
  113. let newUser = new global.db.user({
  114. username: username,
  115. email: {
  116. address: email,
  117. verificationToken: global.generateRandomString("64")
  118. },
  119. services: {
  120. password: {
  121. password: hash
  122. }
  123. }
  124. });
  125. newUser.save(function (err) {
  126. if (err) throw err;
  127. return cb(null, newUser);
  128. });
  129. }
  130. });
  131. }
  132. });
  133. }
  134. });
  135. }
  136. });
  137. } else {
  138. cb("Recaptcha failed");
  139. }
  140. });
  141. },
  142. '/stations': cb => {
  143. cb(stations.getStations().map(station => {
  144. return {
  145. id: station.id,
  146. playlist: station.playlist,
  147. displayName: station.displayName,
  148. description: station.description,
  149. currentSongIndex: station.currentSongIndex,
  150. users: station.users
  151. }
  152. }));
  153. },
  154. '/stations/join/:id': (id, cb) => {
  155. stations.getStation(id).users = stations.getStation(id).users + 1;
  156. cb(stations.getStation(id).users);
  157. },
  158. '/stations/leave/:id': (id, cb) => {
  159. if (stations.getStation(id)) {
  160. stations.getStation(id).users = stations.getStation(id).users - 1;
  161. if (cb) cb(stations.getStation(id).users);
  162. }
  163. },
  164. '/youtube/getVideo/:query': (query, cb) => {
  165. const params = [
  166. 'part=snippet',
  167. `q=${encodeURIComponent(query)}`,
  168. `key=${config.get('apis.youtube.key')}`,
  169. 'type=video',
  170. 'maxResults=15'
  171. ].join('&');
  172. // function params(type, id) {
  173. // if (type == "search") {
  174. // return [
  175. // 'part=snippet',
  176. // `q=${encodeURIComponent(query)}`,
  177. // `key=${config.get('apis.youtube.key')}`,
  178. // 'type=video',
  179. // 'maxResults=15'
  180. // ].join('&');
  181. // } else if (type == "video") {
  182. // return [
  183. // 'part=snippet,contentDetails,statistics,status',
  184. // `id=${encodeURIComponent(id)}`,
  185. // `key=${config.get('apis.youtube.key')}`
  186. // ].join('&');
  187. // }
  188. // }
  189. // let finalResults = [];
  190. request(`https://www.googleapis.com/youtube/v3/search?${params}`, (err, res, body) => {
  191. cb(body);
  192. // for (let i = 0; i < results.items.length; i++) {
  193. // request(`https://www.googleapis.com/youtube/v3/videos?${
  194. // params("video", results.items[i].id.videoId)
  195. // }`, (err, res, body) => {
  196. // finalResults.push(JSON.parse(body));
  197. // });
  198. // }
  199. // setTimeout(() => {
  200. // return cb(finalResults);
  201. // }, 500);
  202. });
  203. },
  204. '/songs/queue/add/:song': (song, user, cb) => {
  205. if (user.logged_in) {
  206. // if (songs.length > 0) {
  207. // let failed = 0;
  208. // let success = 0;
  209. // songs.forEach(function (song) {
  210. // if (typeof song === "object" && song !== null) {
  211. // let obj = {};
  212. // obj.title = song.title;
  213. // obj._id = song.id;
  214. // obj.artists = [];
  215. // obj.image = "test";
  216. // obj.duration = 0;
  217. // obj.genres = ["edm"];
  218. // //TODO Get data from Wikipedia and Spotify
  219. // obj.requestedBy = user._id;
  220. // console.log(user._id);
  221. // console.log(user);
  222. // obj.requestedAt = Date.now();
  223. // let queueSong = new global.db.queueSong(obj);
  224. // queueSong.save(function(err) {
  225. // console.log(err);
  226. // if (err) failed++;
  227. // else success++;
  228. // });
  229. // } else {
  230. // failed++;
  231. // }
  232. // });
  233. // cb({success, failed});
  234. // } else {
  235. // cb({err: "No songs supplied."});
  236. // }
  237. console.log(song);
  238. } else {
  239. cb({err: "Not logged in."});
  240. }
  241. },
  242. '/songs/queue/getSongs': (user, cb) => {
  243. if (user !== null && user !== undefined && user.logged_in) {
  244. global.db.queueSong.find({}, function(err, songs) {
  245. if (err) throw err;
  246. else cb({songs: songs});
  247. });
  248. } else {
  249. cb({err: "Not logged in."});
  250. }
  251. },
  252. '/songs/queue/updateSong/:id': (user, id, object, cb) => {
  253. if (user !== null && user !== undefined && user.logged_in) {
  254. global.db.queueSong.findOne({_id: id}, function(err, song) {
  255. if (err) throw err;
  256. else {
  257. if (song !== undefined && song !== null) {
  258. if (typeof object === "object" && object !== null) {
  259. delete object.requestedBy;
  260. delete object.requestedAt;
  261. global.db.queueSong.update({_id: id}, {$set: object}, function(err, song) {
  262. if (err) throw err;
  263. cb({success: true});
  264. });
  265. } else {
  266. cb({err: "Invalid data."});
  267. }
  268. } else {
  269. cb({err: "Song not found."});
  270. }
  271. }
  272. });
  273. } else {
  274. cb({err: "Not logged in."});
  275. }
  276. }
  277. };