diff --git a/Telegram/SourceFiles/api/api_updates.cpp b/Telegram/SourceFiles/api/api_updates.cpp index ae99d2954..3bbab8cf4 100644 --- a/Telegram/SourceFiles/api/api_updates.cpp +++ b/Telegram/SourceFiles/api/api_updates.cpp @@ -417,13 +417,12 @@ void Updates::channelDifferenceDone( "{ good - after not final channelDifference was received }%1" ).arg(_session->mtp().isTestMode() ? " TESTMODE" : "")); getChannelDifference(channel); - } else if (ranges::contains( - _activeChats, - channel, - [](const auto &pair) { return pair.second.peer; })) { - channel->ptsWaitingForShortPoll(timeout + } else if (inActiveChats(channel)) { + channel->ptsSetWaitingForShortPoll(timeout ? (timeout * crl::time(1000)) : kWaitForChannelGetDifference); + } else { + channel->ptsSetWaitingForShortPoll(-1); } } @@ -743,16 +742,32 @@ void Updates::addActiveChat(rpl::producer chat) { std::move( chat ) | rpl::start_with_next_done([=](PeerData *peer) { - _activeChats[key].peer = peer; - if (const auto channel = peer ? peer->asChannel() : nullptr) { - channel->ptsWaitingForShortPoll( - kWaitForChannelGetDifference); + auto &active = _activeChats[key]; + const auto was = active.peer; + if (was != peer) { + active.peer = peer; + if (const auto channel = was ? was->asChannel() : nullptr) { + if (!inActiveChats(channel)) { + channel->ptsSetWaitingForShortPoll(-1); + } + } + if (const auto channel = peer ? peer->asChannel() : nullptr) { + channel->ptsSetWaitingForShortPoll( + kWaitForChannelGetDifference); + } } }, [=] { _activeChats.erase(key); }, _activeChats[key].lifetime); } +bool Updates::inActiveChats(not_null peer) const { + return ranges::contains( + _activeChats, + peer.get(), + [](const auto &pair) { return pair.second.peer; }); +} + void Updates::requestChannelRangeDifference(not_null history) { Expects(history->peer->isChannel()); diff --git a/Telegram/SourceFiles/api/api_updates.h b/Telegram/SourceFiles/api/api_updates.h index c028ae75a..654e36b51 100644 --- a/Telegram/SourceFiles/api/api_updates.h +++ b/Telegram/SourceFiles/api/api_updates.h @@ -63,6 +63,7 @@ public: void requestChannelRangeDifference(not_null history); void addActiveChat(rpl::producer chat); + [[nodiscard]] bool inActiveChats(not_null peer) const; private: enum class ChannelDifferenceRequest { diff --git a/Telegram/SourceFiles/apiwrap.cpp b/Telegram/SourceFiles/apiwrap.cpp index 773558ca7..5ef4af3d6 100644 --- a/Telegram/SourceFiles/apiwrap.cpp +++ b/Telegram/SourceFiles/apiwrap.cpp @@ -2028,7 +2028,7 @@ void ApiWrap::deleteHistory( } if (const auto channel = peer->asChannel()) { if (!justClear && !revoke) { - channel->ptsWaitingForShortPoll(-1); + channel->ptsSetWaitingForShortPoll(-1); leaveChannel(channel); } else { if (const auto migrated = peer->migrateFrom()) { diff --git a/Telegram/SourceFiles/data/data_channel.h b/Telegram/SourceFiles/data/data_channel.h index 90f9e2b8d..90f3b9274 100644 --- a/Telegram/SourceFiles/data/data_channel.h +++ b/Telegram/SourceFiles/data/data_channel.h @@ -423,7 +423,7 @@ public: return _ptsWaiter.setRequesting(isRequesting); } // < 0 - not waiting - void ptsWaitingForShortPoll(int32 ms) { + void ptsSetWaitingForShortPoll(int32 ms) { return _ptsWaiter.setWaitingForShortPoll(this, ms); } [[nodiscard]] bool ptsWaitingForSkipped() const {