coreHandler.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 () {
  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. r.table('rooms').run(rc, (err, cursor) => {
  51. if (err) {
  52. return cb({ status: 'failure', message: 'Error while fetching the rooms' });
  53. }
  54. else {
  55. cursor.toArray((err, result) => {
  56. if (err) {
  57. return cb({ status: 'failure', message: 'Error while fetching the rooms' });
  58. }
  59. else {
  60. return cb(result);
  61. }
  62. });
  63. }
  64. });
  65. },
  66. room: function (id, cb) {
  67. if (socket.custom.user == null) {
  68. return cb({ status: 'error', message: "You can't join a room until you've logged in" });
  69. }
  70. r.table('rooms').get(id).run(rc, (err, result) => {
  71. if (err) {
  72. return cb({ status: 'error', message: 'Room with that id does not exist' });
  73. }
  74. else {
  75. socket.custom.roomId = id;
  76. var userInfo = {
  77. username: socket.custom.user.username
  78. };
  79. var otherUsersInfo = [];
  80. // tell all the users in this room that someone is joining it
  81. io.sockets.clients().forEach((otherSocket) => {
  82. if (otherSocket != socket && otherSocket.custom.roomId == id) {
  83. otherUsersInfo.push({ username: otherSocket.custom.user.username });
  84. otherSocket.emit('room', { status: 'joining', user: userInfo });
  85. }
  86. });
  87. return cb({
  88. status: 'joined',
  89. data: {
  90. room: result,
  91. users: otherUsersInfo
  92. }
  93. });
  94. }
  95. });
  96. },
  97. search: function (query, cb) {
  98. request('https://www.googleapis.com/youtube/v3/search?' + [
  99. 'part=snippet', `q=${encodeURIComponent(query)}`, `key=${config.get('apis.youtube.key')}`, 'type=video', 'maxResults=25'
  100. ].join('&'), (err, res, body) => {
  101. if (err) {
  102. socket.emit('search', { status: 'error', message: 'Failed to make request' });
  103. }
  104. else {
  105. try {
  106. socket.emit('search', { status: 'success', body: JSON.parse(body) });
  107. }
  108. catch (e) {
  109. socket.emit('search', { status: 'error', message: 'Non JSON response' });
  110. }
  111. }
  112. });
  113. }
  114. };