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

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