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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. // @flow
  2. import { CONNECTION_WILL_CONNECT, SET_LOCATION_URL } from '../connection';
  3. import { JitsiConferenceErrors } from '../lib-jitsi-meet';
  4. import { assign, ReducerRegistry, set } from '../redux';
  5. import { LOCKED_LOCALLY, LOCKED_REMOTELY } from '../../room-lock';
  6. import {
  7. AUTH_STATUS_CHANGED,
  8. CONFERENCE_FAILED,
  9. CONFERENCE_JOINED,
  10. CONFERENCE_LEFT,
  11. CONFERENCE_WILL_JOIN,
  12. CONFERENCE_WILL_LEAVE,
  13. LOCK_STATE_CHANGED,
  14. P2P_STATUS_CHANGED,
  15. SET_AUDIO_ONLY,
  16. SET_DESKTOP_SHARING_ENABLED,
  17. SET_FOLLOW_ME,
  18. SET_MAX_RECEIVER_VIDEO_QUALITY,
  19. SET_PASSWORD,
  20. SET_PREFERRED_RECEIVER_VIDEO_QUALITY,
  21. SET_ROOM,
  22. SET_SIP_GATEWAY_ENABLED,
  23. SET_START_MUTED_POLICY
  24. } from './actionTypes';
  25. import { VIDEO_QUALITY_LEVELS } from './constants';
  26. import { isRoomValid } from './functions';
  27. const DEFAULT_STATE = {
  28. joining: undefined,
  29. maxReceiverVideoQuality: VIDEO_QUALITY_LEVELS.HIGH,
  30. preferredReceiverVideoQuality: VIDEO_QUALITY_LEVELS.HIGH
  31. };
  32. /**
  33. * Listen for actions that contain the conference object, so that it can be
  34. * stored for use by other action creators.
  35. */
  36. ReducerRegistry.register(
  37. 'features/base/conference',
  38. (state = DEFAULT_STATE, action) => {
  39. switch (action.type) {
  40. case AUTH_STATUS_CHANGED:
  41. return _authStatusChanged(state, action);
  42. case CONFERENCE_FAILED:
  43. return _conferenceFailed(state, action);
  44. case CONFERENCE_JOINED:
  45. return _conferenceJoined(state, action);
  46. case CONFERENCE_LEFT:
  47. case CONFERENCE_WILL_LEAVE:
  48. return _conferenceLeftOrWillLeave(state, action);
  49. case CONFERENCE_WILL_JOIN:
  50. return _conferenceWillJoin(state, action);
  51. case CONNECTION_WILL_CONNECT:
  52. return set(state, 'authRequired', undefined);
  53. case LOCK_STATE_CHANGED:
  54. return _lockStateChanged(state, action);
  55. case P2P_STATUS_CHANGED:
  56. return _p2pStatusChanged(state, action);
  57. case SET_AUDIO_ONLY:
  58. return _setAudioOnly(state, action);
  59. case SET_DESKTOP_SHARING_ENABLED:
  60. return _setDesktopSharingEnabled(state, action);
  61. case SET_FOLLOW_ME:
  62. return set(state, 'followMeEnabled', action.enabled);
  63. case SET_LOCATION_URL:
  64. return set(state, 'room', undefined);
  65. case SET_MAX_RECEIVER_VIDEO_QUALITY:
  66. return set(
  67. state,
  68. 'maxReceiverVideoQuality',
  69. action.maxReceiverVideoQuality);
  70. case SET_PASSWORD:
  71. return _setPassword(state, action);
  72. case SET_PREFERRED_RECEIVER_VIDEO_QUALITY:
  73. return set(
  74. state,
  75. 'preferredReceiverVideoQuality',
  76. action.preferredReceiverVideoQuality);
  77. case SET_ROOM:
  78. return _setRoom(state, action);
  79. case SET_SIP_GATEWAY_ENABLED:
  80. return _setSIPGatewayEnabled(state, action);
  81. case SET_START_MUTED_POLICY:
  82. return {
  83. ...state,
  84. startAudioMutedPolicy: action.startAudioMutedPolicy,
  85. startVideoMutedPolicy: action.startVideoMutedPolicy
  86. };
  87. }
  88. return state;
  89. });
  90. /**
  91. * Reduces a specific Redux action AUTH_STATUS_CHANGED of the feature
  92. * base/conference.
  93. *
  94. * @param {Object} state - The Redux state of the feature base/conference.
  95. * @param {Action} action - The Redux action AUTH_STATUS_CHANGED to reduce.
  96. * @private
  97. * @returns {Object} The new state of the feature base/conference after the
  98. * reduction of the specified action.
  99. */
  100. function _authStatusChanged(state, { authEnabled, authLogin }) {
  101. return assign(state, {
  102. authEnabled,
  103. authLogin
  104. });
  105. }
  106. /**
  107. * Reduces a specific Redux action CONFERENCE_FAILED of the feature
  108. * base/conference.
  109. *
  110. * @param {Object} state - The Redux state of the feature base/conference.
  111. * @param {Action} action - The Redux action CONFERENCE_FAILED to reduce.
  112. * @private
  113. * @returns {Object} The new state of the feature base/conference after the
  114. * reduction of the specified action.
  115. */
  116. function _conferenceFailed(state, { conference, error }) {
  117. // The current (similar to getCurrentConference in
  118. // base/conference/functions.js) conference which is joining or joined:
  119. const conference_ = state.conference || state.joining;
  120. if (conference_ && conference_ !== conference) {
  121. return state;
  122. }
  123. let authRequired;
  124. let passwordRequired;
  125. switch (error.name) {
  126. case JitsiConferenceErrors.AUTHENTICATION_REQUIRED:
  127. authRequired = conference;
  128. break;
  129. case JitsiConferenceErrors.PASSWORD_REQUIRED:
  130. passwordRequired = conference;
  131. break;
  132. }
  133. return assign(state, {
  134. authRequired,
  135. conference: undefined,
  136. error,
  137. joining: undefined,
  138. leaving: undefined,
  139. /**
  140. * The indicator of how the conference/room is locked. If falsy, the
  141. * conference/room is unlocked; otherwise, it's either
  142. * {@code LOCKED_LOCALLY} or {@code LOCKED_REMOTELY}.
  143. *
  144. * @type {string}
  145. */
  146. locked: passwordRequired ? LOCKED_REMOTELY : undefined,
  147. password: undefined,
  148. /**
  149. * The JitsiConference instance which requires a password to join.
  150. *
  151. * @type {JitsiConference}
  152. */
  153. passwordRequired
  154. });
  155. }
  156. /**
  157. * Reduces a specific Redux action CONFERENCE_JOINED of the feature
  158. * base/conference.
  159. *
  160. * @param {Object} state - The Redux state of the feature base/conference.
  161. * @param {Action} action - The Redux action CONFERENCE_JOINED to reduce.
  162. * @private
  163. * @returns {Object} The new state of the feature base/conference after the
  164. * reduction of the specified action.
  165. */
  166. function _conferenceJoined(state, { conference }) {
  167. // FIXME The indicator which determines whether a JitsiConference is locked
  168. // i.e. password-protected is private to lib-jitsi-meet. However, the
  169. // library does not fire LOCK_STATE_CHANGED upon joining a JitsiConference
  170. // with a password.
  171. const locked = conference.room.locked ? LOCKED_REMOTELY : undefined;
  172. return assign(state, {
  173. authRequired: undefined,
  174. /**
  175. * The JitsiConference instance represented by the Redux state of the
  176. * feature base/conference.
  177. *
  178. * @type {JitsiConference}
  179. */
  180. conference,
  181. joining: undefined,
  182. leaving: undefined,
  183. /**
  184. * The indicator which determines whether the conference is locked.
  185. *
  186. * @type {boolean}
  187. */
  188. locked,
  189. passwordRequired: undefined
  190. });
  191. }
  192. /**
  193. * Reduces a specific redux action {@link CONFERENCE_LEFT} or
  194. * {@link CONFERENCE_WILL_LEAVE} for the feature base/conference.
  195. *
  196. * @param {Object} state - The redux state of the feature base/conference.
  197. * @param {Action} action - The redux action {@code CONFERENCE_LEFT} or
  198. * {@code CONFERENCE_WILL_LEAVE} to reduce.
  199. * @private
  200. * @returns {Object} The next/new state of the feature base/conference after the
  201. * reduction of the specified action.
  202. */
  203. function _conferenceLeftOrWillLeave(state, { conference, type }) {
  204. const nextState = { ...state };
  205. // The redux action CONFERENCE_LEFT is the last time that we should be
  206. // hearing from a JitsiConference instance.
  207. //
  208. // The redux action CONFERENCE_WILL_LEAVE represents the order of the user
  209. // to leave a JitsiConference instance. From the user's perspective, there's
  210. // no going back (with respect to the instance itself). The app will perform
  211. // due clean-up like leaving the associated room, but the instance is no
  212. // longer the focus of the attention of the user and, consequently, the app.
  213. for (const p in state) {
  214. if (state[p] === conference) {
  215. nextState[p] = undefined;
  216. switch (p) {
  217. case 'conference':
  218. case 'passwordRequired':
  219. // XXX Clear/unset locked & password for a conference which has
  220. // been LOCKED_LOCALLY or LOCKED_REMOTELY.
  221. delete nextState.locked;
  222. delete nextState.password;
  223. break;
  224. }
  225. }
  226. }
  227. if (type === CONFERENCE_WILL_LEAVE) {
  228. // A CONFERENCE_WILL_LEAVE is of further consequence only if it is
  229. // expected i.e. if the specified conference is joining or joined.
  230. if (conference === state.joining || conference === state.conference) {
  231. /**
  232. * The JitsiConference instance which is currently in the process of
  233. * being left.
  234. *
  235. * @type {JitsiConference}
  236. */
  237. nextState.leaving = conference;
  238. }
  239. }
  240. return nextState;
  241. }
  242. /**
  243. * Reduces a specific Redux action CONFERENCE_WILL_JOIN of the feature
  244. * base/conference.
  245. *
  246. * @param {Object} state - The Redux state of the feature base/conference.
  247. * @param {Action} action - The Redux action CONFERENCE_WILL_JOIN to reduce.
  248. * @private
  249. * @returns {Object} The new state of the feature base/conference after the
  250. * reduction of the specified action.
  251. */
  252. function _conferenceWillJoin(state, { conference }) {
  253. return assign(state, {
  254. error: undefined,
  255. joining: conference
  256. });
  257. }
  258. /**
  259. * Reduces a specific Redux action LOCK_STATE_CHANGED of the feature
  260. * base/conference.
  261. *
  262. * @param {Object} state - The Redux state of the feature base/conference.
  263. * @param {Action} action - The Redux action LOCK_STATE_CHANGED to reduce.
  264. * @private
  265. * @returns {Object} The new state of the feature base/conference after the
  266. * reduction of the specified action.
  267. */
  268. function _lockStateChanged(state, { conference, locked }) {
  269. if (state.conference !== conference) {
  270. return state;
  271. }
  272. return assign(state, {
  273. locked: locked ? state.locked || LOCKED_REMOTELY : undefined,
  274. password: locked ? state.password : undefined
  275. });
  276. }
  277. /**
  278. * Reduces a specific Redux action P2P_STATUS_CHANGED of the feature
  279. * base/conference.
  280. *
  281. * @param {Object} state - The Redux state of the feature base/conference.
  282. * @param {Action} action - The Redux action P2P_STATUS_CHANGED to reduce.
  283. * @private
  284. * @returns {Object} The new state of the feature base/conference after the
  285. * reduction of the specified action.
  286. */
  287. function _p2pStatusChanged(state, action) {
  288. return set(state, 'p2p', action.p2p);
  289. }
  290. /**
  291. * Reduces a specific Redux action SET_AUDIO_ONLY of the feature
  292. * base/conference.
  293. *
  294. * @param {Object} state - The Redux state of the feature base/conference.
  295. * @param {Action} action - The Redux action SET_AUDIO_ONLY to reduce.
  296. * @private
  297. * @returns {Object} The new state of the feature base/conference after the
  298. * reduction of the specified action.
  299. */
  300. function _setAudioOnly(state, action) {
  301. return set(state, 'audioOnly', action.audioOnly);
  302. }
  303. /**
  304. * Reduces a specific Redux action SET_DESKTOP_SHARING_ENABLED of the feature
  305. * base/conference.
  306. *
  307. * @param {Object} state - The Redux state of the feature base/conference.
  308. * @param {Action} action - The Redux action SET_DESKTOP_SHARING_ENABLED to
  309. * reduce.
  310. * @private
  311. * @returns {Object} The new state of the feature base/conference after the
  312. * reduction of the specified action.
  313. */
  314. function _setDesktopSharingEnabled(state, action) {
  315. return set(state, 'desktopSharingEnabled', action.desktopSharingEnabled);
  316. }
  317. /**
  318. * Reduces a specific Redux action SET_PASSWORD of the feature base/conference.
  319. *
  320. * @param {Object} state - The Redux state of the feature base/conference.
  321. * @param {Action} action - The Redux action SET_PASSWORD to reduce.
  322. * @private
  323. * @returns {Object} The new state of the feature base/conference after the
  324. * reduction of the specified action.
  325. */
  326. function _setPassword(state, { conference, method, password }) {
  327. switch (method) {
  328. case conference.join:
  329. if (state.passwordRequired === conference) {
  330. return assign(state, {
  331. // XXX 1. The JitsiConference which transitions away from
  332. // passwordRequired MUST remain in the redux state
  333. // features/base/conference until it transitions into
  334. // conference; otherwise, there is a span of time during which
  335. // the redux state does not even know that there is a
  336. // JitsiConference whatsoever.
  337. //
  338. // 2. The redux action setPassword will attempt to join the
  339. // JitsiConference so joining is an appropriate transitional
  340. // redux state.
  341. //
  342. // 3. The redux action setPassword will perform the same check
  343. // before it proceeds with the re-join.
  344. joining: state.conference ? state.joining : conference,
  345. locked: LOCKED_REMOTELY,
  346. /**
  347. * The password with which the conference is to be joined.
  348. *
  349. * @type {string}
  350. */
  351. password,
  352. passwordRequired: undefined
  353. });
  354. }
  355. break;
  356. case conference.lock:
  357. return assign(state, {
  358. locked: password ? LOCKED_LOCALLY : undefined,
  359. password
  360. });
  361. }
  362. return state;
  363. }
  364. /**
  365. * Reduces a specific Redux action SET_ROOM of the feature base/conference.
  366. *
  367. * @param {Object} state - The Redux state of the feature base/conference.
  368. * @param {Action} action - The Redux action SET_ROOM to reduce.
  369. * @private
  370. * @returns {Object} The new state of the feature base/conference after the
  371. * reduction of the specified action.
  372. */
  373. function _setRoom(state, action) {
  374. let { room } = action;
  375. if (!isRoomValid(room)) {
  376. // Technically, there are multiple values which don't represent valid
  377. // room names. Practically, each of them is as bad as the rest of them
  378. // because we can't use any of them to join a conference.
  379. room = undefined;
  380. }
  381. /**
  382. * The name of the room of the conference (to be) joined.
  383. *
  384. * @type {string}
  385. */
  386. return assign(state, {
  387. error: undefined,
  388. room
  389. });
  390. }
  391. /**
  392. * Reduces a specific Redux action SET_SIP_GATEWAY_ENABLED of the feature
  393. * base/conference.
  394. *
  395. * @param {Object} state - The Redux state of the feature base/conference.
  396. * @param {Action} action - The Redux action SET_SIP_GATEWAY_ENABLED to reduce.
  397. * @private
  398. * @returns {Object} The new state of the feature base/conference after the
  399. * reduction of the specified action.
  400. */
  401. function _setSIPGatewayEnabled(state, action) {
  402. return set(state, 'isSIPGatewayEnabled', action.isSIPGatewayEnabled);
  403. }