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));
  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. if (e2eeEnabled) {
  241. dispatch(toggleE2EE(e2eeEnabled));
  242. }
  243. dispatch(participantUpdated({
  244. conference,
  245. id: participantId,
  246. e2eeEnabled
  247. }));
  248. }
  249. /**
  250. * Initializes the local participant and signals that it joined.
  251. *
  252. * @private
  253. * @param {Store} store - The redux store.
  254. * @param {Dispatch} next - The redux dispatch function to dispatch the
  255. * specified action to the specified store.
  256. * @param {Action} action - The redux action which is being dispatched
  257. * in the specified store.
  258. * @private
  259. * @returns {Object} The value returned by {@code next(action)}.
  260. */
  261. function _localParticipantJoined({ getState, dispatch }, next, action) {
  262. const result = next(action);
  263. const settings = getState()['features/base/settings'];
  264. dispatch(localParticipantJoined({
  265. avatarURL: settings.avatarURL,
  266. email: settings.email,
  267. name: settings.displayName
  268. }));
  269. return result;
  270. }
  271. /**
  272. * Signals that the local participant has left.
  273. *
  274. * @param {Store} store - The redux store.
  275. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  276. * specified {@code action} into the specified {@code store}.
  277. * @param {Action} action - The redux action which is being dispatched in the
  278. * specified {@code store}.
  279. * @private
  280. * @returns {Object} The value returned by {@code next(action)}.
  281. */
  282. function _localParticipantLeft({ dispatch }, next, action) {
  283. const result = next(action);
  284. dispatch(localParticipantLeft());
  285. return result;
  286. }
  287. /**
  288. * Plays sounds when participants join/leave conference.
  289. *
  290. * @param {Store} store - The redux store.
  291. * @param {Action} action - The redux action. Should be either
  292. * {@link PARTICIPANT_JOINED} or {@link PARTICIPANT_LEFT}.
  293. * @private
  294. * @returns {void}
  295. */
  296. function _maybePlaySounds({ getState, dispatch }, action) {
  297. const state = getState();
  298. const { startAudioMuted, disableJoinLeaveSounds } = state['features/base/config'];
  299. // If we have join/leave sounds disabled, don't play anything.
  300. if (disableJoinLeaveSounds) {
  301. return;
  302. }
  303. // We're not playing sounds for local participant
  304. // nor when the user is joining past the "startAudioMuted" limit.
  305. // The intention there was to not play user joined notification in big
  306. // conferences where 100th person is joining.
  307. if (!action.participant.local
  308. && (!startAudioMuted
  309. || getParticipantCount(state) < startAudioMuted)) {
  310. if (action.type === PARTICIPANT_JOINED) {
  311. const { presence } = action.participant;
  312. // The sounds for the poltergeist are handled by features/invite.
  313. if (presence !== INVITED && presence !== CALLING) {
  314. dispatch(playSound(PARTICIPANT_JOINED_SOUND_ID));
  315. }
  316. } else if (action.type === PARTICIPANT_LEFT) {
  317. dispatch(playSound(PARTICIPANT_LEFT_SOUND_ID));
  318. }
  319. }
  320. }
  321. /**
  322. * Notifies the feature base/participants that the action
  323. * {@code PARTICIPANT_JOINED} or {@code PARTICIPANT_UPDATED} is being dispatched
  324. * within a specific redux store.
  325. *
  326. * @param {Store} store - The redux store in which the specified {@code action}
  327. * is being dispatched.
  328. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  329. * specified {@code action} in the specified {@code store}.
  330. * @param {Action} action - The redux action {@code PARTICIPANT_JOINED} or
  331. * {@code PARTICIPANT_UPDATED} which is being dispatched in the specified
  332. * {@code store}.
  333. * @private
  334. * @returns {Object} The value returned by {@code next(action)}.
  335. */
  336. function _participantJoinedOrUpdated(store, next, action) {
  337. const { dispatch, getState } = store;
  338. const { participant: { avatarURL, email, id, local, name, raisedHand } } = action;
  339. // Send an external update of the local participant's raised hand state
  340. // if a new raised hand state is defined in the action.
  341. if (typeof raisedHand !== 'undefined') {
  342. if (local) {
  343. const { conference } = getState()['features/base/conference'];
  344. // Send raisedHand signalling only if there is a change
  345. if (conference && raisedHand !== getLocalParticipant(getState()).raisedHand) {
  346. conference.setLocalParticipantProperty('raisedHand', raisedHand);
  347. }
  348. }
  349. }
  350. // Allow the redux update to go through and compare the old avatar
  351. // to the new avatar and emit out change events if necessary.
  352. const result = next(action);
  353. // Only run this if the config is populated, otherwise we preload external resources
  354. // even if disableThirdPartyRequests is set to true in config
  355. if (Object.keys(getState()['features/base/config']).length) {
  356. const { disableThirdPartyRequests } = getState()['features/base/config'];
  357. if (!disableThirdPartyRequests && (avatarURL || email || id || name)) {
  358. const participantId = !id && local ? getLocalParticipant(getState()).id : id;
  359. const updatedParticipant = getParticipantById(getState(), participantId);
  360. getFirstLoadableAvatarUrl(updatedParticipant, store)
  361. .then(url => {
  362. dispatch(setLoadableAvatarUrl(participantId, url));
  363. });
  364. }
  365. }
  366. // Notify external listeners of potential avatarURL changes.
  367. if (typeof APP === 'object') {
  368. const currentKnownId = local ? APP.conference.getMyUserId() : id;
  369. // Force update of local video getting a new id.
  370. APP.UI.refreshAvatarDisplay(currentKnownId);
  371. }
  372. return result;
  373. }
  374. /**
  375. * Handles a raise hand status update.
  376. *
  377. * @param {Function} dispatch - The Redux dispatch function.
  378. * @param {Object} conference - The conference for which we got an update.
  379. * @param {string} participantId - The ID of the participant from which we got an update.
  380. * @param {boolean} newValue - The new value of the raise hand status.
  381. * @returns {void}
  382. */
  383. function _raiseHandUpdated({ dispatch, getState }, conference, participantId, newValue) {
  384. const raisedHand = newValue === 'true';
  385. dispatch(participantUpdated({
  386. conference,
  387. id: participantId,
  388. raisedHand
  389. }));
  390. if (typeof APP !== 'undefined') {
  391. APP.API.notifyRaiseHandUpdated(participantId, raisedHand);
  392. }
  393. if (raisedHand) {
  394. dispatch(showNotification({
  395. titleArguments: {
  396. name: getParticipantDisplayName(getState, participantId)
  397. },
  398. titleKey: 'notify.raisedHand'
  399. }, NOTIFICATION_TIMEOUT));
  400. }
  401. }
  402. /**
  403. * Registers sounds related with the participants feature.
  404. *
  405. * @param {Store} store - The redux store.
  406. * @private
  407. * @returns {void}
  408. */
  409. function _registerSounds({ dispatch }) {
  410. dispatch(
  411. registerSound(PARTICIPANT_JOINED_SOUND_ID, PARTICIPANT_JOINED_FILE));
  412. dispatch(registerSound(PARTICIPANT_LEFT_SOUND_ID, PARTICIPANT_LEFT_FILE));
  413. }
  414. /**
  415. * Unregisters sounds related with the participants feature.
  416. *
  417. * @param {Store} store - The redux store.
  418. * @private
  419. * @returns {void}
  420. */
  421. function _unregisterSounds({ dispatch }) {
  422. dispatch(unregisterSound(PARTICIPANT_JOINED_SOUND_ID));
  423. dispatch(unregisterSound(PARTICIPANT_LEFT_SOUND_ID));
  424. }