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

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