Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

reducer.js 15KB

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