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

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