Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

middleware.js 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // @flow
  2. import { batch } from 'react-redux';
  3. import { createReactionSoundsDisabledEvent, sendAnalytics } from '../analytics';
  4. import { APP_WILL_MOUNT, APP_WILL_UNMOUNT } from '../base/app';
  5. import { getParticipantCount } from '../base/participants';
  6. import { MiddlewareRegistry } from '../base/redux';
  7. import { SETTINGS_UPDATED, updateSettings } from '../base/settings';
  8. import { playSound, registerSound, unregisterSound } from '../base/sounds';
  9. import { getDisabledSounds } from '../base/sounds/functions.any';
  10. import { NOTIFICATION_TIMEOUT_TYPE, showNotification } from '../notifications';
  11. import {
  12. ADD_REACTION_BUFFER,
  13. FLUSH_REACTION_BUFFER,
  14. SEND_REACTIONS,
  15. PUSH_REACTIONS,
  16. SHOW_SOUNDS_NOTIFICATION
  17. } from './actionTypes';
  18. import { displayReactionSoundsNotification } from './actions';
  19. import {
  20. addReactionsToChat,
  21. flushReactionBuffer,
  22. pushReactions,
  23. sendReactions,
  24. setReactionQueue
  25. } from './actions.any';
  26. import {
  27. ENDPOINT_REACTION_NAME,
  28. RAISE_HAND_SOUND_ID,
  29. REACTIONS,
  30. REACTION_SOUND,
  31. SOUNDS_THRESHOLDS
  32. } from './constants';
  33. import {
  34. getReactionMessageFromBuffer,
  35. getReactionsSoundsThresholds,
  36. getReactionsWithId,
  37. sendReactionsWebhook
  38. } from './functions.any';
  39. import { RAISE_HAND_SOUND_FILE } from './sounds';
  40. declare var APP: Object;
  41. /**
  42. * Middleware which intercepts Reactions actions to handle changes to the
  43. * visibility timeout of the Reactions.
  44. *
  45. * @param {Store} store - The redux store.
  46. * @returns {Function}
  47. */
  48. MiddlewareRegistry.register(store => next => action => {
  49. const { dispatch, getState } = store;
  50. switch (action.type) {
  51. case APP_WILL_MOUNT:
  52. batch(() => {
  53. Object.keys(REACTIONS).forEach(key => {
  54. for (let i = 0; i < SOUNDS_THRESHOLDS.length; i++) {
  55. dispatch(registerSound(
  56. `${REACTIONS[key].soundId}${SOUNDS_THRESHOLDS[i]}`,
  57. REACTIONS[key].soundFiles[i]
  58. )
  59. );
  60. }
  61. }
  62. );
  63. dispatch(registerSound(RAISE_HAND_SOUND_ID, RAISE_HAND_SOUND_FILE));
  64. });
  65. break;
  66. case APP_WILL_UNMOUNT:
  67. batch(() => {
  68. Object.keys(REACTIONS).forEach(key => {
  69. for (let i = 0; i < SOUNDS_THRESHOLDS.length; i++) {
  70. dispatch(unregisterSound(`${REACTIONS[key].soundId}${SOUNDS_THRESHOLDS[i]}`));
  71. }
  72. });
  73. dispatch(unregisterSound(RAISE_HAND_SOUND_ID));
  74. });
  75. break;
  76. case ADD_REACTION_BUFFER: {
  77. const { timeoutID, buffer } = getState()['features/reactions'];
  78. const { reaction } = action;
  79. clearTimeout(timeoutID);
  80. buffer.push(reaction);
  81. action.buffer = buffer;
  82. action.timeoutID = setTimeout(() => {
  83. dispatch(flushReactionBuffer());
  84. }, 500);
  85. break;
  86. }
  87. case FLUSH_REACTION_BUFFER: {
  88. const state = getState();
  89. const { buffer } = state['features/reactions'];
  90. const participantCount = getParticipantCount(state);
  91. batch(() => {
  92. if (participantCount > 1) {
  93. dispatch(sendReactions());
  94. }
  95. dispatch(addReactionsToChat(getReactionMessageFromBuffer(buffer)));
  96. dispatch(pushReactions(buffer));
  97. });
  98. sendReactionsWebhook(state, buffer);
  99. break;
  100. }
  101. case PUSH_REACTIONS: {
  102. const state = getState();
  103. const { queue, notificationDisplayed } = state['features/reactions'];
  104. const { soundsReactions } = state['features/base/settings'];
  105. const disabledSounds = getDisabledSounds(state);
  106. const reactions = action.reactions;
  107. batch(() => {
  108. if (!notificationDisplayed && soundsReactions && !disabledSounds.includes(REACTION_SOUND)
  109. && displayReactionSoundsNotification) {
  110. dispatch(displayReactionSoundsNotification());
  111. }
  112. if (soundsReactions) {
  113. const reactionSoundsThresholds = getReactionsSoundsThresholds(reactions);
  114. reactionSoundsThresholds.forEach(reaction =>
  115. dispatch(playSound(`${REACTIONS[reaction.reaction].soundId}${reaction.threshold}`))
  116. );
  117. }
  118. dispatch(setReactionQueue([ ...queue, ...getReactionsWithId(reactions) ]));
  119. });
  120. break;
  121. }
  122. case SEND_REACTIONS: {
  123. const state = getState();
  124. const { buffer } = state['features/reactions'];
  125. const { conference } = state['features/base/conference'];
  126. if (conference) {
  127. conference.sendEndpointMessage('', {
  128. name: ENDPOINT_REACTION_NAME,
  129. reactions: buffer,
  130. timestamp: Date.now()
  131. });
  132. }
  133. break;
  134. }
  135. case SETTINGS_UPDATED: {
  136. const { soundsReactions } = getState()['features/base/settings'];
  137. if (action.settings.soundsReactions === false && soundsReactions === true) {
  138. sendAnalytics(createReactionSoundsDisabledEvent());
  139. }
  140. break;
  141. }
  142. case SHOW_SOUNDS_NOTIFICATION: {
  143. dispatch(showNotification({
  144. titleKey: 'toolbar.disableReactionSounds',
  145. customActionNameKey: 'notify.reactionSounds',
  146. customActionHandler: () => dispatch(updateSettings({
  147. soundsReactions: false
  148. }))
  149. }, NOTIFICATION_TIMEOUT_TYPE.MEDIUM));
  150. break;
  151. }
  152. }
  153. return next(action);
  154. });