您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

middleware.js 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // @flow
  2. import UIUtil from '../../../modules/UI/util/UIUtil';
  3. import { APP_WILL_MOUNT, APP_WILL_UNMOUNT } from '../base/app';
  4. import { CONFERENCE_JOINED, CONFERENCE_WILL_LEAVE } from '../base/conference';
  5. import { JitsiConferenceEvents } from '../base/lib-jitsi-meet';
  6. import { getParticipantById } from '../base/participants';
  7. import { MiddlewareRegistry } from '../base/redux';
  8. import { playSound, registerSound, unregisterSound } from '../base/sounds';
  9. import { isButtonEnabled, showToolbox } from '../toolbox';
  10. import { SEND_MESSAGE } from './actionTypes';
  11. import { addMessage, clearMessages } from './actions';
  12. import { INCOMING_MSG_SOUND_ID } from './constants';
  13. import { INCOMING_MSG_SOUND_FILE } from './sounds';
  14. declare var APP: Object;
  15. declare var interfaceConfig : Object;
  16. /**
  17. * Implements the middleware of the chat feature.
  18. *
  19. * @param {Store} store - The redux store.
  20. * @returns {Function}
  21. */
  22. MiddlewareRegistry.register(store => next => action => {
  23. switch (action.type) {
  24. case APP_WILL_MOUNT:
  25. // Register the chat message sound on Web only because there's no chat
  26. // on mobile.
  27. typeof APP === 'undefined'
  28. || store.dispatch(
  29. registerSound(INCOMING_MSG_SOUND_ID, INCOMING_MSG_SOUND_FILE));
  30. break;
  31. case APP_WILL_UNMOUNT:
  32. // Unregister the chat message sound on Web because it's registered
  33. // there only.
  34. typeof APP === 'undefined'
  35. || store.dispatch(unregisterSound(INCOMING_MSG_SOUND_ID));
  36. break;
  37. case CONFERENCE_JOINED:
  38. typeof APP === 'undefined'
  39. || _addChatMsgListener(action.conference, store);
  40. break;
  41. case CONFERENCE_WILL_LEAVE:
  42. store.dispatch(clearMessages());
  43. break;
  44. case SEND_MESSAGE:
  45. if (typeof APP !== 'undefined') {
  46. const { conference } = store.getState()['features/base/conference'];
  47. if (conference) {
  48. const escapedMessage = UIUtil.escapeHtml(action.message);
  49. APP.API.notifySendingChatMessage(escapedMessage);
  50. conference.sendTextMessage(escapedMessage);
  51. }
  52. }
  53. break;
  54. }
  55. return next(action);
  56. });
  57. /**
  58. * Registers listener for {@link JitsiConferenceEvents.MESSAGE_RECEIVED} which
  59. * will play a sound on the event, given that the chat is not currently visible.
  60. *
  61. * @param {JitsiConference} conference - The conference instance on which the
  62. * new event listener will be registered.
  63. * @param {Object} store - The redux store object.
  64. * @private
  65. * @returns {void}
  66. */
  67. function _addChatMsgListener(conference, { dispatch, getState }) {
  68. if ((typeof interfaceConfig === 'object' && interfaceConfig.filmStripOnly)
  69. || !isButtonEnabled('chat')) {
  70. return;
  71. }
  72. conference.on(
  73. JitsiConferenceEvents.MESSAGE_RECEIVED,
  74. (id, message, timestamp) => {
  75. const state = getState();
  76. const { isOpen: isChatOpen } = state['features/chat'];
  77. if (!isChatOpen) {
  78. dispatch(playSound(INCOMING_MSG_SOUND_ID));
  79. dispatch(showToolbox(4000));
  80. }
  81. // Provide a default for for the case when a message is being
  82. // backfilled for a participant that has left the conference.
  83. const participant = getParticipantById(state, id) || {};
  84. const displayName = participant.name
  85. || `${interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME} (${id})`;
  86. const hasRead = participant.local || isChatOpen;
  87. APP.API.notifyReceivedChatMessage({
  88. body: message,
  89. id,
  90. nick: displayName,
  91. ts: timestamp
  92. });
  93. const timestampToDate = timestamp
  94. ? new Date(timestamp) : new Date();
  95. const millisecondsTimestamp = timestampToDate.getTime();
  96. dispatch(addMessage({
  97. displayName,
  98. hasRead,
  99. id,
  100. messageType: participant.local ? 'local' : 'remote',
  101. message,
  102. timestamp: millisecondsTimestamp
  103. }));
  104. }
  105. );
  106. }