You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

moderator.js 9.1KB

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