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.

reducer.js 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /* @flow */
  2. import { SET_ROOM } from '../conference';
  3. import { JitsiConnectionErrors } from '../lib-jitsi-meet';
  4. import { assign, ReducerRegistry } from '../redux';
  5. import { parseURIString } from '../util';
  6. import {
  7. CONNECTION_DISCONNECTED,
  8. CONNECTION_ESTABLISHED,
  9. CONNECTION_FAILED,
  10. CONNECTION_WILL_CONNECT,
  11. SET_LOCATION_URL
  12. } from './actionTypes';
  13. import type { ConnectionFailedError } from './actions.native';
  14. /**
  15. * Reduces the Redux actions of the feature base/connection.
  16. */
  17. ReducerRegistry.register(
  18. 'features/base/connection',
  19. (state: Object = {}, action: Object) => {
  20. switch (action.type) {
  21. case CONNECTION_DISCONNECTED:
  22. return _connectionDisconnected(state, action);
  23. case CONNECTION_ESTABLISHED:
  24. return _connectionEstablished(state, action);
  25. case CONNECTION_FAILED:
  26. return _connectionFailed(state, action);
  27. case CONNECTION_WILL_CONNECT:
  28. return _connectionWillConnect(state, action);
  29. case SET_LOCATION_URL:
  30. return _setLocationURL(state, action);
  31. case SET_ROOM:
  32. return _setRoom(state);
  33. }
  34. return state;
  35. });
  36. /**
  37. * Reduces a specific Redux action CONNECTION_DISCONNECTED of the feature
  38. * base/connection.
  39. *
  40. * @param {Object} state - The Redux state of the feature base/connection.
  41. * @param {Action} action - The Redux action CONNECTION_DISCONNECTED to reduce.
  42. * @private
  43. * @returns {Object} The new state of the feature base/connection after the
  44. * reduction of the specified action.
  45. */
  46. function _connectionDisconnected(
  47. state: Object,
  48. { connection }: { connection: Object }) {
  49. const connection_ = _getCurrentConnection(state);
  50. if (connection_ !== connection) {
  51. return state;
  52. }
  53. return assign(state, {
  54. connecting: undefined,
  55. connection: undefined,
  56. timeEstablished: undefined
  57. });
  58. }
  59. /**
  60. * Reduces a specific Redux action CONNECTION_ESTABLISHED of the feature
  61. * base/connection.
  62. *
  63. * @param {Object} state - The Redux state of the feature base/connection.
  64. * @param {Action} action - The Redux action CONNECTION_ESTABLISHED to reduce.
  65. * @private
  66. * @returns {Object} The new state of the feature base/connection after the
  67. * reduction of the specified action.
  68. */
  69. function _connectionEstablished(
  70. state: Object,
  71. { connection, timeEstablished }: {
  72. connection: Object,
  73. timeEstablished: number
  74. }) {
  75. return assign(state, {
  76. connecting: undefined,
  77. connection,
  78. error: undefined,
  79. passwordRequired: undefined,
  80. timeEstablished
  81. });
  82. }
  83. /**
  84. * Reduces a specific Redux action CONNECTION_FAILED of the feature
  85. * base/connection.
  86. *
  87. * @param {Object} state - The Redux state of the feature base/connection.
  88. * @param {Action} action - The Redux action CONNECTION_FAILED to reduce.
  89. * @private
  90. * @returns {Object} The new state of the feature base/connection after the
  91. * reduction of the specified action.
  92. */
  93. function _connectionFailed(
  94. state: Object,
  95. { connection, error }: {
  96. connection: Object,
  97. error: ConnectionFailedError
  98. }) {
  99. const connection_ = _getCurrentConnection(state);
  100. if (connection_ && connection_ !== connection) {
  101. return state;
  102. }
  103. return assign(state, {
  104. connecting: undefined,
  105. connection: undefined,
  106. error,
  107. passwordRequired:
  108. error.name === JitsiConnectionErrors.PASSWORD_REQUIRED
  109. ? connection : undefined
  110. });
  111. }
  112. /**
  113. * Reduces a specific Redux action CONNECTION_WILL_CONNECT of the feature
  114. * base/connection.
  115. *
  116. * @param {Object} state - The Redux state of the feature base/connection.
  117. * @param {Action} action - The Redux action CONNECTION_WILL_CONNECT to reduce.
  118. * @private
  119. * @returns {Object} The new state of the feature base/connection after the
  120. * reduction of the specified action.
  121. */
  122. function _connectionWillConnect(
  123. state: Object,
  124. { connection }: { connection: Object }) {
  125. return assign(state, {
  126. connecting: connection,
  127. // We don't care if the previous connection has been closed already,
  128. // because it's an async process and there's no guarantee if it'll be
  129. // done before the new one is established.
  130. connection: undefined,
  131. error: undefined,
  132. passwordRequired: undefined,
  133. timeEstablished: undefined
  134. });
  135. }
  136. /**
  137. * Constructs options to be passed to the constructor of {@code JitsiConnection}
  138. * based on a specific location URL.
  139. *
  140. * @param {string} locationURL - The location URL with which the returned
  141. * options are to be constructed.
  142. * @private
  143. * @returns {Object} The options to be passed to the constructor of
  144. * {@code JitsiConnection} based on the location URL.
  145. */
  146. function _constructOptions(locationURL: URL) {
  147. const locationURI = parseURIString(locationURL.href);
  148. // FIXME The HTTPS scheme for the BOSH URL works with meet.jit.si on both
  149. // mobile & Web. It also works with beta.meet.jit.si on Web. Unfortunately,
  150. // it doesn't work with beta.meet.jit.si on mobile. Temporarily, use the
  151. // HTTP scheme for the BOSH URL with beta.meet.jit.si on mobile.
  152. let { protocol } = locationURI;
  153. const domain = locationURI.hostname;
  154. if (!protocol && domain === 'beta.meet.jit.si') {
  155. const windowLocation = window.location;
  156. windowLocation && (protocol = windowLocation.protocol);
  157. protocol || (protocol = 'http:');
  158. }
  159. // Default to the HTTPS scheme for the BOSH URL.
  160. protocol || (protocol = 'https:');
  161. return {
  162. bosh:
  163. `${String(protocol)}//${domain}${
  164. locationURI.contextRoot || '/'}http-bind`,
  165. hosts: {
  166. domain,
  167. // Required by:
  168. // - lib-jitsi-meet/modules/xmpp/xmpp.js
  169. muc: `conference.${domain}`
  170. }
  171. };
  172. }
  173. /**
  174. * The current (similar to getCurrentConference in base/conference/functions.js)
  175. * connection which is {@code connection} or {@code connecting}.
  176. *
  177. * @param {Object} baseConnectionState - The current state of the
  178. * {@code 'base/connection'} feature.
  179. * @returns {JitsiConnection} - The current {@code JitsiConnection} if any.
  180. * @private
  181. */
  182. function _getCurrentConnection(baseConnectionState: Object): ?Object {
  183. return baseConnectionState.connection || baseConnectionState.connecting;
  184. }
  185. /**
  186. * Reduces a specific redux action {@link SET_LOCATION_URL} of the feature
  187. * base/connection.
  188. *
  189. * @param {Object} state - The redux state of the feature base/connection.
  190. * @param {Action} action - The redux action {@code SET_LOCATION_URL} to reduce.
  191. * @private
  192. * @returns {Object} The new state of the feature base/connection after the
  193. * reduction of the specified action.
  194. */
  195. function _setLocationURL(
  196. state: Object,
  197. { locationURL }: { locationURL: ?URL }) {
  198. return assign(state, {
  199. locationURL,
  200. options: locationURL ? _constructOptions(locationURL) : undefined
  201. });
  202. }
  203. /**
  204. * Reduces a specific redux action {@link SET_ROOM} of the feature
  205. * base/connection.
  206. *
  207. * @param {Object} state - The redux state of the feature base/connection.
  208. * @private
  209. * @returns {Object} The new state of the feature base/connection after the
  210. * reduction of the specified action.
  211. */
  212. function _setRoom(state: Object) {
  213. return assign(state, {
  214. error: undefined,
  215. passwordRequired: undefined
  216. });
  217. }