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

moderator.js 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. my.isModerator = function () {
  14. return connection && connection.emuc.isModerator();
  15. };
  16. my.isPeerModerator = function (peerJid) {
  17. return connection && connection.emuc.getMemberRole(peerJid) === 'moderator';
  18. };
  19. my.isExternalAuthEnabled = function () {
  20. return externalAuthEnabled;
  21. };
  22. my.init = function () {
  23. Moderator.onLocalRoleChange = function (from, member, pres) {
  24. UI.onModeratorStatusChanged(Moderator.isModerator());
  25. };
  26. };
  27. my.onMucLeft = function (jid) {
  28. console.info("Someone left is it focus ? " + jid);
  29. var resource = Strophe.getResourceFromJid(jid);
  30. if (resource === 'focus' && !sessionTerminated) {
  31. console.info(
  32. "Focus has left the room - leaving conference");
  33. //hangUp();
  34. // We'd rather reload to have everything re-initialized
  35. // FIXME: show some message before reload
  36. location.reload();
  37. }
  38. }
  39. my.setFocusUserJid = function (focusJid) {
  40. if (!focusUserJid) {
  41. focusUserJid = focusJid;
  42. console.info("Focus jid set to: " + focusUserJid);
  43. }
  44. };
  45. my.getFocusUserJid = function () {
  46. return focusUserJid;
  47. };
  48. my.getFocusComponent = function () {
  49. // Get focus component address
  50. var focusComponent = config.hosts.focus;
  51. // If not specified use default: 'focus.domain'
  52. if (!focusComponent) {
  53. focusComponent = 'focus.' + config.hosts.domain;
  54. }
  55. return focusComponent;
  56. };
  57. my.createConferenceIq = function () {
  58. // Generate create conference IQ
  59. var elem = $iq({to: Moderator.getFocusComponent(), type: 'set'});
  60. elem.c('conference', {
  61. xmlns: 'http://jitsi.org/protocol/focus',
  62. room: roomName
  63. });
  64. if (config.hosts.bridge !== undefined)
  65. {
  66. elem.c(
  67. 'property',
  68. { name: 'bridge', value: config.hosts.bridge})
  69. .up();
  70. }
  71. if (config.channelLastN !== undefined)
  72. {
  73. elem.c(
  74. 'property',
  75. { name: 'channelLastN', value: config.channelLastN})
  76. .up();
  77. }
  78. if (config.adaptiveLastN !== undefined)
  79. {
  80. elem.c(
  81. 'property',
  82. { name: 'adaptiveLastN', value: config.adaptiveLastN})
  83. .up();
  84. }
  85. if (config.adaptiveSimulcast !== undefined)
  86. {
  87. elem.c(
  88. 'property',
  89. { name: 'adaptiveSimulcast', value: config.adaptiveSimulcast})
  90. .up();
  91. }
  92. if (config.openSctp !== undefined)
  93. {
  94. elem.c(
  95. 'property',
  96. { name: 'openSctp', value: config.openSctp})
  97. .up();
  98. }
  99. if (config.enableFirefoxSupport !== undefined)
  100. {
  101. elem.c(
  102. 'property',
  103. { name: 'enableFirefoxHacks', value: config.enableFirefoxSupport})
  104. .up();
  105. }
  106. elem.up();
  107. return elem;
  108. };
  109. my.parseConfigOptions = function (resultIq) {
  110. Moderator.setFocusUserJid(
  111. $(resultIq).find('conference').attr('focusjid'));
  112. var extAuthParam
  113. = $(resultIq).find('>conference>property[name=\'externalAuth\']');
  114. if (extAuthParam.length) {
  115. externalAuthEnabled = extAuthParam.attr('value') === 'true';
  116. }
  117. console.info("External authentication enabled: " + externalAuthEnabled);
  118. };
  119. // FIXME: we need to show the fact that we're waiting for the focus
  120. // to the user(or that focus is not available)
  121. my.allocateConferenceFocus = function (roomName, callback) {
  122. // Try to use focus user JID from the config
  123. Moderator.setFocusUserJid(config.focusUserJid);
  124. // Send create conference IQ
  125. var iq = Moderator.createConferenceIq();
  126. connection.sendIQ(
  127. iq,
  128. function (result) {
  129. if ('true' === $(result).find('conference').attr('ready')) {
  130. // Reset both timers
  131. getNextTimeout(true);
  132. getNextErrorTimeout(true);
  133. // Setup config options
  134. Moderator.parseConfigOptions(result);
  135. // Exec callback
  136. callback();
  137. } else {
  138. var waitMs = getNextTimeout();
  139. console.info("Waiting for the focus... " + waitMs);
  140. // Reset error timeout
  141. getNextErrorTimeout(true);
  142. window.setTimeout(
  143. function () {
  144. Moderator.allocateConferenceFocus(
  145. roomName, callback);
  146. }, waitMs);
  147. }
  148. },
  149. function (error) {
  150. // Not authorized to create new room
  151. if ($(error).find('>error>not-authorized').length) {
  152. console.warn("Unauthorized to start the conference");
  153. UI.onAuthenticationRequired();
  154. return;
  155. }
  156. var waitMs = getNextErrorTimeout();
  157. console.error("Focus error, retry after " + waitMs, error);
  158. // Show message
  159. UI.messageHandler.notify(
  160. 'Conference focus', 'disconnected',
  161. Moderator.getFocusComponent() +
  162. ' not available - retry in ' + (waitMs / 1000) + ' sec');
  163. // Reset response timeout
  164. getNextTimeout(true);
  165. window.setTimeout(
  166. function () {
  167. Moderator.allocateConferenceFocus(roomName, callback);
  168. }, waitMs);
  169. }
  170. );
  171. };
  172. my.getAuthUrl = function (urlCallback) {
  173. var iq = $iq({to: Moderator.getFocusComponent(), type: 'get'});
  174. iq.c('auth-url', {
  175. xmlns: 'http://jitsi.org/protocol/focus',
  176. room: roomName
  177. });
  178. connection.sendIQ(
  179. iq,
  180. function (result) {
  181. var url = $(result).find('auth-url').attr('url');
  182. if (url) {
  183. console.info("Got auth url: " + url);
  184. urlCallback(url);
  185. } else {
  186. console.error(
  187. "Failed to get auth url fro mthe focus", result);
  188. }
  189. },
  190. function (error) {
  191. console.error("Get auth url error", error);
  192. }
  193. );
  194. };
  195. return my;
  196. }(Moderator || {}));