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

middleware.js 15KB

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