Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

middleware.js 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import { CONNECTION_ESTABLISHED } from '../connection';
  2. import {
  3. getLocalParticipant,
  4. getParticipantById,
  5. PIN_PARTICIPANT
  6. } from '../participants';
  7. import { MiddlewareRegistry } from '../redux';
  8. import { TRACK_ADDED, TRACK_REMOVED } from '../tracks';
  9. import { createConference } from './actions';
  10. import { SET_PASSWORD } from './actionTypes';
  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 SET_PASSWORD:
  29. return _setPassword(store, next, action);
  30. case TRACK_ADDED:
  31. case TRACK_REMOVED:
  32. return _trackAddedOrRemoved(store, next, action);
  33. }
  34. return next(action);
  35. });
  36. /**
  37. * Notifies the feature base/conference that the action CONNECTION_ESTABLISHED
  38. * is being dispatched within a specific Redux store.
  39. *
  40. * @param {Store} store - The Redux store in which the specified action is being
  41. * dispatched.
  42. * @param {Dispatch} next - The Redux dispatch function to dispatch the
  43. * specified action to the specified store.
  44. * @param {Action} action - The Redux action CONNECTION_ESTABLISHED which is
  45. * being dispatched in the specified store.
  46. * @private
  47. * @returns {Object} The new state that is the result of the reduction of the
  48. * specified action.
  49. */
  50. function _connectionEstablished(store, next, action) {
  51. const result = next(action);
  52. store.dispatch(createConference());
  53. return result;
  54. }
  55. /**
  56. * Notifies the feature base/conference that the action PIN_PARTICIPANT is being
  57. * dispatched within a specific Redux store. Pins the specified remote
  58. * participant in the associated conference, ignores the local participant.
  59. *
  60. * @param {Store} store - The Redux store in which the specified action is being
  61. * dispatched.
  62. * @param {Dispatch} next - The Redux dispatch function to dispatch the
  63. * specified action to the specified store.
  64. * @param {Action} action - The Redux action PIN_PARTICIPANT which is being
  65. * dispatched in the specified store.
  66. * @private
  67. * @returns {Object} The new state that is the result of the reduction of the
  68. * specified action.
  69. */
  70. function _pinParticipant(store, next, action) {
  71. const state = store.getState();
  72. const participants = state['features/base/participants'];
  73. const id = action.participant.id;
  74. const participantById = getParticipantById(participants, id);
  75. let pin;
  76. // The following condition prevents signaling to pin local participant. The
  77. // logic is:
  78. // - If we have an ID, we check if the participant identified by that ID is
  79. // local.
  80. // - If we don't have an ID (i.e. no participant identified by an ID), we
  81. // check for local participant. If she's currently pinned, then this
  82. // action will unpin her and that's why we won't signal here too.
  83. if (participantById) {
  84. pin = !participantById.local;
  85. } else {
  86. const localParticipant = getLocalParticipant(participants);
  87. pin = !localParticipant || !localParticipant.pinned;
  88. }
  89. if (pin) {
  90. const conference = state['features/base/conference'].conference;
  91. try {
  92. conference.pinParticipant(id);
  93. } catch (err) {
  94. _handleParticipantError(err);
  95. }
  96. }
  97. return next(action);
  98. }
  99. /**
  100. * Notifies the feature base/conference that the action <tt>SET_PASSWORD</tt> is
  101. * being dispatched within a specific Redux store. Joins or locks a specific
  102. * <tt>JitsiConference</tt> with a specific password.
  103. *
  104. * @param {Store} store - The Redux store in which the specified action is being
  105. * dispatched.
  106. * @param {Dispatch} next - The Redux dispatch function to dispatch the
  107. * specified action to the specified store.
  108. * @param {Action} action - The Redux action <tt>SET_PASSWORD</tt> which is
  109. * being dispatched in the specified store.
  110. * @private
  111. * @returns {Object} The new state that is the result of the reduction of the
  112. * specified action.
  113. */
  114. function _setPassword(store, next, action) {
  115. const { conference, method } = action;
  116. switch (method) {
  117. case conference.join: {
  118. let state = store.getState()['features/base/conference'];
  119. // Make sure that the action will set a password for a conference that
  120. // the application wants joined.
  121. if (state.passwordRequired === conference) {
  122. const result = next(action);
  123. // Join the conference with the newly-set password.
  124. const password = action.password;
  125. // Make sure that the action did set the password.
  126. state = store.getState()['features/base/conference'];
  127. if (state.password === password
  128. && !state.passwordRequired
  129. // Make sure that the application still wants the conference
  130. // joined.
  131. && !state.conference) {
  132. method.call(conference, password);
  133. }
  134. return result;
  135. }
  136. break;
  137. }
  138. }
  139. return next(action);
  140. }
  141. /**
  142. * Synchronizes local tracks from state with local tracks in JitsiConference
  143. * instance.
  144. *
  145. * @param {Store} store - Redux store.
  146. * @param {Object} action - Action object.
  147. * @private
  148. * @returns {Promise}
  149. */
  150. function _syncConferenceLocalTracksWithState(store, action) {
  151. const state = store.getState()['features/base/conference'];
  152. const conference = state.conference;
  153. let promise;
  154. // XXX The conference may already be in the process of being left, that's
  155. // why we should not add/remove local tracks to such conference.
  156. if (conference && conference !== state.leaving) {
  157. const track = action.track.jitsiTrack;
  158. if (action.type === TRACK_ADDED) {
  159. promise = _addLocalTracksToConference(conference, [ track ]);
  160. } else {
  161. promise = _removeLocalTracksFromConference(conference, [ track ]);
  162. }
  163. }
  164. return promise || Promise.resolve();
  165. }
  166. /**
  167. * Notifies the feature base/conference that the action TRACK_ADDED
  168. * or TRACK_REMOVED is being dispatched within a specific Redux store.
  169. *
  170. * @param {Store} store - The Redux store in which the specified action is being
  171. * dispatched.
  172. * @param {Dispatch} next - The Redux dispatch function to dispatch the
  173. * specified action to the specified store.
  174. * @param {Action} action - The Redux action TRACK_ADDED or TRACK_REMOVED which
  175. * is being dispatched in the specified store.
  176. * @private
  177. * @returns {Object} The new state that is the result of the reduction of the
  178. * specified action.
  179. */
  180. function _trackAddedOrRemoved(store, next, action) {
  181. const track = action.track;
  182. if (track && track.local) {
  183. return (
  184. _syncConferenceLocalTracksWithState(store, action)
  185. .then(() => next(action)));
  186. }
  187. return next(action);
  188. }