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

middleware.js 13KB

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