您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

middleware.js 15KB

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