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 17KB

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