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

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