2018-01-23 21:31:54 +00:00
|
|
|
// @flow
|
|
|
|
|
2018-07-11 09:42:43 +00:00
|
|
|
import { APP_WILL_MOUNT, APP_WILL_UNMOUNT } from '../base/app';
|
2018-01-23 21:31:54 +00:00
|
|
|
import { CONFERENCE_JOINED } from '../base/conference';
|
|
|
|
import { JitsiConferenceEvents } from '../base/lib-jitsi-meet';
|
|
|
|
import { MiddlewareRegistry } from '../base/redux';
|
|
|
|
import { playSound, registerSound, unregisterSound } from '../base/sounds';
|
|
|
|
|
|
|
|
import { INCOMING_MSG_SOUND_ID } from './constants';
|
2018-04-05 19:12:24 +00:00
|
|
|
import { INCOMING_MSG_SOUND_FILE } from './sounds';
|
2018-01-23 21:31:54 +00:00
|
|
|
|
|
|
|
declare var APP: Object;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements the middleware of the chat feature.
|
|
|
|
*
|
|
|
|
* @param {Store} store - The redux store.
|
|
|
|
* @returns {Function}
|
|
|
|
*/
|
|
|
|
MiddlewareRegistry.register(store => next => action => {
|
|
|
|
switch (action.type) {
|
2018-02-27 14:09:49 +00:00
|
|
|
case APP_WILL_MOUNT:
|
|
|
|
// Register the chat message sound on Web only because there's no chat
|
|
|
|
// on mobile.
|
|
|
|
typeof APP === 'undefined'
|
|
|
|
|| store.dispatch(
|
2018-04-05 19:12:24 +00:00
|
|
|
registerSound(INCOMING_MSG_SOUND_ID, INCOMING_MSG_SOUND_FILE));
|
2018-01-23 21:31:54 +00:00
|
|
|
break;
|
2018-02-27 14:09:49 +00:00
|
|
|
|
|
|
|
case APP_WILL_UNMOUNT:
|
|
|
|
// Unregister the chat message sound on Web because it's registered
|
|
|
|
// there only.
|
|
|
|
typeof APP === 'undefined'
|
|
|
|
|| store.dispatch(unregisterSound(INCOMING_MSG_SOUND_ID));
|
2018-01-23 21:31:54 +00:00
|
|
|
break;
|
2018-02-27 14:09:49 +00:00
|
|
|
|
2018-01-23 21:31:54 +00:00
|
|
|
case CONFERENCE_JOINED:
|
2018-02-27 14:09:49 +00:00
|
|
|
typeof APP === 'undefined'
|
|
|
|
|| _addChatMsgListener(action.conference, store);
|
2018-01-23 21:31:54 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return next(action);
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers listener for {@link JitsiConferenceEvents.MESSAGE_RECEIVED} which
|
|
|
|
* will play a sound on the event, given that the chat is not currently visible.
|
|
|
|
*
|
|
|
|
* @param {JitsiConference} conference - The conference instance on which the
|
|
|
|
* new event listener will be registered.
|
|
|
|
* @param {Dispatch} next - The redux dispatch function to dispatch the
|
|
|
|
* specified action to the specified store.
|
|
|
|
* @private
|
2018-02-27 14:09:49 +00:00
|
|
|
* @returns {void}
|
2018-01-23 21:31:54 +00:00
|
|
|
*/
|
|
|
|
function _addChatMsgListener(conference, { dispatch }) {
|
2018-02-27 14:09:49 +00:00
|
|
|
// XXX Currently, there's no need to remove the listener, because the
|
|
|
|
// JitsiConference instance cannot be reused. Hence, the listener will be
|
|
|
|
// gone with the JitsiConference instance.
|
2018-01-23 21:31:54 +00:00
|
|
|
conference.on(
|
|
|
|
JitsiConferenceEvents.MESSAGE_RECEIVED,
|
|
|
|
() => {
|
2018-02-27 14:09:49 +00:00
|
|
|
APP.UI.isChatVisible()
|
|
|
|
|| dispatch(playSound(INCOMING_MSG_SOUND_ID));
|
2018-01-23 21:31:54 +00:00
|
|
|
});
|
|
|
|
}
|