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.

app.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /* global $, config, getRoomName, loggingConfig, JitsiMeetJS */
  2. /* application specific logic */
  3. const logger = require("jitsi-meet-logger").getLogger(__filename);
  4. import "babel-polyfill";
  5. import "jquery";
  6. import "jquery-contextmenu";
  7. import "jquery-ui";
  8. import "strophe";
  9. import "strophe-disco";
  10. import "strophe-caps";
  11. import "jQuery-Impromptu";
  12. import "autosize";
  13. import 'aui';
  14. import 'aui-experimental';
  15. import 'aui-css';
  16. import 'aui-experimental-css';
  17. window.toastr = require("toastr");
  18. const Logger = require("jitsi-meet-logger");
  19. const LogCollector = Logger.LogCollector;
  20. import JitsiMeetLogStorage from "./modules/util/JitsiMeetLogStorage";
  21. import URLProcessor from "./modules/config/URLProcessor";
  22. import RoomnameGenerator from './modules/util/RoomnameGenerator';
  23. import UI from "./modules/UI/UI";
  24. import settings from "./modules/settings/Settings";
  25. import conference from './conference';
  26. import ConferenceUrl from './modules/URL/ConferenceUrl';
  27. import API from './modules/API/API';
  28. import UIEvents from './service/UI/UIEvents';
  29. import getTokenData from "./modules/tokendata/TokenData";
  30. import translation from "./modules/translation/translation";
  31. const ConferenceEvents = JitsiMeetJS.events.conference;
  32. /**
  33. * Tries to push history state with the following parameters:
  34. * 'VideoChat', `Room: ${roomName}`, URL. If fail, prints the error and returns
  35. * it.
  36. */
  37. function pushHistoryState(roomName, URL) {
  38. try {
  39. window.history.pushState(
  40. 'VideoChat', `Room: ${roomName}`, URL
  41. );
  42. } catch (e) {
  43. logger.warn("Push history state failed with parameters:",
  44. 'VideoChat', `Room: ${roomName}`, URL, e);
  45. return e;
  46. }
  47. return null;
  48. }
  49. /**
  50. * Replaces current history state(replaces the URL displayed by the browser).
  51. * @param {string} newUrl the URL string which is to be displayed by the browser
  52. * to the user.
  53. */
  54. function replaceHistoryState (newUrl) {
  55. if (window.history
  56. && typeof window.history.replaceState === 'function') {
  57. window.history.replaceState({}, document.title, newUrl);
  58. }
  59. }
  60. /**
  61. * Builds and returns the room name.
  62. */
  63. function buildRoomName () {
  64. let roomName = getRoomName();
  65. if(!roomName) {
  66. let word = RoomnameGenerator.generateRoomWithoutSeparator();
  67. roomName = word.toLowerCase();
  68. let historyURL = window.location.href + word;
  69. //Trying to push state with current URL + roomName
  70. pushHistoryState(word, historyURL);
  71. }
  72. return roomName;
  73. }
  74. /**
  75. * Adjusts the logging levels.
  76. * @private
  77. */
  78. function configureLoggingLevels () {
  79. // NOTE The library Logger is separated from the app loggers, so the levels
  80. // have to be set in two places
  81. // Set default logging level
  82. const defaultLogLevel
  83. = loggingConfig.defaultLogLevel || JitsiMeetJS.logLevels.TRACE;
  84. Logger.setLogLevel(defaultLogLevel);
  85. JitsiMeetJS.setLogLevel(defaultLogLevel);
  86. // NOTE console was used on purpose here to go around the logging
  87. // and always print the default logging level to the console
  88. console.info("Default logging level set to: " + defaultLogLevel);
  89. // Set log level for each logger
  90. if (loggingConfig) {
  91. Object.keys(loggingConfig).forEach(function(loggerName) {
  92. if ('defaultLogLevel' !== loggerName) {
  93. const level = loggingConfig[loggerName];
  94. Logger.setLogLevelById(level, loggerName);
  95. JitsiMeetJS.setLogLevelById(level, loggerName);
  96. }
  97. });
  98. }
  99. }
  100. const APP = {
  101. // Used by do_external_connect.js if we receive the attach data after
  102. // connect was already executed. status property can be "initialized",
  103. // "ready" or "connecting". We are interested in "ready" status only which
  104. // means that connect was executed but we have to wait for the attach data.
  105. // In status "ready" handler property will be set to a function that will
  106. // finish the connect process when the attach data or error is received.
  107. connect: {
  108. status: "initialized",
  109. handler: null
  110. },
  111. // Used for automated performance tests
  112. connectionTimes: {
  113. "index.loaded": window.indexLoadedTime
  114. },
  115. UI,
  116. settings,
  117. conference,
  118. translation,
  119. /**
  120. * The log collector which captures JS console logs for this app.
  121. * @type {LogCollector}
  122. */
  123. logCollector: null,
  124. /**
  125. * Indicates if the log collector has been started (it will not be started
  126. * if the welcome page is displayed).
  127. */
  128. logCollectorStarted : false,
  129. /**
  130. * After the APP has been initialized provides utility methods for dealing
  131. * with the conference room URL(address).
  132. * @type ConferenceUrl
  133. */
  134. ConferenceUrl : null,
  135. connection: null,
  136. API,
  137. init () {
  138. this.initLogging();
  139. this.keyboardshortcut =
  140. require("./modules/keyboardshortcut/keyboardshortcut");
  141. this.configFetch = require("./modules/config/HttpConfigFetch");
  142. this.tokenData = getTokenData();
  143. },
  144. initLogging () {
  145. // Adjust logging level
  146. configureLoggingLevels();
  147. // Create the LogCollector and register it as the global log transport.
  148. // It is done early to capture as much logs as possible. Captured logs
  149. // will be cached, before the JitsiMeetLogStorage gets ready (statistics
  150. // module is initialized).
  151. if (!this.logCollector && !loggingConfig.disableLogCollector) {
  152. this.logCollector = new LogCollector(new JitsiMeetLogStorage());
  153. Logger.addGlobalTransport(this.logCollector);
  154. JitsiMeetJS.addGlobalLogTransport(this.logCollector);
  155. }
  156. }
  157. };
  158. /**
  159. * If JWT token data it will be used for local user settings
  160. */
  161. function setTokenData() {
  162. let localUser = APP.tokenData.caller;
  163. if(localUser) {
  164. APP.settings.setEmail((localUser.getEmail() || "").trim(), true);
  165. APP.settings.setAvatarUrl((localUser.getAvatarUrl() || "").trim());
  166. APP.settings.setDisplayName((localUser.getName() || "").trim(), true);
  167. }
  168. }
  169. function init() {
  170. setTokenData();
  171. // Initialize the conference URL handler
  172. APP.ConferenceUrl = new ConferenceUrl(window.location);
  173. // Clean up the URL displayed by the browser
  174. replaceHistoryState(APP.ConferenceUrl.getInviteUrl());
  175. const isUIReady = APP.UI.start();
  176. if (isUIReady) {
  177. APP.conference.init({roomName: buildRoomName()}).then(function () {
  178. if (APP.logCollector) {
  179. // Start the LogCollector's periodic "store logs" task only if
  180. // we're in the conference and not on the welcome page. This is
  181. // determined by the value of "isUIReady" const above.
  182. APP.logCollector.start();
  183. APP.logCollectorStarted = true;
  184. // Make an attempt to flush in case a lot of logs have been
  185. // cached, before the collector was started.
  186. APP.logCollector.flush();
  187. // This event listener will flush the logs, before
  188. // the statistics module (CallStats) is stopped.
  189. //
  190. // NOTE The LogCollector is not stopped, because this event can
  191. // be triggered multiple times during single conference
  192. // (whenever statistics module is stopped). That includes
  193. // the case when Jicofo terminates the single person left in the
  194. // room. It will then restart the media session when someone
  195. // eventually join the room which will start the stats again.
  196. APP.conference.addConferenceListener(
  197. ConferenceEvents.BEFORE_STATISTICS_DISPOSED,
  198. () => {
  199. if (APP.logCollector) {
  200. APP.logCollector.flush();
  201. }
  202. }
  203. );
  204. }
  205. APP.UI.initConference();
  206. APP.UI.addListener(UIEvents.LANG_CHANGED, function (language) {
  207. APP.translation.setLanguage(language);
  208. APP.settings.setLanguage(language);
  209. });
  210. APP.keyboardshortcut.init();
  211. }).catch(function (err) {
  212. APP.UI.hideRingOverLay();
  213. APP.API.notifyConferenceLeft(APP.conference.roomName);
  214. logger.error(err);
  215. });
  216. }
  217. }
  218. /**
  219. * If we have an HTTP endpoint for getting config.json configured we're going to
  220. * read it and override properties from config.js and interfaceConfig.js.
  221. * If there is no endpoint we'll just continue with initialization.
  222. * Keep in mind that if the endpoint has been configured and we fail to obtain
  223. * the config for any reason then the conference won't start and error message
  224. * will be displayed to the user.
  225. */
  226. function obtainConfigAndInit() {
  227. let roomName = APP.conference.roomName;
  228. if (config.configLocation) {
  229. APP.configFetch.obtainConfig(
  230. config.configLocation, roomName,
  231. // Get config result callback
  232. function(success, error) {
  233. if (success) {
  234. var now = APP.connectionTimes["configuration.fetched"] =
  235. window.performance.now();
  236. logger.log("(TIME) configuration fetched:\t", now);
  237. init();
  238. } else {
  239. // Show obtain config error,
  240. // pass the error object for report
  241. APP.UI.messageHandler.openReportDialog(
  242. null, "dialog.connectError", error);
  243. }
  244. });
  245. } else {
  246. require("./modules/config/BoshAddressChoice").chooseAddress(
  247. config, roomName);
  248. init();
  249. }
  250. }
  251. $(document).ready(function () {
  252. var now = APP.connectionTimes["document.ready"] = window.performance.now();
  253. logger.log("(TIME) document ready:\t", now);
  254. URLProcessor.setConfigParametersFromUrl();
  255. // TODO The execution of the mobile app starts from react/index.native.js.
  256. // Similarly, the execution of the Web app should start from
  257. // react/index.web.js for the sake of consistency and ease of understanding.
  258. // Temporarily though because we are at the beginning of introducing React
  259. // into the Web app, allow the execution of the Web app to start from app.js
  260. // in order to reduce the complexity of the beginning step.
  261. require('./react');
  262. APP.init();
  263. APP.translation.init(settings.getLanguage());
  264. APP.API.init(APP.tokenData.externalAPISettings);
  265. obtainConfigAndInit();
  266. });
  267. $(window).bind('beforeunload', function () {
  268. // Stop the LogCollector
  269. if (APP.logCollectorStarted) {
  270. APP.logCollector.stop();
  271. APP.logCollectorStarted = false;
  272. }
  273. APP.API.dispose();
  274. });
  275. module.exports = APP;