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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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. if (localParticipantId) {
  246. // We left the conference, the local participant must be updated.
  247. _e2eeUpdated(store, conference, localParticipantId, false);
  248. _raiseHandUpdated(store, conference, localParticipantId, false);
  249. }
  250. }
  251. }
  252. );
  253. /**
  254. * Handles a E2EE enabled status update.
  255. *
  256. * @param {Function} dispatch - The Redux dispatch function.
  257. * @param {Object} conference - The conference for which we got an update.
  258. * @param {string} participantId - The ID of the participant from which we got an update.
  259. * @param {boolean} newValue - The new value of the E2EE enabled status.
  260. * @returns {void}
  261. */
  262. function _e2eeUpdated({ dispatch }, conference, participantId, newValue) {
  263. const e2eeEnabled = newValue === 'true';
  264. dispatch(toggleE2EE(e2eeEnabled));
  265. dispatch(participantUpdated({
  266. conference,
  267. id: participantId,
  268. e2eeEnabled
  269. }));
  270. }
  271. /**
  272. * Initializes the local participant and signals that it joined.
  273. *
  274. * @private
  275. * @param {Store} store - The redux store.
  276. * @param {Dispatch} next - The redux dispatch function to dispatch the
  277. * specified action to the specified store.
  278. * @param {Action} action - The redux action which is being dispatched
  279. * in the specified store.
  280. * @private
  281. * @returns {Object} The value returned by {@code next(action)}.
  282. */
  283. function _localParticipantJoined({ getState, dispatch }, next, action) {
  284. const result = next(action);
  285. const settings = getState()['features/base/settings'];
  286. dispatch(localParticipantJoined({
  287. avatarURL: settings.avatarURL,
  288. email: settings.email,
  289. name: settings.displayName
  290. }));
  291. return result;
  292. }
  293. /**
  294. * Signals that the local participant has left.
  295. *
  296. * @param {Store} store - The redux store.
  297. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  298. * specified {@code action} into the specified {@code store}.
  299. * @param {Action} action - The redux action which is being dispatched in the
  300. * specified {@code store}.
  301. * @private
  302. * @returns {Object} The value returned by {@code next(action)}.
  303. */
  304. function _localParticipantLeft({ dispatch }, next, action) {
  305. const result = next(action);
  306. dispatch(localParticipantLeft());
  307. return result;
  308. }
  309. /**
  310. * Plays sounds when participants join/leave conference.
  311. *
  312. * @param {Store} store - The redux store.
  313. * @param {Action} action - The redux action. Should be either
  314. * {@link PARTICIPANT_JOINED} or {@link PARTICIPANT_LEFT}.
  315. * @private
  316. * @returns {void}
  317. */
  318. function _maybePlaySounds({ getState, dispatch }, action) {
  319. const state = getState();
  320. const { startAudioMuted, disableJoinLeaveSounds } = state['features/base/config'];
  321. const { soundsParticipantJoined: joinSound, soundsParticipantLeft: leftSound } = state['features/base/settings'];
  322. // If we have join/leave sounds disabled, don't play anything.
  323. if (disableJoinLeaveSounds) {
  324. return;
  325. }
  326. // We're not playing sounds for local participant
  327. // nor when the user is joining past the "startAudioMuted" limit.
  328. // The intention there was to not play user joined notification in big
  329. // conferences where 100th person is joining.
  330. if (!action.participant.local
  331. && (!startAudioMuted
  332. || getParticipantCount(state) < startAudioMuted)) {
  333. const { isReplacing, isReplaced } = action.participant;
  334. if (action.type === PARTICIPANT_JOINED) {
  335. if (!joinSound) {
  336. return;
  337. }
  338. const { presence } = action.participant;
  339. // The sounds for the poltergeist are handled by features/invite.
  340. if (presence !== INVITED && presence !== CALLING && !isReplacing) {
  341. dispatch(playSound(PARTICIPANT_JOINED_SOUND_ID));
  342. }
  343. } else if (action.type === PARTICIPANT_LEFT && !isReplaced && leftSound) {
  344. dispatch(playSound(PARTICIPANT_LEFT_SOUND_ID));
  345. }
  346. }
  347. }
  348. /**
  349. * Notifies the feature base/participants that the action
  350. * {@code PARTICIPANT_JOINED} or {@code PARTICIPANT_UPDATED} is being dispatched
  351. * within a specific redux store.
  352. *
  353. * @param {Store} store - The redux store in which the specified {@code action}
  354. * is being dispatched.
  355. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  356. * specified {@code action} in the specified {@code store}.
  357. * @param {Action} action - The redux action {@code PARTICIPANT_JOINED} or
  358. * {@code PARTICIPANT_UPDATED} which is being dispatched in the specified
  359. * {@code store}.
  360. * @private
  361. * @returns {Object} The value returned by {@code next(action)}.
  362. */
  363. function _participantJoinedOrUpdated(store, next, action) {
  364. const { dispatch, getState } = store;
  365. const { participant: { avatarURL, email, id, local, name, raisedHand } } = action;
  366. // Send an external update of the local participant's raised hand state
  367. // if a new raised hand state is defined in the action.
  368. if (typeof raisedHand !== 'undefined') {
  369. if (local) {
  370. const { conference } = getState()['features/base/conference'];
  371. // Send raisedHand signalling only if there is a change
  372. if (conference && raisedHand !== getLocalParticipant(getState()).raisedHand) {
  373. conference.setLocalParticipantProperty('raisedHand', raisedHand);
  374. }
  375. }
  376. }
  377. // Allow the redux update to go through and compare the old avatar
  378. // to the new avatar and emit out change events if necessary.
  379. const result = next(action);
  380. // Only run this if the config is populated, otherwise we preload external resources
  381. // even if disableThirdPartyRequests is set to true in config
  382. if (Object.keys(getState()['features/base/config']).length) {
  383. const { disableThirdPartyRequests } = getState()['features/base/config'];
  384. if (!disableThirdPartyRequests && (avatarURL || email || id || name)) {
  385. const participantId = !id && local ? getLocalParticipant(getState()).id : id;
  386. const updatedParticipant = getParticipantById(getState(), participantId);
  387. getFirstLoadableAvatarUrl(updatedParticipant, store)
  388. .then(url => {
  389. dispatch(setLoadableAvatarUrl(participantId, url));
  390. });
  391. }
  392. }
  393. // Notify external listeners of potential avatarURL changes.
  394. if (typeof APP === 'object') {
  395. const currentKnownId = local ? APP.conference.getMyUserId() : id;
  396. // Force update of local video getting a new id.
  397. APP.UI.refreshAvatarDisplay(currentKnownId);
  398. }
  399. return result;
  400. }
  401. /**
  402. * Handles a raise hand status update.
  403. *
  404. * @param {Function} dispatch - The Redux dispatch function.
  405. * @param {Object} conference - The conference for which we got an update.
  406. * @param {string} participantId - The ID of the participant from which we got an update.
  407. * @param {boolean} newValue - The new value of the raise hand status.
  408. * @returns {void}
  409. */
  410. function _raiseHandUpdated({ dispatch, getState }, conference, participantId, newValue) {
  411. const raisedHand = newValue === 'true';
  412. dispatch(participantUpdated({
  413. conference,
  414. id: participantId,
  415. raisedHand
  416. }));
  417. if (typeof APP !== 'undefined') {
  418. APP.API.notifyRaiseHandUpdated(participantId, raisedHand);
  419. }
  420. if (raisedHand) {
  421. dispatch(showNotification({
  422. titleArguments: {
  423. name: getParticipantDisplayName(getState, participantId)
  424. },
  425. titleKey: 'notify.raisedHand'
  426. }, NOTIFICATION_TIMEOUT));
  427. }
  428. }
  429. /**
  430. * Registers sounds related with the participants feature.
  431. *
  432. * @param {Store} store - The redux store.
  433. * @private
  434. * @returns {void}
  435. */
  436. function _registerSounds({ dispatch }) {
  437. dispatch(
  438. registerSound(PARTICIPANT_JOINED_SOUND_ID, PARTICIPANT_JOINED_FILE));
  439. dispatch(registerSound(PARTICIPANT_LEFT_SOUND_ID, PARTICIPANT_LEFT_FILE));
  440. }
  441. /**
  442. * Unregisters sounds related with the participants feature.
  443. *
  444. * @param {Store} store - The redux store.
  445. * @private
  446. * @returns {void}
  447. */
  448. function _unregisterSounds({ dispatch }) {
  449. dispatch(unregisterSound(PARTICIPANT_JOINED_SOUND_ID));
  450. dispatch(unregisterSound(PARTICIPANT_LEFT_SOUND_ID));
  451. }