From c6550ca3e4c590780ef08be9ce177a88452856da Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Tue, 22 Jun 2021 13:54:20 +0300 Subject: [PATCH] Fix syncing while in comments thread; Possible fix for reconnects (#1200) --- .../gramjs/network/connection/Connection.js | 8 ++-- src/modules/actions/api/sync.ts | 46 ++++++++++++++++++- 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/src/lib/gramjs/network/connection/Connection.js b/src/lib/gramjs/network/connection/Connection.js index 38280a96..cccc95c2 100644 --- a/src/lib/gramjs/network/connection/Connection.js +++ b/src/lib/gramjs/network/connection/Connection.js @@ -52,9 +52,11 @@ class Connection { } async disconnect() { - this._connected = false; - await this._recvArray.push(undefined); - await this.socket.close(); + if (this._connected) { + this._connected = false; + await this._recvArray.push(undefined); + await this.socket.close(); + } } async send(data) { diff --git a/src/modules/actions/api/sync.ts b/src/modules/actions/api/sync.ts index f55dc6c6..1341cf2c 100644 --- a/src/modules/actions/api/sync.ts +++ b/src/modules/actions/api/sync.ts @@ -23,7 +23,7 @@ import { replaceThreadParam, } from '../../reducers'; import { - selectUser, selectChat, selectCurrentMessageList, selectDraft, selectChatMessage, + selectUser, selectChat, selectCurrentMessageList, selectDraft, selectChatMessage, selectThreadInfo, } from '../../selectors'; import { isChatPrivate } from '../../helpers'; @@ -187,7 +187,7 @@ async function loadAndReplaceMessages(savedUsers?: ApiUser[]) { let users = savedUsers || []; let global = getGlobal(); - const { chatId: currentChatId } = selectCurrentMessageList(global) || {}; + const { chatId: currentChatId, threadId: currentThreadId } = selectCurrentMessageList(global) || {}; // Memoize drafts const draftChatIds = Object.keys(global.messages.byChatId).map(Number); @@ -200,6 +200,7 @@ async function loadAndReplaceMessages(savedUsers?: ApiUser[]) { const result = await loadTopMessages(global.chats.byId[currentChatId]); global = getGlobal(); const { chatId: newCurrentChatId } = selectCurrentMessageList(global) || {}; + const threadInfo = currentThreadId && selectThreadInfo(global, currentChatId, currentThreadId); if (result && newCurrentChatId === currentChatId) { const currentMessageListInfo = global.messages.byChatId[currentChatId]; @@ -226,6 +227,47 @@ async function loadAndReplaceMessages(savedUsers?: ApiUser[]) { }, }; + if (currentThreadId && threadInfo && threadInfo.originChannelId) { + const { originChannelId } = threadInfo; + const currentMessageListInfoOrigin = global.messages.byChatId[originChannelId]; + const resultOrigin = await loadTopMessages(global.chats.byId[originChannelId]); + if (resultOrigin) { + const byIdOrigin = buildCollectionByKey(resultOrigin.messages, 'id'); + const listedIdsOrigin = Object.keys(byIdOrigin) + .map(Number); + + global = { + ...global, + messages: { + ...global.messages, + byChatId: { + ...global.messages.byChatId, + [threadInfo.originChannelId]: { + byId: byIdOrigin, + threadsById: { + [MAIN_THREAD_ID]: { + ...(currentMessageListInfoOrigin && currentMessageListInfoOrigin.threadsById[MAIN_THREAD_ID]), + listedIds: listedIdsOrigin, + viewportIds: listedIdsOrigin, + outlyingIds: undefined, + }, + }, + }, + [currentChatId]: { + ...global.messages.byChatId[currentChatId], + threadsById: { + ...global.messages.byChatId[currentChatId].threadsById, + [currentThreadId]: { + ...(currentMessageListInfo && currentMessageListInfo.threadsById[currentThreadId]), + outlyingIds: undefined, + }, + }, + }, + }, + }, + }; + } + } global = updateChats(global, buildCollectionByKey(result.chats, 'id')); global = updateThreadInfos(global, currentChatId, result.threadInfos);