2021-01-20 01:06:32 +08:00
|
|
|
import React, {
|
|
|
|
createContext,
|
|
|
|
useReducer,
|
|
|
|
} from 'react';
|
|
|
|
|
|
|
|
import Users from '/imports/api/users';
|
|
|
|
import Auth from '/imports/ui/services/auth';
|
2021-02-12 22:07:53 +08:00
|
|
|
import Storage from '/imports/ui/services/storage/session';
|
2021-01-20 01:06:32 +08:00
|
|
|
import ChatLogger from '/imports/ui/components/chat/chat-logger/ChatLogger';
|
2021-02-02 06:12:04 +08:00
|
|
|
import { _ } from 'lodash';
|
2021-01-20 01:06:32 +08:00
|
|
|
|
|
|
|
const CHAT_CONFIG = Meteor.settings.public.chat;
|
|
|
|
const PUBLIC_CHAT_KEY = CHAT_CONFIG.public_id;
|
|
|
|
const PUBLIC_GROUP_CHAT_KEY = CHAT_CONFIG.public_group_id;
|
2021-02-10 21:14:30 +08:00
|
|
|
const SYSTEM_CHAT_TYPE = CHAT_CONFIG.type_system;
|
2021-02-12 22:07:53 +08:00
|
|
|
const CLOSED_CHAT_LIST_KEY = 'closedChatList';
|
2021-01-20 01:06:32 +08:00
|
|
|
|
|
|
|
export const ACTIONS = {
|
|
|
|
TEST: 'test',
|
|
|
|
ADDED: 'added',
|
|
|
|
CHANGED: 'changed',
|
|
|
|
REMOVED: 'removed',
|
|
|
|
LAST_READ_MESSAGE_TIMESTAMP_CHANGED: 'last_read_message_timestamp_changed',
|
2021-02-02 06:12:04 +08:00
|
|
|
INIT: 'initial_structure',
|
2021-03-10 04:52:20 +08:00
|
|
|
SYNC_STATUS: 'sync_status',
|
|
|
|
HAS_MESSAGE_TO_SYNC: 'has_message_to_sync',
|
|
|
|
CLEAR_ALL: 'clear_all',
|
2021-01-20 01:06:32 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
export const getGroupingTime = () => Meteor.settings.public.chat.grouping_messages_window;
|
|
|
|
export const getGroupChatId = () => Meteor.settings.public.chat.public_group_id;
|
2021-03-10 04:52:20 +08:00
|
|
|
export const getLoginTime = () => (Users.findOne({ userId: Auth.userID }) || {}).authTokenValidatedTime || 0;
|
2021-01-20 01:06:32 +08:00
|
|
|
|
|
|
|
const generateTimeWindow = (timestamp) => {
|
|
|
|
const groupingTime = getGroupingTime();
|
|
|
|
dateInMilliseconds = Math.floor(timestamp);
|
|
|
|
groupIndex = Math.floor(dateInMilliseconds / groupingTime)
|
|
|
|
date = groupIndex * 30000;
|
|
|
|
return date;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const ChatContext = createContext();
|
|
|
|
|
2021-03-10 04:52:20 +08:00
|
|
|
const generateStateWithNewMessage = (msg, state) => {
|
2021-02-09 04:00:18 +08:00
|
|
|
|
2021-01-20 01:06:32 +08:00
|
|
|
const timeWindow = generateTimeWindow(msg.timestamp);
|
2021-03-25 03:57:09 +08:00
|
|
|
const userId = msg.sender;
|
2021-01-20 01:06:32 +08:00
|
|
|
const keyName = userId + '-' + timeWindow;
|
2021-03-10 04:52:20 +08:00
|
|
|
const msgBuilder = (msg, chat) => {
|
2021-01-20 01:06:32 +08:00
|
|
|
const msgTimewindow = generateTimeWindow(msg.timestamp);
|
2021-03-25 03:57:09 +08:00
|
|
|
const key = msg.sender + '-' + msgTimewindow;
|
2021-01-20 01:06:32 +08:00
|
|
|
const chatIndex = chat?.chatIndexes[key];
|
|
|
|
const {
|
|
|
|
_id,
|
|
|
|
...restMsg
|
|
|
|
} = msg;
|
|
|
|
|
|
|
|
const indexValue = chatIndex ? (chatIndex + 1) : 1;
|
|
|
|
const messageKey = key + '-' + indexValue;
|
|
|
|
const tempGroupMessage = {
|
|
|
|
[messageKey]: {
|
|
|
|
...restMsg,
|
|
|
|
key: messageKey,
|
|
|
|
lastTimestamp: msg.timestamp,
|
|
|
|
read: msg.chatId === PUBLIC_CHAT_KEY && msg.timestamp <= getLoginTime() ? true : false,
|
|
|
|
content: [
|
2021-03-25 03:57:09 +08:00
|
|
|
{ id: msg.id, text: msg.message, time: msg.timestamp },
|
2021-01-20 01:06:32 +08:00
|
|
|
],
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return [tempGroupMessage, msg.sender, indexValue];
|
|
|
|
};
|
|
|
|
|
|
|
|
let stateMessages = state[msg.chatId];
|
2021-02-09 04:00:18 +08:00
|
|
|
|
2021-01-20 01:06:32 +08:00
|
|
|
if (!stateMessages) {
|
|
|
|
if (msg.chatId === getGroupChatId()) {
|
|
|
|
state[msg.chatId] = {
|
|
|
|
count: 0,
|
|
|
|
chatIndexes: {},
|
|
|
|
preJoinMessages: {},
|
|
|
|
posJoinMessages: {},
|
2021-03-10 04:52:20 +08:00
|
|
|
synced:true,
|
2021-02-02 06:12:04 +08:00
|
|
|
unreadTimeWindows: new Set(),
|
|
|
|
unreadCount: 0,
|
2021-01-20 01:06:32 +08:00
|
|
|
};
|
|
|
|
} else {
|
|
|
|
state[msg.chatId] = {
|
|
|
|
count: 0,
|
|
|
|
lastSender: '',
|
2021-03-10 04:52:20 +08:00
|
|
|
synced:true,
|
2021-01-20 01:06:32 +08:00
|
|
|
chatIndexes: {},
|
|
|
|
messageGroups: {},
|
2021-02-02 06:12:04 +08:00
|
|
|
unreadTimeWindows: new Set(),
|
|
|
|
unreadCount: 0,
|
2021-01-20 01:06:32 +08:00
|
|
|
};
|
|
|
|
stateMessages = state[msg.chatId];
|
|
|
|
}
|
|
|
|
|
|
|
|
stateMessages = state[msg.chatId];
|
|
|
|
}
|
2021-02-09 04:00:18 +08:00
|
|
|
|
2021-01-20 01:06:32 +08:00
|
|
|
const forPublicChat = msg.timestamp < getLoginTime() ? stateMessages.preJoinMessages : stateMessages.posJoinMessages;
|
|
|
|
const forPrivateChat = stateMessages.messageGroups;
|
|
|
|
const messageGroups = msg.chatId === getGroupChatId() ? forPublicChat : forPrivateChat;
|
|
|
|
const timewindowIndex = stateMessages.chatIndexes[keyName];
|
|
|
|
const groupMessage = messageGroups[keyName + '-' + timewindowIndex];
|
2021-02-09 04:00:18 +08:00
|
|
|
|
2021-03-25 03:57:09 +08:00
|
|
|
if (!groupMessage || (groupMessage && groupMessage.sender !== stateMessages.lastSender)) {
|
2021-02-09 04:00:18 +08:00
|
|
|
|
2021-03-10 04:52:20 +08:00
|
|
|
const [tempGroupMessage, sender, newIndex] = msgBuilder(msg, stateMessages);
|
2021-01-20 01:06:32 +08:00
|
|
|
stateMessages.lastSender = sender;
|
|
|
|
stateMessages.chatIndexes[keyName] = newIndex;
|
|
|
|
stateMessages.lastTimewindow = keyName + '-' + newIndex;
|
|
|
|
ChatLogger.trace('ChatContext::formatMsg::msgBuilder::tempGroupMessage', tempGroupMessage);
|
|
|
|
|
|
|
|
const messageGroupsKeys = Object.keys(tempGroupMessage);
|
2021-02-02 06:12:04 +08:00
|
|
|
messageGroupsKeys.forEach(key => {
|
|
|
|
messageGroups[key] = tempGroupMessage[key];
|
2021-02-10 21:14:30 +08:00
|
|
|
const message = tempGroupMessage[key];
|
2021-03-10 04:52:20 +08:00
|
|
|
const previousMessage = message.timestamp <= getLoginTime();
|
2021-03-25 03:57:09 +08:00
|
|
|
if (!previousMessage && message.sender !== Auth.userID && !message.id.startsWith(SYSTEM_CHAT_TYPE)) {
|
2021-02-02 06:12:04 +08:00
|
|
|
stateMessages.unreadTimeWindows.add(key);
|
|
|
|
}
|
|
|
|
});
|
2021-01-20 01:06:32 +08:00
|
|
|
} else {
|
|
|
|
if (groupMessage) {
|
2021-03-25 03:57:09 +08:00
|
|
|
if (groupMessage.sender === stateMessages.lastSender) {
|
2021-02-12 22:07:53 +08:00
|
|
|
const previousMessage = msg.timestamp <= getLoginTime();
|
2021-02-02 06:12:04 +08:00
|
|
|
const timeWindowKey = keyName + '-' + stateMessages.chatIndexes[keyName];
|
|
|
|
messageGroups[timeWindowKey] = {
|
2021-01-20 01:06:32 +08:00
|
|
|
...groupMessage,
|
|
|
|
lastTimestamp: msg.timestamp,
|
2021-02-02 06:12:04 +08:00
|
|
|
read: previousMessage ? true : false,
|
2021-01-20 01:06:32 +08:00
|
|
|
content: [
|
|
|
|
...groupMessage.content,
|
2021-03-25 03:57:09 +08:00
|
|
|
{ id: msg.id, text: msg.message, time: msg.timestamp }
|
2021-01-20 01:06:32 +08:00
|
|
|
],
|
|
|
|
};
|
2021-03-25 03:57:09 +08:00
|
|
|
if (!previousMessage && groupMessage.sender !== Auth.userID) {
|
2021-02-02 06:12:04 +08:00
|
|
|
stateMessages.unreadTimeWindows.add(timeWindowKey);
|
|
|
|
}
|
2021-01-20 01:06:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2021-02-04 04:49:58 +08:00
|
|
|
const reducer = (state, action) => {
|
2021-01-20 01:06:32 +08:00
|
|
|
switch (action.type) {
|
|
|
|
case ACTIONS.TEST: {
|
|
|
|
ChatLogger.debug(ACTIONS.TEST);
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
...action.value,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
case ACTIONS.ADDED: {
|
|
|
|
ChatLogger.debug(ACTIONS.ADDED);
|
2021-02-12 22:07:53 +08:00
|
|
|
|
2021-02-09 04:00:18 +08:00
|
|
|
const batchMsgs = action.value;
|
2021-02-12 22:07:53 +08:00
|
|
|
const closedChatsToOpen = new Set();
|
|
|
|
const currentClosedChats = Storage.getItem(CLOSED_CHAT_LIST_KEY) || [];
|
|
|
|
const loginTime = getLoginTime();
|
2021-02-09 04:00:18 +08:00
|
|
|
const newState = batchMsgs.reduce((acc, i)=> {
|
2021-03-10 04:52:20 +08:00
|
|
|
const message = i;
|
2021-02-12 22:07:53 +08:00
|
|
|
const chatId = message.chatId;
|
|
|
|
if (
|
|
|
|
chatId !== PUBLIC_GROUP_CHAT_KEY
|
|
|
|
&& message.timestamp > loginTime
|
|
|
|
&& currentClosedChats.includes(chatId) ){
|
|
|
|
closedChatsToOpen.add(chatId)
|
|
|
|
}
|
2021-03-10 04:52:20 +08:00
|
|
|
return generateStateWithNewMessage(message, acc);
|
2021-02-09 04:00:18 +08:00
|
|
|
}, state);
|
2021-02-12 22:07:53 +08:00
|
|
|
|
|
|
|
if (closedChatsToOpen.size) {
|
|
|
|
const closedChats = currentClosedChats.filter(chatId => !closedChatsToOpen.has(chatId));
|
|
|
|
Storage.setItem(CLOSED_CHAT_LIST_KEY, closedChats);
|
|
|
|
}
|
2021-02-09 04:00:18 +08:00
|
|
|
// const newState = generateStateWithNewMessage(action.value, state);
|
2021-01-20 01:06:32 +08:00
|
|
|
return {...newState};
|
|
|
|
}
|
|
|
|
case ACTIONS.CHANGED: {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
...action.value,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
case ACTIONS.REMOVED: {
|
|
|
|
ChatLogger.debug(ACTIONS.REMOVED);
|
2021-02-02 06:12:04 +08:00
|
|
|
if (state[PUBLIC_GROUP_CHAT_KEY]){
|
2021-02-10 21:14:30 +08:00
|
|
|
state[PUBLIC_GROUP_CHAT_KEY] = {
|
|
|
|
count: 0,
|
|
|
|
lastSender: '',
|
|
|
|
chatIndexes: {},
|
2021-03-11 21:02:16 +08:00
|
|
|
syncing: false,
|
2021-02-10 21:14:30 +08:00
|
|
|
preJoinMessages: {},
|
|
|
|
posJoinMessages: {},
|
|
|
|
unreadTimeWindows: new Set(),
|
|
|
|
unreadCount: 0,
|
|
|
|
};
|
2021-01-20 01:06:32 +08:00
|
|
|
}
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
case ACTIONS.LAST_READ_MESSAGE_TIMESTAMP_CHANGED: {
|
|
|
|
ChatLogger.debug(ACTIONS.LAST_READ_MESSAGE_TIMESTAMP_CHANGED);
|
|
|
|
const { timestamp, chatId } = action.value;
|
|
|
|
const newState = {
|
|
|
|
...state,
|
|
|
|
};
|
|
|
|
const selectedChatId = chatId === PUBLIC_CHAT_KEY ? PUBLIC_GROUP_CHAT_KEY : chatId;
|
|
|
|
const chat = state[selectedChatId];
|
2021-03-25 21:41:13 +08:00
|
|
|
if (!chat) return state;
|
2021-01-20 01:06:32 +08:00
|
|
|
['posJoinMessages','preJoinMessages','messageGroups'].forEach( messageGroupName => {
|
|
|
|
const messageGroup = chat[messageGroupName];
|
|
|
|
if (messageGroup){
|
|
|
|
const timeWindowsids = Object.keys(messageGroup);
|
|
|
|
timeWindowsids.forEach( timeWindowId => {
|
|
|
|
const timeWindow = messageGroup[timeWindowId];
|
|
|
|
if(timeWindow) {
|
|
|
|
if (!timeWindow.read) {
|
|
|
|
if (timeWindow.lastTimestamp <= timestamp){
|
2021-02-02 06:12:04 +08:00
|
|
|
newState[selectedChatId].unreadTimeWindows.delete(timeWindowId);
|
|
|
|
|
2021-01-20 01:06:32 +08:00
|
|
|
newState[selectedChatId][messageGroupName][timeWindowId] = {
|
|
|
|
...timeWindow,
|
|
|
|
read: true,
|
|
|
|
};
|
2021-02-02 06:12:04 +08:00
|
|
|
|
|
|
|
|
2021-01-20 01:06:32 +08:00
|
|
|
newState[selectedChatId] = {
|
|
|
|
...newState[selectedChatId],
|
|
|
|
};
|
|
|
|
newState[selectedChatId][messageGroupName] = {
|
|
|
|
...newState[selectedChatId][messageGroupName],
|
|
|
|
};
|
|
|
|
newState[chatId === PUBLIC_CHAT_KEY ? PUBLIC_GROUP_CHAT_KEY : chatId][messageGroupName][timeWindowId] = {
|
|
|
|
...newState[selectedChatId][messageGroupName][timeWindowId],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return newState;
|
|
|
|
}
|
2021-02-02 06:12:04 +08:00
|
|
|
case ACTIONS.INIT: {
|
|
|
|
ChatLogger.debug(ACTIONS.INIT);
|
|
|
|
const { chatId } = action;
|
|
|
|
const newState = { ...state };
|
|
|
|
|
|
|
|
if (!newState[chatId]){
|
|
|
|
newState[chatId] = {
|
|
|
|
count: 0,
|
|
|
|
lastSender: '',
|
|
|
|
chatIndexes: {},
|
|
|
|
messageGroups: {},
|
|
|
|
unreadTimeWindows: new Set(),
|
|
|
|
unreadCount: 0,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return state;
|
|
|
|
}
|
2021-03-10 04:52:20 +08:00
|
|
|
case ACTIONS.SYNC_STATUS: {
|
|
|
|
ChatLogger.debug(ACTIONS.SYNC_STATUS);
|
|
|
|
const newState = { ...state };
|
|
|
|
newState[action.value.chatId].syncedPercent = action.value.percentage;
|
|
|
|
newState[action.value.chatId].syncing = action.value.percentage < 100 ? true : false;
|
|
|
|
|
|
|
|
return newState;
|
|
|
|
}
|
|
|
|
case ACTIONS.CLEAR_ALL: {
|
|
|
|
ChatLogger.debug(ACTIONS.CLEAR_ALL);
|
|
|
|
const newState = { ...state };
|
|
|
|
const chatIds = Object.keys(newState);
|
|
|
|
chatIds.forEach((chatId) => {
|
|
|
|
newState[chatId] = chatId === PUBLIC_GROUP_CHAT_KEY ?
|
|
|
|
{
|
|
|
|
count: 0,
|
|
|
|
lastSender: '',
|
|
|
|
chatIndexes: {},
|
|
|
|
preJoinMessages: {},
|
|
|
|
posJoinMessages: {},
|
|
|
|
syncing: false,
|
|
|
|
syncedPercent: 0,
|
|
|
|
unreadTimeWindows: new Set(),
|
|
|
|
unreadCount: 0,
|
|
|
|
}
|
|
|
|
:
|
|
|
|
{
|
|
|
|
count: 0,
|
|
|
|
lastSender: '',
|
|
|
|
chatIndexes: {},
|
|
|
|
messageGroups: {},
|
|
|
|
syncing: false,
|
|
|
|
syncedPercent: 0,
|
|
|
|
unreadTimeWindows: new Set(),
|
|
|
|
unreadCount: 0,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
return newState;
|
|
|
|
}
|
2021-01-20 01:06:32 +08:00
|
|
|
default: {
|
|
|
|
throw new Error(`Unexpected action: ${JSON.stringify(action)}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const ChatContextProvider = (props) => {
|
|
|
|
const [chatContextState, chatContextDispatch] = useReducer(reducer, {});
|
|
|
|
ChatLogger.debug('dispatch', chatContextDispatch);
|
|
|
|
return (
|
|
|
|
<ChatContext.Provider value={
|
|
|
|
{
|
|
|
|
dispatch: chatContextDispatch,
|
|
|
|
chats: chatContextState,
|
|
|
|
...props,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
>
|
|
|
|
{props.children}
|
|
|
|
</ChatContext.Provider>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export const ContextConsumer = Component => props => (
|
|
|
|
<ChatContext.Consumer>
|
|
|
|
{contexts => <Component {...props} {...contexts} />}
|
|
|
|
</ChatContext.Consumer>
|
|
|
|
);
|
|
|
|
|
|
|
|
export default {
|
|
|
|
ContextConsumer,
|
|
|
|
ChatContextProvider,
|
2021-03-10 04:52:20 +08:00
|
|
|
}
|