Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

middleware.js 4.9KB

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