您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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