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

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