coreHandler.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. r = require('rethinkdb');
  12. // custom modules
  13. const global = require('./global'),
  14. passport = global.passport,
  15. localStrategy = global.localStrategy,
  16. stations = require('./stations');
  17. var eventEmitter = new events.EventEmitter();
  18. module.exports = {
  19. // auth
  20. passport.serializeUser(function(user, cb) {
  21. done(null, user.id);
  22. });
  23. passport.deserializeUser(function(id, cb) {
  24. r.table('users').filter({id}).run(rc, (err, cursor) => {
  25. done(err, cursor.toArray().result);
  26. });
  27. });
  28. app.use(passport.initialize());
  29. app.use(passport.session());
  30. // module functions
  31. on: function (name, cb) {
  32. eventEmitter.on(name, cb);
  33. },
  34. emit: function (name, data) {
  35. eventEmitter.emit(name, data);
  36. },
  37. // core route handlers
  38. '/users/login': function (user, cb) {},
  39. '/users/register': function (user, cb) {
  40. passport.use('local-signup', new localStrategy({
  41. usernameField : user.email,
  42. passwordField : user.password,
  43. passReqToCallback : true
  44. }, (req, email, password, done) => {
  45. process.nextTick(() => {
  46. r.table('users').filter({
  47. email: user.email
  48. }).run(rc, (err, cursor) => {
  49. if (err) return done(err);
  50. else {
  51. cursor.toArray((err, result) => {
  52. if (result) {
  53. return done(null, false);
  54. } else {
  55. r.table('authors').insert([{
  56. email,
  57. password: crypto.createHash('md5').update(password).digest("hex")
  58. }]).run(connection, function(err, result) {
  59. if (err) throw err;
  60. return done(null, result);
  61. console.log(result);
  62. });
  63. }
  64. });
  65. }
  66. });
  67. });
  68. }));
  69. },
  70. '/stations': function (cb) {
  71. cb(stations.getStations().map(function (result) {
  72. return {
  73. id: result.getId(),
  74. displayName: result.getDisplayName(),
  75. description: result.getDescription(),
  76. users: result.getUsers()
  77. }
  78. }));
  79. },
  80. '/stations/join/:id': function (id, user, cb) {
  81. var station = stations.getStation(id);
  82. if (station) {
  83. user.stationId = id;
  84. this.emit('station-joined', {
  85. user: {
  86. id: user.id,
  87. username: user.username
  88. }
  89. });
  90. return cb({
  91. status: 'joined',
  92. data: {
  93. displayName: station.getDisplayName(),
  94. users: station.getUsers(),
  95. currentSong: station.getCurrentSong()
  96. }
  97. });
  98. }
  99. else {
  100. return cb({ status: 'error', message: 'Room with that ID does not exists' });
  101. }
  102. },
  103. '/stations/search/:query': function (query, cb) {
  104. var params = [
  105. 'part=snippet',
  106. `q=${encodeURIComponent(query)}`,
  107. `key=${config.get('apis.youtube.key')}`,
  108. 'type=video',
  109. 'maxResults=25'
  110. ].join('&');
  111. request(`https://www.googleapis.com/youtube/v3/search?${params}`, function (err, res, body) {
  112. if (err) {
  113. return cb({ status: 'error', message: 'Failed to make request' });
  114. }
  115. else {
  116. try {
  117. return cb({ status: 'success', body: JSON.parse(body) });
  118. }
  119. catch (e) {
  120. return cb({ status: 'error', message: 'Non JSON response' });
  121. }
  122. }
  123. });
  124. }
  125. };