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 9.0KB

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