coreHandler.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. 'use strict';
  2. // nodejs modules
  3. const path = require('path'),
  4. fs = require('fs'),
  5. os = require('os');
  6. // npm modules
  7. const config = require('config'),
  8. request = require('request'),
  9. waterfall = require('async/waterfall').
  10. r = require('rethinkdb');
  11. // custom modules
  12. const utils = require('./utils');
  13. var dbConnection = null;
  14. module.exports = {
  15. setup: function (dbConn) {
  16. dbConnection = dbConn;
  17. },
  18. disconnect: function () {//TODO Find out why we even need this.
  19. },
  20. login: function (user, cb) {
  21. if (!user.username || !user.password) {
  22. return cb({ status: 'error', message: 'Invalid login request' });
  23. }
  24. r.table('users').filter({
  25. username: user.username,
  26. password: crypto.createHash('md5').update(user.password).digest("hex")
  27. }).run(rc, (err, cursor) => {
  28. if (err) {
  29. return cb({ status: 'failure', message: 'Error while fetching the user' });
  30. }
  31. else {
  32. cursor.toArray((err, result) => {
  33. if (err) {
  34. return cb({ status: 'failure', message: 'Error while fetching the user' });
  35. }
  36. else {
  37. return cb({ status: 'success', user: result });
  38. }
  39. });
  40. }
  41. });
  42. },
  43. register: function (user, cb) {
  44. if (!user.email || !user.username || !user.password) {
  45. return cb({ status: 'error', message: 'Invalid register request' });
  46. }
  47. // TODO: Implement register
  48. },
  49. rooms: function (cb) {
  50. var _rooms = stations.map(function(result) {
  51. return {
  52. id: result.getId(),
  53. displayName: result.getDisplayName(),
  54. description: result.getDescription(),
  55. users: result.getUsers()
  56. }
  57. });
  58. cb(_rooms);
  59. },
  60. joinRoom: function (id, cb) {//TODO Think of a better name than joinRoom
  61. var room = getStation(id);
  62. socket.custom.roomId = id;
  63. var userInfo = {
  64. username: socket.custom.user.username
  65. };
  66. // tell all the users in this room that someone is joining it
  67. io.sockets.clients().forEach(function (otherSocket) {
  68. if (otherSocket != socket && otherSocket.custom.roomId === id) {
  69. otherSocket.emit('roomUserJoin', { user: userInfo });
  70. }
  71. });
  72. //TODO Add errors.
  73. return cb({
  74. status: 'joined',
  75. data: {
  76. room: room
  77. }
  78. });
  79. },
  80. search: function (query, cb) {//TODO Replace search with a better name.
  81. request('https://www.googleapis.com/youtube/v3/search?' + [
  82. 'part=snippet', `q=${encodeURIComponent(query)}`, `key=${config.get('apis.youtube.key')}`, 'type=video', 'maxResults=25'
  83. ].join('&'), (err, res, body) => {
  84. if (err) {
  85. socket.emit('search', { status: 'error', message: 'Failed to make request' });
  86. }
  87. else {
  88. try {
  89. socket.emit('search', { status: 'success', body: JSON.parse(body) });
  90. }
  91. catch (e) {
  92. socket.emit('search', { status: 'error', message: 'Non JSON response' });
  93. }
  94. }
  95. });
  96. }
  97. };