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

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