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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* global APP */
  2. import { CONNECTION_ESTABLISHED } from '../connection';
  3. import {
  4. getLocalParticipant,
  5. getParticipantById,
  6. PIN_PARTICIPANT
  7. } from '../participants';
  8. import { MiddlewareRegistry } from '../redux';
  9. import { TRACK_ADDED, TRACK_REMOVED } from '../tracks';
  10. import { createConference } from './actions';
  11. import {
  12. _addLocalTracksToConference,
  13. _handleParticipantError,
  14. _removeLocalTracksFromConference
  15. } from './functions';
  16. /**
  17. * Implements the middleware of the feature base/conference.
  18. *
  19. * @param {Store} store - Redux store.
  20. * @returns {Function}
  21. */
  22. MiddlewareRegistry.register(store => next => action => {
  23. switch (action.type) {
  24. case CONNECTION_ESTABLISHED:
  25. return _connectionEstablished(store, next, action);
  26. case PIN_PARTICIPANT:
  27. return _pinParticipant(store, next, action);
  28. case TRACK_ADDED:
  29. case TRACK_REMOVED:
  30. return _trackAddedOrRemoved(store, next, action);
  31. }
  32. return next(action);
  33. });
  34. /**
  35. * Notifies the feature base/conference that the action CONNECTION_ESTABLISHED
  36. * is being dispatched within a specific Redux store.
  37. *
  38. * @param {Store} store - The Redux store in which the specified action is being
  39. * dispatched.
  40. * @param {Dispatch} next - The Redux dispatch function to dispatch the
  41. * specified action to the specified store.
  42. * @param {Action} action - The Redux action CONNECTION_ESTABLISHED which is
  43. * being dispatched in the specified store.
  44. * @private
  45. * @returns {Object} The new state that is the result of the reduction of the
  46. * specified action.
  47. */
  48. function _connectionEstablished(store, next, action) {
  49. const result = next(action);
  50. // FIXME: workaround for the web version. Currently the creation of the
  51. // conference is handled by /conference.js
  52. if (typeof APP === 'undefined') {
  53. store.dispatch(createConference());
  54. }
  55. return result;
  56. }
  57. /**
  58. * Notifies the feature base/conference that the action PIN_PARTICIPANT is being
  59. * dispatched within a specific Redux store. Pins the specified remote
  60. * participant in the associated conference, ignores the local participant.
  61. *
  62. * @param {Store} store - The Redux store in which the specified action is being
  63. * dispatched.
  64. * @param {Dispatch} next - The Redux dispatch function to dispatch the
  65. * specified action to the specified store.
  66. * @param {Action} action - The Redux action PIN_PARTICIPANT which is being
  67. * dispatched in the specified store.
  68. * @private
  69. * @returns {Object} The new state that is the result of the reduction of the
  70. * specified action.
  71. */
  72. function _pinParticipant(store, next, action) {
  73. const state = store.getState();
  74. const participants = state['features/base/participants'];
  75. const id = action.participant.id;
  76. const participantById = getParticipantById(participants, id);
  77. let pin;
  78. // The following condition prevents signaling to pin local participant. The
  79. // logic is:
  80. // - If we have an ID, we check if the participant identified by that ID is
  81. // local.
  82. // - If we don't have an ID (i.e. no participant identified by an ID), we
  83. // check for local participant. If she's currently pinned, then this
  84. // action will unpin her and that's why we won't signal here too.
  85. if (participantById) {
  86. pin = !participantById.local;
  87. } else {
  88. const localParticipant = getLocalParticipant(participants);
  89. pin = !localParticipant || !localParticipant.pinned;
  90. }
  91. if (pin) {
  92. const conference = state['features/base/conference'].conference;
  93. try {
  94. conference.pinParticipant(id);
  95. } catch (err) {
  96. _handleParticipantError(err);
  97. }
  98. }
  99. return next(action);
  100. }
  101. /**
  102. * Synchronizes local tracks from state with local tracks in JitsiConference
  103. * instance.
  104. *
  105. * @param {Store} store - Redux store.
  106. * @param {Object} action - Action object.
  107. * @private
  108. * @returns {Promise}
  109. */
  110. function _syncConferenceLocalTracksWithState(store, action) {
  111. const state = store.getState()['features/base/conference'];
  112. const conference = state.conference;
  113. let promise;
  114. // XXX The conference may already be in the process of being left, that's
  115. // why we should not add/remove local tracks to such conference.
  116. if (conference && conference !== state.leaving) {
  117. const track = action.track.jitsiTrack;
  118. if (action.type === TRACK_ADDED) {
  119. promise = _addLocalTracksToConference(conference, [ track ]);
  120. } else {
  121. promise = _removeLocalTracksFromConference(conference, [ track ]);
  122. }
  123. }
  124. return promise || Promise.resolve();
  125. }
  126. /**
  127. * Notifies the feature base/conference that the action TRACK_ADDED
  128. * or TRACK_REMOVED is being dispatched within a specific Redux store.
  129. *
  130. * @param {Store} store - The Redux store in which the specified action is being
  131. * dispatched.
  132. * @param {Dispatch} next - The Redux dispatch function to dispatch the
  133. * specified action to the specified store.
  134. * @param {Action} action - The Redux action TRACK_ADDED or TRACK_REMOVED which
  135. * is being dispatched in the specified store.
  136. * @private
  137. * @returns {Object} The new state that is the result of the reduction of the
  138. * specified action.
  139. */
  140. function _trackAddedOrRemoved(store, next, action) {
  141. const track = action.track;
  142. if (track && track.local) {
  143. return (
  144. _syncConferenceLocalTracksWithState(store, action)
  145. .then(() => next(action)));
  146. }
  147. return next(action);
  148. }