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 { RAISE_HAND_SOUND_ID } from '../../reactions/constants';
  8. import { APP_WILL_MOUNT, APP_WILL_UNMOUNT } from '../app';
  9. import {
  10. CONFERENCE_WILL_JOIN,
  11. forEachConference,
  12. getCurrentConference
  13. } from '../conference';
  14. import { getDisableRemoveRaisedHandOnFocus } from '../config/functions.any';
  15. import { JitsiConferenceEvents } from '../lib-jitsi-meet';
  16. import { MiddlewareRegistry, StateListenerRegistry } from '../redux';
  17. import { playSound, registerSound, unregisterSound } from '../sounds';
  18. import {
  19. DOMINANT_SPEAKER_CHANGED,
  20. GRANT_MODERATOR,
  21. KICK_PARTICIPANT,
  22. LOCAL_PARTICIPANT_RAISE_HAND,
  23. MUTE_REMOTE_PARTICIPANT,
  24. PARTICIPANT_DISPLAY_NAME_CHANGED,
  25. PARTICIPANT_JOINED,
  26. PARTICIPANT_LEFT,
  27. PARTICIPANT_UPDATED
  28. } from './actionTypes';
  29. import {
  30. localParticipantIdChanged,
  31. localParticipantJoined,
  32. localParticipantLeft,
  33. participantLeft,
  34. participantUpdated,
  35. setLoadableAvatarUrl
  36. } from './actions';
  37. import {
  38. LOCAL_PARTICIPANT_DEFAULT_ID,
  39. PARTICIPANT_JOINED_SOUND_ID,
  40. PARTICIPANT_LEFT_SOUND_ID
  41. } from './constants';
  42. import {
  43. getFirstLoadableAvatarUrl,
  44. getLocalParticipant,
  45. getParticipantById,
  46. getParticipantCount,
  47. getParticipantDisplayName,
  48. getRemoteParticipants
  49. } from './functions';
  50. import { PARTICIPANT_JOINED_FILE, PARTICIPANT_LEFT_FILE } from './sounds';
  51. declare var APP: Object;
  52. /**
  53. * Middleware that captures CONFERENCE_JOINED and CONFERENCE_LEFT actions and
  54. * updates respectively ID of local participant.
  55. *
  56. * @param {Store} store - The redux store.
  57. * @returns {Function}
  58. */
  59. MiddlewareRegistry.register(store => next => action => {
  60. switch (action.type) {
  61. case APP_WILL_MOUNT:
  62. _registerSounds(store);
  63. return _localParticipantJoined(store, next, action);
  64. case APP_WILL_UNMOUNT:
  65. _unregisterSounds(store);
  66. return _localParticipantLeft(store, next, action);
  67. case CONFERENCE_WILL_JOIN:
  68. store.dispatch(localParticipantIdChanged(action.conference.myUserId()));
  69. break;
  70. case DOMINANT_SPEAKER_CHANGED: {
  71. // Ensure the raised hand state is cleared for the dominant speaker
  72. // and only if it was set when this is the local participant
  73. const { conference, id } = action.participant;
  74. const state = store.getState();
  75. const participant = getLocalParticipant(state);
  76. const isLocal = participant && participant.id === id;
  77. if (isLocal && participant.raisedHand === undefined) {
  78. // if local was undefined, let's leave it like that
  79. // avoids sending unnecessary presence updates
  80. break;
  81. }
  82. if (!getDisableRemoveRaisedHandOnFocus(state)) {
  83. participant && store.dispatch(participantUpdated({
  84. conference,
  85. id,
  86. local: isLocal,
  87. raisedHand: false
  88. }));
  89. }
  90. break;
  91. }
  92. case GRANT_MODERATOR: {
  93. const { conference } = store.getState()['features/base/conference'];
  94. conference.grantOwner(action.id);
  95. break;
  96. }
  97. case KICK_PARTICIPANT: {
  98. const { conference } = store.getState()['features/base/conference'];
  99. conference.kickParticipant(action.id);
  100. break;
  101. }
  102. case LOCAL_PARTICIPANT_RAISE_HAND: {
  103. const { enabled } = action;
  104. const localId = getLocalParticipant(store.getState())?.id;
  105. store.dispatch(participantUpdated({
  106. // XXX Only the local participant is allowed to update without
  107. // stating the JitsiConference instance (i.e. participant property
  108. // `conference` for a remote participant) because the local
  109. // participant is uniquely identified by the very fact that there is
  110. // only one local participant.
  111. id: localId,
  112. local: true,
  113. raisedHand: enabled
  114. }));
  115. if (typeof APP !== 'undefined') {
  116. APP.API.notifyRaiseHandUpdated(localId, enabled);
  117. }
  118. break;
  119. }
  120. case MUTE_REMOTE_PARTICIPANT: {
  121. const { conference } = store.getState()['features/base/conference'];
  122. conference.muteParticipant(action.id, action.mediaType);
  123. break;
  124. }
  125. // TODO Remove this middleware when the local display name update flow is
  126. // fully brought into redux.
  127. case PARTICIPANT_DISPLAY_NAME_CHANGED: {
  128. if (typeof APP !== 'undefined') {
  129. const participant = getLocalParticipant(store.getState());
  130. if (participant && participant.id === action.id) {
  131. APP.UI.emitEvent(UIEvents.NICKNAME_CHANGED, action.name);
  132. }
  133. }
  134. break;
  135. }
  136. case PARTICIPANT_JOINED: {
  137. _maybePlaySounds(store, action);
  138. return _participantJoinedOrUpdated(store, next, action);
  139. }
  140. case PARTICIPANT_LEFT:
  141. _maybePlaySounds(store, action);
  142. break;
  143. case PARTICIPANT_UPDATED:
  144. return _participantJoinedOrUpdated(store, next, action);
  145. }
  146. return next(action);
  147. });
  148. /**
  149. * Syncs the redux state features/base/participants up with the redux state
  150. * features/base/conference by ensuring that the former does not contain remote
  151. * participants no longer relevant to the latter. Introduced to address an issue
  152. * with multiplying thumbnails in the filmstrip.
  153. */
  154. StateListenerRegistry.register(
  155. /* selector */ state => getCurrentConference(state),
  156. /* listener */ (conference, { dispatch, getState }) => {
  157. batch(() => {
  158. for (const [ id, p ] of getRemoteParticipants(getState())) {
  159. (!conference || p.conference !== conference)
  160. && dispatch(participantLeft(id, p.conference, p.isReplaced));
  161. }
  162. });
  163. });
  164. /**
  165. * Reset the ID of the local participant to
  166. * {@link LOCAL_PARTICIPANT_DEFAULT_ID}. Such a reset is deemed possible only if
  167. * the local participant and, respectively, her ID is not involved in a
  168. * conference which is still of interest to the user and, consequently, the app.
  169. * For example, a conference which is in the process of leaving is no longer of
  170. * interest the user, is unrecoverable from the perspective of the user and,
  171. * consequently, the app.
  172. */
  173. StateListenerRegistry.register(
  174. /* selector */ state => state['features/base/conference'],
  175. /* listener */ ({ leaving }, { dispatch, getState }) => {
  176. const state = getState();
  177. const localParticipant = getLocalParticipant(state);
  178. let id;
  179. if (!localParticipant
  180. || (id = localParticipant.id)
  181. === LOCAL_PARTICIPANT_DEFAULT_ID) {
  182. // The ID of the local participant has been reset already.
  183. return;
  184. }
  185. // The ID of the local may be reset only if it is not in use.
  186. const dispatchLocalParticipantIdChanged
  187. = forEachConference(
  188. state,
  189. conference =>
  190. conference === leaving || conference.myUserId() !== id);
  191. dispatchLocalParticipantIdChanged
  192. && dispatch(
  193. localParticipantIdChanged(LOCAL_PARTICIPANT_DEFAULT_ID));
  194. });
  195. /**
  196. * Registers listeners for participant change events.
  197. */
  198. StateListenerRegistry.register(
  199. state => state['features/base/conference'].conference,
  200. (conference, store) => {
  201. if (conference) {
  202. const propertyHandlers = {
  203. 'e2ee.enabled': (participant, value) => _e2eeUpdated(store, conference, participant.getId(), value),
  204. 'features_e2ee': (participant, value) =>
  205. store.dispatch(participantUpdated({
  206. conference,
  207. id: participant.getId(),
  208. e2eeSupported: value
  209. })),
  210. 'features_jigasi': (participant, value) =>
  211. store.dispatch(participantUpdated({
  212. conference,
  213. id: participant.getId(),
  214. isJigasi: value
  215. })),
  216. 'features_screen-sharing': (participant, value) => // eslint-disable-line no-unused-vars
  217. store.dispatch(participantUpdated({
  218. conference,
  219. id: participant.getId(),
  220. features: { 'screen-sharing': true }
  221. })),
  222. 'raisedHand': (participant, value) => _raiseHandUpdated(store, conference, participant.getId(), value),
  223. 'remoteControlSessionStatus': (participant, value) =>
  224. store.dispatch(participantUpdated({
  225. conference,
  226. id: participant.getId(),
  227. remoteControlSessionStatus: value
  228. }))
  229. };
  230. // update properties for the participants that are already in the conference
  231. conference.getParticipants().forEach(participant => {
  232. Object.keys(propertyHandlers).forEach(propertyName => {
  233. const value = participant.getProperty(propertyName);
  234. if (value !== undefined) {
  235. propertyHandlers[propertyName](participant, value);
  236. }
  237. });
  238. });
  239. // We joined a conference
  240. conference.on(
  241. JitsiConferenceEvents.PARTICIPANT_PROPERTY_CHANGED,
  242. (participant, propertyName, oldValue, newValue) => {
  243. if (propertyHandlers.hasOwnProperty(propertyName)) {
  244. propertyHandlers[propertyName](participant, newValue);
  245. }
  246. });
  247. } else {
  248. const localParticipantId = getLocalParticipant(store.getState).id;
  249. // We left the conference, the local participant must be updated.
  250. _e2eeUpdated(store, conference, localParticipantId, false);
  251. _raiseHandUpdated(store, conference, localParticipantId, false);
  252. }
  253. }
  254. );
  255. /**
  256. * Handles a E2EE enabled status update.
  257. *
  258. * @param {Function} dispatch - The Redux dispatch function.
  259. * @param {Object} conference - The conference for which we got an update.
  260. * @param {string} participantId - The ID of the participant from which we got an update.
  261. * @param {boolean} newValue - The new value of the E2EE enabled status.
  262. * @returns {void}
  263. */
  264. function _e2eeUpdated({ dispatch }, conference, participantId, newValue) {
  265. const e2eeEnabled = newValue === 'true';
  266. dispatch(toggleE2EE(e2eeEnabled));
  267. dispatch(participantUpdated({
  268. conference,
  269. id: participantId,
  270. e2eeEnabled
  271. }));
  272. }
  273. /**
  274. * Initializes the local participant and signals that it joined.
  275. *
  276. * @private
  277. * @param {Store} store - The redux store.
  278. * @param {Dispatch} next - The redux dispatch function to dispatch the
  279. * specified action to the specified store.
  280. * @param {Action} action - The redux action which is being dispatched
  281. * in the specified store.
  282. * @private
  283. * @returns {Object} The value returned by {@code next(action)}.
  284. */
  285. function _localParticipantJoined({ getState, dispatch }, next, action) {
  286. const result = next(action);
  287. const settings = getState()['features/base/settings'];
  288. dispatch(localParticipantJoined({
  289. avatarURL: settings.avatarURL,
  290. email: settings.email,
  291. name: settings.displayName
  292. }));
  293. return result;
  294. }
  295. /**
  296. * Signals that the local participant has left.
  297. *
  298. * @param {Store} store - The redux store.
  299. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  300. * specified {@code action} into the specified {@code store}.
  301. * @param {Action} action - The redux action which is being dispatched in the
  302. * specified {@code store}.
  303. * @private
  304. * @returns {Object} The value returned by {@code next(action)}.
  305. */
  306. function _localParticipantLeft({ dispatch }, next, action) {
  307. const result = next(action);
  308. dispatch(localParticipantLeft());
  309. return result;
  310. }
  311. /**
  312. * Plays sounds when participants join/leave conference.
  313. *
  314. * @param {Store} store - The redux store.
  315. * @param {Action} action - The redux action. Should be either
  316. * {@link PARTICIPANT_JOINED} or {@link PARTICIPANT_LEFT}.
  317. * @private
  318. * @returns {void}
  319. */
  320. function _maybePlaySounds({ getState, dispatch }, action) {
  321. const state = getState();
  322. const { startAudioMuted } = state['features/base/config'];
  323. const { soundsParticipantJoined: joinSound, soundsParticipantLeft: leftSound } = state['features/base/settings'];
  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. dispatch(playSound(RAISE_HAND_SOUND_ID));
  426. }
  427. }
  428. /**
  429. * Registers sounds related with the participants feature.
  430. *
  431. * @param {Store} store - The redux store.
  432. * @private
  433. * @returns {void}
  434. */
  435. function _registerSounds({ dispatch }) {
  436. dispatch(
  437. registerSound(PARTICIPANT_JOINED_SOUND_ID, PARTICIPANT_JOINED_FILE));
  438. dispatch(registerSound(PARTICIPANT_LEFT_SOUND_ID, PARTICIPANT_LEFT_FILE));
  439. }
  440. /**
  441. * Unregisters sounds related with the participants feature.
  442. *
  443. * @param {Store} store - The redux store.
  444. * @private
  445. * @returns {void}
  446. */
  447. function _unregisterSounds({ dispatch }) {
  448. dispatch(unregisterSound(PARTICIPANT_JOINED_SOUND_ID));
  449. dispatch(unregisterSound(PARTICIPANT_LEFT_SOUND_ID));
  450. }