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

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