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

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