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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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. const propertyHandlers = {
  176. 'e2eeEnabled': (participant, value) => _e2eeUpdated(store, conference, participant.getId(), value),
  177. 'features_e2ee': (participant, value) =>
  178. store.dispatch(participantUpdated({
  179. conference,
  180. id: participant.getId(),
  181. e2eeSupported: value
  182. })),
  183. 'features_jigasi': (participant, value) =>
  184. store.dispatch(participantUpdated({
  185. conference,
  186. id: participant.getId(),
  187. isJigasi: value
  188. })),
  189. 'features_screen-sharing': (participant, value) => // eslint-disable-line no-unused-vars
  190. store.dispatch(participantUpdated({
  191. conference,
  192. id: participant.getId(),
  193. features: { 'screen-sharing': true }
  194. })),
  195. 'raisedHand': (participant, value) => _raiseHandUpdated(store, conference, participant.getId(), value),
  196. 'remoteControlSessionStatus': (participant, value) =>
  197. store.dispatch(participantUpdated({
  198. conference,
  199. id: participant.getId(),
  200. remoteControlSessionStatus: value
  201. }))
  202. };
  203. // update properties for the participants that are already in the conference
  204. conference.getParticipants().forEach(participant => {
  205. Object.keys(propertyHandlers).forEach(propertyName => {
  206. const value = participant.getProperty(propertyName);
  207. if (value !== undefined) {
  208. propertyHandlers[propertyName](participant, value);
  209. }
  210. });
  211. });
  212. // We joined a conference
  213. conference.on(
  214. JitsiConferenceEvents.PARTICIPANT_PROPERTY_CHANGED,
  215. (participant, propertyName, oldValue, newValue) => {
  216. if (propertyHandlers.hasOwnProperty(propertyName)) {
  217. propertyHandlers[propertyName](participant, newValue);
  218. }
  219. });
  220. } else {
  221. const localParticipantId = getLocalParticipant(store.getState).id;
  222. // We left the conference, the local participant must be updated.
  223. _e2eeUpdated(store, conference, localParticipantId, false);
  224. _raiseHandUpdated(store, conference, localParticipantId, false);
  225. }
  226. }
  227. );
  228. /**
  229. * Handles a E2EE enabled status update.
  230. *
  231. * @param {Function} dispatch - The Redux dispatch function.
  232. * @param {Object} conference - The conference for which we got an update.
  233. * @param {string} participantId - The ID of the participant from which we got an update.
  234. * @param {boolean} newValue - The new value of the E2EE enabled status.
  235. * @returns {void}
  236. */
  237. function _e2eeUpdated({ dispatch }, conference, participantId, newValue) {
  238. const e2eeEnabled = newValue === 'true';
  239. dispatch(participantUpdated({
  240. conference,
  241. id: participantId,
  242. e2eeEnabled
  243. }));
  244. }
  245. /**
  246. * Initializes the local participant and signals that it joined.
  247. *
  248. * @private
  249. * @param {Store} store - The redux store.
  250. * @param {Dispatch} next - The redux dispatch function to dispatch the
  251. * specified action to the specified store.
  252. * @param {Action} action - The redux action which is being dispatched
  253. * in the specified store.
  254. * @private
  255. * @returns {Object} The value returned by {@code next(action)}.
  256. */
  257. function _localParticipantJoined({ getState, dispatch }, next, action) {
  258. const result = next(action);
  259. const settings = getState()['features/base/settings'];
  260. dispatch(localParticipantJoined({
  261. avatarURL: settings.avatarURL,
  262. email: settings.email,
  263. name: settings.displayName
  264. }));
  265. return result;
  266. }
  267. /**
  268. * Signals that the local participant has left.
  269. *
  270. * @param {Store} store - The redux store.
  271. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  272. * specified {@code action} into the specified {@code store}.
  273. * @param {Action} action - The redux action which is being dispatched in the
  274. * specified {@code store}.
  275. * @private
  276. * @returns {Object} The value returned by {@code next(action)}.
  277. */
  278. function _localParticipantLeft({ dispatch }, next, action) {
  279. const result = next(action);
  280. dispatch(localParticipantLeft());
  281. return result;
  282. }
  283. /**
  284. * Plays sounds when participants join/leave conference.
  285. *
  286. * @param {Store} store - The redux store.
  287. * @param {Action} action - The redux action. Should be either
  288. * {@link PARTICIPANT_JOINED} or {@link PARTICIPANT_LEFT}.
  289. * @private
  290. * @returns {void}
  291. */
  292. function _maybePlaySounds({ getState, dispatch }, action) {
  293. const state = getState();
  294. const { startAudioMuted } = state['features/base/config'];
  295. // We're not playing sounds for local participant
  296. // nor when the user is joining past the "startAudioMuted" limit.
  297. // The intention there was to not play user joined notification in big
  298. // conferences where 100th person is joining.
  299. if (!action.participant.local
  300. && (!startAudioMuted
  301. || getParticipantCount(state) < startAudioMuted)) {
  302. if (action.type === PARTICIPANT_JOINED) {
  303. const { presence } = action.participant;
  304. // The sounds for the poltergeist are handled by features/invite.
  305. if (presence !== INVITED && presence !== CALLING) {
  306. dispatch(playSound(PARTICIPANT_JOINED_SOUND_ID));
  307. }
  308. } else if (action.type === PARTICIPANT_LEFT) {
  309. dispatch(playSound(PARTICIPANT_LEFT_SOUND_ID));
  310. }
  311. }
  312. }
  313. /**
  314. * Notifies the feature base/participants that the action
  315. * {@code PARTICIPANT_JOINED} or {@code PARTICIPANT_UPDATED} is being dispatched
  316. * within a specific redux store.
  317. *
  318. * @param {Store} store - The redux store in which the specified {@code action}
  319. * is being dispatched.
  320. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  321. * specified {@code action} in the specified {@code store}.
  322. * @param {Action} action - The redux action {@code PARTICIPANT_JOINED} or
  323. * {@code PARTICIPANT_UPDATED} which is being dispatched in the specified
  324. * {@code store}.
  325. * @private
  326. * @returns {Object} The value returned by {@code next(action)}.
  327. */
  328. function _participantJoinedOrUpdated(store, next, action) {
  329. const { dispatch, getState } = store;
  330. const { participant: { avatarURL, e2eeEnabled, email, id, local, name, raisedHand } } = action;
  331. // Send an external update of the local participant's raised hand state
  332. // if a new raised hand state is defined in the action.
  333. if (typeof raisedHand !== 'undefined') {
  334. if (local) {
  335. const { conference } = getState()['features/base/conference'];
  336. // Send raisedHand signalling only if there is a change
  337. if (conference && raisedHand !== getLocalParticipant(getState()).raisedHand) {
  338. conference.setLocalParticipantProperty('raisedHand', raisedHand);
  339. }
  340. }
  341. }
  342. // Send an external update of the local participant's E2EE enabled state
  343. // if a new state is defined in the action.
  344. if (typeof e2eeEnabled !== 'undefined') {
  345. if (local) {
  346. const { conference } = getState()['features/base/conference'];
  347. conference && conference.setLocalParticipantProperty('e2eeEnabled', e2eeEnabled);
  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. const { disableThirdPartyRequests } = getState()['features/base/config'];
  354. if (!disableThirdPartyRequests && (avatarURL || email || id || name)) {
  355. const participantId = !id && local ? getLocalParticipant(getState()).id : id;
  356. const updatedParticipant = getParticipantById(getState(), participantId);
  357. getFirstLoadableAvatarUrl(updatedParticipant, store)
  358. .then(url => {
  359. dispatch(setLoadableAvatarUrl(participantId, url));
  360. });
  361. }
  362. // Notify external listeners of potential avatarURL changes.
  363. if (typeof APP === 'object') {
  364. const currentKnownId = local ? APP.conference.getMyUserId() : id;
  365. // Force update of local video getting a new id.
  366. APP.UI.refreshAvatarDisplay(currentKnownId);
  367. }
  368. return result;
  369. }
  370. /**
  371. * Handles a raise hand status update.
  372. *
  373. * @param {Function} dispatch - The Redux dispatch function.
  374. * @param {Object} conference - The conference for which we got an update.
  375. * @param {string} participantId - The ID of the participant from which we got an update.
  376. * @param {boolean} newValue - The new value of the raise hand status.
  377. * @returns {void}
  378. */
  379. function _raiseHandUpdated({ dispatch, getState }, conference, participantId, newValue) {
  380. const raisedHand = newValue === 'true';
  381. dispatch(participantUpdated({
  382. conference,
  383. id: participantId,
  384. raisedHand
  385. }));
  386. if (typeof APP !== 'undefined') {
  387. APP.API.notifyRaiseHandUpdated(participantId, raisedHand);
  388. }
  389. if (raisedHand) {
  390. dispatch(showNotification({
  391. titleArguments: {
  392. name: getParticipantDisplayName(getState, participantId)
  393. },
  394. titleKey: 'notify.raisedHand'
  395. }, NOTIFICATION_TIMEOUT));
  396. }
  397. }
  398. /**
  399. * Registers sounds related with the participants feature.
  400. *
  401. * @param {Store} store - The redux store.
  402. * @private
  403. * @returns {void}
  404. */
  405. function _registerSounds({ dispatch }) {
  406. dispatch(
  407. registerSound(PARTICIPANT_JOINED_SOUND_ID, PARTICIPANT_JOINED_FILE));
  408. dispatch(registerSound(PARTICIPANT_LEFT_SOUND_ID, PARTICIPANT_LEFT_FILE));
  409. }
  410. /**
  411. * Unregisters sounds related with the participants feature.
  412. *
  413. * @param {Store} store - The redux store.
  414. * @private
  415. * @returns {void}
  416. */
  417. function _unregisterSounds({ dispatch }) {
  418. dispatch(unregisterSound(PARTICIPANT_JOINED_SOUND_ID));
  419. dispatch(unregisterSound(PARTICIPANT_LEFT_SOUND_ID));
  420. }