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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // @flow
  2. import UIUtil from '../../../modules/UI/util/UIUtil';
  3. import { APP_WILL_MOUNT, APP_WILL_UNMOUNT } from '../base/app';
  4. import {
  5. CONFERENCE_JOINED,
  6. getCurrentConference
  7. } from '../base/conference';
  8. import { JitsiConferenceEvents } from '../base/lib-jitsi-meet';
  9. import {
  10. getParticipantById,
  11. getParticipantDisplayName
  12. } from '../base/participants';
  13. import { MiddlewareRegistry, StateListenerRegistry } from '../base/redux';
  14. import { playSound, registerSound, unregisterSound } from '../base/sounds';
  15. import { isButtonEnabled, showToolbox } from '../toolbox';
  16. import { SEND_MESSAGE } from './actionTypes';
  17. import { addMessage, clearMessages, toggleChat } from './actions';
  18. import { INCOMING_MSG_SOUND_ID } from './constants';
  19. import { INCOMING_MSG_SOUND_FILE } from './sounds';
  20. declare var APP: Object;
  21. declare var interfaceConfig : Object;
  22. /**
  23. * Implements the middleware of the chat feature.
  24. *
  25. * @param {Store} store - The redux store.
  26. * @returns {Function}
  27. */
  28. MiddlewareRegistry.register(store => next => action => {
  29. switch (action.type) {
  30. case APP_WILL_MOUNT:
  31. store.dispatch(
  32. registerSound(INCOMING_MSG_SOUND_ID, INCOMING_MSG_SOUND_FILE));
  33. break;
  34. case APP_WILL_UNMOUNT:
  35. store.dispatch(unregisterSound(INCOMING_MSG_SOUND_ID));
  36. break;
  37. case CONFERENCE_JOINED:
  38. _addChatMsgListener(action.conference, store);
  39. break;
  40. case SEND_MESSAGE: {
  41. const { conference } = store.getState()['features/base/conference'];
  42. if (conference) {
  43. const escapedMessage = UIUtil.escapeHtml(action.message);
  44. if (typeof APP !== 'undefined') {
  45. APP.API.notifySendingChatMessage(escapedMessage);
  46. }
  47. conference.sendTextMessage(escapedMessage);
  48. }
  49. break;
  50. }
  51. }
  52. return next(action);
  53. });
  54. /**
  55. * Set up state change listener to perform maintenance tasks when the conference
  56. * is left or failed, e.g. clear messages or close the chat modal if it's left
  57. * open.
  58. */
  59. StateListenerRegistry.register(
  60. state => getCurrentConference(state),
  61. (conference, { dispatch, getState }, previousConference) => {
  62. if (conference !== previousConference) {
  63. // conference changed, left or failed...
  64. if (getState()['features/chat'].isOpen) {
  65. // Closes the chat if it's left open.
  66. dispatch(toggleChat());
  67. }
  68. // Clear chat messages.
  69. dispatch(clearMessages());
  70. }
  71. });
  72. StateListenerRegistry.register(
  73. state => state['features/chat'].isOpen,
  74. (isOpen, { dispatch }) => {
  75. if (typeof APP !== 'undefined' && isOpen) {
  76. dispatch(showToolbox());
  77. }
  78. }
  79. );
  80. /**
  81. * Registers listener for {@link JitsiConferenceEvents.MESSAGE_RECEIVED} that
  82. * will perform various chat related activities.
  83. *
  84. * @param {JitsiConference} conference - The conference instance on which the
  85. * new event listener will be registered.
  86. * @param {Object} store - The redux store object.
  87. * @private
  88. * @returns {void}
  89. */
  90. function _addChatMsgListener(conference, { dispatch, getState }) {
  91. if ((typeof interfaceConfig === 'object' && interfaceConfig.filmStripOnly)
  92. || (typeof APP !== 'undefined' && !isButtonEnabled('chat'))
  93. || getState()['features/base/config'].iAmRecorder) {
  94. // We don't register anything on web if we're in filmStripOnly mode, or
  95. // the chat button is not enabled in interfaceConfig.
  96. // or we are in iAmRecorder mode
  97. return;
  98. }
  99. conference.on(
  100. JitsiConferenceEvents.MESSAGE_RECEIVED,
  101. (id, message, timestamp, nick) => {
  102. // Logic for all platforms:
  103. const state = getState();
  104. const { isOpen: isChatOpen } = state['features/chat'];
  105. if (!isChatOpen) {
  106. dispatch(playSound(INCOMING_MSG_SOUND_ID));
  107. }
  108. // Provide a default for for the case when a message is being
  109. // backfilled for a participant that has left the conference.
  110. const participant = getParticipantById(state, id) || {};
  111. const displayName = participant.name || nick || getParticipantDisplayName(state, id);
  112. const hasRead = participant.local || isChatOpen;
  113. const timestampToDate = timestamp
  114. ? new Date(timestamp) : new Date();
  115. const millisecondsTimestamp = timestampToDate.getTime();
  116. dispatch(addMessage({
  117. displayName,
  118. hasRead,
  119. id,
  120. messageType: participant.local ? 'local' : 'remote',
  121. message,
  122. timestamp: millisecondsTimestamp
  123. }));
  124. if (typeof APP !== 'undefined') {
  125. // Logic for web only:
  126. APP.API.notifyReceivedChatMessage({
  127. body: message,
  128. id,
  129. nick: displayName,
  130. ts: timestamp
  131. });
  132. dispatch(showToolbox(4000));
  133. }
  134. }
  135. );
  136. }