bigbluebutton-Github/bigbluebutton-html5/imports/ui/components/chat/alert/component.jsx

224 lines
7.4 KiB
React
Raw Normal View History

2019-04-10 01:04:33 +08:00
import React, { PureComponent, Fragment } from 'react';
import PropTypes from 'prop-types';
import { defineMessages, injectIntl } from 'react-intl';
2018-10-17 01:48:27 +08:00
import { Session } from 'meteor/session';
import _ from 'lodash';
import UnreadMessages from '/imports/ui/services/unread-messages';
import ChatAudioAlert from './audio-alert/component';
import ChatPushAlert from './push-alert/component';
import Service from '../service';
import { styles } from '../styles';
const propTypes = {
2019-01-14 21:23:35 +08:00
pushAlertDisabled: PropTypes.bool.isRequired,
activeChats: PropTypes.arrayOf(PropTypes.object).isRequired,
audioAlertDisabled: PropTypes.bool.isRequired,
joinTimestamp: PropTypes.number.isRequired,
};
const intlMessages = defineMessages({
appToastChatPublic: {
id: 'app.toast.chat.public',
description: 'when entry various message',
},
appToastChatPrivate: {
id: 'app.toast.chat.private',
description: 'when entry various message',
},
appToastChatSystem: {
id: 'app.toast.chat.system',
description: 'system for use',
},
publicChatClear: {
id: 'app.chat.clearPublicChatMessage',
description: 'message of when clear the public chat',
},
});
2019-01-14 21:23:35 +08:00
const ALERT_INTERVAL = 5000; // 5 seconds
const ALERT_DURATION = 4000; // 4 seconds
class ChatAlert extends PureComponent {
constructor(props) {
super(props);
this.state = {
2019-01-15 00:23:11 +08:00
alertEnabledTimestamp: props.joinTimestamp,
2019-01-14 21:23:35 +08:00
lastAlertTimestampByChat: {},
pendingNotificationsByChat: {},
};
}
2019-01-14 21:23:35 +08:00
componentDidUpdate(prevProps) {
const {
2019-01-14 21:23:35 +08:00
pushAlertDisabled,
activeChats,
joinTimestamp,
} = this.props;
2019-01-14 21:23:35 +08:00
const {
2019-01-15 00:23:11 +08:00
alertEnabledTimestamp,
2019-01-14 21:23:35 +08:00
lastAlertTimestampByChat,
pendingNotificationsByChat,
} = this.state;
// Avoid alerting messages received before enabling alerts
if (prevProps.pushAlertDisabled && !pushAlertDisabled) {
2019-01-16 21:08:20 +08:00
const newAlertEnabledTimestamp = Service.getLastMessageTimestampFromChatList(activeChats);
this.setAlertEnabledTimestamp(newAlertEnabledTimestamp);
return;
}
2019-01-14 21:23:35 +08:00
// Keep track of messages that was not alerted yet
const unalertedMessagesByChatId = {};
activeChats
.filter(chat => chat.id !== Session.get('idChatOpen'))
.filter(chat => chat.unreadCounter > 0)
.forEach((chat) => {
const chatId = (chat.id === 'public') ? 'MAIN-PUBLIC-GROUP-CHAT' : chat.id;
const thisChatUnreadMessages = UnreadMessages.getUnreadMessages(chatId);
unalertedMessagesByChatId[chatId] = thisChatUnreadMessages.filter((msg) => {
const messageChatId = (msg.chatId === 'MAIN-PUBLIC-GROUP-CHAT') ? msg.chatId : msg.sender;
const retorno = (msg
2019-01-15 00:23:11 +08:00
&& msg.timestamp > alertEnabledTimestamp
2019-01-14 21:23:35 +08:00
&& msg.timestamp > joinTimestamp
&& msg.timestamp > (lastAlertTimestampByChat[messageChatId] || 0)
);
return retorno;
});
if (!unalertedMessagesByChatId[chatId].length) delete unalertedMessagesByChatId[chatId];
});
2019-01-14 21:23:35 +08:00
const lastUnalertedMessageTimestampByChat = {};
Object.keys(unalertedMessagesByChatId).forEach((chatId) => {
lastUnalertedMessageTimestampByChat[chatId] = unalertedMessagesByChatId[chatId]
.reduce(Service.maxTimestampReducer, 0);
});
2019-01-14 21:23:35 +08:00
// Keep track of chats that need to be alerted now (considering alert interval)
const chatsWithPendingAlerts = Object.keys(lastUnalertedMessageTimestampByChat)
.filter(chatId => lastUnalertedMessageTimestampByChat[chatId]
> ((lastAlertTimestampByChat[chatId] || 0) + ALERT_INTERVAL)
&& !(chatId in pendingNotificationsByChat));
if (!chatsWithPendingAlerts.length) return;
const newPendingNotificationsByChat = Object.assign({},
...chatsWithPendingAlerts.map(chatId => ({ [chatId]: unalertedMessagesByChatId[chatId] })));
// Mark messages as alerted
const newLastAlertTimestampByChat = { ...lastAlertTimestampByChat };
chatsWithPendingAlerts.forEach(
(chatId) => {
newLastAlertTimestampByChat[chatId] = lastUnalertedMessageTimestampByChat[chatId];
},
);
2019-04-10 01:04:33 +08:00
this.setChatMessagesState(newPendingNotificationsByChat, newLastAlertTimestampByChat);
2019-01-14 21:23:35 +08:00
}
2019-01-16 21:08:20 +08:00
setAlertEnabledTimestamp(newAlertEnabledTimestamp) {
2019-01-15 00:23:11 +08:00
const { alertEnabledTimestamp } = this.state;
2019-01-16 21:08:20 +08:00
if (newAlertEnabledTimestamp > 0 && alertEnabledTimestamp !== newAlertEnabledTimestamp) {
this.setState({ alertEnabledTimestamp: newAlertEnabledTimestamp });
2019-01-14 21:23:35 +08:00
}
}
2019-01-14 21:23:35 +08:00
setChatMessagesState(pendingNotificationsByChat, lastAlertTimestampByChat) {
this.setState({ pendingNotificationsByChat, lastAlertTimestampByChat });
}
mapContentText(message) {
const {
intl,
} = this.props;
const contentMessage = message
.map((content) => {
if (content.text === 'PUBLIC_CHAT_CLEAR') return intl.formatMessage(intlMessages.publicChatClear);
2019-01-14 21:23:35 +08:00
/* this code is to remove html tags that come in the server's messages */
const tempDiv = document.createElement('div');
tempDiv.innerHTML = content.text;
const textWithoutTag = tempDiv.innerText;
return textWithoutTag;
});
2019-01-14 21:23:35 +08:00
return contentMessage;
}
createMessage(name, message) {
return (
<div className={styles.pushMessageContent}>
<h3 className={styles.userNameMessage}>{name}</h3>
<div className={styles.contentMessage}>
{
this.mapContentText(message)
2019-01-14 21:23:35 +08:00
.reduce((acc, text) => [...acc, (<br key={_.uniqueId('br_')} />), text], [])
}
</div>
</div>
);
}
2019-01-14 21:23:35 +08:00
render() {
const {
2019-01-14 21:23:35 +08:00
audioAlertDisabled,
pushAlertDisabled,
intl,
} = this.props;
const {
2019-01-14 21:23:35 +08:00
pendingNotificationsByChat,
} = this.state;
2019-01-14 21:23:35 +08:00
const shouldPlay = Object.keys(pendingNotificationsByChat).length > 0;
return (
2019-04-10 01:04:33 +08:00
<Fragment>
2019-01-14 21:23:35 +08:00
{!audioAlertDisabled ? <ChatAudioAlert play={shouldPlay} /> : null}
{
2019-01-14 21:23:35 +08:00
!pushAlertDisabled
? Object.keys(pendingNotificationsByChat)
.map((chatId) => {
// Only display the latest group of messages (up to 5 messages)
const reducedMessage = Service
.reduceAndMapGroupMessages(pendingNotificationsByChat[chatId].slice(-5)).pop();
if (!reducedMessage) return null;
const content = this
.createMessage(reducedMessage.sender.name, reducedMessage.content);
return (
<ChatPushAlert
key={chatId}
chatId={chatId}
content={content}
title={
(chatId === 'MAIN-PUBLIC-GROUP-CHAT')
? <span>{intl.formatMessage(intlMessages.appToastChatPublic)}</span>
: <span>{intl.formatMessage(intlMessages.appToastChatPrivate)}</span>
}
onOpen={
() => {
let pendingNotifications = pendingNotificationsByChat;
delete pendingNotifications[chatId];
pendingNotifications = { ...pendingNotifications };
this.setState({ pendingNotificationsByChat: pendingNotifications });
}}
alertDuration={ALERT_DURATION}
/>
);
})
: null
}
2019-04-10 01:04:33 +08:00
</Fragment>
);
}
}
ChatAlert.propTypes = propTypes;
export default injectIntl(ChatAlert);