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

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