From f77199bdbf6788677f27223023e804d40f70b910 Mon Sep 17 00:00:00 2001 From: Tainan Felipe Date: Thu, 8 Jul 2021 14:08:32 -0300 Subject: [PATCH] Make message sync start after the subscription be ready --- .../components-data/chat-context/adapter.jsx | 32 ++++++++++++------- .../ui/components/subscriptions/component.jsx | 3 ++ 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/bigbluebutton-html5/imports/ui/components/components-data/chat-context/adapter.jsx b/bigbluebutton-html5/imports/ui/components/components-data/chat-context/adapter.jsx index 29bb43ba04..c812f5d471 100644 --- a/bigbluebutton-html5/imports/ui/components/components-data/chat-context/adapter.jsx +++ b/bigbluebutton-html5/imports/ui/components/components-data/chat-context/adapter.jsx @@ -16,6 +16,7 @@ const CHAT_CLEAR_MESSAGE = CHAT_CONFIG.system_messages_keys.chat_clear; const ITENS_PER_PAGE = CHAT_CONFIG.itemsPerPage; const TIME_BETWEEN_FETCHS = CHAT_CONFIG.timeBetweenFetchs; const EVENT_NAME = 'bbb-group-chat-messages-subscription-has-stoppped'; +const EVENT_NAME_SUBSCRIPTION_READY = 'bbb-group-chat-messages-subscriptions-ready'; const getMessagesBeforeJoinCounter = async () => { const counter = await makeCall('chatMessageBeforeJoinCounter'); @@ -24,7 +25,8 @@ const getMessagesBeforeJoinCounter = async () => { const startSyncMessagesbeforeJoin = async (dispatch) => { const chatsMessagesCount = await getMessagesBeforeJoinCounter(); - const pagesPerChat = chatsMessagesCount.map(chat => ({ ...chat, pages: Math.ceil(chat.count / ITENS_PER_PAGE), syncedPages: 0 })); + const pagesPerChat = chatsMessagesCount + .map((chat) => ({ ...chat, pages: Math.ceil(chat.count / ITENS_PER_PAGE), syncedPages: 0 })); const syncRoutine = async (chatsToSync) => { if (!chatsToSync.length) return; @@ -49,9 +51,8 @@ const startSyncMessagesbeforeJoin = async (dispatch) => { }); } - - await new Promise(r => setTimeout(r, TIME_BETWEEN_FETCHS)); - syncRoutine(pagesToFetch.filter(chat => !(chat.syncedPages > chat.pages))); + await new Promise((r) => setTimeout(r, TIME_BETWEEN_FETCHS)); + syncRoutine(pagesToFetch.filter((chat) => !(chat.syncedPages > chat.pages))); }; syncRoutine(pagesPerChat); }; @@ -62,6 +63,7 @@ const Adapter = () => { const usingUsersContext = useContext(UsersContext); const { users } = usingUsersContext; const [syncStarted, setSync] = useState(true); + const [subscriptionReady, setSubscriptionReady] = useState(false); ChatLogger.trace('chatAdapter::body::users', users[Auth.meetingID]); useEffect(() => { @@ -74,22 +76,28 @@ const Adapter = () => { }); } }); + + window.addEventListener(EVENT_NAME_SUBSCRIPTION_READY, () => { + console.log('aconteceu'); + setSubscriptionReady(true); + }); }, []); useEffect(() => { const connectionStatus = Meteor.status(); - if (connectionStatus.connected && !syncStarted && Auth.userID) { - setSync(true); - - startSyncMessagesbeforeJoin(dispatch); + if (connectionStatus.connected && !syncStarted && Auth.userID && subscriptionReady) { + setTimeout(() => { + setSync(true); + console.log('iniciou o sync'); + startSyncMessagesbeforeJoin(dispatch); + }, 1000); } - }, [Meteor.status().connected, syncStarted, Auth.userID]); - + }, [Meteor.status().connected, syncStarted, Auth.userID, subscriptionReady]); /* 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] && users[Auth.meetingID][Auth.userID]) { + if (users[Auth.meetingID]) { if (currentUserData?.role !== users[Auth.meetingID][Auth.userID].role) { prevUserData = currentUserData; } @@ -114,7 +122,7 @@ const Adapter = () => { }, 1000, { trailing: true, leading: true }); Meteor.connection._stream.socket.addEventListener('message', (msg) => { - if (msg.data.indexOf('{"msg":"added","collection":"group-chat-msg"') != -1) { + if (msg.data.indexOf('{"msg":"added","collection":"group-chat-msg"') !== -1) { const parsedMsg = JSON.parse(msg.data); if (parsedMsg.msg === 'added') { const { fields } = parsedMsg; diff --git a/bigbluebutton-html5/imports/ui/components/subscriptions/component.jsx b/bigbluebutton-html5/imports/ui/components/subscriptions/component.jsx index 8bcabd533b..b49acdd86f 100755 --- a/bigbluebutton-html5/imports/ui/components/subscriptions/component.jsx +++ b/bigbluebutton-html5/imports/ui/components/subscriptions/component.jsx @@ -23,12 +23,15 @@ const SUBSCRIPTIONS = [ ]; const EVENT_NAME = 'bbb-group-chat-messages-subscription-has-stoppped'; +const EVENT_NAME_SUBSCRIPTION_READY = 'bbb-group-chat-messages-subscriptions-ready'; class Subscriptions extends Component { componentDidUpdate() { const { subscriptionsReady } = this.props; if (subscriptionsReady) { Session.set('subscriptionsReady', true); + const event = new Event(EVENT_NAME_SUBSCRIPTION_READY); + window.dispatchEvent(event); } }