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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // @flow
  2. import { batch } from 'react-redux';
  3. import { ENDPOINT_REACTION_NAME } from '../../../modules/API/constants';
  4. import { APP_WILL_MOUNT, APP_WILL_UNMOUNT } from '../base/app';
  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 {
  18. addReactionsToChat,
  19. flushReactionBuffer,
  20. pushReactions,
  21. sendReactions,
  22. setReactionQueue
  23. } from './actions.any';
  24. import { displayReactionSoundsNotification } from './actions.web';
  25. import { 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. batch(() => {
  84. dispatch(sendReactions());
  85. dispatch(addReactionsToChat(getReactionMessageFromBuffer(buffer)));
  86. dispatch(pushReactions(buffer));
  87. });
  88. if (isVpaasMeeting(state)) {
  89. sendReactionsWebhook(state, buffer);
  90. }
  91. break;
  92. }
  93. case SEND_REACTIONS: {
  94. const state = getState();
  95. const { buffer } = state['features/reactions'];
  96. const { conference } = state['features/base/conference'];
  97. if (conference) {
  98. conference.sendEndpointMessage('', {
  99. name: ENDPOINT_REACTION_NAME,
  100. reactions: buffer,
  101. timestamp: Date.now()
  102. });
  103. }
  104. break;
  105. }
  106. case PUSH_REACTIONS: {
  107. const state = getState();
  108. const { queue, notificationDisplayed } = state['features/reactions'];
  109. const { soundsReactions } = state['features/base/settings'];
  110. const reactions = action.reactions;
  111. batch(() => {
  112. if (!notificationDisplayed && soundsReactions) {
  113. dispatch(displayReactionSoundsNotification());
  114. }
  115. if (soundsReactions) {
  116. const reactionSoundsThresholds = getReactionsSoundsThresholds(reactions);
  117. reactionSoundsThresholds.forEach(reaction =>
  118. dispatch(playSound(`${REACTIONS[reaction.reaction].soundId}${reaction.threshold}`))
  119. );
  120. }
  121. dispatch(setReactionQueue([ ...queue, ...getReactionsWithId(reactions) ]));
  122. });
  123. break;
  124. }
  125. case SHOW_SOUNDS_NOTIFICATION: {
  126. dispatch(showNotification({
  127. titleKey: 'toolbar.disableReactionSounds',
  128. customActionNameKey: 'notify.reactionSounds',
  129. customActionHandler: () => dispatch(updateSettings({
  130. soundsReactions: false
  131. }))
  132. }, NOTIFICATION_TIMEOUT));
  133. break;
  134. }
  135. }
  136. return next(action);
  137. });