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

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