Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

middleware.js 16KB

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