coreHandler.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. passport = require('passport');
  12. // custom modules
  13. const global = require('./global'),
  14. stations = require('./stations');
  15. var eventEmitter = new events.EventEmitter();
  16. module.exports = {
  17. // module functions
  18. on: function (name, cb) {
  19. eventEmitter.on(name, cb);
  20. },
  21. emit: function (name, data) {
  22. eventEmitter.emit(name, data);
  23. },
  24. // core route handlers
  25. '/users/login': function (user, cb) {
  26. passport.authenticate('local-login', {
  27. // successRedirect: cb({ status: 'success', message: 'Successfully logged in' }),
  28. // failureRedirect: cb({ status: 'error', message: 'Error while trying to log in' })
  29. });
  30. },
  31. '/users/register': function (user, cb) {
  32. passport.authenticate('local-signup', {
  33. // successRedirect: cb({ status: 'success', message: 'Successfully signed up' }),
  34. // failureRedirect: cb({ status: 'error', message: 'Error while trying to sign up' })
  35. });
  36. },
  37. '/stations': function (cb) {
  38. cb(stations.getStations().map(function (result) {
  39. return {
  40. id: result.getId(),
  41. displayName: result.getDisplayName(),
  42. description: result.getDescription(),
  43. users: result.getUsers()
  44. }
  45. }));
  46. },
  47. '/stations/join/:id': function (id, user, cb) {
  48. var station = stations.getStation(id);
  49. if (station) {
  50. user.stationId = id;
  51. this.emit('station-joined', {
  52. user: {
  53. id: user.id,
  54. username: user.username
  55. }
  56. });
  57. return cb({
  58. status: 'joined',
  59. data: {
  60. displayName: station.getDisplayName(),
  61. users: station.getUsers(),
  62. currentSong: station.getCurrentSong()
  63. }
  64. });
  65. }
  66. else {
  67. return cb({ status: 'error', message: 'Room with that ID does not exists' });
  68. }
  69. },
  70. '/stations/search/:query': function (query, cb) {
  71. var params = [
  72. 'part=snippet',
  73. `q=${encodeURIComponent(query)}`,
  74. `key=${config.get('apis.youtube.key')}`,
  75. 'type=video',
  76. 'maxResults=25'
  77. ].join('&');
  78. request(`https://www.googleapis.com/youtube/v3/search?${params}`, function (err, res, body) {
  79. if (err) {
  80. return cb({ status: 'error', message: 'Failed to make request' });
  81. }
  82. else {
  83. try {
  84. return cb({ status: 'success', body: JSON.parse(body) });
  85. }
  86. catch (e) {
  87. return cb({ status: 'error', message: 'Non JSON response' });
  88. }
  89. }
  90. });
  91. }
  92. };