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

moderator.js 8.9KB

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