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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // @flow
  2. import UIEvents from '../../../../service/UI/UIEvents';
  3. import { APP_WILL_MOUNT, APP_WILL_UNMOUNT } from '../../app';
  4. import {
  5. CONFERENCE_JOINED,
  6. CONFERENCE_LEFT
  7. } from '../conference';
  8. import { MiddlewareRegistry } from '../redux';
  9. import { playSound, registerSound, unregisterSound } from '../sounds';
  10. import {
  11. localParticipantIdChanged,
  12. participantUpdated
  13. } from './actions';
  14. import {
  15. DOMINANT_SPEAKER_CHANGED,
  16. KICK_PARTICIPANT,
  17. MUTE_REMOTE_PARTICIPANT,
  18. PARTICIPANT_DISPLAY_NAME_CHANGED,
  19. PARTICIPANT_JOINED,
  20. PARTICIPANT_LEFT,
  21. PARTICIPANT_UPDATED
  22. } from './actionTypes';
  23. import {
  24. LOCAL_PARTICIPANT_DEFAULT_ID,
  25. PARTICIPANT_JOINED_SOUND_ID,
  26. PARTICIPANT_LEFT_SOUND_ID
  27. } from './constants';
  28. import {
  29. getAvatarURLByParticipantId,
  30. getLocalParticipant,
  31. getParticipantById,
  32. getParticipantCount
  33. } from './functions';
  34. import {
  35. PARTICIPANT_JOINED_SRC,
  36. PARTICIPANT_LEFT_SRC
  37. } from './sounds';
  38. declare var APP: Object;
  39. /**
  40. * Middleware that captures CONFERENCE_JOINED and CONFERENCE_LEFT actions and
  41. * updates respectively ID of local participant.
  42. *
  43. * @param {Store} store - Redux store.
  44. * @returns {Function}
  45. */
  46. MiddlewareRegistry.register(store => next => action => {
  47. const { conference } = store.getState()['features/base/conference'];
  48. if (action.type === PARTICIPANT_JOINED
  49. || action.type === PARTICIPANT_LEFT) {
  50. _maybePlaySounds(store, action);
  51. }
  52. switch (action.type) {
  53. case APP_WILL_MOUNT:
  54. _registerSounds(store);
  55. break;
  56. case APP_WILL_UNMOUNT:
  57. _unregisterSounds(store);
  58. break;
  59. case CONFERENCE_JOINED:
  60. store.dispatch(localParticipantIdChanged(action.conference.myUserId()));
  61. break;
  62. case CONFERENCE_LEFT:
  63. store.dispatch(localParticipantIdChanged(LOCAL_PARTICIPANT_DEFAULT_ID));
  64. break;
  65. case DOMINANT_SPEAKER_CHANGED: {
  66. // Ensure the raised hand state is cleared for the dominant speaker.
  67. const participant = getLocalParticipant(store.getState());
  68. if (participant) {
  69. const local = participant.id === action.participant.id;
  70. store.dispatch(participantUpdated({
  71. id: action.participant.id,
  72. local,
  73. raisedHand: false
  74. }));
  75. }
  76. if (typeof APP === 'object') {
  77. APP.UI.markDominantSpeaker(action.participant.id);
  78. }
  79. break;
  80. }
  81. case KICK_PARTICIPANT:
  82. conference.kickParticipant(action.id);
  83. break;
  84. case MUTE_REMOTE_PARTICIPANT:
  85. conference.muteParticipant(action.id);
  86. break;
  87. // TODO Remove this middleware when the local display name update flow is
  88. // fully brought into redux.
  89. case PARTICIPANT_DISPLAY_NAME_CHANGED: {
  90. if (typeof APP !== 'undefined') {
  91. const participant = getLocalParticipant(store.getState());
  92. if (participant && participant.id === action.id) {
  93. APP.UI.emitEvent(UIEvents.NICKNAME_CHANGED, action.name);
  94. }
  95. }
  96. break;
  97. }
  98. case PARTICIPANT_JOINED:
  99. case PARTICIPANT_UPDATED: {
  100. const { participant } = action;
  101. const { id, local, raisedHand } = participant;
  102. // Send an external update of the local participant's raised hand state
  103. // if a new raised hand state is defined in the action.
  104. if (typeof raisedHand !== 'undefined') {
  105. if (local) {
  106. conference.setLocalParticipantProperty(
  107. 'raisedHand',
  108. raisedHand);
  109. }
  110. if (typeof APP === 'object') {
  111. if (local) {
  112. APP.UI.onLocalRaiseHandChanged(raisedHand);
  113. APP.UI.setLocalRaisedHandStatus(raisedHand);
  114. } else {
  115. const remoteParticipant
  116. = getParticipantById(store.getState(), id);
  117. remoteParticipant
  118. && APP.UI.setRaisedHandStatus(
  119. remoteParticipant.id,
  120. remoteParticipant.name,
  121. raisedHand);
  122. }
  123. }
  124. }
  125. // Notify external listeners of potential avatarURL changes.
  126. if (typeof APP === 'object') {
  127. const preUpdateAvatarURL
  128. = getAvatarURLByParticipantId(store.getState(), id);
  129. // Allow the redux update to go through and compare the old avatar
  130. // to the new avatar and emit out change events if necessary.
  131. const result = next(action);
  132. const postUpdateAvatarURL
  133. = getAvatarURLByParticipantId(store.getState(), id);
  134. if (preUpdateAvatarURL !== postUpdateAvatarURL) {
  135. const currentKnownId = local
  136. ? APP.conference.getMyUserId() : id;
  137. APP.UI.refreshAvatarDisplay(
  138. currentKnownId, postUpdateAvatarURL);
  139. APP.API.notifyAvatarChanged(
  140. currentKnownId, postUpdateAvatarURL);
  141. }
  142. return result;
  143. }
  144. break;
  145. }
  146. }
  147. return next(action);
  148. });
  149. /**
  150. * Plays sounds when participants join/leave conference.
  151. *
  152. * @param {Store} store - The Redux store.
  153. * @param {Action} action - The Redux action. Should be either
  154. * {@link PARTICIPANT_JOINED} or {@link PARTICIPANT_LEFT}.
  155. * @private
  156. * @returns {void}
  157. */
  158. function _maybePlaySounds({ getState, dispatch }, action) {
  159. const state = getState();
  160. const { startAudioMuted } = state['features/base/config'];
  161. // We're not playing sounds for local participant
  162. // nor when the user is joining past the "startAudioMuted" limit.
  163. // The intention there was to not play user joined notification in big
  164. // conferences where 100th person is joining.
  165. if (!action.participant.local
  166. && (!startAudioMuted
  167. || getParticipantCount(state) < startAudioMuted)) {
  168. if (action.type === PARTICIPANT_JOINED) {
  169. dispatch(playSound(PARTICIPANT_JOINED_SOUND_ID));
  170. } else if (action.type === PARTICIPANT_LEFT) {
  171. dispatch(playSound(PARTICIPANT_LEFT_SOUND_ID));
  172. }
  173. }
  174. }
  175. /**
  176. * Registers sounds related with the participants feature.
  177. *
  178. * @param {Store} store - The Redux store.
  179. * @private
  180. * @returns {void}
  181. */
  182. function _registerSounds({ dispatch }) {
  183. dispatch(
  184. registerSound(PARTICIPANT_JOINED_SOUND_ID, PARTICIPANT_JOINED_SRC));
  185. dispatch(
  186. registerSound(PARTICIPANT_LEFT_SOUND_ID, PARTICIPANT_LEFT_SRC));
  187. }
  188. /**
  189. * Unregisters sounds related with the participants feature.
  190. *
  191. * @param {Store} store - The Redux store.
  192. * @private
  193. * @returns {void}
  194. */
  195. function _unregisterSounds({ dispatch }) {
  196. dispatch(
  197. unregisterSound(PARTICIPANT_JOINED_SOUND_ID, PARTICIPANT_JOINED_SRC));
  198. dispatch(
  199. unregisterSound(PARTICIPANT_LEFT_SOUND_ID, PARTICIPANT_LEFT_SRC));
  200. }