2018-12-17 19:48:34 +08:00
|
|
|
import React, { PureComponent } from 'react';
|
2016-06-14 01:00:38 +08:00
|
|
|
import { defineMessages, injectIntl } from 'react-intl';
|
2018-01-08 12:44:42 +08:00
|
|
|
import { withTracker } from 'meteor/react-meteor-data';
|
2018-10-12 04:41:37 +08:00
|
|
|
import { Session } from 'meteor/session';
|
2018-10-08 22:22:45 +08:00
|
|
|
import Auth from '/imports/ui/services/auth';
|
2017-07-13 00:47:58 +08:00
|
|
|
import Chat from './component';
|
|
|
|
import ChatService from './service';
|
2016-04-29 03:02:51 +08:00
|
|
|
|
2016-08-17 23:48:03 +08:00
|
|
|
const CHAT_CONFIG = Meteor.settings.public.chat;
|
|
|
|
const PUBLIC_CHAT_KEY = CHAT_CONFIG.public_id;
|
2017-11-22 02:31:53 +08:00
|
|
|
const CHAT_CLEAR = CHAT_CONFIG.system_messages_keys.chat_clear;
|
2016-06-02 00:33:19 +08:00
|
|
|
|
2016-06-14 01:00:38 +08:00
|
|
|
const intlMessages = defineMessages({
|
2017-11-22 02:31:53 +08:00
|
|
|
[CHAT_CLEAR]: {
|
2017-11-23 21:00:41 +08:00
|
|
|
id: 'app.chat.clearPublicChatMessage',
|
|
|
|
description: 'message of when clear the public chat',
|
2017-11-22 02:31:53 +08:00
|
|
|
},
|
2016-06-14 01:00:38 +08:00
|
|
|
titlePublic: {
|
|
|
|
id: 'app.chat.titlePublic',
|
|
|
|
description: 'Public chat title',
|
|
|
|
},
|
|
|
|
titlePrivate: {
|
|
|
|
id: 'app.chat.titlePrivate',
|
|
|
|
description: 'Private chat title',
|
|
|
|
},
|
|
|
|
partnerDisconnected: {
|
|
|
|
id: 'app.chat.partnerDisconnected',
|
|
|
|
description: 'System chat message when the private chat partnet disconnect from the meeting',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2018-12-17 19:48:34 +08:00
|
|
|
class ChatContainer extends PureComponent {
|
2017-12-19 04:01:13 +08:00
|
|
|
componentDidMount() {
|
2017-12-19 04:12:31 +08:00
|
|
|
// in case of reopening a chat, need to make sure it's removed from closed list
|
2018-10-16 04:03:17 +08:00
|
|
|
ChatService.removeFromClosedChatsSession();
|
2017-12-19 04:01:13 +08:00
|
|
|
}
|
2018-12-17 19:48:34 +08:00
|
|
|
|
2017-12-19 03:56:08 +08:00
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<Chat {...this.props}>
|
|
|
|
{this.props.children}
|
|
|
|
</Chat>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2016-04-29 03:02:51 +08:00
|
|
|
|
2018-10-12 04:41:37 +08:00
|
|
|
export default injectIntl(withTracker(({ intl }) => {
|
|
|
|
const chatID = Session.get('idChatOpen') || PUBLIC_CHAT_KEY;
|
2016-06-02 00:33:19 +08:00
|
|
|
let messages = [];
|
2017-11-24 18:52:03 +08:00
|
|
|
let isChatLocked = ChatService.isChatLocked(chatID);
|
2016-12-07 21:48:29 +08:00
|
|
|
let title = intl.formatMessage(intlMessages.titlePublic);
|
|
|
|
let chatName = title;
|
2017-12-01 20:01:07 +08:00
|
|
|
let partnerIsLoggedOut = false;
|
|
|
|
let systemMessageIntl = {};
|
2016-06-02 00:33:19 +08:00
|
|
|
|
|
|
|
if (chatID === PUBLIC_CHAT_KEY) {
|
2018-10-08 22:22:45 +08:00
|
|
|
const { welcomeProp } = ChatService.getMeeting();
|
|
|
|
const user = ChatService.getUser(Auth.userID);
|
|
|
|
|
|
|
|
messages = ChatService.getPublicGroupMessages();
|
|
|
|
|
2019-01-14 21:23:35 +08:00
|
|
|
const time = user.loginTime;
|
2018-10-08 22:22:45 +08:00
|
|
|
const welcomeId = `welcome-msg-${time}`;
|
|
|
|
|
|
|
|
const welcomeMsg = {
|
|
|
|
id: welcomeId,
|
|
|
|
content: [{
|
|
|
|
id: welcomeId,
|
|
|
|
text: welcomeProp.welcomeMsg,
|
|
|
|
time,
|
|
|
|
}],
|
|
|
|
time,
|
|
|
|
sender: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
const moderatorTime = time + 1;
|
|
|
|
const moderatorId = `moderator-msg-${moderatorTime}`;
|
|
|
|
|
|
|
|
const moderatorMsg = {
|
|
|
|
id: moderatorId,
|
|
|
|
content: [{
|
|
|
|
id: moderatorId,
|
|
|
|
text: welcomeProp.modOnlyMessage,
|
|
|
|
time: moderatorTime,
|
|
|
|
}],
|
|
|
|
time: moderatorTime,
|
|
|
|
sender: null,
|
|
|
|
};
|
|
|
|
|
2019-03-26 07:02:37 +08:00
|
|
|
const messagesBeforeWelcomeMsg = ChatService.reduceAndMapGroupMessages(
|
|
|
|
messages.filter(message => message.timestamp < time));
|
|
|
|
const messagesAfterWelcomeMsg = ChatService.reduceAndMapGroupMessages(
|
|
|
|
messages.filter(message => message.timestamp >= time));
|
2018-10-08 22:22:45 +08:00
|
|
|
|
|
|
|
const messagesFormated = messagesBeforeWelcomeMsg
|
2019-03-05 01:33:59 +08:00
|
|
|
.concat(welcomeMsg)
|
2019-03-26 07:02:37 +08:00
|
|
|
.concat(user.isModerator ? moderatorMsg : [])
|
2018-10-08 22:22:45 +08:00
|
|
|
.concat(messagesAfterWelcomeMsg);
|
|
|
|
|
|
|
|
messages = messagesFormated.sort((a, b) => (a.time - b.time));
|
2016-06-02 00:33:19 +08:00
|
|
|
} else {
|
2018-10-17 00:06:37 +08:00
|
|
|
messages = ChatService.getPrivateGroupMessages();
|
2018-07-26 22:56:26 +08:00
|
|
|
|
2017-12-01 20:01:07 +08:00
|
|
|
const user = ChatService.getUser(chatID);
|
|
|
|
chatName = user.name;
|
|
|
|
systemMessageIntl = { 0: user.name };
|
|
|
|
title = intl.formatMessage(intlMessages.titlePrivate, systemMessageIntl);
|
|
|
|
partnerIsLoggedOut = !user.isOnline;
|
|
|
|
|
|
|
|
if (partnerIsLoggedOut) {
|
|
|
|
const time = Date.now();
|
|
|
|
const id = `partner-disconnected-${time}`;
|
|
|
|
const messagePartnerLoggedOut = {
|
|
|
|
id,
|
|
|
|
content: [{
|
|
|
|
id,
|
|
|
|
text: 'partnerDisconnected',
|
|
|
|
time,
|
|
|
|
}],
|
|
|
|
time,
|
|
|
|
sender: null,
|
|
|
|
};
|
2016-12-07 21:48:29 +08:00
|
|
|
|
2017-12-01 20:01:07 +08:00
|
|
|
messages.push(messagePartnerLoggedOut);
|
|
|
|
isChatLocked = true;
|
|
|
|
}
|
|
|
|
}
|
2017-04-12 20:03:14 +08:00
|
|
|
|
2017-11-22 02:31:53 +08:00
|
|
|
messages = messages.map((message) => {
|
|
|
|
if (message.sender) return message;
|
|
|
|
|
|
|
|
return {
|
|
|
|
...message,
|
|
|
|
content: message.content.map(content => ({
|
|
|
|
...content,
|
2019-03-26 07:02:37 +08:00
|
|
|
text: content.text in intlMessages
|
|
|
|
? `<b><i>${intl.formatMessage(intlMessages[content.text], systemMessageIntl)}</i></b>` : content.text,
|
2017-11-22 02:31:53 +08:00
|
|
|
})),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2016-07-01 01:10:36 +08:00
|
|
|
const scrollPosition = ChatService.getScrollPosition(chatID);
|
2016-07-06 00:47:40 +08:00
|
|
|
const hasUnreadMessages = ChatService.hasUnreadMessages(chatID);
|
2016-07-12 03:42:54 +08:00
|
|
|
const lastReadMessageTime = ChatService.lastReadMessageTime(chatID);
|
2016-07-01 01:10:36 +08:00
|
|
|
|
2019-06-03 22:19:56 +08:00
|
|
|
const { connected: meteorIsConnected } = Meteor.status();
|
|
|
|
|
2016-06-02 00:33:19 +08:00
|
|
|
return {
|
2018-12-19 22:39:32 +08:00
|
|
|
chatID,
|
2016-12-07 21:48:29 +08:00
|
|
|
chatName,
|
2016-06-02 00:33:19 +08:00
|
|
|
title,
|
|
|
|
messages,
|
2016-07-12 03:42:54 +08:00
|
|
|
lastReadMessageTime,
|
2016-07-05 02:53:47 +08:00
|
|
|
hasUnreadMessages,
|
2017-04-07 05:08:20 +08:00
|
|
|
partnerIsLoggedOut,
|
2016-06-07 22:19:19 +08:00
|
|
|
isChatLocked,
|
2016-07-01 01:10:36 +08:00
|
|
|
scrollPosition,
|
2019-06-03 22:19:56 +08:00
|
|
|
meteorIsConnected,
|
2017-06-01 22:24:29 +08:00
|
|
|
minMessageLength: CHAT_CONFIG.min_message_length,
|
|
|
|
maxMessageLength: CHAT_CONFIG.max_message_length,
|
2019-02-09 03:23:35 +08:00
|
|
|
UnsentMessagesCollection: ChatService.UnsentMessagesCollection,
|
2016-06-03 02:40:27 +08:00
|
|
|
actions: {
|
2017-07-13 00:47:58 +08:00
|
|
|
handleClosePrivateChat: chatId => ChatService.closePrivateChat(chatId),
|
2017-03-18 02:25:41 +08:00
|
|
|
|
2017-06-03 03:25:02 +08:00
|
|
|
handleSendMessage: (message) => {
|
2018-10-16 04:03:17 +08:00
|
|
|
ChatService.updateScrollPosition(null);
|
|
|
|
return ChatService.sendGroupMessage(message);
|
2016-07-01 01:10:36 +08:00
|
|
|
},
|
|
|
|
|
2018-10-16 04:03:17 +08:00
|
|
|
handleScrollUpdate: position => ChatService.updateScrollPosition(position),
|
2016-07-05 02:53:47 +08:00
|
|
|
|
2018-10-16 04:03:17 +08:00
|
|
|
handleReadMessage: timestamp => ChatService.updateUnreadMessage(timestamp),
|
2016-06-03 02:40:27 +08:00
|
|
|
},
|
2016-06-02 00:33:19 +08:00
|
|
|
};
|
2018-01-08 12:44:42 +08:00
|
|
|
})(ChatContainer));
|