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.0KB

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