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

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