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.5KB

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