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 8.6KB

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