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.

middleware.js 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. // @flow
  2. import { batch } from 'react-redux';
  3. import UIEvents from '../../../../service/UI/UIEvents';
  4. import { toggleE2EE } from '../../e2ee/actions';
  5. import { NOTIFICATION_TIMEOUT, showNotification } from '../../notifications';
  6. import { CALLING, INVITED } from '../../presence-status';
  7. import { APP_WILL_MOUNT, APP_WILL_UNMOUNT } from '../app';
  8. import {
  9. CONFERENCE_WILL_JOIN,
  10. forEachConference,
  11. getCurrentConference
  12. } from '../conference';
  13. import { JitsiConferenceEvents } from '../lib-jitsi-meet';
  14. import { MiddlewareRegistry, StateListenerRegistry } from '../redux';
  15. import { playSound, registerSound, unregisterSound } from '../sounds';
  16. import {
  17. DOMINANT_SPEAKER_CHANGED,
  18. GRANT_MODERATOR,
  19. KICK_PARTICIPANT,
  20. LOCAL_PARTICIPANT_RAISE_HAND,
  21. MUTE_REMOTE_PARTICIPANT,
  22. PARTICIPANT_DISPLAY_NAME_CHANGED,
  23. PARTICIPANT_JOINED,
  24. PARTICIPANT_LEFT,
  25. PARTICIPANT_UPDATED
  26. } from './actionTypes';
  27. import {
  28. localParticipantIdChanged,
  29. localParticipantJoined,
  30. localParticipantLeft,
  31. participantLeft,
  32. participantUpdated,
  33. setLoadableAvatarUrl
  34. } from './actions';
  35. import {
  36. LOCAL_PARTICIPANT_DEFAULT_ID,
  37. PARTICIPANT_JOINED_SOUND_ID,
  38. PARTICIPANT_LEFT_SOUND_ID
  39. } from './constants';
  40. import {
  41. getFirstLoadableAvatarUrl,
  42. getLocalParticipant,
  43. getParticipantById,
  44. getParticipantCount,
  45. getParticipantDisplayName,
  46. getRemoteParticipants
  47. } from './functions';
  48. import { PARTICIPANT_JOINED_FILE, PARTICIPANT_LEFT_FILE } from './sounds';
  49. declare var APP: Object;
  50. /**
  51. * Middleware that captures CONFERENCE_JOINED and CONFERENCE_LEFT actions and
  52. * updates respectively ID of local participant.
  53. *
  54. * @param {Store} store - The redux store.
  55. * @returns {Function}
  56. */
  57. MiddlewareRegistry.register(store => next => action => {
  58. switch (action.type) {
  59. case APP_WILL_MOUNT:
  60. _registerSounds(store);
  61. return _localParticipantJoined(store, next, action);
  62. case APP_WILL_UNMOUNT:
  63. _unregisterSounds(store);
  64. return _localParticipantLeft(store, next, action);
  65. case CONFERENCE_WILL_JOIN:
  66. store.dispatch(localParticipantIdChanged(action.conference.myUserId()));
  67. break;
  68. case DOMINANT_SPEAKER_CHANGED: {
  69. // Ensure the raised hand state is cleared for the dominant speaker
  70. // and only if it was set when this is the local participant
  71. const { conference, id } = action.participant;
  72. const participant = getLocalParticipant(store.getState());
  73. const isLocal = participant && participant.id === id;
  74. if (isLocal && participant.raisedHand === undefined) {
  75. // if local was undefined, let's leave it like that
  76. // avoids sending unnecessary presence updates
  77. break;
  78. }
  79. participant
  80. && store.dispatch(participantUpdated({
  81. conference,
  82. id,
  83. local: isLocal,
  84. raisedHand: false
  85. }));
  86. break;
  87. }
  88. case GRANT_MODERATOR: {
  89. const { conference } = store.getState()['features/base/conference'];
  90. conference.grantOwner(action.id);
  91. break;
  92. }
  93. case KICK_PARTICIPANT: {
  94. const { conference } = store.getState()['features/base/conference'];
  95. conference.kickParticipant(action.id);
  96. break;
  97. }
  98. case LOCAL_PARTICIPANT_RAISE_HAND: {
  99. const { enabled } = action;
  100. const localId = getLocalParticipant(store.getState())?.id;
  101. store.dispatch(participantUpdated({
  102. // XXX Only the local participant is allowed to update without
  103. // stating the JitsiConference instance (i.e. participant property
  104. // `conference` for a remote participant) because the local
  105. // participant is uniquely identified by the very fact that there is
  106. // only one local participant.
  107. id: localId,
  108. local: true,
  109. raisedHand: enabled
  110. }));
  111. if (typeof APP !== 'undefined') {
  112. APP.API.notifyRaiseHandUpdated(localId, enabled);
  113. }
  114. break;
  115. }
  116. case MUTE_REMOTE_PARTICIPANT: {
  117. const { conference } = store.getState()['features/base/conference'];
  118. conference.muteParticipant(action.id, action.mediaType);
  119. break;
  120. }
  121. // TODO Remove this middleware when the local display name update flow is
  122. // fully brought into redux.
  123. case PARTICIPANT_DISPLAY_NAME_CHANGED: {
  124. if (typeof APP !== 'undefined') {
  125. const participant = getLocalParticipant(store.getState());
  126. if (participant && participant.id === action.id) {
  127. APP.UI.emitEvent(UIEvents.NICKNAME_CHANGED, action.name);
  128. }
  129. }
  130. break;
  131. }
  132. case PARTICIPANT_JOINED: {
  133. _maybePlaySounds(store, action);
  134. return _participantJoinedOrUpdated(store, next, action);
  135. }
  136. case PARTICIPANT_LEFT:
  137. _maybePlaySounds(store, action);
  138. break;
  139. case PARTICIPANT_UPDATED:
  140. return _participantJoinedOrUpdated(store, next, action);
  141. }
  142. return next(action);
  143. });
  144. /**
  145. * Syncs the redux state features/base/participants up with the redux state
  146. * features/base/conference by ensuring that the former does not contain remote
  147. * participants no longer relevant to the latter. Introduced to address an issue
  148. * with multiplying thumbnails in the filmstrip.
  149. */
  150. StateListenerRegistry.register(
  151. /* selector */ state => getCurrentConference(state),
  152. /* listener */ (conference, { dispatch, getState }) => {
  153. batch(() => {
  154. for (const [ id, p ] of getRemoteParticipants(getState())) {
  155. (!conference || p.conference !== conference)
  156. && dispatch(participantLeft(id, p.conference, p.isReplaced));
  157. }
  158. });
  159. });
  160. /**
  161. * Reset the ID of the local participant to
  162. * {@link LOCAL_PARTICIPANT_DEFAULT_ID}. Such a reset is deemed possible only if
  163. * the local participant and, respectively, her ID is not involved in a
  164. * conference which is still of interest to the user and, consequently, the app.
  165. * For example, a conference which is in the process of leaving is no longer of
  166. * interest the user, is unrecoverable from the perspective of the user and,
  167. * consequently, the app.
  168. */
  169. StateListenerRegistry.register(
  170. /* selector */ state => state['features/base/conference'],
  171. /* listener */ ({ leaving }, { dispatch, getState }) => {
  172. const state = getState();
  173. const localParticipant = getLocalParticipant(state);
  174. let id;
  175. if (!localParticipant
  176. || (id = localParticipant.id)
  177. === LOCAL_PARTICIPANT_DEFAULT_ID) {
  178. // The ID of the local participant has been reset already.
  179. return;
  180. }
  181. // The ID of the local may be reset only if it is not in use.
  182. const dispatchLocalParticipantIdChanged
  183. = forEachConference(
  184. state,
  185. conference =>
  186. conference === leaving || conference.myUserId() !== id);
  187. dispatchLocalParticipantIdChanged
  188. && dispatch(
  189. localParticipantIdChanged(LOCAL_PARTICIPANT_DEFAULT_ID));
  190. });
  191. /**
  192. * Registers listeners for participant change events.
  193. */
  194. StateListenerRegistry.register(
  195. state => state['features/base/conference'].conference,
  196. (conference, store) => {
  197. if (conference) {
  198. const propertyHandlers = {
  199. 'e2ee.enabled': (participant, value) => _e2eeUpdated(store, conference, participant.getId(), value),
  200. 'features_e2ee': (participant, value) =>
  201. store.dispatch(participantUpdated({
  202. conference,
  203. id: participant.getId(),
  204. e2eeSupported: value
  205. })),
  206. 'features_jigasi': (participant, value) =>
  207. store.dispatch(participantUpdated({
  208. conference,
  209. id: participant.getId(),
  210. isJigasi: value
  211. })),
  212. 'features_screen-sharing': (participant, value) => // eslint-disable-line no-unused-vars
  213. store.dispatch(participantUpdated({
  214. conference,
  215. id: participant.getId(),
  216. features: { 'screen-sharing': true }
  217. })),
  218. 'raisedHand': (participant, value) => _raiseHandUpdated(store, conference, participant.getId(), value),
  219. 'remoteControlSessionStatus': (participant, value) =>
  220. store.dispatch(participantUpdated({
  221. conference,
  222. id: participant.getId(),
  223. remoteControlSessionStatus: value
  224. }))
  225. };
  226. // update properties for the participants that are already in the conference
  227. conference.getParticipants().forEach(participant => {
  228. Object.keys(propertyHandlers).forEach(propertyName => {
  229. const value = participant.getProperty(propertyName);
  230. if (value !== undefined) {
  231. propertyHandlers[propertyName](participant, value);
  232. }
  233. });
  234. });
  235. // We joined a conference
  236. conference.on(
  237. JitsiConferenceEvents.PARTICIPANT_PROPERTY_CHANGED,
  238. (participant, propertyName, oldValue, newValue) => {
  239. if (propertyHandlers.hasOwnProperty(propertyName)) {
  240. propertyHandlers[propertyName](participant, newValue);
  241. }
  242. });
  243. } else {
  244. const localParticipantId = getLocalParticipant(store.getState).id;
  245. // We left the conference, the local participant must be updated.
  246. _e2eeUpdated(store, conference, localParticipantId, false);
  247. _raiseHandUpdated(store, conference, localParticipantId, false);
  248. }
  249. }
  250. );
  251. /**
  252. * Handles a E2EE enabled status update.
  253. *
  254. * @param {Function} dispatch - The Redux dispatch function.
  255. * @param {Object} conference - The conference for which we got an update.
  256. * @param {string} participantId - The ID of the participant from which we got an update.
  257. * @param {boolean} newValue - The new value of the E2EE enabled status.
  258. * @returns {void}
  259. */
  260. function _e2eeUpdated({ dispatch }, conference, participantId, newValue) {
  261. const e2eeEnabled = newValue === 'true';
  262. dispatch(toggleE2EE(e2eeEnabled));
  263. dispatch(participantUpdated({
  264. conference,
  265. id: participantId,
  266. e2eeEnabled
  267. }));
  268. }
  269. /**
  270. * Initializes the local participant and signals that it joined.
  271. *
  272. * @private
  273. * @param {Store} store - The redux store.
  274. * @param {Dispatch} next - The redux dispatch function to dispatch the
  275. * specified action to the specified store.
  276. * @param {Action} action - The redux action which is being dispatched
  277. * in the specified store.
  278. * @private
  279. * @returns {Object} The value returned by {@code next(action)}.
  280. */
  281. function _localParticipantJoined({ getState, dispatch }, next, action) {
  282. const result = next(action);
  283. const settings = getState()['features/base/settings'];
  284. dispatch(localParticipantJoined({
  285. avatarURL: settings.avatarURL,
  286. email: settings.email,
  287. name: settings.displayName
  288. }));
  289. return result;
  290. }
  291. /**
  292. * Signals that the local participant has left.
  293. *
  294. * @param {Store} store - The redux store.
  295. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  296. * specified {@code action} into the specified {@code store}.
  297. * @param {Action} action - The redux action which is being dispatched in the
  298. * specified {@code store}.
  299. * @private
  300. * @returns {Object} The value returned by {@code next(action)}.
  301. */
  302. function _localParticipantLeft({ dispatch }, next, action) {
  303. const result = next(action);
  304. dispatch(localParticipantLeft());
  305. return result;
  306. }
  307. /**
  308. * Plays sounds when participants join/leave conference.
  309. *
  310. * @param {Store} store - The redux store.
  311. * @param {Action} action - The redux action. Should be either
  312. * {@link PARTICIPANT_JOINED} or {@link PARTICIPANT_LEFT}.
  313. * @private
  314. * @returns {void}
  315. */
  316. function _maybePlaySounds({ getState, dispatch }, action) {
  317. const state = getState();
  318. const { startAudioMuted, disableJoinLeaveSounds } = state['features/base/config'];
  319. const { soundsParticipantJoined: joinSound, soundsParticipantLeft: leftSound } = state['features/base/settings'];
  320. // If we have join/leave sounds disabled, don't play anything.
  321. if (disableJoinLeaveSounds) {
  322. return;
  323. }
  324. // We're not playing sounds for local participant
  325. // nor when the user is joining past the "startAudioMuted" limit.
  326. // The intention there was to not play user joined notification in big
  327. // conferences where 100th person is joining.
  328. if (!action.participant.local
  329. && (!startAudioMuted
  330. || getParticipantCount(state) < startAudioMuted)) {
  331. const { isReplacing, isReplaced } = action.participant;
  332. if (action.type === PARTICIPANT_JOINED) {
  333. if (!joinSound) {
  334. return;
  335. }
  336. const { presence } = action.participant;
  337. // The sounds for the poltergeist are handled by features/invite.
  338. if (presence !== INVITED && presence !== CALLING && !isReplacing) {
  339. dispatch(playSound(PARTICIPANT_JOINED_SOUND_ID));
  340. }
  341. } else if (action.type === PARTICIPANT_LEFT && !isReplaced && leftSound) {
  342. dispatch(playSound(PARTICIPANT_LEFT_SOUND_ID));
  343. }
  344. }
  345. }
  346. /**
  347. * Notifies the feature base/participants that the action
  348. * {@code PARTICIPANT_JOINED} or {@code PARTICIPANT_UPDATED} is being dispatched
  349. * within a specific redux store.
  350. *
  351. * @param {Store} store - The redux store in which the specified {@code action}
  352. * is being dispatched.
  353. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  354. * specified {@code action} in the specified {@code store}.
  355. * @param {Action} action - The redux action {@code PARTICIPANT_JOINED} or
  356. * {@code PARTICIPANT_UPDATED} which is being dispatched in the specified
  357. * {@code store}.
  358. * @private
  359. * @returns {Object} The value returned by {@code next(action)}.
  360. */
  361. function _participantJoinedOrUpdated(store, next, action) {
  362. const { dispatch, getState } = store;
  363. const { participant: { avatarURL, email, id, local, name, raisedHand } } = action;
  364. // Send an external update of the local participant's raised hand state
  365. // if a new raised hand state is defined in the action.
  366. if (typeof raisedHand !== 'undefined') {
  367. if (local) {
  368. const { conference } = getState()['features/base/conference'];
  369. // Send raisedHand signalling only if there is a change
  370. if (conference && raisedHand !== getLocalParticipant(getState()).raisedHand) {
  371. conference.setLocalParticipantProperty('raisedHand', raisedHand);
  372. }
  373. }
  374. }
  375. // Allow the redux update to go through and compare the old avatar
  376. // to the new avatar and emit out change events if necessary.
  377. const result = next(action);
  378. // Only run this if the config is populated, otherwise we preload external resources
  379. // even if disableThirdPartyRequests is set to true in config
  380. if (Object.keys(getState()['features/base/config']).length) {
  381. const { disableThirdPartyRequests } = getState()['features/base/config'];
  382. if (!disableThirdPartyRequests && (avatarURL || email || id || name)) {
  383. const participantId = !id && local ? getLocalParticipant(getState()).id : id;
  384. const updatedParticipant = getParticipantById(getState(), participantId);
  385. getFirstLoadableAvatarUrl(updatedParticipant, store)
  386. .then(url => {
  387. dispatch(setLoadableAvatarUrl(participantId, url));
  388. });
  389. }
  390. }
  391. // Notify external listeners of potential avatarURL changes.
  392. if (typeof APP === 'object') {
  393. const currentKnownId = local ? APP.conference.getMyUserId() : id;
  394. // Force update of local video getting a new id.
  395. APP.UI.refreshAvatarDisplay(currentKnownId);
  396. }
  397. return result;
  398. }
  399. /**
  400. * Handles a raise hand status update.
  401. *
  402. * @param {Function} dispatch - The Redux dispatch function.
  403. * @param {Object} conference - The conference for which we got an update.
  404. * @param {string} participantId - The ID of the participant from which we got an update.
  405. * @param {boolean} newValue - The new value of the raise hand status.
  406. * @returns {void}
  407. */
  408. function _raiseHandUpdated({ dispatch, getState }, conference, participantId, newValue) {
  409. const raisedHand = newValue === 'true';
  410. dispatch(participantUpdated({
  411. conference,
  412. id: participantId,
  413. raisedHand
  414. }));
  415. if (typeof APP !== 'undefined') {
  416. APP.API.notifyRaiseHandUpdated(participantId, raisedHand);
  417. }
  418. if (raisedHand) {
  419. dispatch(showNotification({
  420. titleArguments: {
  421. name: getParticipantDisplayName(getState, participantId)
  422. },
  423. titleKey: 'notify.raisedHand'
  424. }, NOTIFICATION_TIMEOUT));
  425. }
  426. }
  427. /**
  428. * Registers sounds related with the participants feature.
  429. *
  430. * @param {Store} store - The redux store.
  431. * @private
  432. * @returns {void}
  433. */
  434. function _registerSounds({ dispatch }) {
  435. dispatch(
  436. registerSound(PARTICIPANT_JOINED_SOUND_ID, PARTICIPANT_JOINED_FILE));
  437. dispatch(registerSound(PARTICIPANT_LEFT_SOUND_ID, PARTICIPANT_LEFT_FILE));
  438. }
  439. /**
  440. * Unregisters sounds related with the participants feature.
  441. *
  442. * @param {Store} store - The redux store.
  443. * @private
  444. * @returns {void}
  445. */
  446. function _unregisterSounds({ dispatch }) {
  447. dispatch(unregisterSound(PARTICIPANT_JOINED_SOUND_ID));
  448. dispatch(unregisterSound(PARTICIPANT_LEFT_SOUND_ID));
  449. }