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.8KB

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