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.

actions.js 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import { setRoom } from '../base/conference';
  2. import { loadConfigError, setConfig } from '../base/config';
  3. import { setLocationURL } from '../base/connection';
  4. import { loadConfig } from '../base/lib-jitsi-meet';
  5. import { parseURIString } from '../base/util';
  6. import { APP_WILL_MOUNT, APP_WILL_UNMOUNT } from './actionTypes';
  7. declare var APP: Object;
  8. /**
  9. * Triggers an in-app navigation to a specific route. Allows navigation to be
  10. * abstracted between the mobile/React Native and Web/React applications.
  11. *
  12. * @param {(string|undefined)} uri - The URI to which to navigate. It may be a
  13. * full URL with an HTTP(S) scheme, a full or partial URI with the app-specific
  14. * scheme, or a mere room name.
  15. * @returns {Function}
  16. */
  17. export function appNavigate(uri: ?string) {
  18. return (dispatch: Dispatch<*>, getState: Function) =>
  19. _appNavigateToOptionalLocation(dispatch, getState, parseURIString(uri));
  20. }
  21. /**
  22. * Triggers an in-app navigation to a specific location URI.
  23. *
  24. * @param {Dispatch} dispatch - The redux {@code dispatch} function.
  25. * @param {Function} getState - The redux function that gets/retrieves the redux
  26. * state.
  27. * @param {Object} newLocation - The location URI to navigate to. The value
  28. * cannot be undefined and is assumed to have all properties such as
  29. * {@code host}, {@code contextRoot}, and {@code room} defined. Depending on the
  30. * property, it may have a value equal to {@code undefined} and that may be
  31. * acceptable.
  32. * @private
  33. * @returns {void}
  34. */
  35. function _appNavigateToMandatoryLocation(
  36. dispatch: Dispatch<*>, getState: Function,
  37. newLocation: Object) {
  38. const { room } = newLocation;
  39. return (
  40. _loadConfig(newLocation)
  41. .then(
  42. config => loadConfigSettled(/* error */ undefined, config),
  43. error => loadConfigSettled(error, /* config */ undefined))
  44. .then(() => dispatch(setRoom(room))));
  45. /**
  46. * Notifies that an attempt to load a configuration has completed. Due to
  47. * the asynchronous nature of the loading, the specified {@code config} may
  48. * or may not be required by the time the notification arrives.
  49. *
  50. * @param {string|undefined} error - If the loading has failed, the error
  51. * detailing the cause of the failure.
  52. * @param {Object|undefined} config - If the loading has succeeded, the
  53. * loaded configuration.
  54. * @returns {void}
  55. */
  56. function loadConfigSettled(error, config) {
  57. // FIXME Due to the asynchronous nature of the loading, the specified
  58. // config may or may not be required by the time the notification
  59. // arrives.
  60. if (error) {
  61. // XXX The failure could be, for example, because of a
  62. // certificate-related error. In which case the connection will
  63. // fail later in Strophe anyway.
  64. dispatch(loadConfigError(error, newLocation));
  65. // Cannot go to a room if its configuration failed to load.
  66. if (room) {
  67. dispatch(appNavigate(undefined));
  68. throw error;
  69. }
  70. }
  71. return (
  72. dispatch(setLocationURL(new URL(newLocation.toString())))
  73. .then(() => dispatch(setConfig(config))));
  74. }
  75. }
  76. /**
  77. * Triggers an in-app navigation to a specific or undefined location (URI).
  78. *
  79. * @param {Dispatch} dispatch - The redux {@code dispatch} function.
  80. * @param {Function} getState - The redux function that gets/retrieves the redux
  81. * state.
  82. * @param {Object} location - The location (URI) to navigate to. The value may
  83. * be undefined.
  84. * @private
  85. * @returns {void}
  86. */
  87. function _appNavigateToOptionalLocation(
  88. dispatch: Dispatch<*>, getState: Function,
  89. location: Object) {
  90. // If the specified location (URI) does not identify a host, use the app's
  91. // default.
  92. if (!location || !location.host) {
  93. const defaultLocation
  94. = parseURIString(getState()['features/app'].app._getDefaultURL());
  95. if (location) {
  96. location.host = defaultLocation.host;
  97. // FIXME Turn location's host, hostname, and port properties into
  98. // setters in order to reduce the risks of inconsistent state.
  99. location.hostname = defaultLocation.hostname;
  100. location.port = defaultLocation.port;
  101. location.protocol = defaultLocation.protocol;
  102. } else {
  103. // eslint-disable-next-line no-param-reassign
  104. location = defaultLocation;
  105. }
  106. }
  107. location.protocol || (location.protocol = 'https:');
  108. return _appNavigateToMandatoryLocation(dispatch, getState, location);
  109. }
  110. /**
  111. * Signals that a specific App will mount (in the terms of React).
  112. *
  113. * @param {App} app - The App which will mount.
  114. * @returns {{
  115. * type: APP_WILL_MOUNT,
  116. * app: App
  117. * }}
  118. */
  119. export function appWillMount(app) {
  120. return (dispatch: Dispatch<*>) => {
  121. dispatch({
  122. type: APP_WILL_MOUNT,
  123. app
  124. });
  125. // TODO There was a redux action creator appInit which I did not like
  126. // because we already had the redux action creator appWillMount and,
  127. // respectively, the redux action APP_WILL_MOUNT. So I set out to remove
  128. // appInit and managed to move everything it was doing but the
  129. // following. Which is not extremely bad because we haven't moved the
  130. // API module into its own feature yet so we're bound to work on that in
  131. // the future.
  132. typeof APP === 'object' && APP.API.init();
  133. };
  134. }
  135. /**
  136. * Signals that a specific App will unmount (in the terms of React).
  137. *
  138. * @param {App} app - The App which will unmount.
  139. * @returns {{
  140. * type: APP_WILL_UNMOUNT,
  141. * app: App
  142. * }}
  143. */
  144. export function appWillUnmount(app) {
  145. return {
  146. type: APP_WILL_UNMOUNT,
  147. app
  148. };
  149. }
  150. /**
  151. * Loads config.js from a specific host.
  152. *
  153. * @param {Object} location - The location URI which specifies the host to load
  154. * the config.js from.
  155. * @private
  156. * @returns {Promise<Object>}
  157. */
  158. function _loadConfig({ contextRoot, host, protocol, room }) {
  159. // XXX As the mobile/React Native app does not employ config on the
  160. // WelcomePage, do not download config.js from the deployment when
  161. // navigating to the WelcomePage - the perceived/visible navigation will be
  162. // faster.
  163. if (!room && typeof APP === 'undefined') {
  164. return Promise.resolve();
  165. }
  166. /* eslint-disable no-param-reassign */
  167. protocol = protocol.toLowerCase();
  168. // The React Native app supports an app-specific scheme which is sure to not
  169. // be supported by fetch (or whatever loadConfig utilizes).
  170. protocol !== 'http:' && protocol !== 'https:' && (protocol = 'https:');
  171. // TDOO userinfo
  172. const baseURL = `${protocol}//${host}${contextRoot || '/'}`;
  173. let url = `${baseURL}config.js`;
  174. // XXX In order to support multiple shards, tell the room to the deployment.
  175. room && (url += `?room=${room.toLowerCase()}`);
  176. /* eslint-enable no-param-reassign */
  177. const key = `config.js/${baseURL}`;
  178. return loadConfig(url).then(
  179. /* onFulfilled */ config => {
  180. // Try to store the configuration in localStorage. If the deployment
  181. // specified 'getroom' as a function, for example, it does not make
  182. // sense to and it will not be stored.
  183. try {
  184. if (typeof window.config === 'undefined'
  185. || window.config !== config) {
  186. window.localStorage.setItem(key, JSON.stringify(config));
  187. }
  188. } catch (e) {
  189. // Ignore the error because the caching is optional.
  190. }
  191. return config;
  192. },
  193. /* onRejected */ error => {
  194. // XXX The (down)loading of config failed. Try to use the last
  195. // successfully fetched for that deployment. It may not match the
  196. // shard.
  197. let storage;
  198. try {
  199. // XXX Even reading the property localStorage of window may
  200. // throw an error (which is user agent-specific behavior).
  201. storage = window.localStorage;
  202. const config = storage.getItem(key);
  203. if (config) {
  204. return JSON.parse(config);
  205. }
  206. } catch (e) {
  207. // Somehow incorrect data ended up in the storage. Clean it up.
  208. storage && storage.removeItem(key);
  209. }
  210. throw error;
  211. });
  212. }