2016-04-29 03:02:51 +08:00
|
|
|
import React, { Component, PropTypes } from 'react';
|
2016-06-14 01:00:38 +08:00
|
|
|
import { defineMessages, injectIntl } from 'react-intl';
|
2016-04-29 03:02:51 +08:00
|
|
|
import { createContainer } from 'meteor/react-meteor-data';
|
|
|
|
|
2016-07-01 01:10:36 +08:00
|
|
|
const PUBLIC_CHAT_KEY = 'public';
|
|
|
|
|
2016-05-23 21:09:47 +08:00
|
|
|
import Chat from './component';
|
2016-06-02 00:33:19 +08:00
|
|
|
import ChatService from './service';
|
|
|
|
|
2016-06-14 01:00:38 +08:00
|
|
|
const intlMessages = defineMessages({
|
|
|
|
titlePublic: {
|
|
|
|
id: 'app.chat.titlePublic',
|
|
|
|
defaultMessage: 'Public Chat',
|
|
|
|
description: 'Public chat title',
|
|
|
|
},
|
|
|
|
titlePrivate: {
|
|
|
|
id: 'app.chat.titlePrivate',
|
|
|
|
defaultMessage: 'Private Chat with {name}',
|
|
|
|
description: 'Private chat title',
|
|
|
|
},
|
|
|
|
partnerDisconnected: {
|
|
|
|
id: 'app.chat.partnerDisconnected',
|
|
|
|
defaultMessage: '{name} has left the meeting',
|
|
|
|
description: 'System chat message when the private chat partnet disconnect from the meeting',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2016-04-29 03:02:51 +08:00
|
|
|
class ChatContainer extends Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
2016-06-02 00:33:19 +08:00
|
|
|
<Chat {...this.props}>
|
2016-04-29 03:02:51 +08:00
|
|
|
{this.props.children}
|
|
|
|
</Chat>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-14 01:00:38 +08:00
|
|
|
export default injectIntl(createContainer(({ params, intl }) => {
|
2016-06-02 00:33:19 +08:00
|
|
|
const chatID = params.chatID || PUBLIC_CHAT_KEY;
|
|
|
|
|
|
|
|
let messages = [];
|
2016-06-14 01:00:38 +08:00
|
|
|
let isChatLocked = ChatService.isChatLocked(chatID);
|
2016-06-02 00:33:19 +08:00
|
|
|
|
|
|
|
if (chatID === PUBLIC_CHAT_KEY) {
|
|
|
|
messages = ChatService.getPublicMessages();
|
2016-06-14 01:00:38 +08:00
|
|
|
title = intl.formatMessage(intlMessages.titlePublic);
|
2016-06-02 00:33:19 +08:00
|
|
|
} else {
|
|
|
|
messages = ChatService.getPrivateMessages(chatID);
|
2016-06-07 22:19:19 +08:00
|
|
|
let user = ChatService.getUser(chatID);
|
|
|
|
|
2016-06-14 01:00:38 +08:00
|
|
|
if (user) {
|
|
|
|
title = intl.formatMessage(intlMessages.titlePrivate, { name: user.name });
|
|
|
|
} else {
|
2016-06-14 01:15:15 +08:00
|
|
|
// let partnerName = messages.find(m => m.user && m.user.id === chatID).map(m => m.user.name);
|
2016-07-11 20:34:58 +08:00
|
|
|
let partnerName = '{{NAME}}'; // placeholder until the server sends the name
|
2016-06-14 01:00:38 +08:00
|
|
|
messages.push({
|
|
|
|
content: [intl.formatMessage(intlMessages.partnerDisconnected, { name: partnerName })],
|
|
|
|
time: Date.now(),
|
|
|
|
sender: null,
|
|
|
|
});
|
|
|
|
isChatLocked = true;
|
|
|
|
}
|
2016-06-02 00:33:19 +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
|
|
|
|
2016-06-02 00:33:19 +08:00
|
|
|
return {
|
2016-07-01 01:10:36 +08:00
|
|
|
chatID,
|
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,
|
2016-06-07 22:19:19 +08:00
|
|
|
isChatLocked,
|
2016-07-01 01:10:36 +08:00
|
|
|
scrollPosition,
|
2016-06-03 02:40:27 +08:00
|
|
|
actions: {
|
2016-07-01 01:10:36 +08:00
|
|
|
handleSendMessage: message => {
|
2016-07-06 00:47:40 +08:00
|
|
|
let sentMessage = ChatService.sendMessage(chatID, message);
|
2016-07-11 20:34:58 +08:00
|
|
|
ChatService.updateScrollPosition(chatID, null); //null so its scrolls to bottom
|
2016-07-07 20:50:32 +08:00
|
|
|
// ChatService.updateUnreadMessage(chatID, sentMessage.from_time);
|
2016-07-01 01:10:36 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
handleScrollUpdate: position => ChatService.updateScrollPosition(chatID, position),
|
2016-07-05 02:53:47 +08:00
|
|
|
|
|
|
|
handleReadMessage: timestamp => ChatService.updateUnreadMessage(chatID, timestamp),
|
2016-06-03 02:40:27 +08:00
|
|
|
},
|
2016-06-02 00:33:19 +08:00
|
|
|
};
|
2016-06-14 01:00:38 +08:00
|
|
|
}, ChatContainer));
|