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

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