2021-01-20 01:06:32 +08:00
|
|
|
import React, { useEffect, useContext, useState } 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';
|
2021-02-02 20:59:39 +08:00
|
|
|
import _ from 'lodash';
|
2018-10-08 22:22:45 +08:00
|
|
|
import Auth from '/imports/ui/services/auth';
|
2020-08-15 01:13:51 +08:00
|
|
|
import Storage from '/imports/ui/services/storage/session';
|
|
|
|
import { meetingIsBreakout } from '/imports/ui/components/app/service';
|
2021-01-20 01:06:32 +08:00
|
|
|
import { ChatContext, getLoginTime } from '../components-data/chat-context/context';
|
2021-02-02 06:12:04 +08:00
|
|
|
import { GroupChatContext } from '../components-data/group-chat-context/context';
|
2021-01-20 01:06:32 +08:00
|
|
|
import ChatLogger from '/imports/ui/components/chat/chat-logger/ChatLogger';
|
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;
|
2021-01-20 01:06:32 +08:00
|
|
|
const PUBLIC_GROUP_CHAT_KEY = CHAT_CONFIG.public_group_id;
|
2017-11-22 02:31:53 +08:00
|
|
|
const CHAT_CLEAR = CHAT_CONFIG.system_messages_keys.chat_clear;
|
2020-01-22 05:08:48 +08:00
|
|
|
const SYSTEM_CHAT_TYPE = CHAT_CONFIG.type_system;
|
2019-08-09 02:53:08 +08:00
|
|
|
const ROLE_MODERATOR = Meteor.settings.public.user.role_moderator;
|
|
|
|
const CONNECTION_STATUS = 'online';
|
2021-02-04 04:49:58 +08:00
|
|
|
const DEBOUNCE_TIME = 1000;
|
2016-06-02 00:33:19 +08:00
|
|
|
|
2021-01-20 01:06:32 +08:00
|
|
|
const sysMessagesIds = {
|
|
|
|
welcomeId: `${SYSTEM_CHAT_TYPE}-welcome-msg`,
|
|
|
|
moderatorId: `${SYSTEM_CHAT_TYPE}-moderator-msg`
|
2021-02-13 01:43:04 +08:00
|
|
|
};
|
2021-01-20 01:06:32 +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',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-02-02 06:12:04 +08:00
|
|
|
let previousChatId = null;
|
|
|
|
let debounceTimeout = null;
|
2021-02-09 00:55:10 +08:00
|
|
|
let messages = null;
|
2021-02-13 01:43:04 +08:00
|
|
|
let globalAppplyStateToProps = () => { }
|
2021-02-04 04:49:58 +08:00
|
|
|
|
2021-02-13 01:43:04 +08:00
|
|
|
const throttledFunc = _.throttle(() => {
|
2021-02-04 04:49:58 +08:00
|
|
|
globalAppplyStateToProps();
|
2021-02-13 01:43:04 +08:00
|
|
|
}, DEBOUNCE_TIME, { trailing: true, leading: true });
|
2021-02-04 04:49:58 +08:00
|
|
|
|
2021-01-20 01:06:32 +08:00
|
|
|
const ChatContainer = (props) => {
|
|
|
|
useEffect(() => {
|
|
|
|
ChatService.removeFromClosedChatsSession();
|
|
|
|
}, []);
|
2021-02-13 01:43:04 +08:00
|
|
|
|
2021-01-20 01:06:32 +08:00
|
|
|
const modOnlyMessage = Storage.getItem('ModeratorOnlyMessage');
|
|
|
|
const { welcomeProp } = ChatService.getWelcomeProp();
|
2021-02-13 01:43:04 +08:00
|
|
|
|
2021-01-20 01:06:32 +08:00
|
|
|
const {
|
|
|
|
children,
|
|
|
|
unmounting,
|
|
|
|
chatID,
|
|
|
|
amIModerator,
|
|
|
|
loginTime,
|
2021-02-02 06:12:04 +08:00
|
|
|
intl,
|
2021-01-20 01:06:32 +08:00
|
|
|
} = props;
|
2021-02-13 01:43:04 +08:00
|
|
|
|
2021-02-02 06:12:04 +08:00
|
|
|
const isPublicChat = chatID === PUBLIC_CHAT_KEY;
|
2021-01-20 01:06:32 +08:00
|
|
|
const systemMessages = {
|
2021-02-13 01:43:04 +08:00
|
|
|
[sysMessagesIds.welcomeId]: {
|
2021-01-20 01:06:32 +08:00
|
|
|
id: sysMessagesIds.welcomeId,
|
|
|
|
content: [{
|
|
|
|
id: sysMessagesIds.welcomeId,
|
|
|
|
text: welcomeProp.welcomeMsg,
|
|
|
|
time: loginTime,
|
|
|
|
}],
|
|
|
|
key: sysMessagesIds.welcomeId,
|
|
|
|
time: loginTime,
|
|
|
|
sender: null,
|
|
|
|
},
|
|
|
|
[sysMessagesIds.moderatorId]: {
|
|
|
|
id: sysMessagesIds.moderatorId,
|
|
|
|
content: [{
|
|
|
|
id: sysMessagesIds.moderatorId,
|
|
|
|
text: modOnlyMessage,
|
2021-02-13 01:43:04 +08:00
|
|
|
time: loginTime + 1,
|
2021-01-20 01:06:32 +08:00
|
|
|
}],
|
|
|
|
key: sysMessagesIds.moderatorId,
|
2021-02-13 01:43:04 +08:00
|
|
|
time: loginTime + 1,
|
2021-01-20 01:06:32 +08:00
|
|
|
sender: null,
|
2020-03-06 07:14:22 +08:00
|
|
|
}
|
2021-01-20 01:06:32 +08:00
|
|
|
};
|
2021-02-13 03:13:21 +08:00
|
|
|
|
2021-02-13 01:43:04 +08:00
|
|
|
const systemMessagesIds = [sysMessagesIds.welcomeId, amIModerator && modOnlyMessage && sysMessagesIds.moderatorId].filter(i => i);
|
2020-03-06 07:14:22 +08:00
|
|
|
|
2021-01-20 01:06:32 +08:00
|
|
|
const usingChatContext = useContext(ChatContext);
|
2021-02-02 06:12:04 +08:00
|
|
|
const usingGroupChatContext = useContext(GroupChatContext);
|
2021-01-20 01:06:32 +08:00
|
|
|
const [stateLastMsg, setLastMsg] = useState(null);
|
2021-02-13 01:43:04 +08:00
|
|
|
const [stateTimeWindows, setTimeWindows] = useState(isPublicChat ? [...systemMessagesIds.map((item) => systemMessages[item])] : []);
|
|
|
|
|
2021-02-02 06:12:04 +08:00
|
|
|
const { groupChat } = usingGroupChatContext;
|
|
|
|
const participants = groupChat[chatID]?.participants;
|
2021-02-13 01:43:04 +08:00
|
|
|
const chatName = participants?.filter((user) => user.id !== Auth.userID)[0]?.name;
|
2021-02-13 04:54:44 +08:00
|
|
|
const title = chatName ? intl.formatMessage(intlMessages.titlePrivate, { 0: chatName}) : intl.formatMessage(intlMessages.titlePublic);
|
2021-02-13 03:13:21 +08:00
|
|
|
|
2021-01-20 01:06:32 +08:00
|
|
|
if (unmounting === true) {
|
|
|
|
return null;
|
2017-12-19 03:56:08 +08:00
|
|
|
}
|
2021-02-13 01:43:04 +08:00
|
|
|
|
2021-02-02 06:12:04 +08:00
|
|
|
const contextChat = usingChatContext?.chats[isPublicChat ? PUBLIC_GROUP_CHAT_KEY : chatID];
|
2021-01-20 01:06:32 +08:00
|
|
|
const lastTimeWindow = contextChat?.lastTimewindow;
|
2021-02-13 01:43:04 +08:00
|
|
|
const lastMsg = contextChat && (isPublicChat
|
|
|
|
? contextChat.preJoinMessages[lastTimeWindow] || contextChat.posJoinMessages[lastTimeWindow]
|
|
|
|
: contextChat.messageGroups[lastTimeWindow]);
|
|
|
|
applyPropsToState = () => {
|
|
|
|
if (!_.isEqualWith(lastMsg, stateLastMsg) || previousChatId !== chatID) {
|
|
|
|
const timeWindowsValues = isPublicChat
|
|
|
|
? [...Object.values(contextChat?.preJoinMessages || {}), ...systemMessagesIds.map((item) => systemMessages[item]),
|
|
|
|
...Object.values(contextChat?.posJoinMessages || {})]
|
|
|
|
: [...Object.values(contextChat?.messageGroups || {})];
|
|
|
|
if (previousChatId !== chatID) {
|
|
|
|
previousChatId = chatID;
|
|
|
|
}
|
|
|
|
|
|
|
|
setLastMsg(lastMsg ? { ...lastMsg } : lastMsg);
|
|
|
|
setTimeWindows(timeWindowsValues);
|
2021-01-20 01:06:32 +08:00
|
|
|
}
|
2021-02-13 01:43:04 +08:00
|
|
|
}
|
|
|
|
globalAppplyStateToProps = applyPropsToState;
|
|
|
|
throttledFunc();
|
2021-02-02 06:12:04 +08:00
|
|
|
|
2021-01-20 01:06:32 +08:00
|
|
|
return (
|
2021-02-13 01:43:04 +08:00
|
|
|
<Chat {...{
|
|
|
|
...props,
|
|
|
|
chatID,
|
|
|
|
amIModerator,
|
|
|
|
count: (contextChat?.unreadTimeWindows.size || 0),
|
|
|
|
timeWindowsValues: stateTimeWindows,
|
|
|
|
dispatch: usingChatContext?.dispatch,
|
|
|
|
title,
|
|
|
|
chatName,
|
|
|
|
contextChat,
|
2021-02-02 06:12:04 +08:00
|
|
|
}}>
|
2021-01-20 01:06:32 +08:00
|
|
|
{children}
|
|
|
|
</Chat>
|
|
|
|
);
|
|
|
|
};
|
2016-04-29 03:02:51 +08:00
|
|
|
|
2018-10-12 04:41:37 +08:00
|
|
|
export default injectIntl(withTracker(({ intl }) => {
|
2020-03-06 07:14:22 +08:00
|
|
|
const chatID = Session.get('idChatOpen');
|
2017-11-24 18:52:03 +08:00
|
|
|
let isChatLocked = ChatService.isChatLocked(chatID);
|
2021-02-13 01:43:04 +08:00
|
|
|
|
2021-02-02 06:12:04 +08:00
|
|
|
// let chatName = title;
|
2017-12-01 20:01:07 +08:00
|
|
|
let partnerIsLoggedOut = false;
|
2016-06-02 00:33:19 +08:00
|
|
|
|
2019-09-07 04:28:02 +08:00
|
|
|
const currentUser = ChatService.getUser(Auth.userID);
|
|
|
|
const amIModerator = currentUser.role === ROLE_MODERATOR;
|
|
|
|
|
2021-01-20 01:06:32 +08:00
|
|
|
if (!chatID) {
|
2020-03-06 07:14:22 +08:00
|
|
|
// No chatID is set so the panel is closed, about to close, or wasn't opened correctly
|
|
|
|
return {
|
|
|
|
unmounting: true,
|
|
|
|
};
|
2017-12-01 20:01:07 +08:00
|
|
|
}
|
2017-04-12 20:03:14 +08:00
|
|
|
|
2017-11-22 02:31:53 +08:00
|
|
|
|
|
|
|
|
2019-06-27 00:29:34 +08:00
|
|
|
const { connected: isMeteorConnected } = Meteor.status();
|
2019-06-03 22:19:56 +08:00
|
|
|
|
2016-06-02 00:33:19 +08:00
|
|
|
return {
|
2018-12-19 22:39:32 +08:00
|
|
|
chatID,
|
2021-02-02 06:12:04 +08:00
|
|
|
intl,
|
2021-01-20 01:06:32 +08:00
|
|
|
messages: [],
|
2017-04-07 05:08:20 +08:00
|
|
|
partnerIsLoggedOut,
|
2016-06-07 22:19:19 +08:00
|
|
|
isChatLocked,
|
2019-06-27 00:29:34 +08:00
|
|
|
isMeteorConnected,
|
2019-09-07 04:28:02 +08:00
|
|
|
amIModerator,
|
2020-08-15 01:13:51 +08:00
|
|
|
meetingIsBreakout: meetingIsBreakout(),
|
2021-01-20 01:06:32 +08:00
|
|
|
loginTime: getLoginTime(),
|
2016-06-03 02:40:27 +08:00
|
|
|
actions: {
|
2019-08-09 02:53:08 +08:00
|
|
|
handleClosePrivateChat: ChatService.closePrivateChat,
|
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));
|