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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. /**
  73. * Registers listener for {@link JitsiConferenceEvents.MESSAGE_RECEIVED} that
  74. * will perform various chat related activities.
  75. *
  76. * @param {JitsiConference} conference - The conference instance on which the
  77. * new event listener will be registered.
  78. * @param {Object} store - The redux store object.
  79. * @private
  80. * @returns {void}
  81. */
  82. function _addChatMsgListener(conference, { dispatch, getState }) {
  83. if ((typeof interfaceConfig === 'object' && interfaceConfig.filmStripOnly)
  84. || (typeof APP !== 'undefined' && !isButtonEnabled('chat'))) {
  85. // We don't register anything on web if we're in filmStripOnly mode, or
  86. // the chat button is not enabled in interfaceConfig.
  87. return;
  88. }
  89. conference.on(
  90. JitsiConferenceEvents.MESSAGE_RECEIVED,
  91. (id, message, timestamp) => {
  92. // Logic for all platforms:
  93. const state = getState();
  94. const { isOpen: isChatOpen } = state['features/chat'];
  95. if (!isChatOpen) {
  96. dispatch(playSound(INCOMING_MSG_SOUND_ID));
  97. }
  98. // Provide a default for for the case when a message is being
  99. // backfilled for a participant that has left the conference.
  100. const participant = getParticipantById(state, id) || {};
  101. const displayName = getParticipantDisplayName(getState, id);
  102. const hasRead = participant.local || isChatOpen;
  103. const timestampToDate = timestamp
  104. ? new Date(timestamp) : new Date();
  105. const millisecondsTimestamp = timestampToDate.getTime();
  106. dispatch(addMessage({
  107. displayName,
  108. hasRead,
  109. id,
  110. messageType: participant.local ? 'local' : 'remote',
  111. message,
  112. timestamp: millisecondsTimestamp
  113. }));
  114. if (typeof APP !== 'undefined') {
  115. // Logic for web only:
  116. APP.API.notifyReceivedChatMessage({
  117. body: message,
  118. id,
  119. nick: displayName,
  120. ts: timestamp
  121. });
  122. dispatch(showToolbox(4000));
  123. }
  124. }
  125. );
  126. }