選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

actions.js 8.4KB

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