diff --git a/Telegram/SourceFiles/apiwrap.cpp b/Telegram/SourceFiles/apiwrap.cpp index 1b7de7656..694141ca7 100644 --- a/Telegram/SourceFiles/apiwrap.cpp +++ b/Telegram/SourceFiles/apiwrap.cpp @@ -1892,7 +1892,9 @@ void ApiWrap::requestStickers(TimeId now) { default: Unexpected("Type in ApiWrap::stickersDone()"); } }; - _stickersUpdateRequest = request(MTPmessages_GetAllStickers(MTP_int(Local::countStickersHash(true)))).done(onDone).fail([this, onDone](const RPCError &error) { + _stickersUpdateRequest = request(MTPmessages_GetAllStickers( + MTP_int(Local::countStickersHash(true)) + )).done(onDone).fail([this, onDone](const RPCError &error) { LOG(("App Fail: Failed to get stickers!")); onDone(MTP_messages_allStickersNotModified()); }).send(); @@ -2654,7 +2656,7 @@ void ApiWrap::requestFeedMessages( } Unexpected("Direction in PrepareSearchRequest"); }(); - const auto sourcesHash = int32(0); + const auto sourcesHash = int32(0);// feed->channelsHash(); // #TODO const auto hash = int32(0); const auto flags = (messageId && messageId.fullId.channel) ? MTPchannels_GetFeed::Flag::f_offset_position @@ -2712,13 +2714,11 @@ void ApiWrap::feedMessagesDone( till = candidate; } }; - const auto checkPosition = [&](const auto &position) { - if (slice == SliceType::Before && !(position < messageId)) { - return false; - } else if (slice == SliceType::After && !(messageId < position)) { - return false; - } - return true; + const auto tooLargePosition = [&](const auto &position) { + return (slice == SliceType::Before) && !(position < messageId); + }; + const auto tooSmallPosition = [&](const auto &position) { + return (slice == SliceType::After) && !(messageId < position); }; App::feedUsers(data.vusers); App::feedChats(data.vchats); @@ -2727,7 +2727,11 @@ void ApiWrap::feedMessagesDone( for (const auto &msg : messages) { if (const auto item = App::histories().addNewMessage(msg, type)) { const auto position = item->position(); - if (!checkPosition(position)) { + if (tooLargePosition(position)) { + accumulateTill(noSkipRange.till, position); + continue; + } else if (tooSmallPosition(position)) { + accumulateFrom(noSkipRange.from, position); continue; } ids.push_back(position); @@ -2741,14 +2745,14 @@ void ApiWrap::feedMessagesDone( accumulateFrom( noSkipRange.from, Data::FeedPositionFromMTP(data.vmin_position)); - } else { + } else if (slice == SliceType::Before) { noSkipRange.from = Data::MinMessagePosition; } if (data.has_max_position() && !ids.empty()) { accumulateTill( noSkipRange.till, Data::FeedPositionFromMTP(data.vmax_position)); - } else { + } else if (slice == SliceType::After) { noSkipRange.till = Data::MaxMessagePosition; } diff --git a/Telegram/SourceFiles/apiwrap.h b/Telegram/SourceFiles/apiwrap.h index 198ebbf5e..457f4414c 100644 --- a/Telegram/SourceFiles/apiwrap.h +++ b/Telegram/SourceFiles/apiwrap.h @@ -37,6 +37,16 @@ inline const MTPVector *getChatsFromMessagesChats(const MTPmessages_Cha return nullptr; } +template +inline int32 CountHash(IntRange &&range) { + uint32 acc = 0; + for (auto value : range) { + acc += (acc * 20261) + uint32(value); + } + return int32(acc & 0x7FFFFFFF); +} + + } // namespace Api class ApiWrap : private MTP::Sender, private base::Subscriber { diff --git a/Telegram/SourceFiles/data/data_feed.cpp b/Telegram/SourceFiles/data/data_feed.cpp index 9aa2a1a65..eeb98af69 100644 --- a/Telegram/SourceFiles/data/data_feed.cpp +++ b/Telegram/SourceFiles/data/data_feed.cpp @@ -15,6 +15,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "storage/storage_facade.h" #include "storage/storage_feed_messages.h" #include "auth_session.h" +#include "apiwrap.h" +#include "mainwidget.h" namespace Data { @@ -100,12 +102,13 @@ void Feed::unregisterOne(not_null channel) { _parent->session().storage().remove( Storage::FeedMessagesRemoveAll(_id, channel->bareId())); - history->updateChatListExistence(); if (visible && _channels.size() < 2) { updateChatListExistence(); for (const auto history : _channels) { history->updateChatListExistence(); } + } else { + history->updateChatListExistence(); } _parent->notifyFeedUpdated(this, FeedUpdateFlag::Channels); } @@ -151,6 +154,14 @@ const std::vector> &Feed::channels() const { return _channels; } +int32 Feed::channelsHash() const { + return Api::CountHash(ranges::view::all( + _channels + ) | ranges::view::transform([](not_null history) { + return history->peer->bareId(); + })); +} + bool Feed::justSetLastMessage(not_null item) { if (_lastMessage && item->position() <= _lastMessage->position()) { return false; diff --git a/Telegram/SourceFiles/data/data_feed.h b/Telegram/SourceFiles/data/data_feed.h index 7518f4cc4..bba7b9109 100644 --- a/Telegram/SourceFiles/data/data_feed.h +++ b/Telegram/SourceFiles/data/data_feed.h @@ -65,6 +65,7 @@ public: int size) const override; const std::vector> &channels() const; + int32 channelsHash() const; private: void indexNameParts(); diff --git a/Telegram/SourceFiles/dialogs/dialogs_entry.h b/Telegram/SourceFiles/dialogs/dialogs_entry.h index 6e210a861..96d5f3a8b 100644 --- a/Telegram/SourceFiles/dialogs/dialogs_entry.h +++ b/Telegram/SourceFiles/dialogs/dialogs_entry.h @@ -63,7 +63,7 @@ public: } void updateChatListSortPosition(); void setChatsListDate(const QDateTime &date); - void updateChatListExistence(); + virtual void updateChatListExistence(); bool needUpdateInChatList() const; virtual bool toImportant() const = 0; diff --git a/Telegram/SourceFiles/dialogs/dialogs_widget.cpp b/Telegram/SourceFiles/dialogs/dialogs_widget.cpp index 63cb4516a..5ddbadb58 100644 --- a/Telegram/SourceFiles/dialogs/dialogs_widget.cpp +++ b/Telegram/SourceFiles/dialogs/dialogs_widget.cpp @@ -707,8 +707,11 @@ void DialogsWidget::searchReceived( switch (result.type()) { case mtpc_messages_messages: { auto &d = result.c_messages_messages(); - App::feedUsers(d.vusers); - App::feedChats(d.vchats); + if (_searchRequest != 0) { + // Don't apply cached data! + App::feedUsers(d.vusers); + App::feedChats(d.vchats); + } auto &msgs = d.vmessages.v; if (!_inner->searchReceived(msgs, type, msgs.size())) { if (type == DialogsSearchMigratedFromStart || type == DialogsSearchMigratedFromOffset) { @@ -721,8 +724,11 @@ void DialogsWidget::searchReceived( case mtpc_messages_messagesSlice: { auto &d = result.c_messages_messagesSlice(); - App::feedUsers(d.vusers); - App::feedChats(d.vchats); + if (_searchRequest != 0) { + // Don't apply cached data! + App::feedUsers(d.vusers); + App::feedChats(d.vchats); + } auto &msgs = d.vmessages.v; if (!_inner->searchReceived(msgs, type, d.vcount.v)) { if (type == DialogsSearchMigratedFromStart || type == DialogsSearchMigratedFromOffset) { @@ -740,8 +746,11 @@ void DialogsWidget::searchReceived( } else { LOG(("API Error: received messages.channelMessages when no channel was passed! (DialogsWidget::searchReceived)")); } - App::feedUsers(d.vusers); - App::feedChats(d.vchats); + if (_searchRequest != 0) { + // Don't apply cached data! + App::feedUsers(d.vusers); + App::feedChats(d.vchats); + } auto &msgs = d.vmessages.v; if (!_inner->searchReceived(msgs, type, d.vcount.v)) { if (type == DialogsSearchMigratedFromStart || type == DialogsSearchMigratedFromOffset) { diff --git a/Telegram/SourceFiles/history/history.cpp b/Telegram/SourceFiles/history/history.cpp index bd598d69e..b9e18a956 100644 --- a/Telegram/SourceFiles/history/history.cpp +++ b/Telegram/SourceFiles/history/history.cpp @@ -2271,6 +2271,19 @@ void History::setLastMessage(HistoryItem *msg) { } } +void History::updateChatListExistence() { + Entry::updateChatListExistence(); + if (!lastMsg + && (!loadedAtBottom() || !loadedAtTop())) { + if (const auto channel = peer->asChannel()) { + if (!channel->feed()) { + // After ungrouping from a feed we need to load lastMsg. + App::main()->checkPeerHistory(peer); + } + } + } +} + bool History::shouldBeInChatList() const { if (peer->migrateTo()) { return false; diff --git a/Telegram/SourceFiles/history/history.h b/Telegram/SourceFiles/history/history.h index 56f97b8d9..c8bfdfd58 100644 --- a/Telegram/SourceFiles/history/history.h +++ b/Telegram/SourceFiles/history/history.h @@ -312,6 +312,7 @@ public: HistoryItemsList validateForwardDraft(); void setForwardDraft(MessageIdsList &&items); + void updateChatListExistence() override; bool shouldBeInChatList() const override; bool toImportant() const override { return !mute(); diff --git a/Telegram/SourceFiles/history/view/history_view_list_widget.cpp b/Telegram/SourceFiles/history/view/history_view_list_widget.cpp index d94b69ea6..c885640f7 100644 --- a/Telegram/SourceFiles/history/view/history_view_list_widget.cpp +++ b/Telegram/SourceFiles/history/view/history_view_list_widget.cpp @@ -804,12 +804,12 @@ void ListWidget::checkMoveToOtherViewer() { + (visibleHeight / minItemHeight); auto preloadBefore = kPreloadIfLessThanScreens * visibleHeight; - auto after = _slice.skippedAfter; - auto preloadTop = (_visibleTop < preloadBefore); - auto topLoaded = after && (*after == 0); auto before = _slice.skippedBefore; + auto preloadTop = (_visibleTop < preloadBefore); + auto topLoaded = before && (*before == 0); + auto after = _slice.skippedAfter; auto preloadBottom = (height() - _visibleBottom < preloadBefore); - auto bottomLoaded = before && (*before == 0); + auto bottomLoaded = after && (*after == 0); auto minScreenDelta = kPreloadedScreensCount - kPreloadIfLessThanScreens; diff --git a/Telegram/SourceFiles/history/view/history_view_service_message.cpp b/Telegram/SourceFiles/history/view/history_view_service_message.cpp index 24800c013..3a0dd9813 100644 --- a/Telegram/SourceFiles/history/view/history_view_service_message.cpp +++ b/Telegram/SourceFiles/history/view/history_view_service_message.cpp @@ -343,7 +343,7 @@ QSize Service::performCountCurrentSize(int newWidth) { } newHeight += st::msgServicePadding.top() + st::msgServicePadding.bottom() + st::msgServiceMargin.top() + st::msgServiceMargin.bottom(); if (media) { - newHeight += st::msgServiceMargin.top() + media->resizeGetHeight(media->width()); + newHeight += st::msgServiceMargin.top() + media->resizeGetHeight(media->maxWidth()); } } diff --git a/Telegram/SourceFiles/mainwidget.cpp b/Telegram/SourceFiles/mainwidget.cpp index 449aae2b5..4f4fa80c3 100644 --- a/Telegram/SourceFiles/mainwidget.cpp +++ b/Telegram/SourceFiles/mainwidget.cpp @@ -1282,6 +1282,7 @@ void MainWidget::checkedHistory(PeerData *peer, const MTPmessages_Messages &resu } } } + history->updateChatListExistence(); } Auth().data().sendHistoryChangeNotifications(); } diff --git a/Telegram/SourceFiles/window/window_peer_menu.cpp b/Telegram/SourceFiles/window/window_peer_menu.cpp index a306209fe..2fb97007a 100644 --- a/Telegram/SourceFiles/window/window_peer_menu.cpp +++ b/Telegram/SourceFiles/window/window_peer_menu.cpp @@ -350,10 +350,13 @@ void Filler::addChatActions(not_null chat) { void Filler::addChannelActions(not_null channel) { auto isGroup = channel->isMegagroup(); if (!isGroup) { - const auto grouped = (channel->feed() != nullptr); - _addAction( - lang(grouped ? lng_feed_ungroup : lng_feed_group), - [=] { ToggleChannelGrouping(channel, !grouped); }); + const auto feed = channel->feed(); + const auto grouped = (feed != nullptr); + if (!grouped || feed->channels().size() > 1) { + _addAction( + lang(grouped ? lng_feed_ungroup : lng_feed_group), + [=] { ToggleChannelGrouping(channel, !grouped); }); + } } if (_source != PeerMenuSource::ChatsList) { if (ManagePeerBox::Available(channel)) {