Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

actions.js 8.3KB

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