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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. // @flow
  2. import { LOCKED_LOCALLY, LOCKED_REMOTELY } from '../../room-lock';
  3. import { CONNECTION_WILL_CONNECT, SET_LOCATION_URL } from '../connection';
  4. import { JitsiConferenceErrors } from '../lib-jitsi-meet';
  5. import { assign, ReducerRegistry, set } from '../redux';
  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_FOLLOW_ME,
  18. SET_PASSWORD,
  19. SET_PENDING_SUBJECT_CHANGE,
  20. SET_ROOM,
  21. SET_START_MUTED_POLICY
  22. } from './actionTypes';
  23. import { isRoomValid } from './functions';
  24. const DEFAULT_STATE = {
  25. conference: undefined,
  26. e2eeSupported: undefined,
  27. joining: undefined,
  28. leaving: undefined,
  29. locked: undefined,
  30. membersOnly: undefined,
  31. password: undefined,
  32. passwordRequired: undefined
  33. };
  34. /**
  35. * Listen for actions that contain the conference object, so that it can be
  36. * stored for use by other action creators.
  37. */
  38. ReducerRegistry.register(
  39. 'features/base/conference',
  40. (state = DEFAULT_STATE, action) => {
  41. switch (action.type) {
  42. case AUTH_STATUS_CHANGED:
  43. return _authStatusChanged(state, action);
  44. case CONFERENCE_FAILED:
  45. return _conferenceFailed(state, action);
  46. case CONFERENCE_JOINED:
  47. return _conferenceJoined(state, action);
  48. case CONFERENCE_SUBJECT_CHANGED:
  49. return set(state, 'subject', action.subject);
  50. case CONFERENCE_TIMESTAMP_CHANGED:
  51. return set(state, 'conferenceTimestamp', action.conferenceTimestamp);
  52. case CONFERENCE_LEFT:
  53. case CONFERENCE_WILL_LEAVE:
  54. return _conferenceLeftOrWillLeave(state, action);
  55. case CONFERENCE_WILL_JOIN:
  56. return _conferenceWillJoin(state, action);
  57. case CONNECTION_WILL_CONNECT:
  58. return set(state, 'authRequired', undefined);
  59. case LOCK_STATE_CHANGED:
  60. return _lockStateChanged(state, action);
  61. case P2P_STATUS_CHANGED:
  62. return _p2pStatusChanged(state, action);
  63. case SET_FOLLOW_ME:
  64. return set(state, 'followMeEnabled', action.enabled);
  65. case SET_LOCATION_URL:
  66. return set(state, 'room', undefined);
  67. case SET_PASSWORD:
  68. return _setPassword(state, action);
  69. case SET_PENDING_SUBJECT_CHANGE:
  70. return set(state, 'pendingSubjectChange', action.subject);
  71. case SET_ROOM:
  72. return _setRoom(state, action);
  73. case SET_START_MUTED_POLICY:
  74. return {
  75. ...state,
  76. startAudioMutedPolicy: action.startAudioMutedPolicy,
  77. startVideoMutedPolicy: action.startVideoMutedPolicy
  78. };
  79. }
  80. return state;
  81. });
  82. /**
  83. * Reduces a specific Redux action AUTH_STATUS_CHANGED of the feature
  84. * base/conference.
  85. *
  86. * @param {Object} state - The Redux state of the feature base/conference.
  87. * @param {Action} action - The Redux action AUTH_STATUS_CHANGED to reduce.
  88. * @private
  89. * @returns {Object} The new state of the feature base/conference after the
  90. * reduction of the specified action.
  91. */
  92. function _authStatusChanged(state, { authEnabled, authLogin }) {
  93. return assign(state, {
  94. authEnabled,
  95. authLogin
  96. });
  97. }
  98. /**
  99. * Reduces a specific Redux action CONFERENCE_FAILED of the feature
  100. * base/conference.
  101. *
  102. * @param {Object} state - The Redux state of the feature base/conference.
  103. * @param {Action} action - The Redux action CONFERENCE_FAILED to reduce.
  104. * @private
  105. * @returns {Object} The new state of the feature base/conference after the
  106. * reduction of the specified action.
  107. */
  108. function _conferenceFailed(state, { conference, error }) {
  109. // The current (similar to getCurrentConference in
  110. // base/conference/functions.js) conference which is joining or joined:
  111. const conference_ = state.conference || state.joining;
  112. if (conference_ && conference_ !== conference) {
  113. return state;
  114. }
  115. let authRequired;
  116. let membersOnly;
  117. let passwordRequired;
  118. switch (error.name) {
  119. case JitsiConferenceErrors.AUTHENTICATION_REQUIRED:
  120. authRequired = conference;
  121. break;
  122. case JitsiConferenceErrors.CONFERENCE_ACCESS_DENIED:
  123. case JitsiConferenceErrors.MEMBERS_ONLY_ERROR:
  124. membersOnly = conference;
  125. break;
  126. case JitsiConferenceErrors.PASSWORD_REQUIRED:
  127. passwordRequired = conference;
  128. break;
  129. }
  130. return assign(state, {
  131. authRequired,
  132. conference: undefined,
  133. e2eeSupported: undefined,
  134. error,
  135. joining: undefined,
  136. leaving: undefined,
  137. /**
  138. * The indicator of how the conference/room is locked. If falsy, the
  139. * conference/room is unlocked; otherwise, it's either
  140. * {@code LOCKED_LOCALLY} or {@code LOCKED_REMOTELY}.
  141. *
  142. * @type {string}
  143. */
  144. locked: passwordRequired ? LOCKED_REMOTELY : undefined,
  145. membersOnly,
  146. password: undefined,
  147. /**
  148. * The JitsiConference instance which requires a password to join.
  149. *
  150. * @type {JitsiConference}
  151. */
  152. passwordRequired
  153. });
  154. }
  155. /**
  156. * Reduces a specific Redux action CONFERENCE_JOINED of the feature
  157. * base/conference.
  158. *
  159. * @param {Object} state - The Redux state of the feature base/conference.
  160. * @param {Action} action - The Redux action CONFERENCE_JOINED to reduce.
  161. * @private
  162. * @returns {Object} The new state of the feature base/conference after the
  163. * reduction of the specified action.
  164. */
  165. function _conferenceJoined(state, { conference }) {
  166. // FIXME The indicator which determines whether a JitsiConference is locked
  167. // i.e. password-protected is private to lib-jitsi-meet. However, the
  168. // library does not fire LOCK_STATE_CHANGED upon joining a JitsiConference
  169. // with a password.
  170. // FIXME Technically JitsiConference.room is a private field.
  171. const locked = conference.room && conference.room.locked ? LOCKED_REMOTELY : undefined;
  172. return assign(state, {
  173. authRequired: undefined,
  174. /**
  175. * The JitsiConference instance represented by the Redux state of the
  176. * feature base/conference.
  177. *
  178. * @type {JitsiConference}
  179. */
  180. conference,
  181. e2eeSupported: conference.isE2EESupported(),
  182. joining: undefined,
  183. membersOnly: undefined,
  184. leaving: undefined,
  185. /**
  186. * The indicator which determines whether the conference is locked.
  187. *
  188. * @type {boolean}
  189. */
  190. locked,
  191. passwordRequired: undefined
  192. });
  193. }
  194. /**
  195. * Reduces a specific redux action {@link CONFERENCE_LEFT} or
  196. * {@link CONFERENCE_WILL_LEAVE} for the feature base/conference.
  197. *
  198. * @param {Object} state - The redux state of the feature base/conference.
  199. * @param {Action} action - The redux action {@code CONFERENCE_LEFT} or
  200. * {@code CONFERENCE_WILL_LEAVE} to reduce.
  201. * @private
  202. * @returns {Object} The next/new state of the feature base/conference after the
  203. * reduction of the specified action.
  204. */
  205. function _conferenceLeftOrWillLeave(state, { conference, type }) {
  206. const nextState = { ...state };
  207. // The redux action CONFERENCE_LEFT is the last time that we should be
  208. // hearing from a JitsiConference instance.
  209. //
  210. // The redux action CONFERENCE_WILL_LEAVE represents the order of the user
  211. // to leave a JitsiConference instance. From the user's perspective, there's
  212. // no going back (with respect to the instance itself). The app will perform
  213. // due clean-up like leaving the associated room, but the instance is no
  214. // longer the focus of the attention of the user and, consequently, the app.
  215. for (const p in state) {
  216. if (state[p] === conference) {
  217. nextState[p] = undefined;
  218. switch (p) {
  219. case 'conference':
  220. case 'passwordRequired':
  221. // XXX Clear/unset locked & password for a conference which has
  222. // been LOCKED_LOCALLY or LOCKED_REMOTELY.
  223. delete nextState.locked;
  224. delete nextState.password;
  225. break;
  226. }
  227. }
  228. }
  229. if (type === CONFERENCE_WILL_LEAVE) {
  230. // A CONFERENCE_WILL_LEAVE is of further consequence only if it is
  231. // expected i.e. if the specified conference is joining or joined.
  232. if (conference === state.joining || conference === state.conference) {
  233. /**
  234. * The JitsiConference instance which is currently in the process of
  235. * being left.
  236. *
  237. * @type {JitsiConference}
  238. */
  239. nextState.leaving = conference;
  240. }
  241. }
  242. return nextState;
  243. }
  244. /**
  245. * Reduces a specific Redux action CONFERENCE_WILL_JOIN of the feature
  246. * base/conference.
  247. *
  248. * @param {Object} state - The Redux state of the feature base/conference.
  249. * @param {Action} action - The Redux action CONFERENCE_WILL_JOIN to reduce.
  250. * @private
  251. * @returns {Object} The new state of the feature base/conference after the
  252. * reduction of the specified action.
  253. */
  254. function _conferenceWillJoin(state, { conference }) {
  255. return assign(state, {
  256. error: undefined,
  257. joining: conference
  258. });
  259. }
  260. /**
  261. * Reduces a specific Redux action LOCK_STATE_CHANGED 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 LOCK_STATE_CHANGED 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 _lockStateChanged(state, { conference, locked }) {
  271. if (state.conference !== conference) {
  272. return state;
  273. }
  274. return assign(state, {
  275. locked: locked ? state.locked || LOCKED_REMOTELY : undefined,
  276. password: locked ? state.password : undefined
  277. });
  278. }
  279. /**
  280. * Reduces a specific Redux action P2P_STATUS_CHANGED of the feature
  281. * base/conference.
  282. *
  283. * @param {Object} state - The Redux state of the feature base/conference.
  284. * @param {Action} action - The Redux action P2P_STATUS_CHANGED to reduce.
  285. * @private
  286. * @returns {Object} The new state of the feature base/conference after the
  287. * reduction of the specified action.
  288. */
  289. function _p2pStatusChanged(state, action) {
  290. return set(state, 'p2p', action.p2p);
  291. }
  292. /**
  293. * Reduces a specific Redux action SET_PASSWORD of the feature base/conference.
  294. *
  295. * @param {Object} state - The Redux state of the feature base/conference.
  296. * @param {Action} action - The Redux action SET_PASSWORD to reduce.
  297. * @private
  298. * @returns {Object} The new state of the feature base/conference after the
  299. * reduction of the specified action.
  300. */
  301. function _setPassword(state, { conference, method, password }) {
  302. switch (method) {
  303. case conference.join:
  304. return assign(state, {
  305. // 1. The JitsiConference which transitions away from
  306. // passwordRequired MUST remain in the redux state
  307. // features/base/conference until it transitions into
  308. // conference; otherwise, there is a span of time during which
  309. // the redux state does not even know that there is a
  310. // JitsiConference whatsoever.
  311. //
  312. // 2. The redux action setPassword will attempt to join the
  313. // JitsiConference so joining is an appropriate transitional
  314. // redux state.
  315. //
  316. // 3. The redux action setPassword will perform the same check
  317. // before it proceeds with the re-join.
  318. joining: state.conference ? state.joining : conference,
  319. locked: LOCKED_REMOTELY,
  320. /**
  321. * The password with which the conference is to be joined.
  322. *
  323. * @type {string}
  324. */
  325. password
  326. });
  327. case conference.lock:
  328. return assign(state, {
  329. locked: password ? LOCKED_LOCALLY : undefined,
  330. password
  331. });
  332. }
  333. return state;
  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 assign(state, {
  358. error: undefined,
  359. room
  360. });
  361. }