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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // @flow
  2. import { batch } from 'react-redux';
  3. import { ENDPOINT_REACTION_NAME } from '../../../modules/API/constants';
  4. import { MiddlewareRegistry } from '../base/redux';
  5. import { isVpaasMeeting } from '../jaas/functions';
  6. import {
  7. ADD_REACTION_BUFFER,
  8. FLUSH_REACTION_BUFFER,
  9. SEND_REACTIONS,
  10. PUSH_REACTIONS
  11. } from './actionTypes';
  12. import {
  13. addReactionsToChat,
  14. flushReactionBuffer,
  15. pushReactions,
  16. sendReactions,
  17. setReactionQueue
  18. } from './actions.any';
  19. import { getReactionMessageFromBuffer, getReactionsWithId, sendReactionsWebhook } from './functions.any';
  20. declare var APP: Object;
  21. /**
  22. * Middleware which intercepts Reactions actions to handle changes to the
  23. * visibility timeout of the Reactions.
  24. *
  25. * @param {Store} store - The redux store.
  26. * @returns {Function}
  27. */
  28. MiddlewareRegistry.register(store => next => action => {
  29. const { dispatch, getState } = store;
  30. switch (action.type) {
  31. case ADD_REACTION_BUFFER: {
  32. const { timeoutID, buffer } = getState()['features/reactions'];
  33. const { reaction } = action;
  34. clearTimeout(timeoutID);
  35. buffer.push(reaction);
  36. action.buffer = buffer;
  37. action.timeoutID = setTimeout(() => {
  38. dispatch(flushReactionBuffer());
  39. }, 500);
  40. break;
  41. }
  42. case FLUSH_REACTION_BUFFER: {
  43. const state = getState();
  44. const { buffer } = state['features/reactions'];
  45. batch(() => {
  46. dispatch(sendReactions());
  47. dispatch(addReactionsToChat(getReactionMessageFromBuffer(buffer)));
  48. dispatch(pushReactions(buffer));
  49. });
  50. if (isVpaasMeeting(state)) {
  51. sendReactionsWebhook(state, buffer);
  52. }
  53. break;
  54. }
  55. case SEND_REACTIONS: {
  56. const state = getState();
  57. const { buffer } = state['features/reactions'];
  58. const { conference } = state['features/base/conference'];
  59. if (conference) {
  60. conference.sendEndpointMessage('', {
  61. name: ENDPOINT_REACTION_NAME,
  62. reactions: buffer,
  63. timestamp: Date.now()
  64. });
  65. }
  66. break;
  67. }
  68. case PUSH_REACTIONS: {
  69. const queue = store.getState()['features/reactions'].queue;
  70. const reactions = action.reactions;
  71. dispatch(setReactionQueue([ ...queue, ...getReactionsWithId(reactions) ]));
  72. }
  73. }
  74. return next(action);
  75. });