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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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. sendReaction: Function;
  130. sendTextMessage: Function;
  131. sendTones: Function;
  132. sessionId: string;
  133. setAssumedBandwidthBps: (value: number) => void;
  134. setDesktopSharingFrameRate: Function;
  135. setDisplayName: Function;
  136. setIsSilent: Function;
  137. setLocalParticipantProperty: Function;
  138. setMediaEncryptionKey: Function;
  139. setReceiverConstraints: Function;
  140. setSenderVideoConstraint: Function;
  141. setStartMutedPolicy: Function;
  142. setSubject: Function;
  143. startRecording: Function;
  144. startVerification: Function;
  145. stopRecording: Function;
  146. toggleE2EE: Function;
  147. }
  148. export interface IConferenceState {
  149. assumedBandwidthBps?: number;
  150. authEnabled?: boolean;
  151. authLogin?: string;
  152. authRequired?: IJitsiConference;
  153. conference?: IJitsiConference;
  154. conferenceTimestamp?: number;
  155. dataChannelOpen?: boolean;
  156. e2eeSupported?: boolean;
  157. error?: Error;
  158. followMeEnabled?: boolean;
  159. followMeRecorderEnabled?: boolean;
  160. joining?: IJitsiConference;
  161. leaving?: IJitsiConference;
  162. lobbyWaitingForHost?: boolean;
  163. localSubject?: string;
  164. locked?: string;
  165. membersOnly?: IJitsiConference;
  166. metadata?: IConferenceMetadata;
  167. obfuscatedRoom?: string;
  168. obfuscatedRoomSource?: string;
  169. p2p?: Object;
  170. password?: string;
  171. passwordRequired?: IJitsiConference;
  172. pendingSubjectChange?: string;
  173. room?: string;
  174. startAudioMutedPolicy?: boolean;
  175. startReactionsMuted?: boolean;
  176. startVideoMutedPolicy?: boolean;
  177. subject?: string;
  178. }
  179. export interface IJitsiConferenceRoom {
  180. locked: boolean;
  181. myroomjid: string;
  182. roomjid: string;
  183. xmpp: {
  184. moderator: {
  185. logout: Function;
  186. };
  187. };
  188. }
  189. interface IConferenceFailedError extends Error {
  190. params: Array<any>;
  191. }
  192. /**
  193. * Listen for actions that contain the conference object, so that it can be
  194. * stored for use by other action creators.
  195. */
  196. ReducerRegistry.register<IConferenceState>('features/base/conference',
  197. (state = DEFAULT_STATE, action): IConferenceState => {
  198. switch (action.type) {
  199. case AUTH_STATUS_CHANGED:
  200. return _authStatusChanged(state, action);
  201. case CONFERENCE_FAILED:
  202. return _conferenceFailed(state, action);
  203. case CONFERENCE_JOINED:
  204. return _conferenceJoined(state, action);
  205. case CONFERENCE_SUBJECT_CHANGED:
  206. return set(state, 'subject', action.subject);
  207. case CONFERENCE_LOCAL_SUBJECT_CHANGED:
  208. return set(state, 'localSubject', action.localSubject);
  209. case CONFERENCE_TIMESTAMP_CHANGED:
  210. return set(state, 'conferenceTimestamp', action.conferenceTimestamp);
  211. case CONFERENCE_LEFT:
  212. case CONFERENCE_WILL_LEAVE:
  213. return _conferenceLeftOrWillLeave(state, action);
  214. case CONFERENCE_WILL_JOIN:
  215. return _conferenceWillJoin(state, action);
  216. case CONNECTION_WILL_CONNECT:
  217. return set(state, 'authRequired', undefined);
  218. case DATA_CHANNEL_CLOSED:
  219. return set(state, 'dataChannelOpen', false);
  220. case DATA_CHANNEL_OPENED:
  221. return set(state, 'dataChannelOpen', true);
  222. case LOCK_STATE_CHANGED:
  223. return _lockStateChanged(state, action);
  224. case P2P_STATUS_CHANGED:
  225. return _p2pStatusChanged(state, action);
  226. case SET_ASSUMED_BANDWIDTH_BPS: {
  227. const assumedBandwidthBps = action.assumedBandwidthBps >= 0
  228. ? Number(action.assumedBandwidthBps)
  229. : undefined;
  230. return set(state, 'assumedBandwidthBps', assumedBandwidthBps);
  231. }
  232. case SET_FOLLOW_ME:
  233. return set(state, 'followMeEnabled', action.enabled);
  234. case SET_FOLLOW_ME_RECORDER:
  235. return { ...state,
  236. followMeRecorderEnabled: action.enabled,
  237. followMeEnabled: action.enabled
  238. };
  239. case SET_START_REACTIONS_MUTED:
  240. return set(state, 'startReactionsMuted', action.muted);
  241. case SET_LOCATION_URL:
  242. return set(state, 'room', undefined);
  243. case SET_OBFUSCATED_ROOM:
  244. return { ...state,
  245. obfuscatedRoom: action.obfuscatedRoom,
  246. obfuscatedRoomSource: action.obfuscatedRoomSource
  247. };
  248. case SET_PASSWORD:
  249. return _setPassword(state, action);
  250. case SET_PENDING_SUBJECT_CHANGE:
  251. return set(state, 'pendingSubjectChange', action.subject);
  252. case SET_ROOM:
  253. return _setRoom(state, action);
  254. case SET_START_MUTED_POLICY:
  255. return {
  256. ...state,
  257. startAudioMutedPolicy: action.startAudioMutedPolicy,
  258. startVideoMutedPolicy: action.startVideoMutedPolicy
  259. };
  260. case UPDATE_CONFERENCE_METADATA:
  261. return {
  262. ...state,
  263. metadata: action.metadata
  264. };
  265. case SET_CONFIG:
  266. return _setConfig(state, action);
  267. }
  268. return state;
  269. });
  270. /**
  271. * Processes subject and local subject of the conference based on the new config.
  272. *
  273. * @param {Object} state - The Redux state of feature base/conference.
  274. * @param {Action} action - The Redux action SET_CONFIG to reduce.
  275. * @private
  276. * @returns {Object} The new state after the reduction of the specified action.
  277. */
  278. function _setConfig(state: IConferenceState, { config }: { config: IConfig; }) {
  279. const { localSubject, subject } = config;
  280. return {
  281. ...state,
  282. localSubject,
  283. pendingSubjectChange: subject,
  284. subject: undefined
  285. };
  286. }
  287. /**
  288. * Reduces a specific Redux action AUTH_STATUS_CHANGED of the feature
  289. * base/conference.
  290. *
  291. * @param {Object} state - The Redux state of the feature base/conference.
  292. * @param {Action} action - The Redux action AUTH_STATUS_CHANGED to reduce.
  293. * @private
  294. * @returns {Object} The new state of the feature base/conference after the
  295. * reduction of the specified action.
  296. */
  297. function _authStatusChanged(state: IConferenceState,
  298. { authEnabled, authLogin }: { authEnabled: boolean; authLogin: string; }) {
  299. return assign(state, {
  300. authEnabled,
  301. authLogin
  302. });
  303. }
  304. /**
  305. * Reduces a specific Redux action CONFERENCE_FAILED of the feature
  306. * base/conference.
  307. *
  308. * @param {Object} state - The Redux state of the feature base/conference.
  309. * @param {Action} action - The Redux action CONFERENCE_FAILED to reduce.
  310. * @private
  311. * @returns {Object} The new state of the feature base/conference after the
  312. * reduction of the specified action.
  313. */
  314. function _conferenceFailed(state: IConferenceState, { conference, error }: {
  315. conference: IJitsiConference; error: IConferenceFailedError; }) {
  316. // The current (similar to getCurrentConference in
  317. // base/conference/functions.any.js) conference which is joining or joined:
  318. const conference_ = state.conference || state.joining;
  319. if (conference_ && conference_ !== conference) {
  320. return state;
  321. }
  322. let authRequired;
  323. let membersOnly;
  324. let passwordRequired;
  325. let lobbyWaitingForHost;
  326. switch (error.name) {
  327. case JitsiConferenceErrors.AUTHENTICATION_REQUIRED:
  328. authRequired = conference;
  329. break;
  330. case JitsiConferenceErrors.CONFERENCE_ACCESS_DENIED:
  331. case JitsiConferenceErrors.MEMBERS_ONLY_ERROR: {
  332. membersOnly = conference;
  333. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  334. const [ _lobbyJid, _lobbyWaitingForHost ] = error.params;
  335. lobbyWaitingForHost = _lobbyWaitingForHost;
  336. break;
  337. }
  338. case JitsiConferenceErrors.PASSWORD_REQUIRED:
  339. passwordRequired = conference;
  340. break;
  341. }
  342. return assign(state, {
  343. authRequired,
  344. conference: undefined,
  345. e2eeSupported: undefined,
  346. error,
  347. joining: undefined,
  348. leaving: undefined,
  349. lobbyWaitingForHost,
  350. /**
  351. * The indicator of how the conference/room is locked. If falsy, the
  352. * conference/room is unlocked; otherwise, it's either
  353. * {@code LOCKED_LOCALLY} or {@code LOCKED_REMOTELY}.
  354. *
  355. * @type {string}
  356. */
  357. locked: passwordRequired ? LOCKED_REMOTELY : undefined,
  358. membersOnly,
  359. password: undefined,
  360. /**
  361. * The JitsiConference instance which requires a password to join.
  362. *
  363. * @type {JitsiConference}
  364. */
  365. passwordRequired
  366. });
  367. }
  368. /**
  369. * Reduces a specific Redux action CONFERENCE_JOINED of the feature
  370. * base/conference.
  371. *
  372. * @param {Object} state - The Redux state of the feature base/conference.
  373. * @param {Action} action - The Redux action CONFERENCE_JOINED to reduce.
  374. * @private
  375. * @returns {Object} The new state of the feature base/conference after the
  376. * reduction of the specified action.
  377. */
  378. function _conferenceJoined(state: IConferenceState, { conference }: { conference: IJitsiConference; }) {
  379. // FIXME The indicator which determines whether a JitsiConference is locked
  380. // i.e. password-protected is private to lib-jitsi-meet. However, the
  381. // library does not fire LOCK_STATE_CHANGED upon joining a JitsiConference
  382. // with a password.
  383. // FIXME Technically JitsiConference.room is a private field.
  384. const locked = conference.room?.locked ? LOCKED_REMOTELY : undefined;
  385. return assign(state, {
  386. authRequired: undefined,
  387. /**
  388. * The JitsiConference instance represented by the Redux state of the
  389. * feature base/conference.
  390. *
  391. * @type {JitsiConference}
  392. */
  393. conference,
  394. e2eeSupported: conference.isE2EESupported(),
  395. joining: undefined,
  396. membersOnly: undefined,
  397. leaving: undefined,
  398. lobbyWaitingForHost: undefined,
  399. /**
  400. * The indicator which determines whether the conference is locked.
  401. *
  402. * @type {boolean}
  403. */
  404. locked,
  405. passwordRequired: undefined
  406. });
  407. }
  408. /**
  409. * Reduces a specific redux action {@link CONFERENCE_LEFT} or
  410. * {@link CONFERENCE_WILL_LEAVE} for the feature base/conference.
  411. *
  412. * @param {Object} state - The redux state of the feature base/conference.
  413. * @param {Action} action - The redux action {@code CONFERENCE_LEFT} or
  414. * {@code CONFERENCE_WILL_LEAVE} to reduce.
  415. * @private
  416. * @returns {Object} The next/new state of the feature base/conference after the
  417. * reduction of the specified action.
  418. */
  419. function _conferenceLeftOrWillLeave(state: IConferenceState, { conference, type }:
  420. { conference: IJitsiConference; type: string; }) {
  421. const nextState = { ...state };
  422. // The redux action CONFERENCE_LEFT is the last time that we should be
  423. // hearing from a JitsiConference instance.
  424. //
  425. // The redux action CONFERENCE_WILL_LEAVE represents the order of the user
  426. // to leave a JitsiConference instance. From the user's perspective, there's
  427. // no going back (with respect to the instance itself). The app will perform
  428. // due clean-up like leaving the associated room, but the instance is no
  429. // longer the focus of the attention of the user and, consequently, the app.
  430. for (const p in state) {
  431. if (state[p as keyof IConferenceState] === conference) {
  432. nextState[p as keyof IConferenceState] = undefined;
  433. switch (p) {
  434. case 'conference':
  435. case 'passwordRequired':
  436. // XXX Clear/unset locked & password for a conference which has
  437. // been LOCKED_LOCALLY or LOCKED_REMOTELY.
  438. delete nextState.locked;
  439. delete nextState.password;
  440. break;
  441. }
  442. }
  443. }
  444. if (type === CONFERENCE_WILL_LEAVE) {
  445. // A CONFERENCE_WILL_LEAVE is of further consequence only if it is
  446. // expected i.e. if the specified conference is joining or joined.
  447. if (conference === state.joining || conference === state.conference) {
  448. /**
  449. * The JitsiConference instance which is currently in the process of
  450. * being left.
  451. *
  452. * @type {JitsiConference}
  453. */
  454. nextState.leaving = conference;
  455. }
  456. }
  457. return nextState;
  458. }
  459. /**
  460. * Reduces a specific Redux action CONFERENCE_WILL_JOIN of the feature
  461. * base/conference.
  462. *
  463. * @param {Object} state - The Redux state of the feature base/conference.
  464. * @param {Action} action - The Redux action CONFERENCE_WILL_JOIN to reduce.
  465. * @private
  466. * @returns {Object} The new state of the feature base/conference after the
  467. * reduction of the specified action.
  468. */
  469. function _conferenceWillJoin(state: IConferenceState, { conference }: { conference: IJitsiConference; }) {
  470. return assign(state, {
  471. error: undefined,
  472. joining: conference
  473. });
  474. }
  475. /**
  476. * Reduces a specific Redux action LOCK_STATE_CHANGED of the feature
  477. * base/conference.
  478. *
  479. * @param {Object} state - The Redux state of the feature base/conference.
  480. * @param {Action} action - The Redux action LOCK_STATE_CHANGED to reduce.
  481. * @private
  482. * @returns {Object} The new state of the feature base/conference after the
  483. * reduction of the specified action.
  484. */
  485. function _lockStateChanged(state: IConferenceState, { conference, locked }: { conference: Object; locked: boolean; }) {
  486. if (state.conference !== conference) {
  487. return state;
  488. }
  489. return assign(state, {
  490. locked: locked ? state.locked || LOCKED_REMOTELY : undefined,
  491. password: locked ? state.password : undefined
  492. });
  493. }
  494. /**
  495. * Reduces a specific Redux action P2P_STATUS_CHANGED of the feature
  496. * base/conference.
  497. *
  498. * @param {Object} state - The Redux state of the feature base/conference.
  499. * @param {Action} action - The Redux action P2P_STATUS_CHANGED to reduce.
  500. * @private
  501. * @returns {Object} The new state of the feature base/conference after the
  502. * reduction of the specified action.
  503. */
  504. function _p2pStatusChanged(state: IConferenceState, action: AnyAction) {
  505. return set(state, 'p2p', action.p2p);
  506. }
  507. /**
  508. * Reduces a specific Redux action SET_PASSWORD of the feature base/conference.
  509. *
  510. * @param {Object} state - The Redux state of the feature base/conference.
  511. * @param {Action} action - The Redux action SET_PASSWORD to reduce.
  512. * @private
  513. * @returns {Object} The new state of the feature base/conference after the
  514. * reduction of the specified action.
  515. */
  516. function _setPassword(state: IConferenceState, { conference, method, password }: {
  517. conference: IJitsiConference; method: Object; password: string; }) {
  518. switch (method) {
  519. case conference.join:
  520. return assign(state, {
  521. // 1. The JitsiConference which transitions away from
  522. // passwordRequired MUST remain in the redux state
  523. // features/base/conference until it transitions into
  524. // conference; otherwise, there is a span of time during which
  525. // the redux state does not even know that there is a
  526. // JitsiConference whatsoever.
  527. //
  528. // 2. The redux action setPassword will attempt to join the
  529. // JitsiConference so joining is an appropriate transitional
  530. // redux state.
  531. //
  532. // 3. The redux action setPassword will perform the same check
  533. // before it proceeds with the re-join.
  534. joining: state.conference ? state.joining : conference,
  535. locked: LOCKED_REMOTELY,
  536. /**
  537. * The password with which the conference is to be joined.
  538. *
  539. * @type {string}
  540. */
  541. password
  542. });
  543. case conference.lock:
  544. return assign(state, {
  545. locked: password ? LOCKED_LOCALLY : undefined,
  546. password
  547. });
  548. }
  549. return state;
  550. }
  551. /**
  552. * Reduces a specific Redux action SET_ROOM of the feature base/conference.
  553. *
  554. * @param {Object} state - The Redux state of the feature base/conference.
  555. * @param {Action} action - The Redux action SET_ROOM to reduce.
  556. * @private
  557. * @returns {Object} The new state of the feature base/conference after the
  558. * reduction of the specified action.
  559. */
  560. function _setRoom(state: IConferenceState, action: AnyAction) {
  561. let { room } = action;
  562. if (!isRoomValid(room)) {
  563. // Technically, there are multiple values which don't represent valid
  564. // room names. Practically, each of them is as bad as the rest of them
  565. // because we can't use any of them to join a conference.
  566. room = undefined;
  567. }
  568. /**
  569. * The name of the room of the conference (to be) joined.
  570. *
  571. * @type {string}
  572. */
  573. return assign(state, {
  574. error: undefined,
  575. room
  576. });
  577. }