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

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