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

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