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

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