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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. if (state.connection !== connection) {
  50. return state;
  51. }
  52. return assign(state, {
  53. connecting: undefined,
  54. connection: undefined
  55. });
  56. }
  57. /**
  58. * Reduces a specific Redux action CONNECTION_ESTABLISHED of the feature
  59. * base/connection.
  60. *
  61. * @param {Object} state - The Redux state of the feature base/connection.
  62. * @param {Action} action - The Redux action CONNECTION_ESTABLISHED to reduce.
  63. * @private
  64. * @returns {Object} The new state of the feature base/connection after the
  65. * reduction of the specified action.
  66. */
  67. function _connectionEstablished(
  68. state: Object,
  69. { connection }: { connection: Object }) {
  70. return assign(state, {
  71. connecting: undefined,
  72. connection,
  73. error: undefined,
  74. passwordRequired: undefined
  75. });
  76. }
  77. /**
  78. * Reduces a specific Redux action CONNECTION_FAILED of the feature
  79. * base/connection.
  80. *
  81. * @param {Object} state - The Redux state of the feature base/connection.
  82. * @param {Action} action - The Redux action CONNECTION_FAILED to reduce.
  83. * @private
  84. * @returns {Object} The new state of the feature base/connection after the
  85. * reduction of the specified action.
  86. */
  87. function _connectionFailed(
  88. state: Object,
  89. { connection, error }: {
  90. connection: Object,
  91. error: ConnectionFailedError
  92. }) {
  93. // The current (similar to getCurrentConference in
  94. // base/conference/functions.js) connection which is connecting or
  95. // connected:
  96. const connection_ = state.connection || state.connecting;
  97. if (connection_ && connection_ !== connection) {
  98. return state;
  99. }
  100. return assign(state, {
  101. connecting: undefined,
  102. connection: undefined,
  103. error,
  104. passwordRequired:
  105. error.name === JitsiConnectionErrors.PASSWORD_REQUIRED
  106. ? connection : undefined
  107. });
  108. }
  109. /**
  110. * Reduces a specific Redux action CONNECTION_WILL_CONNECT of the feature
  111. * base/connection.
  112. *
  113. * @param {Object} state - The Redux state of the feature base/connection.
  114. * @param {Action} action - The Redux action CONNECTION_WILL_CONNECT to reduce.
  115. * @private
  116. * @returns {Object} The new state of the feature base/connection after the
  117. * reduction of the specified action.
  118. */
  119. function _connectionWillConnect(
  120. state: Object,
  121. { connection }: { connection: Object }) {
  122. return assign(state, {
  123. connecting: connection,
  124. error: undefined,
  125. passwordRequired: undefined
  126. });
  127. }
  128. /**
  129. * Constructs options to be passed to the constructor of {@code JitsiConnection}
  130. * based on a specific location URL.
  131. *
  132. * @param {string} locationURL - The location URL with which the returned
  133. * options are to be constructed.
  134. * @private
  135. * @returns {Object} The options to be passed to the constructor of
  136. * {@code JitsiConnection} based on the location URL.
  137. */
  138. function _constructOptions(locationURL: URL) {
  139. const locationURI = parseURIString(locationURL.href);
  140. // FIXME The HTTPS scheme for the BOSH URL works with meet.jit.si on both
  141. // mobile & Web. It also works with beta.meet.jit.si on Web. Unfortunately,
  142. // it doesn't work with beta.meet.jit.si on mobile. Temporarily, use the
  143. // HTTP scheme for the BOSH URL with beta.meet.jit.si on mobile.
  144. let { protocol } = locationURI;
  145. const domain = locationURI.hostname;
  146. if (!protocol && domain === 'beta.meet.jit.si') {
  147. const windowLocation = window.location;
  148. windowLocation && (protocol = windowLocation.protocol);
  149. protocol || (protocol = 'http:');
  150. }
  151. // Default to the HTTPS scheme for the BOSH URL.
  152. protocol || (protocol = 'https:');
  153. return {
  154. bosh:
  155. `${String(protocol)}//${domain}${
  156. locationURI.contextRoot || '/'}http-bind`,
  157. hosts: {
  158. domain,
  159. // Required by:
  160. // - lib-jitsi-meet/modules/xmpp/xmpp.js
  161. muc: `conference.${domain}`
  162. }
  163. };
  164. }
  165. /**
  166. * Reduces a specific redux action {@link SET_LOCATION_URL} of the feature
  167. * base/connection.
  168. *
  169. * @param {Object} state - The redux state of the feature base/connection.
  170. * @param {Action} action - The redux action {@code SET_LOCATION_URL} to reduce.
  171. * @private
  172. * @returns {Object} The new state of the feature base/connection after the
  173. * reduction of the specified action.
  174. */
  175. function _setLocationURL(
  176. state: Object,
  177. { locationURL }: { locationURL: ?URL }) {
  178. return assign(state, {
  179. locationURL,
  180. options: locationURL ? _constructOptions(locationURL) : undefined
  181. });
  182. }
  183. /**
  184. * Reduces a specific redux action {@link SET_ROOM} of the feature
  185. * base/connection.
  186. *
  187. * @param {Object} state - The redux state of the feature base/connection.
  188. * @private
  189. * @returns {Object} The new state of the feature base/connection after the
  190. * reduction of the specified action.
  191. */
  192. function _setRoom(state: Object) {
  193. return assign(state, {
  194. error: undefined,
  195. passwordRequired: undefined
  196. });
  197. }