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

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