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';
|
2023-03-02 21:25:08 +08:00
|
|
|
import { throttle } from '/imports/utils/throttle';
|
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-04-07 03:58:11 +08:00
|
|
|
import { UsersContext } from '../components-data/users-context/context';
|
2021-01-20 01:06:32 +08:00
|
|
|
import ChatLogger from '/imports/ui/components/chat/chat-logger/ChatLogger';
|
2021-04-07 03:58:11 +08:00
|
|
|
import lockContextContainer from '/imports/ui/components/lock-viewers/context/container';
|
2021-05-18 04:25:07 +08:00
|
|
|
import Chat from '/imports/ui/components/chat/component';
|
2017-07-13 00:47:58 +08:00
|
|
|
import ChatService from './service';
|
2021-09-11 04:48:52 +08:00
|
|
|
import { layoutSelect, layoutDispatch } from '../layout/context';
|
2022-05-26 22:29:05 +08:00
|
|
|
import { escapeHtml } from '/imports/utils/string-utils';
|
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;
|
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`,
|
2021-03-10 04:52:20 +08:00
|
|
|
moderatorId: `${SYSTEM_CHAT_TYPE}-moderator-msg`,
|
2021-05-18 04:25:07 +08:00
|
|
|
syncId: `${SYSTEM_CHAT_TYPE}-sync-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-03-10 04:52:20 +08:00
|
|
|
loading: {
|
|
|
|
id: 'app.chat.loading',
|
|
|
|
description: 'loading message',
|
|
|
|
},
|
2016-06-14 01:00:38 +08:00
|
|
|
});
|
|
|
|
|
2021-02-02 06:12:04 +08:00
|
|
|
let previousChatId = null;
|
2021-03-10 04:52:20 +08:00
|
|
|
let prevSync = false;
|
2021-04-09 22:16:49 +08:00
|
|
|
let prevPartnerIsLoggedOut = false;
|
2021-03-10 04:52:20 +08:00
|
|
|
|
2021-05-18 04:25:07 +08:00
|
|
|
let globalAppplyStateToProps = () => { };
|
2021-02-04 04:49:58 +08:00
|
|
|
|
2023-03-02 02:13:29 +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) => {
|
|
|
|
const {
|
|
|
|
children,
|
|
|
|
loginTime,
|
2021-02-02 06:12:04 +08:00
|
|
|
intl,
|
2021-04-07 03:58:11 +08:00
|
|
|
userLocks,
|
|
|
|
lockSettings,
|
2021-04-14 01:18:21 +08:00
|
|
|
isChatLockedPublic,
|
|
|
|
isChatLockedPrivate,
|
2021-04-07 03:58:11 +08:00
|
|
|
users: propUsers,
|
|
|
|
...restProps
|
2021-01-20 01:06:32 +08:00
|
|
|
} = props;
|
2021-09-10 21:16:44 +08:00
|
|
|
|
2021-09-11 04:48:52 +08:00
|
|
|
const idChatOpen = layoutSelect((i) => i.idChatOpen);
|
|
|
|
const layoutContextDispatch = layoutDispatch();
|
2021-09-10 21:16:44 +08:00
|
|
|
|
2021-05-18 04:25:07 +08:00
|
|
|
const isPublicChat = idChatOpen === PUBLIC_CHAT_KEY;
|
|
|
|
|
|
|
|
const chatID = idChatOpen;
|
|
|
|
|
|
|
|
if (!chatID) return null;
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
ChatService.removeFromClosedChatsSession();
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const modOnlyMessage = Storage.getItem('ModeratorOnlyMessage');
|
|
|
|
const { welcomeProp } = ChatService.getWelcomeProp();
|
|
|
|
|
2021-03-10 04:52:20 +08:00
|
|
|
ChatLogger.debug('ChatContainer::render::props', props);
|
|
|
|
|
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,
|
2021-05-18 04:25:07 +08:00
|
|
|
},
|
2021-01-20 01:06:32 +08:00
|
|
|
};
|
2021-04-07 03:58:11 +08:00
|
|
|
const usingUsersContext = useContext(UsersContext);
|
|
|
|
const { users } = usingUsersContext;
|
2021-04-15 20:12:21 +08:00
|
|
|
const currentUser = users[Auth.meetingID][Auth.userID];
|
2021-04-07 03:58:11 +08:00
|
|
|
const amIModerator = currentUser.role === ROLE_MODERATOR;
|
2021-05-18 04:25:07 +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-05-18 04:25:07 +08:00
|
|
|
|
|
|
|
const [
|
|
|
|
stateTimeWindows, setTimeWindows,
|
|
|
|
] = useState(isPublicChat ? [...systemMessagesIds.map((item) => systemMessages[item])] : []);
|
2021-03-25 21:41:13 +08:00
|
|
|
const [lastTimeWindowValuesBuild, setLastTimeWindowValuesBuild] = useState(0);
|
2021-05-18 04:25:07 +08:00
|
|
|
|
2021-02-02 06:12:04 +08:00
|
|
|
const { groupChat } = usingGroupChatContext;
|
2021-05-18 04:25:07 +08:00
|
|
|
const participants = groupChat[idChatOpen]?.participants;
|
2021-02-13 01:43:04 +08:00
|
|
|
const chatName = participants?.filter((user) => user.id !== Auth.userID)[0]?.name;
|
2021-05-18 04:25:07 +08:00
|
|
|
const title = chatName
|
|
|
|
? intl.formatMessage(intlMessages.titlePrivate, { 0: chatName })
|
|
|
|
: intl.formatMessage(intlMessages.titlePublic);
|
2021-02-13 03:13:21 +08:00
|
|
|
|
2021-04-09 22:16:49 +08:00
|
|
|
let partnerIsLoggedOut = false;
|
|
|
|
|
2021-04-14 01:18:21 +08:00
|
|
|
let isChatLocked;
|
2021-05-18 04:25:07 +08:00
|
|
|
if (!isPublicChat) {
|
2021-04-09 22:16:49 +08:00
|
|
|
const idUser = participants?.filter((user) => user.id !== Auth.userID)[0]?.id;
|
2021-05-18 04:25:07 +08:00
|
|
|
partnerIsLoggedOut = !!(users[Auth.meetingID][idUser]?.loggedOut
|
|
|
|
|| users[Auth.meetingID][idUser]?.ejected);
|
2021-04-19 21:35:00 +08:00
|
|
|
isChatLocked = isChatLockedPrivate && !(users[Auth.meetingID][idUser]?.role === ROLE_MODERATOR);
|
2024-07-18 00:56:33 +08:00
|
|
|
} else if (users[Auth.meetingID][Auth.userID]?.chatLocked === true) {
|
|
|
|
isChatLocked = true;
|
2021-04-14 01:18:21 +08:00
|
|
|
} else {
|
|
|
|
isChatLocked = isChatLockedPublic;
|
2021-04-09 22:16:49 +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
|
2021-03-18 01:49:44 +08:00
|
|
|
? contextChat?.preJoinMessages[lastTimeWindow] || contextChat?.posJoinMessages[lastTimeWindow]
|
|
|
|
: contextChat?.messageGroups[lastTimeWindow]);
|
2021-05-18 04:25:07 +08:00
|
|
|
ChatLogger.debug('ChatContainer::render::chatData', contextChat);
|
|
|
|
const applyPropsToState = () => {
|
|
|
|
ChatLogger.debug('ChatContainer::applyPropsToState::chatData', lastMsg, stateLastMsg, contextChat?.syncing);
|
2021-03-10 04:52:20 +08:00
|
|
|
if (
|
|
|
|
(lastMsg?.lastTimestamp !== stateLastMsg?.lastTimestamp)
|
2021-05-18 04:25:07 +08:00
|
|
|
|| (previousChatId !== idChatOpen)
|
2021-03-10 04:52:20 +08:00
|
|
|
|| (prevSync !== contextChat?.syncing)
|
2021-04-09 22:16:49 +08:00
|
|
|
|| (prevPartnerIsLoggedOut !== partnerIsLoggedOut)
|
2021-05-18 04:25:07 +08:00
|
|
|
) {
|
2021-03-10 04:52:20 +08:00
|
|
|
prevSync = contextChat?.syncing;
|
2021-04-09 22:16:49 +08:00
|
|
|
prevPartnerIsLoggedOut = partnerIsLoggedOut;
|
|
|
|
|
2021-02-13 01:43:04 +08:00
|
|
|
const timeWindowsValues = isPublicChat
|
2021-03-10 04:52:20 +08:00
|
|
|
? [
|
|
|
|
...(
|
|
|
|
!contextChat?.syncing ? Object.values(contextChat?.preJoinMessages || {}) : [
|
|
|
|
{
|
|
|
|
id: sysMessagesIds.syncId,
|
|
|
|
content: [{
|
|
|
|
id: 'synced',
|
2021-05-18 04:25:07 +08:00
|
|
|
text: intl.formatMessage(intlMessages.loading, { 0: contextChat?.syncedPercent }),
|
2021-03-10 04:52:20 +08:00
|
|
|
time: loginTime + 1,
|
|
|
|
}],
|
|
|
|
key: sysMessagesIds.syncId,
|
|
|
|
time: loginTime + 1,
|
|
|
|
sender: null,
|
2021-05-18 04:25:07 +08:00
|
|
|
},
|
2021-03-10 04:52:20 +08:00
|
|
|
]
|
2021-05-18 04:25:07 +08:00
|
|
|
), ...systemMessagesIds.map((item) => systemMessages[item]),
|
|
|
|
...Object.values(contextChat?.posJoinMessages || {})]
|
2021-02-13 01:43:04 +08:00
|
|
|
: [...Object.values(contextChat?.messageGroups || {})];
|
2021-05-18 04:25:07 +08:00
|
|
|
if (previousChatId !== idChatOpen) {
|
|
|
|
previousChatId = idChatOpen;
|
2021-02-13 01:43:04 +08:00
|
|
|
}
|
|
|
|
|
2021-04-09 22:16:49 +08:00
|
|
|
if (partnerIsLoggedOut) {
|
|
|
|
const time = Date.now();
|
|
|
|
const id = `partner-disconnected-${time}`;
|
|
|
|
const messagePartnerLoggedOut = {
|
|
|
|
id,
|
|
|
|
content: [{
|
|
|
|
id,
|
2022-05-26 22:29:05 +08:00
|
|
|
text: escapeHtml(intl.formatMessage(intlMessages.partnerDisconnected, { 0: chatName })),
|
2021-04-09 22:16:49 +08:00
|
|
|
time,
|
|
|
|
}],
|
|
|
|
time,
|
|
|
|
sender: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
timeWindowsValues.push(messagePartnerLoggedOut);
|
|
|
|
}
|
|
|
|
|
2021-02-13 01:43:04 +08:00
|
|
|
setLastMsg(lastMsg ? { ...lastMsg } : lastMsg);
|
|
|
|
setTimeWindows(timeWindowsValues);
|
2021-03-25 21:41:13 +08:00
|
|
|
setLastTimeWindowValuesBuild(Date.now());
|
2021-01-20 01:06:32 +08:00
|
|
|
}
|
2021-05-18 04:25:07 +08:00
|
|
|
};
|
2021-02-13 01:43:04 +08:00
|
|
|
globalAppplyStateToProps = applyPropsToState;
|
|
|
|
throttledFunc();
|
2021-02-02 06:12:04 +08:00
|
|
|
|
2021-03-29 10:12:58 +08:00
|
|
|
ChatService.removePackagedClassAttribute(
|
2021-05-18 04:25:07 +08:00
|
|
|
['ReactVirtualized__Grid', 'ReactVirtualized__Grid__innerScrollContainer'],
|
|
|
|
'role',
|
2021-03-29 10:12:58 +08:00
|
|
|
);
|
2021-04-07 03:58:11 +08:00
|
|
|
|
2021-01-20 01:06:32 +08:00
|
|
|
return (
|
2021-02-13 01:43:04 +08:00
|
|
|
<Chat {...{
|
2021-05-18 04:25:07 +08:00
|
|
|
idChatOpen,
|
2021-04-14 01:18:21 +08:00
|
|
|
isChatLocked,
|
2021-05-18 04:25:07 +08:00
|
|
|
...restProps,
|
2021-02-13 01:43:04 +08:00
|
|
|
chatID,
|
|
|
|
amIModerator,
|
|
|
|
count: (contextChat?.unreadTimeWindows.size || 0),
|
|
|
|
timeWindowsValues: stateTimeWindows,
|
|
|
|
dispatch: usingChatContext?.dispatch,
|
|
|
|
title,
|
2021-03-10 04:52:20 +08:00
|
|
|
syncing: contextChat?.syncing,
|
|
|
|
syncedPercent: contextChat?.syncedPercent,
|
2021-02-13 01:43:04 +08:00
|
|
|
chatName,
|
2021-05-18 04:25:07 +08:00
|
|
|
contextChat,
|
2021-08-05 19:03:24 +08:00
|
|
|
layoutContextDispatch,
|
2021-03-25 21:41:13 +08:00
|
|
|
lastTimeWindowValuesBuild,
|
2021-05-18 04:25:07 +08:00
|
|
|
partnerIsLoggedOut,
|
|
|
|
}}
|
|
|
|
>
|
2021-01-20 01:06:32 +08:00
|
|
|
{children}
|
|
|
|
</Chat>
|
|
|
|
);
|
|
|
|
};
|
2016-04-29 03:02:51 +08:00
|
|
|
|
2021-04-07 03:58:11 +08:00
|
|
|
export default lockContextContainer(injectIntl(withTracker(({ intl, userLocks }) => {
|
2021-04-14 01:18:21 +08:00
|
|
|
const isChatLockedPublic = userLocks.userPublicChat;
|
|
|
|
const isChatLockedPrivate = userLocks.userPrivateChat;
|
2021-04-12 03:48:25 +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 {
|
2021-02-02 06:12:04 +08:00
|
|
|
intl,
|
2021-04-14 01:18:21 +08:00
|
|
|
isChatLockedPublic,
|
|
|
|
isChatLockedPrivate,
|
2019-06-27 00:29:34 +08:00
|
|
|
isMeteorConnected,
|
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
|
|
|
};
|
2021-09-10 21:16:44 +08:00
|
|
|
})(ChatContainer)));
|