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.ts 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import { IStore } from '../app/types';
  2. import { CONFERENCE_JOIN_IN_PROGRESS } from '../base/conference/actionTypes';
  3. import { getCurrentConference } from '../base/conference/functions';
  4. import { JitsiConferenceEvents } from '../base/lib-jitsi-meet';
  5. import MiddlewareRegistry from '../base/redux/MiddlewareRegistry';
  6. import StateListenerRegistry from '../base/redux/StateListenerRegistry';
  7. import { playSound } from '../base/sounds/actions';
  8. import { INCOMING_MSG_SOUND_ID } from '../chat/constants';
  9. import { showNotification } from '../notifications/actions';
  10. import { NOTIFICATION_TIMEOUT_TYPE, NOTIFICATION_TYPE } from '../notifications/constants';
  11. import { RECEIVE_POLL } from './actionTypes';
  12. import { clearPolls, receiveAnswer, receivePoll } from './actions';
  13. import {
  14. COMMAND_ANSWER_POLL,
  15. COMMAND_NEW_POLL,
  16. COMMAND_OLD_POLLS
  17. } from './constants';
  18. import { IAnswer, IPoll, IPollData } from './types';
  19. /**
  20. * Set up state change listener to perform maintenance tasks when the conference
  21. * is left or failed, e.g. Clear messages or close the chat modal if it's left
  22. * open.
  23. */
  24. StateListenerRegistry.register(
  25. state => getCurrentConference(state),
  26. (conference, { dispatch }, previousConference) => {
  27. if (conference !== previousConference) {
  28. // conference changed, left or failed...
  29. // clean old polls
  30. dispatch(clearPolls());
  31. }
  32. });
  33. const parsePollData = (pollData: IPollData): IPoll | null => {
  34. if (typeof pollData !== 'object' || pollData === null) {
  35. return null;
  36. }
  37. const { id, senderId, question, answers } = pollData;
  38. if (typeof id !== 'string' || typeof senderId !== 'string'
  39. || typeof question !== 'string' || !(answers instanceof Array)) {
  40. return null;
  41. }
  42. return {
  43. changingVote: false,
  44. senderId,
  45. question,
  46. showResults: true,
  47. lastVote: null,
  48. answers
  49. };
  50. };
  51. MiddlewareRegistry.register(({ dispatch, getState }) => next => action => {
  52. const result = next(action);
  53. switch (action.type) {
  54. case CONFERENCE_JOIN_IN_PROGRESS: {
  55. const { conference } = action;
  56. conference.on(JitsiConferenceEvents.ENDPOINT_MESSAGE_RECEIVED,
  57. (user: any, data: any) => {
  58. data.type === COMMAND_NEW_POLL ? data.senderId = user._id : data.voterId = user._id;
  59. _handleReceivePollsMessage(data, dispatch);
  60. });
  61. conference.on(JitsiConferenceEvents.NON_PARTICIPANT_MESSAGE_RECEIVED,
  62. (id: any, data: any) => {
  63. data.type === COMMAND_NEW_POLL ? data.senderId = id : data.voterId = id;
  64. _handleReceivePollsMessage(data, dispatch);
  65. });
  66. break;
  67. }
  68. // Middleware triggered when a poll is received
  69. case RECEIVE_POLL: {
  70. const state = getState();
  71. const isChatOpen: boolean = state['features/chat'].isOpen;
  72. const isPollsTabFocused: boolean = state['features/chat'].isPollsTabFocused;
  73. // Finally, we notify user they received a new poll if their pane is not opened
  74. if (action.notify && (!isChatOpen || !isPollsTabFocused)) {
  75. dispatch(playSound(INCOMING_MSG_SOUND_ID));
  76. }
  77. break;
  78. }
  79. }
  80. return result;
  81. });
  82. /**
  83. * Handles receiving of polls message command.
  84. *
  85. * @param {Object} data - The json data carried by the polls message.
  86. * @param {Function} dispatch - The dispatch function.
  87. *
  88. * @returns {void}
  89. */
  90. function _handleReceivePollsMessage(data: any, dispatch: IStore['dispatch']) {
  91. switch (data.type) {
  92. case COMMAND_NEW_POLL: {
  93. const { question, answers, pollId, senderId } = data;
  94. const poll = {
  95. changingVote: false,
  96. senderId,
  97. showResults: false,
  98. lastVote: null,
  99. question,
  100. answers: answers.map((answer: IAnswer) => {
  101. return {
  102. name: answer,
  103. voters: []
  104. };
  105. })
  106. };
  107. dispatch(receivePoll(pollId, poll, true));
  108. dispatch(showNotification({
  109. appearance: NOTIFICATION_TYPE.NORMAL,
  110. titleKey: 'polls.notification.title',
  111. descriptionKey: 'polls.notification.description'
  112. }, NOTIFICATION_TIMEOUT_TYPE.MEDIUM));
  113. break;
  114. }
  115. case COMMAND_ANSWER_POLL: {
  116. const { pollId, answers, voterId } = data;
  117. const receivedAnswer: IAnswer = {
  118. voterId,
  119. pollId,
  120. answers
  121. };
  122. dispatch(receiveAnswer(pollId, receivedAnswer));
  123. break;
  124. }
  125. case COMMAND_OLD_POLLS: {
  126. const { polls } = data;
  127. for (const pollData of polls) {
  128. const poll = parsePollData(pollData);
  129. if (poll === null) {
  130. console.warn('[features/polls] Invalid old poll data');
  131. } else {
  132. dispatch(receivePoll(pollData.id, poll, false));
  133. }
  134. }
  135. break;
  136. }
  137. }
  138. }