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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 { openDialog } from '../base/dialog';
  8. import { JitsiConferenceEvents } from '../base/lib-jitsi-meet';
  9. import {
  10. getLocalParticipant,
  11. getParticipantById,
  12. getParticipantDisplayName
  13. } from '../base/participants';
  14. import { MiddlewareRegistry, StateListenerRegistry } from '../base/redux';
  15. import { playSound, registerSound, unregisterSound } from '../base/sounds';
  16. import { isButtonEnabled, showToolbox } from '../toolbox';
  17. import { SEND_MESSAGE, SET_PRIVATE_MESSAGE_RECIPIENT } from './actionTypes';
  18. import { addMessage, clearMessages, toggleChat } from './actions';
  19. import { ChatPrivacyDialog } from './components';
  20. import { INCOMING_MSG_SOUND_ID } from './constants';
  21. import { INCOMING_MSG_SOUND_FILE } from './sounds';
  22. declare var APP: Object;
  23. declare var interfaceConfig : Object;
  24. /**
  25. * Timeout for when to show the privacy notice after a private message was received.
  26. *
  27. * E.g. if this value is 20 secs (20000ms), then we show the privacy notice when sending a non private
  28. * message after we have received a private message in the last 20 seconds.
  29. */
  30. const PRIVACY_NOTICE_TIMEOUT = 20 * 1000;
  31. /**
  32. * Implements the middleware of the chat feature.
  33. *
  34. * @param {Store} store - The redux store.
  35. * @returns {Function}
  36. */
  37. MiddlewareRegistry.register(store => next => action => {
  38. const { dispatch } = store;
  39. switch (action.type) {
  40. case APP_WILL_MOUNT:
  41. dispatch(
  42. registerSound(INCOMING_MSG_SOUND_ID, INCOMING_MSG_SOUND_FILE));
  43. break;
  44. case APP_WILL_UNMOUNT:
  45. dispatch(unregisterSound(INCOMING_MSG_SOUND_ID));
  46. break;
  47. case CONFERENCE_JOINED:
  48. _addChatMsgListener(action.conference, store);
  49. break;
  50. case SEND_MESSAGE: {
  51. const state = store.getState();
  52. const { conference } = state['features/base/conference'];
  53. if (conference) {
  54. // There may be cases when we intend to send a private message but we forget to set the
  55. // recipient. This logic tries to mitigate this risk.
  56. const shouldSendPrivateMessageTo = _shouldSendPrivateMessageTo(state, action);
  57. if (shouldSendPrivateMessageTo) {
  58. dispatch(openDialog(ChatPrivacyDialog, {
  59. message: action.message,
  60. participantID: shouldSendPrivateMessageTo
  61. }));
  62. } else {
  63. // Sending the message if privacy notice doesn't need to be shown.
  64. if (typeof APP !== 'undefined') {
  65. APP.API.notifySendingChatMessage(action.message);
  66. }
  67. const { privateMessageRecipient } = state['features/chat'];
  68. if (privateMessageRecipient) {
  69. conference.sendPrivateTextMessage(privateMessageRecipient.id, action.message);
  70. _persistSentPrivateMessage(store, privateMessageRecipient.id, action.message);
  71. } else {
  72. conference.sendTextMessage(action.message);
  73. }
  74. }
  75. }
  76. break;
  77. }
  78. case SET_PRIVATE_MESSAGE_RECIPIENT: {
  79. _maybeFocusField();
  80. break;
  81. }
  82. }
  83. return next(action);
  84. });
  85. /**
  86. * Set up state change listener to perform maintenance tasks when the conference
  87. * is left or failed, e.g. clear messages or close the chat modal if it's left
  88. * open.
  89. */
  90. StateListenerRegistry.register(
  91. state => getCurrentConference(state),
  92. (conference, { dispatch, getState }, previousConference) => {
  93. if (conference !== previousConference) {
  94. // conference changed, left or failed...
  95. if (getState()['features/chat'].isOpen) {
  96. // Closes the chat if it's left open.
  97. dispatch(toggleChat());
  98. }
  99. // Clear chat messages.
  100. dispatch(clearMessages());
  101. }
  102. });
  103. StateListenerRegistry.register(
  104. state => state['features/chat'].isOpen,
  105. (isOpen, { dispatch }) => {
  106. if (typeof APP !== 'undefined' && isOpen) {
  107. dispatch(showToolbox());
  108. }
  109. }
  110. );
  111. /**
  112. * Registers listener for {@link JitsiConferenceEvents.MESSAGE_RECEIVED} that
  113. * will perform various chat related activities.
  114. *
  115. * @param {JitsiConference} conference - The conference instance on which the
  116. * new event listener will be registered.
  117. * @param {Object} store - The redux store object.
  118. * @private
  119. * @returns {void}
  120. */
  121. function _addChatMsgListener(conference, { dispatch, getState }) {
  122. if ((typeof interfaceConfig === 'object' && interfaceConfig.filmStripOnly)
  123. || (typeof APP !== 'undefined' && !isButtonEnabled('chat'))
  124. || getState()['features/base/config'].iAmRecorder) {
  125. // We don't register anything on web if we're in filmStripOnly mode, or
  126. // the chat button is not enabled in interfaceConfig.
  127. // or we are in iAmRecorder mode
  128. return;
  129. }
  130. conference.on(
  131. JitsiConferenceEvents.MESSAGE_RECEIVED,
  132. (id, message, timestamp, nick) => {
  133. _handleReceivedMessage({
  134. dispatch,
  135. getState
  136. }, {
  137. id,
  138. message,
  139. nick,
  140. privateMessage: false,
  141. timestamp
  142. });
  143. }
  144. );
  145. conference.on(
  146. JitsiConferenceEvents.PRIVATE_MESSAGE_RECEIVED,
  147. (id, message, timestamp) => {
  148. _handleReceivedMessage({
  149. dispatch,
  150. getState
  151. }, {
  152. id,
  153. message,
  154. privateMessage: true,
  155. timestamp,
  156. nick: undefined
  157. });
  158. }
  159. );
  160. }
  161. /**
  162. * Function to handle an incoming chat message.
  163. *
  164. * @param {Store} store - The Redux store.
  165. * @param {Object} message - The message object.
  166. * @returns {void}
  167. */
  168. function _handleReceivedMessage({ dispatch, getState }, { id, message, nick, privateMessage, timestamp }) {
  169. // Logic for all platforms:
  170. const state = getState();
  171. const { isOpen: isChatOpen } = state['features/chat'];
  172. if (!isChatOpen) {
  173. dispatch(playSound(INCOMING_MSG_SOUND_ID));
  174. }
  175. // Provide a default for for the case when a message is being
  176. // backfilled for a participant that has left the conference.
  177. const participant = getParticipantById(state, id) || {};
  178. const localParticipant = getLocalParticipant(getState);
  179. const displayName = participant.name || nick || getParticipantDisplayName(state, id);
  180. const hasRead = participant.local || isChatOpen;
  181. const timestampToDate = timestamp
  182. ? new Date(timestamp) : new Date();
  183. const millisecondsTimestamp = timestampToDate.getTime();
  184. dispatch(addMessage({
  185. displayName,
  186. hasRead,
  187. id,
  188. messageType: participant.local ? 'local' : 'remote',
  189. message,
  190. privateMessage,
  191. recipient: getParticipantDisplayName(state, localParticipant.id),
  192. timestamp: millisecondsTimestamp
  193. }));
  194. if (typeof APP !== 'undefined') {
  195. // Logic for web only:
  196. APP.API.notifyReceivedChatMessage({
  197. body: message,
  198. id,
  199. nick: displayName,
  200. ts: timestamp
  201. });
  202. dispatch(showToolbox(4000));
  203. }
  204. }
  205. /**
  206. * Focuses the chat text field on web after the message recipient was updated, if needed.
  207. *
  208. * @returns {void}
  209. */
  210. function _maybeFocusField() {
  211. if (navigator.product !== 'ReactNative') {
  212. const textField = document.getElementById('usermsg');
  213. textField && textField.focus();
  214. }
  215. }
  216. /**
  217. * Persists the sent private messages as if they were received over the muc.
  218. *
  219. * This is required as we rely on the fact that we receive all messages from the muc that we send
  220. * (as they are sent to everybody), but we don't receive the private messages we send to another participant.
  221. * But those messages should be in the store as well, otherwise they don't appear in the chat window.
  222. *
  223. * @param {Store} store - The Redux store.
  224. * @param {string} recipientID - The ID of the recipient the private message was sent to.
  225. * @param {string} message - The sent message.
  226. * @returns {void}
  227. */
  228. function _persistSentPrivateMessage({ dispatch, getState }, recipientID, message) {
  229. const localParticipant = getLocalParticipant(getState);
  230. const displayName = getParticipantDisplayName(getState, localParticipant.id);
  231. dispatch(addMessage({
  232. displayName,
  233. hasRead: true,
  234. id: localParticipant.id,
  235. messageType: 'local',
  236. message,
  237. privateMessage: true,
  238. recipient: getParticipantDisplayName(getState, recipientID),
  239. timestamp: Date.now()
  240. }));
  241. }
  242. /**
  243. * Returns the ID of the participant who we may have wanted to send the message
  244. * that we're about to send.
  245. *
  246. * @param {Object} state - The Redux state.
  247. * @param {Object} action - The action being dispatched now.
  248. * @returns {string?}
  249. */
  250. function _shouldSendPrivateMessageTo(state, action): ?string {
  251. if (action.ignorePrivacy) {
  252. // Shortcut: this is only true, if we already displayed the notice, so no need to show it again.
  253. return undefined;
  254. }
  255. const { messages, privateMessageRecipient } = state['features/chat'];
  256. if (privateMessageRecipient) {
  257. // We're already sending a private message, no need to warn about privacy.
  258. return undefined;
  259. }
  260. if (!messages.length) {
  261. // No messages yet, no need to warn for privacy.
  262. return undefined;
  263. }
  264. // Platforms sort messages differently
  265. const lastMessage = navigator.product === 'ReactNative'
  266. ? messages[0] : messages[messages.length - 1];
  267. if (lastMessage.messageType === 'local') {
  268. // The sender is probably aware of any private messages as already sent
  269. // a message since then. Doesn't make sense to display the notice now.
  270. return undefined;
  271. }
  272. if (lastMessage.privateMessage) {
  273. // We show the notice if the last received message was private.
  274. return lastMessage.id;
  275. }
  276. // But messages may come rapidly, we want to protect our users from mis-sending a message
  277. // even when there was a reasonable recently received private message.
  278. const now = Date.now();
  279. const recentPrivateMessages = messages.filter(
  280. message =>
  281. message.messageType !== 'local'
  282. && message.privateMessage
  283. && message.timestamp + PRIVACY_NOTICE_TIMEOUT > now);
  284. const recentPrivateMessage = navigator.product === 'ReactNative'
  285. ? recentPrivateMessages[0] : recentPrivateMessages[recentPrivateMessages.length - 1];
  286. if (recentPrivateMessage) {
  287. return recentPrivateMessage.id;
  288. }
  289. return undefined;
  290. }