socketManager.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. module.exports = {
  14. setup: (io, rc) => {
  15. r.table('comments')
  16. },
  17. handle: (socket, io, rc) => {
  18. socket.custom = {};
  19. socket.on('disconnect', () => {
  20. });
  21. socket.on('login', (user) => {
  22. if (!user.username || !user.password) {
  23. socket.emit('login', { status: 'error', message: 'Invalid login request' });
  24. return;
  25. }
  26. r.table('users').filter({
  27. username: user.username, password: crypto.createHash('md5').update(user.password).digest("hex")
  28. }).run(rc, (err, cursor) => {
  29. if (err) {
  30. socket.emit('login', { status: 'failure', message: 'Error while fetching the user' });
  31. }
  32. else {
  33. cursor.toArray((err, result) => {
  34. if (err) {
  35. socket.emit('login', { status: 'failure', message: 'Error while fetching the user' });
  36. }
  37. else {
  38. socket.emit('login', { status: 'success', user: result });
  39. }
  40. });
  41. }
  42. });
  43. });
  44. socket.on('register', (user) => {
  45. console.log(user);
  46. if (!user.email || !user.username || !user.password) {
  47. socket.emit('register', { status: 'error', message: 'Invalid register request' });
  48. return;
  49. }
  50. });
  51. socket.on('rooms', () => {
  52. r.table('rooms').run(rc, (err, cursor) => {
  53. if (err) {
  54. socket.emit('rooms', { status: 'failure', message: 'Error while fetching the rooms' });
  55. }
  56. else {
  57. cursor.toArray((err, result) => {
  58. if (err) {
  59. socket.emit('rooms', { status: 'failure', message: 'Error while fetching the rooms' });
  60. }
  61. else {
  62. socket.emit('rooms', result);
  63. }
  64. });
  65. }
  66. });
  67. });
  68. socket.on('room', (id) => {
  69. if (socket.custom.user == null) {
  70. socket.emit('room', { status: 'error', message: "You can't join a room until you've logged in" });
  71. return;
  72. }
  73. r.table('rooms').get(id).run(rc, (err, result) => {
  74. if (err) {
  75. socket.emit('room', { status: 'error', message: 'Room with that id does not exist' });
  76. }
  77. else {
  78. socket.custom.roomId = id;
  79. var userInfo = {
  80. username: socket.custom.user.username
  81. };
  82. var otherUsersInfo = [];
  83. // tell all the users in this room that someone is joining it
  84. io.sockets.clients().forEach((otherSocket) => {
  85. if (otherSocket != socket && otherSocket.custom.roomId == id) {
  86. otherUsersInfo.push({ username: otherSocket.custom.user.username });
  87. otherSocket.emit('room', { status: 'joining', user: userInfo });
  88. }
  89. });
  90. socket.emit('room', { status: 'joined', data: {
  91. room: result, users: otherUsersInfo
  92. }});
  93. }
  94. });
  95. });
  96. socket.on('search', (query) => {
  97. request('https://www.googleapis.com/youtube/v3/search?' + [
  98. 'part=snippet', `q=${encodeURIComponent(query)}`, `key=${config.get('apis.youtube.key')}`, 'type=video', 'maxResults=25'
  99. ].join('&'), (err, res, body) => {
  100. if (err) {
  101. socket.emit('search', { status: 'error', message: 'Failed to make request' });
  102. }
  103. else {
  104. try {
  105. socket.emit('search', { status: 'success', body: JSON.parse(body) });
  106. }
  107. catch (e) {
  108. socket.emit('search', { status: 'error', message: 'Non JSON response' });
  109. }
  110. }
  111. });
  112. });
  113. socket.emit('ready');
  114. }
  115. };