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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. if (state.conference !== conference) {
  158. return state;
  159. }
  160. return assign(state, {
  161. authRequired: undefined,
  162. conference: undefined,
  163. joining: undefined,
  164. leaving: undefined,
  165. locked: undefined,
  166. password: undefined,
  167. passwordRequired: undefined
  168. });
  169. }
  170. /**
  171. * Reduces a specific Redux action CONFERENCE_WILL_JOIN of the feature
  172. * base/conference.
  173. *
  174. * @param {Object} state - The Redux state of the feature base/conference.
  175. * @param {Action} action - The Redux action CONFERENCE_WILL_JOIN to reduce.
  176. * @private
  177. * @returns {Object} The new state of the feature base/conference after the
  178. * reduction of the specified action.
  179. */
  180. function _conferenceWillJoin(state, { conference }) {
  181. return set(state, 'joining', conference);
  182. }
  183. /**
  184. * Reduces a specific Redux action CONFERENCE_WILL_LEAVE 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_LEAVE 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 _conferenceWillLeave(state, { conference }) {
  194. if (state.conference !== conference) {
  195. return state;
  196. }
  197. return assign(state, {
  198. authRequired: undefined,
  199. joining: undefined,
  200. /**
  201. * The JitsiConference instance which is currently in the process of
  202. * being left.
  203. *
  204. * @type {JitsiConference}
  205. */
  206. leaving: conference,
  207. passwordRequired: undefined
  208. });
  209. }
  210. /**
  211. * Reduces a specific Redux action LOCK_STATE_CHANGED of the feature
  212. * base/conference.
  213. *
  214. * @param {Object} state - The Redux state of the feature base/conference.
  215. * @param {Action} action - The Redux action LOCK_STATE_CHANGED to reduce.
  216. * @private
  217. * @returns {Object} The new state of the feature base/conference after the
  218. * reduction of the specified action.
  219. */
  220. function _lockStateChanged(state, { conference, locked }) {
  221. if (state.conference !== conference) {
  222. return state;
  223. }
  224. return assign(state, {
  225. locked: locked ? state.locked || LOCKED_REMOTELY : undefined,
  226. password: locked ? state.password : undefined
  227. });
  228. }
  229. /**
  230. * Reduces a specific Redux action P2P_STATUS_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 P2P_STATUS_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 _p2pStatusChanged(state, action) {
  240. return set(state, 'p2p', action.p2p);
  241. }
  242. /**
  243. * Reduces a specific Redux action SET_AUDIO_ONLY 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 SET_AUDIO_ONLY 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 _setAudioOnly(state, action) {
  253. return set(state, 'audioOnly', action.audioOnly);
  254. }
  255. /**
  256. * Reduces a specific Redux action SET_PASSWORD of the feature base/conference.
  257. *
  258. * @param {Object} state - The Redux state of the feature base/conference.
  259. * @param {Action} action - The Redux action SET_PASSWORD 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 _setPassword(state, { conference, method, password }) {
  265. switch (method) {
  266. case conference.join:
  267. if (state.passwordRequired === conference) {
  268. return assign(state, {
  269. locked: LOCKED_REMOTELY,
  270. /**
  271. * The password with which the conference is to be joined.
  272. *
  273. * @type {string}
  274. */
  275. password,
  276. passwordRequired: undefined
  277. });
  278. }
  279. break;
  280. case conference.lock:
  281. return assign(state, {
  282. locked: password ? LOCKED_LOCALLY : undefined,
  283. password
  284. });
  285. }
  286. return state;
  287. }
  288. /**
  289. * Reduces a specific Redux action SET_RECEIVE_VIDEO_QUALITY of the feature
  290. * base/conference.
  291. *
  292. * @param {Object} state - The Redux state of the feature base/conference.
  293. * @param {Action} action - The Redux action SET_RECEIVE_VIDEO_QUALITY to
  294. * 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 _setReceiveVideoQuality(state, action) {
  300. return set(state, 'receiveVideoQuality', action.receiveVideoQuality);
  301. }
  302. /**
  303. * Reduces a specific Redux action SET_ROOM of the feature base/conference.
  304. *
  305. * @param {Object} state - The Redux state of the feature base/conference.
  306. * @param {Action} action - The Redux action SET_ROOM to reduce.
  307. * @private
  308. * @returns {Object} The new state of the feature base/conference after the
  309. * reduction of the specified action.
  310. */
  311. function _setRoom(state, action) {
  312. let { room } = action;
  313. if (!isRoomValid(room)) {
  314. // Technically, there are multiple values which don't represent valid
  315. // room names. Practically, each of them is as bad as the rest of them
  316. // because we can't use any of them to join a conference.
  317. room = undefined;
  318. }
  319. /**
  320. * The name of the room of the conference (to be) joined.
  321. *
  322. * @type {string}
  323. */
  324. return set(state, 'room', room);
  325. }
  326. /**
  327. * Reduces a specific Redux action SET_SIP_GATEWAY_ENABLED of the feature
  328. * base/conference.
  329. *
  330. * @param {Object} state - The Redux state of the feature base/conference.
  331. * @param {Action} action - The Redux action SET_SIP_GATEWAY_ENABLED to reduce.
  332. * @private
  333. * @returns {Object} The new state of the feature base/conference after the
  334. * reduction of the specified action.
  335. */
  336. function _setSIPGatewayEnabled(state, action) {
  337. return set(state, 'isSIPGatewayEnabled', action.isSIPGatewayEnabled);
  338. }