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

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