Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

moderator.js 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /* global $, $iq, config, connection, UI, messageHandler,
  2. roomName, sessionTerminated, Strophe, Util */
  3. var XMPPEvents = require("../../service/xmpp/XMPPEvents");
  4. /**
  5. * Contains logic responsible for enabling/disabling functionality available
  6. * only to moderator users.
  7. */
  8. var connection = null;
  9. var focusUserJid;
  10. function createExpBackoffTimer(step) {
  11. var count = 1;
  12. return function (reset) {
  13. // Reset call
  14. if (reset) {
  15. count = 1;
  16. return;
  17. }
  18. // Calculate next timeout
  19. var timeout = Math.pow(2, count - 1);
  20. count += 1;
  21. return timeout * step;
  22. };
  23. }
  24. var getNextTimeout = createExpBackoffTimer(1000);
  25. var getNextErrorTimeout = createExpBackoffTimer(1000);
  26. // External authentication stuff
  27. var externalAuthEnabled = false;
  28. // Sip gateway can be enabled by configuring Jigasi host in config.js or
  29. // it will be enabled automatically if focus detects the component through
  30. // service discovery.
  31. var sipGatewayEnabled = config.hosts.call_control !== undefined;
  32. var eventEmitter = null;
  33. var Moderator = {
  34. isModerator: function () {
  35. return connection && connection.emuc.isModerator();
  36. },
  37. isPeerModerator: function (peerJid) {
  38. return connection &&
  39. connection.emuc.getMemberRole(peerJid) === 'moderator';
  40. },
  41. isExternalAuthEnabled: function () {
  42. return externalAuthEnabled;
  43. },
  44. isSipGatewayEnabled: function () {
  45. return sipGatewayEnabled;
  46. },
  47. setConnection: function (con) {
  48. connection = con;
  49. },
  50. init: function (xmpp, emitter) {
  51. this.xmppService = xmpp;
  52. eventEmitter = emitter;
  53. },
  54. onMucLeft: function (jid) {
  55. console.info("Someone left is it focus ? " + jid);
  56. var resource = Strophe.getResourceFromJid(jid);
  57. if (resource === 'focus' && !this.xmppService.sessionTerminated) {
  58. console.info(
  59. "Focus has left the room - leaving conference");
  60. //hangUp();
  61. // We'd rather reload to have everything re-initialized
  62. // FIXME: show some message before reload
  63. location.reload();
  64. }
  65. },
  66. setFocusUserJid: function (focusJid) {
  67. if (!focusUserJid) {
  68. focusUserJid = focusJid;
  69. console.info("Focus jid set to: " + focusUserJid);
  70. }
  71. },
  72. getFocusUserJid: function () {
  73. return focusUserJid;
  74. },
  75. getFocusComponent: function () {
  76. // Get focus component address
  77. var focusComponent = config.hosts.focus;
  78. // If not specified use default: 'focus.domain'
  79. if (!focusComponent) {
  80. focusComponent = 'focus.' + config.hosts.domain;
  81. }
  82. return focusComponent;
  83. },
  84. createConferenceIq: function (roomName) {
  85. // Generate create conference IQ
  86. var elem = $iq({to: Moderator.getFocusComponent(), type: 'set'});
  87. elem.c('conference', {
  88. xmlns: 'http://jitsi.org/protocol/focus',
  89. room: roomName
  90. });
  91. if (config.hosts.bridge !== undefined) {
  92. elem.c(
  93. 'property',
  94. { name: 'bridge', value: config.hosts.bridge})
  95. .up();
  96. }
  97. // Tell the focus we have Jigasi configured
  98. if (config.hosts.call_control !== undefined) {
  99. elem.c(
  100. 'property',
  101. { name: 'call_control', value: config.hosts.call_control})
  102. .up();
  103. }
  104. if (config.channelLastN !== undefined) {
  105. elem.c(
  106. 'property',
  107. { name: 'channelLastN', value: config.channelLastN})
  108. .up();
  109. }
  110. if (config.adaptiveLastN !== undefined) {
  111. elem.c(
  112. 'property',
  113. { name: 'adaptiveLastN', value: config.adaptiveLastN})
  114. .up();
  115. }
  116. if (config.adaptiveSimulcast !== undefined) {
  117. elem.c(
  118. 'property',
  119. { name: 'adaptiveSimulcast', value: config.adaptiveSimulcast})
  120. .up();
  121. }
  122. if (config.openSctp !== undefined) {
  123. elem.c(
  124. 'property',
  125. { name: 'openSctp', value: config.openSctp})
  126. .up();
  127. }
  128. if (config.enableFirefoxSupport !== undefined) {
  129. elem.c(
  130. 'property',
  131. { name: 'enableFirefoxHacks',
  132. value: config.enableFirefoxSupport})
  133. .up();
  134. }
  135. elem.up();
  136. return elem;
  137. },
  138. parseConfigOptions: function (resultIq) {
  139. Moderator.setFocusUserJid(
  140. $(resultIq).find('conference').attr('focusjid'));
  141. var extAuthParam
  142. = $(resultIq).find('>conference>property[name=\'externalAuth\']');
  143. if (extAuthParam.length) {
  144. externalAuthEnabled = extAuthParam.attr('value') === 'true';
  145. }
  146. console.info("External authentication enabled: " + externalAuthEnabled);
  147. // Check if focus has auto-detected Jigasi component(this will be also
  148. // included if we have passed our host from the config)
  149. if ($(resultIq).find(
  150. '>conference>property[name=\'sipGatewayEnabled\']').length) {
  151. sipGatewayEnabled = true;
  152. }
  153. console.info("Sip gateway enabled: " + sipGatewayEnabled);
  154. },
  155. // FIXME: we need to show the fact that we're waiting for the focus
  156. // to the user(or that focus is not available)
  157. allocateConferenceFocus: function (roomName, callback) {
  158. // Try to use focus user JID from the config
  159. Moderator.setFocusUserJid(config.focusUserJid);
  160. // Send create conference IQ
  161. var iq = Moderator.createConferenceIq(roomName);
  162. var self = this;
  163. connection.sendIQ(
  164. iq,
  165. function (result) {
  166. if ('true' === $(result).find('conference').attr('ready')) {
  167. // Reset both timers
  168. getNextTimeout(true);
  169. getNextErrorTimeout(true);
  170. // Setup config options
  171. Moderator.parseConfigOptions(result);
  172. // Exec callback
  173. callback();
  174. } else {
  175. var waitMs = getNextTimeout();
  176. console.info("Waiting for the focus... " + waitMs);
  177. // Reset error timeout
  178. getNextErrorTimeout(true);
  179. window.setTimeout(
  180. function () {
  181. Moderator.allocateConferenceFocus(
  182. roomName, callback);
  183. }, waitMs);
  184. }
  185. },
  186. function (error) {
  187. // Not authorized to create new room
  188. if ($(error).find('>error>not-authorized').length) {
  189. console.warn("Unauthorized to start the conference");
  190. var toDomain
  191. = Strophe.getDomainFromJid(error.getAttribute('to'));
  192. if (toDomain === config.hosts.anonymousdomain) {
  193. // we are connected with anonymous domain and
  194. // only non anonymous users can create rooms
  195. // we must authorize the user
  196. self.xmppService.promptLogin();
  197. } else {
  198. eventEmitter.emit(XMPPEvents.AUTHENTICATION_REQUIRED, // External authentication mode
  199. function () {
  200. Moderator.allocateConferenceFocus(
  201. roomName, callback);
  202. });
  203. }
  204. return;
  205. }
  206. var waitMs = getNextErrorTimeout();
  207. console.error("Focus error, retry after " + waitMs, error);
  208. // Show message
  209. APP.UI.messageHandler.notify( null, "notify.focus",
  210. 'Conference focus', 'disconnected',"notify.focusFail",
  211. Moderator.getFocusComponent() +
  212. ' not available - retry in ' +
  213. (waitMs / 1000) + ' sec',
  214. {component: Moderator.getFocusComponent(),
  215. ms: (waitMs / 1000)});
  216. // Reset response timeout
  217. getNextTimeout(true);
  218. window.setTimeout(
  219. function () {
  220. Moderator.allocateConferenceFocus(roomName, callback);
  221. }, waitMs);
  222. }
  223. );
  224. },
  225. getAuthUrl: function (roomName, urlCallback) {
  226. var iq = $iq({to: Moderator.getFocusComponent(), type: 'get'});
  227. iq.c('auth-url', {
  228. xmlns: 'http://jitsi.org/protocol/focus',
  229. room: roomName
  230. });
  231. connection.sendIQ(
  232. iq,
  233. function (result) {
  234. var url = $(result).find('auth-url').attr('url');
  235. if (url) {
  236. console.info("Got auth url: " + url);
  237. urlCallback(url);
  238. } else {
  239. console.error(
  240. "Failed to get auth url fro mthe focus", result);
  241. }
  242. },
  243. function (error) {
  244. console.error("Get auth url error", error);
  245. }
  246. );
  247. }
  248. };
  249. module.exports = Moderator;