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.ts 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. import { AnyAction } from 'redux';
  2. import { FaceLandmarks } from '../../face-landmarks/types';
  3. import { LOCKED_LOCALLY, LOCKED_REMOTELY } from '../../room-lock/constants';
  4. import { ISpeakerStats } from '../../speaker-stats/reducer';
  5. import { SET_CONFIG } from '../config/actionTypes';
  6. import { IConfig } from '../config/configType';
  7. import { CONNECTION_WILL_CONNECT, SET_LOCATION_URL } from '../connection/actionTypes';
  8. import { JitsiConferenceErrors } from '../lib-jitsi-meet';
  9. import ReducerRegistry from '../redux/ReducerRegistry';
  10. import { assign, set } from '../redux/functions';
  11. import {
  12. AUTH_STATUS_CHANGED,
  13. CONFERENCE_FAILED,
  14. CONFERENCE_JOINED,
  15. CONFERENCE_LEFT,
  16. CONFERENCE_LOCAL_SUBJECT_CHANGED,
  17. CONFERENCE_SUBJECT_CHANGED,
  18. CONFERENCE_TIMESTAMP_CHANGED,
  19. CONFERENCE_WILL_JOIN,
  20. CONFERENCE_WILL_LEAVE,
  21. DATA_CHANNEL_CLOSED,
  22. DATA_CHANNEL_OPENED,
  23. LOCK_STATE_CHANGED,
  24. P2P_STATUS_CHANGED,
  25. SET_ASSUMED_BANDWIDTH_BPS,
  26. SET_FOLLOW_ME,
  27. SET_FOLLOW_ME_RECORDER,
  28. SET_OBFUSCATED_ROOM,
  29. SET_PASSWORD,
  30. SET_PENDING_SUBJECT_CHANGE,
  31. SET_ROOM,
  32. SET_START_MUTED_POLICY,
  33. SET_START_REACTIONS_MUTED,
  34. UPDATE_CONFERENCE_METADATA
  35. } from './actionTypes';
  36. import { isRoomValid } from './functions';
  37. const DEFAULT_STATE = {
  38. assumedBandwidthBps: undefined,
  39. conference: undefined,
  40. dataChannelOpen: undefined,
  41. e2eeSupported: undefined,
  42. joining: undefined,
  43. leaving: undefined,
  44. locked: undefined,
  45. membersOnly: undefined,
  46. metadata: undefined,
  47. password: undefined,
  48. passwordRequired: undefined
  49. };
  50. export interface IConferenceMetadata {
  51. recording?: {
  52. isTranscribingEnabled: boolean;
  53. };
  54. visitors?: {
  55. live: boolean;
  56. };
  57. whiteboard?: {
  58. collabDetails: {
  59. roomId: string;
  60. roomKey: string;
  61. };
  62. };
  63. }
  64. export interface IJitsiConference {
  65. addCommandListener: Function;
  66. addLobbyMessageListener: Function;
  67. addTrack: Function;
  68. authenticateAndUpgradeRole: Function;
  69. avModerationApprove: Function;
  70. avModerationReject: Function;
  71. callUUID?: string;
  72. createVideoSIPGWSession: Function;
  73. dial: Function;
  74. disableAVModeration: Function;
  75. disableLobby: Function;
  76. enableAVModeration: Function;
  77. enableLobby: Function;
  78. end: Function;
  79. getBreakoutRooms: Function;
  80. getConnection: Function;
  81. getLocalParticipantProperty: Function;
  82. getLocalTracks: Function;
  83. getMeetingUniqueId: Function;
  84. getMetadataHandler: Function;
  85. getName: Function;
  86. getParticipantById: Function;
  87. getParticipantCount: Function;
  88. getParticipants: Function;
  89. getRole: Function;
  90. getSpeakerStats: () => ISpeakerStats;
  91. getSsrcByTrack: Function;
  92. getTranscriptionStatus: Function;
  93. grantOwner: Function;
  94. isAVModerationSupported: Function;
  95. isE2EEEnabled: Function;
  96. isE2EESupported: Function;
  97. isEndConferenceSupported: Function;
  98. isLobbySupported: Function;
  99. isP2PActive: Function;
  100. isSIPCallingSupported: Function;
  101. isStartAudioMuted: Function;
  102. isStartVideoMuted: Function;
  103. join: Function;
  104. joinLobby: Function;
  105. kickParticipant: Function;
  106. leave: Function;
  107. lobbyApproveAccess: Function;
  108. lobbyDenyAccess: Function;
  109. lock: Function;
  110. markParticipantVerified: Function;
  111. muteParticipant: Function;
  112. myLobbyUserId: Function;
  113. myUserId: Function;
  114. off: Function;
  115. on: Function;
  116. options: any;
  117. removeTrack: Function;
  118. replaceTrack: Function;
  119. room: IJitsiConferenceRoom;
  120. sendApplicationLog: Function;
  121. sendCommand: Function;
  122. sendCommandOnce: Function;
  123. sendEndpointMessage: Function;
  124. sendFaceLandmarks: (faceLandmarks: FaceLandmarks) => void;
  125. sendFeedback: Function;
  126. sendLobbyMessage: Function;
  127. sendMessage: Function;
  128. sendPrivateTextMessage: Function;
  129. sendTextMessage: Function;
  130. sendTones: Function;
  131. sessionId: string;
  132. setAssumedBandwidthBps: (value: number) => void;
  133. setDesktopSharingFrameRate: Function;
  134. setDisplayName: Function;
  135. setIsSilent: Function;
  136. setLocalParticipantProperty: Function;
  137. setMediaEncryptionKey: Function;
  138. setReceiverConstraints: Function;
  139. setSenderVideoConstraint: Function;
  140. setStartMutedPolicy: Function;
  141. setSubject: Function;
  142. startRecording: Function;
  143. startVerification: Function;
  144. stopRecording: Function;
  145. toggleE2EE: Function;
  146. }
  147. export interface IConferenceState {
  148. assumedBandwidthBps?: number;
  149. authEnabled?: boolean;
  150. authLogin?: string;
  151. authRequired?: IJitsiConference;
  152. conference?: IJitsiConference;
  153. conferenceTimestamp?: number;
  154. dataChannelOpen?: boolean;
  155. e2eeSupported?: boolean;
  156. error?: Error;
  157. followMeEnabled?: boolean;
  158. followMeRecorderEnabled?: boolean;
  159. joining?: IJitsiConference;
  160. leaving?: IJitsiConference;
  161. lobbyWaitingForHost?: boolean;
  162. localSubject?: string;
  163. locked?: string;
  164. membersOnly?: IJitsiConference;
  165. metadata?: IConferenceMetadata;
  166. obfuscatedRoom?: string;
  167. obfuscatedRoomSource?: string;
  168. p2p?: Object;
  169. password?: string;
  170. passwordRequired?: IJitsiConference;
  171. pendingSubjectChange?: string;
  172. room?: string;
  173. startAudioMutedPolicy?: boolean;
  174. startReactionsMuted?: boolean;
  175. startVideoMutedPolicy?: boolean;
  176. subject?: string;
  177. }
  178. export interface IJitsiConferenceRoom {
  179. locked: boolean;
  180. myroomjid: string;
  181. roomjid: string;
  182. xmpp: {
  183. moderator: {
  184. logout: Function;
  185. };
  186. };
  187. }
  188. interface IConferenceFailedError extends Error {
  189. params: Array<any>;
  190. }
  191. /**
  192. * Listen for actions that contain the conference object, so that it can be
  193. * stored for use by other action creators.
  194. */
  195. ReducerRegistry.register<IConferenceState>('features/base/conference',
  196. (state = DEFAULT_STATE, action): IConferenceState => {
  197. switch (action.type) {
  198. case AUTH_STATUS_CHANGED:
  199. return _authStatusChanged(state, action);
  200. case CONFERENCE_FAILED:
  201. return _conferenceFailed(state, action);
  202. case CONFERENCE_JOINED:
  203. return _conferenceJoined(state, action);
  204. case CONFERENCE_SUBJECT_CHANGED:
  205. return set(state, 'subject', action.subject);
  206. case CONFERENCE_LOCAL_SUBJECT_CHANGED:
  207. return set(state, 'localSubject', action.localSubject);
  208. case CONFERENCE_TIMESTAMP_CHANGED:
  209. return set(state, 'conferenceTimestamp', action.conferenceTimestamp);
  210. case CONFERENCE_LEFT:
  211. case CONFERENCE_WILL_LEAVE:
  212. return _conferenceLeftOrWillLeave(state, action);
  213. case CONFERENCE_WILL_JOIN:
  214. return _conferenceWillJoin(state, action);
  215. case CONNECTION_WILL_CONNECT:
  216. return set(state, 'authRequired', undefined);
  217. case DATA_CHANNEL_CLOSED:
  218. return set(state, 'dataChannelOpen', false);
  219. case DATA_CHANNEL_OPENED:
  220. return set(state, 'dataChannelOpen', true);
  221. case LOCK_STATE_CHANGED:
  222. return _lockStateChanged(state, action);
  223. case P2P_STATUS_CHANGED:
  224. return _p2pStatusChanged(state, action);
  225. case SET_ASSUMED_BANDWIDTH_BPS: {
  226. const assumedBandwidthBps = action.assumedBandwidthBps >= 0
  227. ? Number(action.assumedBandwidthBps)
  228. : undefined;
  229. return set(state, 'assumedBandwidthBps', assumedBandwidthBps);
  230. }
  231. case SET_FOLLOW_ME:
  232. return set(state, 'followMeEnabled', action.enabled);
  233. case SET_FOLLOW_ME_RECORDER:
  234. return { ...state,
  235. followMeRecorderEnabled: action.enabled,
  236. followMeEnabled: action.enabled
  237. };
  238. case SET_START_REACTIONS_MUTED:
  239. return set(state, 'startReactionsMuted', action.muted);
  240. case SET_LOCATION_URL:
  241. return set(state, 'room', undefined);
  242. case SET_OBFUSCATED_ROOM:
  243. return { ...state,
  244. obfuscatedRoom: action.obfuscatedRoom,
  245. obfuscatedRoomSource: action.obfuscatedRoomSource
  246. };
  247. case SET_PASSWORD:
  248. return _setPassword(state, action);
  249. case SET_PENDING_SUBJECT_CHANGE:
  250. return set(state, 'pendingSubjectChange', action.subject);
  251. case SET_ROOM:
  252. return _setRoom(state, action);
  253. case SET_START_MUTED_POLICY:
  254. return {
  255. ...state,
  256. startAudioMutedPolicy: action.startAudioMutedPolicy,
  257. startVideoMutedPolicy: action.startVideoMutedPolicy
  258. };
  259. case UPDATE_CONFERENCE_METADATA:
  260. return {
  261. ...state,
  262. metadata: action.metadata
  263. };
  264. case SET_CONFIG:
  265. return _setConfig(state, action);
  266. }
  267. return state;
  268. });
  269. /**
  270. * Processes subject and local subject of the conference based on the new config.
  271. *
  272. * @param {Object} state - The Redux state of feature base/conference.
  273. * @param {Action} action - The Redux action SET_CONFIG to reduce.
  274. * @private
  275. * @returns {Object} The new state after the reduction of the specified action.
  276. */
  277. function _setConfig(state: IConferenceState, { config }: { config: IConfig; }) {
  278. const { localSubject, subject } = config;
  279. return {
  280. ...state,
  281. localSubject,
  282. pendingSubjectChange: subject,
  283. subject: undefined
  284. };
  285. }
  286. /**
  287. * Reduces a specific Redux action AUTH_STATUS_CHANGED of the feature
  288. * base/conference.
  289. *
  290. * @param {Object} state - The Redux state of the feature base/conference.
  291. * @param {Action} action - The Redux action AUTH_STATUS_CHANGED to reduce.
  292. * @private
  293. * @returns {Object} The new state of the feature base/conference after the
  294. * reduction of the specified action.
  295. */
  296. function _authStatusChanged(state: IConferenceState,
  297. { authEnabled, authLogin }: { authEnabled: boolean; authLogin: string; }) {
  298. return assign(state, {
  299. authEnabled,
  300. authLogin
  301. });
  302. }
  303. /**
  304. * Reduces a specific Redux action CONFERENCE_FAILED of the feature
  305. * base/conference.
  306. *
  307. * @param {Object} state - The Redux state of the feature base/conference.
  308. * @param {Action} action - The Redux action CONFERENCE_FAILED to reduce.
  309. * @private
  310. * @returns {Object} The new state of the feature base/conference after the
  311. * reduction of the specified action.
  312. */
  313. function _conferenceFailed(state: IConferenceState, { conference, error }: {
  314. conference: IJitsiConference; error: IConferenceFailedError; }) {
  315. // The current (similar to getCurrentConference in
  316. // base/conference/functions.any.js) conference which is joining or joined:
  317. const conference_ = state.conference || state.joining;
  318. if (conference_ && conference_ !== conference) {
  319. return state;
  320. }
  321. let authRequired;
  322. let membersOnly;
  323. let passwordRequired;
  324. let lobbyWaitingForHost;
  325. switch (error.name) {
  326. case JitsiConferenceErrors.AUTHENTICATION_REQUIRED:
  327. authRequired = conference;
  328. break;
  329. case JitsiConferenceErrors.CONFERENCE_ACCESS_DENIED:
  330. case JitsiConferenceErrors.MEMBERS_ONLY_ERROR: {
  331. membersOnly = conference;
  332. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  333. const [ _lobbyJid, _lobbyWaitingForHost ] = error.params;
  334. lobbyWaitingForHost = _lobbyWaitingForHost;
  335. break;
  336. }
  337. case JitsiConferenceErrors.PASSWORD_REQUIRED:
  338. passwordRequired = conference;
  339. break;
  340. }
  341. return assign(state, {
  342. authRequired,
  343. conference: undefined,
  344. e2eeSupported: undefined,
  345. error,
  346. joining: undefined,
  347. leaving: undefined,
  348. lobbyWaitingForHost,
  349. /**
  350. * The indicator of how the conference/room is locked. If falsy, the
  351. * conference/room is unlocked; otherwise, it's either
  352. * {@code LOCKED_LOCALLY} or {@code LOCKED_REMOTELY}.
  353. *
  354. * @type {string}
  355. */
  356. locked: passwordRequired ? LOCKED_REMOTELY : undefined,
  357. membersOnly,
  358. password: undefined,
  359. /**
  360. * The JitsiConference instance which requires a password to join.
  361. *
  362. * @type {JitsiConference}
  363. */
  364. passwordRequired
  365. });
  366. }
  367. /**
  368. * Reduces a specific Redux action CONFERENCE_JOINED of the feature
  369. * base/conference.
  370. *
  371. * @param {Object} state - The Redux state of the feature base/conference.
  372. * @param {Action} action - The Redux action CONFERENCE_JOINED to reduce.
  373. * @private
  374. * @returns {Object} The new state of the feature base/conference after the
  375. * reduction of the specified action.
  376. */
  377. function _conferenceJoined(state: IConferenceState, { conference }: { conference: IJitsiConference; }) {
  378. // FIXME The indicator which determines whether a JitsiConference is locked
  379. // i.e. password-protected is private to lib-jitsi-meet. However, the
  380. // library does not fire LOCK_STATE_CHANGED upon joining a JitsiConference
  381. // with a password.
  382. // FIXME Technically JitsiConference.room is a private field.
  383. const locked = conference.room?.locked ? LOCKED_REMOTELY : undefined;
  384. return assign(state, {
  385. authRequired: undefined,
  386. /**
  387. * The JitsiConference instance represented by the Redux state of the
  388. * feature base/conference.
  389. *
  390. * @type {JitsiConference}
  391. */
  392. conference,
  393. e2eeSupported: conference.isE2EESupported(),
  394. joining: undefined,
  395. membersOnly: undefined,
  396. leaving: undefined,
  397. lobbyWaitingForHost: undefined,
  398. /**
  399. * The indicator which determines whether the conference is locked.
  400. *
  401. * @type {boolean}
  402. */
  403. locked,
  404. passwordRequired: undefined
  405. });
  406. }
  407. /**
  408. * Reduces a specific redux action {@link CONFERENCE_LEFT} or
  409. * {@link CONFERENCE_WILL_LEAVE} for the feature base/conference.
  410. *
  411. * @param {Object} state - The redux state of the feature base/conference.
  412. * @param {Action} action - The redux action {@code CONFERENCE_LEFT} or
  413. * {@code CONFERENCE_WILL_LEAVE} to reduce.
  414. * @private
  415. * @returns {Object} The next/new state of the feature base/conference after the
  416. * reduction of the specified action.
  417. */
  418. function _conferenceLeftOrWillLeave(state: IConferenceState, { conference, type }:
  419. { conference: IJitsiConference; type: string; }) {
  420. const nextState = { ...state };
  421. // The redux action CONFERENCE_LEFT is the last time that we should be
  422. // hearing from a JitsiConference instance.
  423. //
  424. // The redux action CONFERENCE_WILL_LEAVE represents the order of the user
  425. // to leave a JitsiConference instance. From the user's perspective, there's
  426. // no going back (with respect to the instance itself). The app will perform
  427. // due clean-up like leaving the associated room, but the instance is no
  428. // longer the focus of the attention of the user and, consequently, the app.
  429. for (const p in state) {
  430. if (state[p as keyof IConferenceState] === conference) {
  431. nextState[p as keyof IConferenceState] = undefined;
  432. switch (p) {
  433. case 'conference':
  434. case 'passwordRequired':
  435. // XXX Clear/unset locked & password for a conference which has
  436. // been LOCKED_LOCALLY or LOCKED_REMOTELY.
  437. delete nextState.locked;
  438. delete nextState.password;
  439. break;
  440. }
  441. }
  442. }
  443. if (type === CONFERENCE_WILL_LEAVE) {
  444. // A CONFERENCE_WILL_LEAVE is of further consequence only if it is
  445. // expected i.e. if the specified conference is joining or joined.
  446. if (conference === state.joining || conference === state.conference) {
  447. /**
  448. * The JitsiConference instance which is currently in the process of
  449. * being left.
  450. *
  451. * @type {JitsiConference}
  452. */
  453. nextState.leaving = conference;
  454. }
  455. }
  456. return nextState;
  457. }
  458. /**
  459. * Reduces a specific Redux action CONFERENCE_WILL_JOIN of the feature
  460. * base/conference.
  461. *
  462. * @param {Object} state - The Redux state of the feature base/conference.
  463. * @param {Action} action - The Redux action CONFERENCE_WILL_JOIN to reduce.
  464. * @private
  465. * @returns {Object} The new state of the feature base/conference after the
  466. * reduction of the specified action.
  467. */
  468. function _conferenceWillJoin(state: IConferenceState, { conference }: { conference: IJitsiConference; }) {
  469. return assign(state, {
  470. error: undefined,
  471. joining: conference
  472. });
  473. }
  474. /**
  475. * Reduces a specific Redux action LOCK_STATE_CHANGED of the feature
  476. * base/conference.
  477. *
  478. * @param {Object} state - The Redux state of the feature base/conference.
  479. * @param {Action} action - The Redux action LOCK_STATE_CHANGED to reduce.
  480. * @private
  481. * @returns {Object} The new state of the feature base/conference after the
  482. * reduction of the specified action.
  483. */
  484. function _lockStateChanged(state: IConferenceState, { conference, locked }: { conference: Object; locked: boolean; }) {
  485. if (state.conference !== conference) {
  486. return state;
  487. }
  488. return assign(state, {
  489. locked: locked ? state.locked || LOCKED_REMOTELY : undefined,
  490. password: locked ? state.password : undefined
  491. });
  492. }
  493. /**
  494. * Reduces a specific Redux action P2P_STATUS_CHANGED of the feature
  495. * base/conference.
  496. *
  497. * @param {Object} state - The Redux state of the feature base/conference.
  498. * @param {Action} action - The Redux action P2P_STATUS_CHANGED to reduce.
  499. * @private
  500. * @returns {Object} The new state of the feature base/conference after the
  501. * reduction of the specified action.
  502. */
  503. function _p2pStatusChanged(state: IConferenceState, action: AnyAction) {
  504. return set(state, 'p2p', action.p2p);
  505. }
  506. /**
  507. * Reduces a specific Redux action SET_PASSWORD of the feature base/conference.
  508. *
  509. * @param {Object} state - The Redux state of the feature base/conference.
  510. * @param {Action} action - The Redux action SET_PASSWORD to reduce.
  511. * @private
  512. * @returns {Object} The new state of the feature base/conference after the
  513. * reduction of the specified action.
  514. */
  515. function _setPassword(state: IConferenceState, { conference, method, password }: {
  516. conference: IJitsiConference; method: Object; password: string; }) {
  517. switch (method) {
  518. case conference.join:
  519. return assign(state, {
  520. // 1. The JitsiConference which transitions away from
  521. // passwordRequired MUST remain in the redux state
  522. // features/base/conference until it transitions into
  523. // conference; otherwise, there is a span of time during which
  524. // the redux state does not even know that there is a
  525. // JitsiConference whatsoever.
  526. //
  527. // 2. The redux action setPassword will attempt to join the
  528. // JitsiConference so joining is an appropriate transitional
  529. // redux state.
  530. //
  531. // 3. The redux action setPassword will perform the same check
  532. // before it proceeds with the re-join.
  533. joining: state.conference ? state.joining : conference,
  534. locked: LOCKED_REMOTELY,
  535. /**
  536. * The password with which the conference is to be joined.
  537. *
  538. * @type {string}
  539. */
  540. password
  541. });
  542. case conference.lock:
  543. return assign(state, {
  544. locked: password ? LOCKED_LOCALLY : undefined,
  545. password
  546. });
  547. }
  548. return state;
  549. }
  550. /**
  551. * Reduces a specific Redux action SET_ROOM of the feature base/conference.
  552. *
  553. * @param {Object} state - The Redux state of the feature base/conference.
  554. * @param {Action} action - The Redux action SET_ROOM to reduce.
  555. * @private
  556. * @returns {Object} The new state of the feature base/conference after the
  557. * reduction of the specified action.
  558. */
  559. function _setRoom(state: IConferenceState, action: AnyAction) {
  560. let { room } = action;
  561. if (!isRoomValid(room)) {
  562. // Technically, there are multiple values which don't represent valid
  563. // room names. Practically, each of them is as bad as the rest of them
  564. // because we can't use any of them to join a conference.
  565. room = undefined;
  566. }
  567. /**
  568. * The name of the room of the conference (to be) joined.
  569. *
  570. * @type {string}
  571. */
  572. return assign(state, {
  573. error: undefined,
  574. room
  575. });
  576. }