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

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