coreHandler.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 globals = require('./globals'),
  15. stations = require('./stations');
  16. var eventEmitter = new events.EventEmitter();
  17. const edmStation = new stations.Station("edm", {
  18. "genres": ["edm"],
  19. playlist: [
  20. 'gCYcHz2k5x0'
  21. ],
  22. currentSongIndex: 0,
  23. paused: false,
  24. displayName: "EDM",
  25. description: "EDM Music"
  26. });
  27. const chillStation = new stations.Station("chill", {
  28. "genres": ["chill"],
  29. playlist: [
  30. 'gCYcHz2k5x0'
  31. ],
  32. currentSongIndex: 0,
  33. paused: false,
  34. displayName: "Chill",
  35. description: "Chill Music"
  36. });
  37. stations.addStation(edmStation);
  38. stations.addStation(chillStation);
  39. module.exports = {
  40. // module functions
  41. on: (name, cb) => eventEmitter.on(name, cb),
  42. emit: (name, data) => eventEmitter.emit(name, data),
  43. // core route handlers
  44. '/users/login': (session, identifier, password, cb) => {
  45. waterfall([
  46. // check if a user with the requested identifier exists
  47. (next) => globals.db.models.user.findOne({
  48. $or: [{ 'username': identifier }, { 'email.address': identifier }]
  49. }, next),
  50. // if the user doesn't exist, respond with a failure
  51. // otherwise compare the requested password and the actual users password
  52. (user, next) => {
  53. if (!user) return next(true, { status: 'failure', message: 'User not found' });
  54. bcrypt.compare(password, user.services.password.password, next);
  55. },
  56. // if the user exists, and the passwords match, respond with a success
  57. (result, next) => {
  58. // TODO: Authenticate the user with Passport here I think?
  59. // TODO: We need to figure out how other login methods will work
  60. next(null, {
  61. status: result ? 'success': 'failure',
  62. message: result ? 'Logged in' : 'User not found'
  63. });
  64. }
  65. ], (err, payload) => {
  66. // log this error somewhere
  67. if (err && err !== true) {
  68. console.error(err);
  69. return cb({ status: 'error', message: 'An error occurred while logging in' });
  70. }
  71. // respond with the payload that was passed to us earlier
  72. cb(payload);
  73. });
  74. },
  75. '/users/register': (session, username, email, password, recaptcha, cb) => {
  76. waterfall([
  77. // verify the request with google recaptcha
  78. (next) => {
  79. request({
  80. url: 'https://www.google.com/recaptcha/api/siteverify',
  81. method: 'POST',
  82. form: {
  83. 'secret': config.get("apis.recaptcha.secret"),
  84. 'response': recaptcha
  85. }
  86. }, next);
  87. },
  88. // check if the response from Google recaptcha is successful
  89. // if it is, we check if a user with the requested username already exists
  90. (response, body, next) => {
  91. let json = JSON.parse(body);
  92. console.log(json);
  93. if (json.success !== true) return next('Response from recaptcha was not successful');
  94. globals.db.models.user.findOne({ 'username': username }, next);
  95. },
  96. // if the user already exists, respond with that
  97. // otherwise check if a user with the requested email already exists
  98. (user, next) => {
  99. if (user) return next(true, { status: 'failure', message: 'A user with that username already exists' });
  100. globals.db.models.user.findOne({ 'email.address': email }, next);
  101. },
  102. // if the user already exists, respond with that
  103. // otherwise, generate a salt to use with hashing the new users password
  104. (user, next) => {
  105. if (user) return next(true, { status: 'failure', message: 'A user with that email already exists' });
  106. bcrypt.genSalt(10, next);
  107. },
  108. // hash the password
  109. (salt, next) => {
  110. bcrypt.hash(password, salt, next)
  111. },
  112. // save the new user to the database
  113. (hash, next) => {
  114. globals.db.models.user.create({
  115. username: username,
  116. email: {
  117. address: email,
  118. verificationToken: globals.utils.generateRandomString(64)
  119. },
  120. services: {
  121. password: {
  122. password: hash
  123. }
  124. }
  125. }, next);
  126. },
  127. // respond with the new user
  128. (newUser, next) => {
  129. next(null, { status: 'success', user: newUser })
  130. }
  131. ], (err, payload) => {
  132. // log this error somewhere
  133. if (err && err !== true) {
  134. console.error(err);
  135. return cb({ status: 'error', message: 'An error occurred while registering for an account' });
  136. }
  137. // respond with the payload that was passed to us earlier
  138. cb(payload);
  139. });
  140. },
  141. '/users/logout': (req, cb) => {
  142. if (!req.user || !req.user.logged_in) return cb({ status: 'failure', message: `You're not currently logged in` });
  143. req.logout();
  144. cb({ status: 'success', message: `You've been successfully logged out` });
  145. },
  146. '/stations': (session, cb) => {
  147. cb(stations.getStations().map(station => {
  148. return {
  149. id: station.id,
  150. playlist: station.playlist,
  151. displayName: station.displayName,
  152. description: station.description,
  153. currentSongIndex: station.currentSongIndex,
  154. users: station.users
  155. }
  156. }));
  157. },
  158. '/stations/join/:id': (session, id, cb) => {
  159. let station = stations.getStation(id);
  160. if (!station) return cb({ status: 'error', message: `Station with id '${id}' does not exist` });
  161. session.station_id = id;
  162. station.users++;
  163. cb({ status: 'success', users: station.users });
  164. },
  165. // leaves the users current station
  166. // returns the count of users that are still in that station
  167. '/stations/leave': (session, cb) => {
  168. let station = stations.getStation(session.station_id);
  169. if (!station) return cb({ status: 'failure', message: `Not currently in a station, or station doesn't exist` });
  170. session.station_id = "";
  171. station.users--;
  172. cb({ status: 'success', users: station.users });
  173. },
  174. '/youtube/getVideo/:query': (session, query, cb) => {
  175. const params = [
  176. 'part=snippet',
  177. `q=${encodeURIComponent(query)}`,
  178. `key=${config.get('apis.youtube.key')}`,
  179. 'type=video',
  180. 'maxResults=15'
  181. ].join('&');
  182. request(`https://www.googleapis.com/youtube/v3/search?${params}`, (err, res, body) => {
  183. if (err) {
  184. console.error(err);
  185. return cb({ status: 'error', message: 'Failed to search youtube with the requested query' });
  186. }
  187. try {
  188. let json = JSON.parse(body);
  189. }
  190. catch (e) {
  191. return cb({ status: 'error', message: 'Invalid response from youtube' });
  192. }
  193. cb({ status: 'success', data: json });
  194. });
  195. },
  196. '/stations/add/:song': (session, station, song, cb) => {
  197. if (!session.logged_in) return cb({ status: 'failure', message: 'You must be logged in to add a song' });
  198. const params = [
  199. 'part=snippet,contentDetails,statistics,status',
  200. `id=${encodeURIComponent(song.id)}`,
  201. `key=${config.get('apis.youtube.key')}`
  202. ].join('&');
  203. request(`https://www.googleapis.com/youtube/v3/videos?${params}`, (err, res, body) => {
  204. // TODO: Get data from Wikipedia and Spotify
  205. if (err) {
  206. console.error(err);
  207. return cb({ status: 'error', message: 'Failed to find song from youtube' });
  208. }
  209. try {
  210. let json = JSON.parse(body);
  211. }
  212. catch (e) {
  213. return cb({ status: 'error', message: 'Invalid response from youtube' });
  214. }
  215. const newSong = new globals.db.models.song({
  216. id: json.items[0].id,
  217. title: json.items[0].snippet.title,
  218. duration: globals.utils.convertTime(json.items[0].contentDetails.duration),
  219. thumbnail: json.items[0].snippet.thumbnails.high.url
  220. });
  221. // save the song to the database
  222. newSong.save(err => {
  223. if (err) {
  224. console.error(err);
  225. return cb({ status: 'error', message: 'Failed to save song from youtube to the database' });
  226. }
  227. stations.getStation(station).playlist.push(newSong);
  228. cb({ status: 'success', data: stations.getStation(station.playlist) });
  229. });
  230. });
  231. },
  232. '/songs': (session, cb) => {
  233. globals.db.models.song.find({}, (err, songs) => {
  234. if (err) throw err;
  235. cb(songs);
  236. });
  237. },
  238. '/songs/:song/update': (session, song, cb) => {
  239. globals.db.models.song.findOneAndUpdate({ id: song.id }, song, { upsert: true }, (err, updatedSong) => {
  240. if (err) throw err;
  241. cb(updatedSong);
  242. });
  243. },
  244. '/songs/:song/remove': (session, song, cb) => {
  245. globals.db.models.song.find({ id: song.id }).remove().exec();
  246. }
  247. };