bigbluebutton-Github/bigbluebutton-html5/imports/ui/components/components-data/chat-context/adapter.jsx

142 lines
4.9 KiB
React
Raw Normal View History

2021-03-10 04:52:20 +08:00
import { useContext, useEffect, useState } from 'react';
2021-02-09 04:00:18 +08:00
import _ from 'lodash';
2021-04-14 01:35:46 +08:00
import { ChatContext, ACTIONS, MESSAGE_TYPES } from './context';
import { UsersContext } from '../users-context/context';
2021-03-10 04:52:20 +08:00
import { makeCall } from '/imports/ui/services/api';
import ChatLogger from '/imports/ui/components/chat/chat-logger/ChatLogger';
import Auth from '/imports/ui/services/auth';
import CollectionEventsBroker from '/imports/ui/services/LiveDataEventBroker/LiveDataEventBroker';
2021-04-23 02:43:45 +08:00
let prevUserData = {};
let currentUserData = {};
2021-02-09 04:00:18 +08:00
let messageQueue = [];
2021-03-10 04:52:20 +08:00
const CHAT_CONFIG = Meteor.settings.public.chat;
const SYSTEM_CHAT_TYPE = CHAT_CONFIG.type_system;
const CHAT_CLEAR_MESSAGE = CHAT_CONFIG.system_messages_keys.chat_clear;
2021-03-10 04:52:20 +08:00
const ITENS_PER_PAGE = CHAT_CONFIG.itemsPerPage;
const TIME_BETWEEN_FETCHS = CHAT_CONFIG.timeBetweenFetchs;
2021-04-14 01:35:46 +08:00
const EVENT_NAME = 'bbb-group-chat-messages-subscription-has-stoppped';
const EVENT_NAME_SUBSCRIPTION_READY = 'bbb-group-chat-messages-subscriptions-ready';
2021-03-10 04:52:20 +08:00
const getMessagesBeforeJoinCounter = async () => {
const counter = await makeCall('chatMessageBeforeJoinCounter');
return counter;
};
const startSyncMessagesbeforeJoin = async (dispatch) => {
const chatsMessagesCount = await getMessagesBeforeJoinCounter();
const pagesPerChat = chatsMessagesCount
.map((chat) => ({ ...chat, pages: Math.ceil(chat.count / ITENS_PER_PAGE), syncedPages: 0 }));
2021-03-10 04:52:20 +08:00
const syncRoutine = async (chatsToSync) => {
if (!chatsToSync.length) return;
const pagesToFetch = [...chatsToSync].sort((a, b) => a.pages - b.pages);
const chatWithLessPages = pagesToFetch[0];
chatWithLessPages.syncedPages += 1;
const messagesFromPage = await makeCall('fetchMessagePerPage', chatWithLessPages.chatId, chatWithLessPages.syncedPages);
if (messagesFromPage.length) {
dispatch({
type: ACTIONS.ADDED,
value: messagesFromPage,
2021-04-14 01:35:46 +08:00
messageType: MESSAGE_TYPES.HISTORY,
2021-03-10 04:52:20 +08:00
});
dispatch({
type: ACTIONS.SYNC_STATUS,
value: {
chatId: chatWithLessPages.chatId,
percentage: Math.floor((chatWithLessPages.syncedPages / chatWithLessPages.pages) * 100),
},
});
}
await new Promise((r) => setTimeout(r, TIME_BETWEEN_FETCHS));
syncRoutine(pagesToFetch.filter((chat) => !(chat.syncedPages > chat.pages)));
2021-03-10 04:52:20 +08:00
};
syncRoutine(pagesPerChat);
};
const Adapter = () => {
const usingChatContext = useContext(ChatContext);
const { dispatch } = usingChatContext;
const usingUsersContext = useContext(UsersContext);
const { users } = usingUsersContext;
2021-03-10 04:52:20 +08:00
const [syncStarted, setSync] = useState(true);
const [subscriptionReady, setSubscriptionReady] = useState(false);
2021-04-15 20:12:21 +08:00
ChatLogger.trace('chatAdapter::body::users', users[Auth.meetingID]);
2021-02-09 04:00:18 +08:00
2021-04-14 01:35:46 +08:00
useEffect(() => {
2021-04-23 02:43:45 +08:00
window.addEventListener(EVENT_NAME, () => {
2021-04-23 04:01:12 +08:00
/* needed to prevent an issue with dupĺicated messages when user role is changed
more info: https://github.com/bigbluebutton/bigbluebutton/issues/11842 */
2021-04-23 02:43:45 +08:00
if (prevUserData.role && prevUserData?.role !== currentUserData?.role) {
2021-04-21 04:36:04 +08:00
dispatch({
type: ACTIONS.CLEAR_STREAM_MESSAGES,
});
}
2021-04-14 01:35:46 +08:00
});
window.addEventListener(EVENT_NAME_SUBSCRIPTION_READY, () => {
setSubscriptionReady(true);
});
2021-04-14 01:35:46 +08:00
}, []);
2021-03-10 04:52:20 +08:00
useEffect(() => {
const connectionStatus = Meteor.status();
if (connectionStatus.connected && !syncStarted && Auth.userID && subscriptionReady) {
setTimeout(() => {
setSync(true);
startSyncMessagesbeforeJoin(dispatch);
}, 1000);
2021-03-10 04:52:20 +08:00
}
}, [Meteor.status().connected, syncStarted, Auth.userID, subscriptionReady]);
2021-03-10 04:52:20 +08:00
2021-04-23 04:01:12 +08:00
/* needed to prevent an issue with dupĺicated messages when user role is changed
more info: https://github.com/bigbluebutton/bigbluebutton/issues/11842 */
useEffect(() => {
if (users[Auth.meetingID]) {
2021-04-23 02:43:45 +08:00
if (currentUserData?.role !== users[Auth.meetingID][Auth.userID].role) {
prevUserData = currentUserData;
}
currentUserData = users[Auth.meetingID][Auth.userID];
}
}, [usingUsersContext]);
useEffect(() => {
2021-03-10 04:52:20 +08:00
if (!Meteor.status().connected) return;
setSync(false);
dispatch({
type: ACTIONS.CLEAR_ALL,
});
2021-02-09 04:00:18 +08:00
const throttledDispatch = _.throttle(() => {
const dispatchedMessageQueue = [...messageQueue];
messageQueue = [];
dispatch({
type: ACTIONS.ADDED,
value: dispatchedMessageQueue,
2021-04-14 01:35:46 +08:00
messageType: MESSAGE_TYPES.STREAM,
2021-02-09 04:00:18 +08:00
});
}, 1000, { trailing: true, leading: true });
const insertToContext = (fields) => {
if (fields.id === `${SYSTEM_CHAT_TYPE}-${CHAT_CLEAR_MESSAGE}`) {
messageQueue = [];
dispatch({
type: ACTIONS.REMOVED,
});
2021-02-04 04:49:58 +08:00
}
messageQueue.push(fields);
throttledDispatch();
};
2021-09-22 21:05:43 +08:00
CollectionEventsBroker.addListener('group-chat-msg', 'added', insertToContext);
2021-03-24 04:30:38 +08:00
}, [Meteor.status().connected, Meteor.connection._lastSessionId]);
return null;
};
export default Adapter;