Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

moderator.js 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /* global $, $iq, APP, config, messageHandler,
  2. roomName, sessionTerminated, Strophe, Util */
  3. var XMPPEvents = require("../../service/xmpp/XMPPEvents");
  4. var Settings = require("../settings/Settings");
  5. var AuthenticationEvents
  6. = require("../../service/authentication/AuthenticationEvents");
  7. /**
  8. * Contains logic responsible for enabling/disabling functionality available
  9. * only to moderator users.
  10. */
  11. var connection = null;
  12. var focusUserJid;
  13. function createExpBackoffTimer(step) {
  14. var count = 1;
  15. return function (reset) {
  16. // Reset call
  17. if (reset) {
  18. count = 1;
  19. return;
  20. }
  21. // Calculate next timeout
  22. var timeout = Math.pow(2, count - 1);
  23. count += 1;
  24. return timeout * step;
  25. };
  26. }
  27. var getNextTimeout = createExpBackoffTimer(1000);
  28. var getNextErrorTimeout = createExpBackoffTimer(1000);
  29. // External authentication stuff
  30. var externalAuthEnabled = false;
  31. // Sip gateway can be enabled by configuring Jigasi host in config.js or
  32. // it will be enabled automatically if focus detects the component through
  33. // service discovery.
  34. var sipGatewayEnabled = null;
  35. var eventEmitter = null;
  36. var Moderator = {
  37. isModerator: function () {
  38. return connection && connection.emuc.isModerator();
  39. },
  40. isPeerModerator: function (peerJid) {
  41. return connection &&
  42. connection.emuc.getMemberRole(peerJid) === 'moderator';
  43. },
  44. isExternalAuthEnabled: function () {
  45. return externalAuthEnabled;
  46. },
  47. isSipGatewayEnabled: function () {
  48. return sipGatewayEnabled;
  49. },
  50. setConnection: function (con) {
  51. connection = con;
  52. },
  53. init: function (xmpp, emitter) {
  54. this.xmppService = xmpp;
  55. sipGatewayEnabled = this.xmppService.options.hosts.call_control !== undefined;
  56. eventEmitter = emitter;
  57. // Message listener that talks to POPUP window
  58. function listener(event) {
  59. if (event.data && event.data.sessionId) {
  60. if (event.origin !== window.location.origin) {
  61. console.warn("Ignoring sessionId from different origin: " +
  62. event.origin);
  63. return;
  64. }
  65. localStorage.setItem('sessionId', event.data.sessionId);
  66. // After popup is closed we will authenticate
  67. }
  68. }
  69. // Register
  70. if (window.addEventListener) {
  71. window.addEventListener("message", listener, false);
  72. } else {
  73. window.attachEvent("onmessage", listener);
  74. }
  75. },
  76. onMucMemberLeft: function (jid) {
  77. console.info("Someone left is it focus ? " + jid);
  78. var resource = Strophe.getResourceFromJid(jid);
  79. if (resource === 'focus' && !this.xmppService.sessionTerminated) {
  80. console.info(
  81. "Focus has left the room - leaving conference");
  82. //hangUp();
  83. // We'd rather reload to have everything re-initialized
  84. // FIXME: show some message before reload
  85. location.reload();
  86. }
  87. },
  88. setFocusUserJid: function (focusJid) {
  89. if (!focusUserJid) {
  90. focusUserJid = focusJid;
  91. console.info("Focus jid set to: " + focusUserJid);
  92. }
  93. },
  94. getFocusUserJid: function () {
  95. return focusUserJid;
  96. },
  97. getFocusComponent: function () {
  98. // Get focus component address
  99. var focusComponent = this.xmppService.options.hosts.focus;
  100. // If not specified use default: 'focus.domain'
  101. if (!focusComponent) {
  102. focusComponent = 'focus.' + this.xmppService.options.hosts.domain;
  103. }
  104. return focusComponent;
  105. },
  106. createConferenceIq: function (roomName) {
  107. // Generate create conference IQ
  108. var elem = $iq({to: Moderator.getFocusComponent(), type: 'set'});
  109. // Session Id used for authentication
  110. var sessionId = localStorage.getItem('sessionId');
  111. var machineUID = Settings.getSettings().uid;
  112. console.info(
  113. "Session ID: " + sessionId + " machine UID: " + machineUID);
  114. elem.c('conference', {
  115. xmlns: 'http://jitsi.org/protocol/focus',
  116. room: roomName,
  117. 'machine-uid': machineUID
  118. });
  119. if (sessionId) {
  120. elem.attrs({ 'session-id': sessionId});
  121. }
  122. if (this.xmppService.options.hosts.bridge !== undefined) {
  123. elem.c(
  124. 'property',
  125. { name: 'bridge', value: this.xmppService.options.hosts.bridge})
  126. .up();
  127. }
  128. // Tell the focus we have Jigasi configured
  129. if (this.xmppService.options.hosts.call_control !== undefined) {
  130. elem.c(
  131. 'property',
  132. { name: 'call_control', value: this.xmppService.options.hosts.call_control})
  133. .up();
  134. }
  135. if (this.xmppService.options.channelLastN !== undefined) {
  136. elem.c(
  137. 'property',
  138. { name: 'channelLastN', value: this.xmppService.options.channelLastN})
  139. .up();
  140. }
  141. if (this.xmppService.options.adaptiveLastN !== undefined) {
  142. elem.c(
  143. 'property',
  144. { name: 'adaptiveLastN', value: this.xmppService.options.adaptiveLastN})
  145. .up();
  146. }
  147. if (this.xmppService.options.adaptiveSimulcast !== undefined) {
  148. elem.c(
  149. 'property',
  150. { name: 'adaptiveSimulcast', value: this.xmppService.options.adaptiveSimulcast})
  151. .up();
  152. }
  153. if (this.xmppService.options.openSctp !== undefined) {
  154. elem.c(
  155. 'property',
  156. { name: 'openSctp', value: this.xmppService.options.openSctp})
  157. .up();
  158. }
  159. if(this.xmppService.options.startAudioMuted !== undefined)
  160. {
  161. elem.c(
  162. 'property',
  163. { name: 'startAudioMuted', value: this.xmppService.options.startAudioMuted})
  164. .up();
  165. }
  166. if(this.xmppService.options.startVideoMuted !== undefined)
  167. {
  168. elem.c(
  169. 'property',
  170. { name: 'startVideoMuted', value: this.xmppService.options.startVideoMuted})
  171. .up();
  172. }
  173. elem.c(
  174. 'property',
  175. { name: 'simulcastMode', value: 'rewriting'})
  176. .up();
  177. elem.up();
  178. return elem;
  179. },
  180. parseSessionId: function (resultIq) {
  181. var sessionId = $(resultIq).find('conference').attr('session-id');
  182. if (sessionId) {
  183. console.info('Received sessionId: ' + sessionId);
  184. localStorage.setItem('sessionId', sessionId);
  185. }
  186. },
  187. parseConfigOptions: function (resultIq) {
  188. Moderator.setFocusUserJid(
  189. $(resultIq).find('conference').attr('focusjid'));
  190. var authenticationEnabled
  191. = $(resultIq).find(
  192. '>conference>property' +
  193. '[name=\'authentication\'][value=\'true\']').length > 0;
  194. console.info("Authentication enabled: " + authenticationEnabled);
  195. externalAuthEnabled = $(resultIq).find(
  196. '>conference>property' +
  197. '[name=\'externalAuth\'][value=\'true\']').length > 0;
  198. console.info('External authentication enabled: ' + externalAuthEnabled);
  199. if (!externalAuthEnabled) {
  200. // We expect to receive sessionId in 'internal' authentication mode
  201. Moderator.parseSessionId(resultIq);
  202. }
  203. var authIdentity = $(resultIq).find('>conference').attr('identity');
  204. eventEmitter.emit(AuthenticationEvents.IDENTITY_UPDATED,
  205. authenticationEnabled, authIdentity);
  206. // Check if focus has auto-detected Jigasi component(this will be also
  207. // included if we have passed our host from the config)
  208. if ($(resultIq).find(
  209. '>conference>property' +
  210. '[name=\'sipGatewayEnabled\'][value=\'true\']').length) {
  211. sipGatewayEnabled = true;
  212. }
  213. console.info("Sip gateway enabled: " + sipGatewayEnabled);
  214. },
  215. // FIXME: we need to show the fact that we're waiting for the focus
  216. // to the user(or that focus is not available)
  217. allocateConferenceFocus: function (roomName, callback) {
  218. // Try to use focus user JID from the config
  219. Moderator.setFocusUserJid(this.xmppService.options.focusUserJid);
  220. // Send create conference IQ
  221. var iq = Moderator.createConferenceIq(roomName);
  222. var self = this;
  223. connection.sendIQ(
  224. iq,
  225. function (result) {
  226. // Setup config options
  227. Moderator.parseConfigOptions(result);
  228. if ('true' === $(result).find('conference').attr('ready')) {
  229. // Reset both timers
  230. getNextTimeout(true);
  231. getNextErrorTimeout(true);
  232. // Exec callback
  233. callback();
  234. } else {
  235. var waitMs = getNextTimeout();
  236. console.info("Waiting for the focus... " + waitMs);
  237. // Reset error timeout
  238. getNextErrorTimeout(true);
  239. window.setTimeout(
  240. function () {
  241. Moderator.allocateConferenceFocus(
  242. roomName, callback);
  243. }, waitMs);
  244. }
  245. },
  246. function (error) {
  247. // Invalid session ? remove and try again
  248. // without session ID to get a new one
  249. var invalidSession
  250. = $(error).find('>error>session-invalid').length;
  251. if (invalidSession) {
  252. console.info("Session expired! - removing");
  253. localStorage.removeItem("sessionId");
  254. }
  255. if ($(error).find('>error>graceful-shutdown').length) {
  256. eventEmitter.emit(XMPPEvents.GRACEFUL_SHUTDOWN);
  257. return;
  258. }
  259. // Check for error returned by the reservation system
  260. var reservationErr = $(error).find('>error>reservation-error');
  261. if (reservationErr.length) {
  262. // Trigger error event
  263. var errorCode = reservationErr.attr('error-code');
  264. var errorMsg;
  265. if ($(error).find('>error>text')) {
  266. errorMsg = $(error).find('>error>text').text();
  267. }
  268. eventEmitter.emit(
  269. XMPPEvents.RESERVATION_ERROR, errorCode, errorMsg);
  270. return;
  271. }
  272. // Not authorized to create new room
  273. if ($(error).find('>error>not-authorized').length) {
  274. console.warn("Unauthorized to start the conference", error);
  275. var toDomain
  276. = Strophe.getDomainFromJid(error.getAttribute('to'));
  277. if (toDomain !== this.xmppService.options.hosts.anonymousdomain) {
  278. // FIXME: "is external" should come either from
  279. // the focus or config.js
  280. externalAuthEnabled = true;
  281. }
  282. eventEmitter.emit(
  283. XMPPEvents.AUTHENTICATION_REQUIRED,
  284. function () {
  285. Moderator.allocateConferenceFocus(
  286. roomName, callback);
  287. });
  288. return;
  289. }
  290. var waitMs = getNextErrorTimeout();
  291. console.error("Focus error, retry after " + waitMs, error);
  292. // Show message
  293. var focusComponent = Moderator.getFocusComponent();
  294. var retrySec = waitMs / 1000;
  295. // FIXME: message is duplicated ?
  296. // Do not show in case of session invalid
  297. // which means just a retry
  298. if (!invalidSession) {
  299. eventEmitter.emit(XMPPEvents.FOCUS_DISCONNECTED,
  300. focusComponent, retrySec);
  301. }
  302. // Reset response timeout
  303. getNextTimeout(true);
  304. window.setTimeout(
  305. function () {
  306. Moderator.allocateConferenceFocus(roomName, callback);
  307. }, waitMs);
  308. }
  309. );
  310. },
  311. getLoginUrl: function (roomName, urlCallback) {
  312. var iq = $iq({to: Moderator.getFocusComponent(), type: 'get'});
  313. iq.c('login-url', {
  314. xmlns: 'http://jitsi.org/protocol/focus',
  315. room: roomName,
  316. 'machine-uid': Settings.getSettings().uid
  317. });
  318. connection.sendIQ(
  319. iq,
  320. function (result) {
  321. var url = $(result).find('login-url').attr('url');
  322. url = url = decodeURIComponent(url);
  323. if (url) {
  324. console.info("Got auth url: " + url);
  325. urlCallback(url);
  326. } else {
  327. console.error(
  328. "Failed to get auth url from the focus", result);
  329. }
  330. },
  331. function (error) {
  332. console.error("Get auth url error", error);
  333. }
  334. );
  335. },
  336. getPopupLoginUrl: function (roomName, urlCallback) {
  337. var iq = $iq({to: Moderator.getFocusComponent(), type: 'get'});
  338. iq.c('login-url', {
  339. xmlns: 'http://jitsi.org/protocol/focus',
  340. room: roomName,
  341. 'machine-uid': Settings.getSettings().uid,
  342. popup: true
  343. });
  344. connection.sendIQ(
  345. iq,
  346. function (result) {
  347. var url = $(result).find('login-url').attr('url');
  348. url = url = decodeURIComponent(url);
  349. if (url) {
  350. console.info("Got POPUP auth url: " + url);
  351. urlCallback(url);
  352. } else {
  353. console.error(
  354. "Failed to get POPUP auth url from the focus", result);
  355. }
  356. },
  357. function (error) {
  358. console.error('Get POPUP auth url error', error);
  359. }
  360. );
  361. },
  362. logout: function (callback) {
  363. var iq = $iq({to: Moderator.getFocusComponent(), type: 'set'});
  364. var sessionId = localStorage.getItem('sessionId');
  365. if (!sessionId) {
  366. callback();
  367. return;
  368. }
  369. iq.c('logout', {
  370. xmlns: 'http://jitsi.org/protocol/focus',
  371. 'session-id': sessionId
  372. });
  373. connection.sendIQ(
  374. iq,
  375. function (result) {
  376. var logoutUrl = $(result).find('logout').attr('logout-url');
  377. if (logoutUrl) {
  378. logoutUrl = decodeURIComponent(logoutUrl);
  379. }
  380. console.info("Log out OK, url: " + logoutUrl, result);
  381. localStorage.removeItem('sessionId');
  382. callback(logoutUrl);
  383. },
  384. function (error) {
  385. console.error("Logout error", error);
  386. }
  387. );
  388. }
  389. };
  390. module.exports = Moderator;