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

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