您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

moderator.js 8.8KB

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