From c7741cb62aab30f9ef3d27eb8cf6097bdb7c5545 Mon Sep 17 00:00:00 2001 From: John Preston Date: Tue, 8 Nov 2022 15:36:46 +0400 Subject: [PATCH] Apply short topic info from channelMessages. --- .../SourceFiles/api/api_messages_search.cpp | 18 ++++--- Telegram/SourceFiles/apiwrap.cpp | 12 ++--- Telegram/SourceFiles/data/data_channel.cpp | 7 ++- Telegram/SourceFiles/data/data_channel.h | 2 + Telegram/SourceFiles/data/data_forum.cpp | 14 +++-- Telegram/SourceFiles/data/data_forum.h | 3 ++ .../SourceFiles/data/data_forum_topic.cpp | 54 +++++++++++-------- .../SourceFiles/data/data_replies_list.cpp | 40 +++++++------- .../data/data_search_controller.cpp | 5 +- Telegram/SourceFiles/data/data_session.cpp | 1 + Telegram/SourceFiles/data/data_web_page.cpp | 1 + .../SourceFiles/dialogs/dialogs_widget.cpp | 1 + .../history/history_inner_widget.cpp | 6 ++- .../history/history_inner_widget.h | 8 ++- .../history/history_unread_things.cpp | 1 + .../SourceFiles/history/history_widget.cpp | 16 ++++-- Telegram/SourceFiles/history/history_widget.h | 6 +-- 17 files changed, 121 insertions(+), 74 deletions(-) diff --git a/Telegram/SourceFiles/api/api_messages_search.cpp b/Telegram/SourceFiles/api/api_messages_search.cpp index d6c7c3029..09f9d6a21 100644 --- a/Telegram/SourceFiles/api/api_messages_search.cpp +++ b/Telegram/SourceFiles/api/api_messages_search.cpp @@ -152,18 +152,22 @@ void MessagesSearch::searchReceived( const auto total = int(data.vcount().v); return FoundMessages{ total, std::move(items), nextToken }; }, [&](const MTPDmessages_channelMessages &data) { - if (const auto channel = _history->peer->asChannel()) { - channel->ptsReceived(data.vpts().v); - } else { - LOG(("API Error: " - "received messages.channelMessages when no channel " - "was passed!")); - } if (_requestId != 0) { // Don't apply cached data! owner.processUsers(data.vusers()); owner.processChats(data.vchats()); } + if (const auto channel = _history->peer->asChannel()) { + channel->ptsReceived(data.vpts().v); + if (_requestId != 0) { + // Don't apply cached data! + channel->processTopics(data.vtopics()); + } + } else { + LOG(("API Error: " + "received messages.channelMessages when no channel " + "was passed!")); + } auto items = HistoryItemsFromTL(&owner, data.vmessages().v); const auto total = int(data.vcount().v); return FoundMessages{ total, std::move(items), nextToken }; diff --git a/Telegram/SourceFiles/apiwrap.cpp b/Telegram/SourceFiles/apiwrap.cpp index 22efe9e3a..bfcb78975 100644 --- a/Telegram/SourceFiles/apiwrap.cpp +++ b/Telegram/SourceFiles/apiwrap.cpp @@ -2870,18 +2870,18 @@ void ApiWrap::requestMessageAfterDate( }; const auto list = result.match([&]( const MTPDmessages_messages &data) { - return handleMessages(result.c_messages_messages()); + return handleMessages(data); }, [&](const MTPDmessages_messagesSlice &data) { - return handleMessages(result.c_messages_messagesSlice()); + return handleMessages(data); }, [&](const MTPDmessages_channelMessages &data) { - const auto &messages = result.c_messages_channelMessages(); - if (peer && peer->isChannel()) { - peer->asChannel()->ptsReceived(messages.vpts().v); + if (const auto channel = peer->asChannel()) { + channel->ptsReceived(data.vpts().v); + channel->processTopics(data.vtopics()); } else { LOG(("API Error: received messages.channelMessages when " "no channel was passed! (ApiWrap::jumpToDate)")); } - return handleMessages(messages); + return handleMessages(data); }, [&](const MTPDmessages_messagesNotModified &) { LOG(("API Error: received messages.messagesNotModified! " "(ApiWrap::jumpToDate)")); diff --git a/Telegram/SourceFiles/data/data_channel.cpp b/Telegram/SourceFiles/data/data_channel.cpp index 3faf62904..8f5781277 100644 --- a/Telegram/SourceFiles/data/data_channel.cpp +++ b/Telegram/SourceFiles/data/data_channel.cpp @@ -83,7 +83,6 @@ std::unique_ptr MegagroupInfo::takeForumData() { return result; } return nullptr; - } ChannelData::ChannelData(not_null owner, PeerId id) @@ -876,6 +875,12 @@ const Data::AllowedReactions &ChannelData::allowedReactions() const { return _allowedReactions; } +void ChannelData::processTopics(const MTPVector &topics) { + if (const auto forum = this->forum()) { + forum->applyReceivedTopics(topics); + } +} + namespace Data { void ApplyMigration( diff --git a/Telegram/SourceFiles/data/data_channel.h b/Telegram/SourceFiles/data/data_channel.h index 6e139bb0e..af19a2121 100644 --- a/Telegram/SourceFiles/data/data_channel.h +++ b/Telegram/SourceFiles/data/data_channel.h @@ -437,6 +437,8 @@ public: return mgInfo ? mgInfo->forum() : nullptr; } + void processTopics(const MTPVector &topics); + // Still public data members. uint64 access = 0; diff --git a/Telegram/SourceFiles/data/data_forum.cpp b/Telegram/SourceFiles/data/data_forum.cpp index 8e3230cad..7035e0f71 100644 --- a/Telegram/SourceFiles/data/data_forum.cpp +++ b/Telegram/SourceFiles/data/data_forum.cpp @@ -204,7 +204,16 @@ void Forum::applyReceivedTopics( owner().processChats(data.vchats()); owner().processMessages(data.vmessages(), NewMessageType::Existing); channel()->ptsReceived(data.vpts().v); - const auto &list = data.vtopics().v; + applyReceivedTopics(data.vtopics(), std::move(callback)); + if (!_staleRootIds.empty()) { + requestSomeStale(); + } +} + +void Forum::applyReceivedTopics( + const MTPVector &topics, + Fn)> callback) { + const auto &list = topics.v; for (const auto &topic : list) { const auto rootId = topic.match([&](const auto &data) { return data.vid().v; @@ -235,9 +244,6 @@ void Forum::applyReceivedTopics( } }); } - if (!_staleRootIds.empty()) { - requestSomeStale(); - } } void Forum::requestSomeStale() { diff --git a/Telegram/SourceFiles/data/data_forum.h b/Telegram/SourceFiles/data/data_forum.h index 89477c931..7bef39ae0 100644 --- a/Telegram/SourceFiles/data/data_forum.h +++ b/Telegram/SourceFiles/data/data_forum.h @@ -74,6 +74,9 @@ public: void applyReceivedTopics( const MTPmessages_ForumTopics &topics, Fn)> callback = nullptr); + void applyReceivedTopics( + const MTPVector &topics, + Fn)> callback = nullptr); [[nodiscard]] MsgId reserveCreatingId( const QString &title, diff --git a/Telegram/SourceFiles/data/data_forum_topic.cpp b/Telegram/SourceFiles/data/data_forum_topic.cpp index 82ac881b9..c363ebb90 100644 --- a/Telegram/SourceFiles/data/data_forum_topic.cpp +++ b/Telegram/SourceFiles/data/data_forum_topic.cpp @@ -296,6 +296,8 @@ void ForumTopic::readTillEnd() { void ForumTopic::applyTopic(const MTPDforumTopic &data) { Expects(_rootId == data.vid().v); + const auto min = data.is_short(); + applyCreator(peerFromMTP(data.vfrom_id())); applyCreationDate(data.vdate().v); @@ -307,32 +309,38 @@ void ForumTopic::applyTopic(const MTPDforumTopic &data) { } applyColorId(data.vicon_color().v); - if (data.is_pinned()) { - owner().setChatPinned(this, 0, true); - } else { - _list->pinned()->setPinned(this, false); - } - - owner().notifySettings().apply(this, data.vnotify_settings()); - - const auto draft = data.vdraft(); - if (draft && draft->type() == mtpc_draftMessage) { - Data::ApplyPeerCloudDraft( - &session(), - channel()->id, - _rootId, - draft->c_draftMessage()); - } applyIsMy(data.is_my()); setClosed(data.is_closed()); - _replies->setInboxReadTill( - data.vread_inbox_max_id().v, - data.vunread_count().v); - _replies->setOutboxReadTill(data.vread_outbox_max_id().v); - applyTopicTopMessage(data.vtop_message().v); - unreadMentions().setCount(data.vunread_mentions_count().v); - unreadReactions().setCount(data.vunread_reactions_count().v); + if (min) { + int a = 0; + } else { + if (data.is_pinned()) { + owner().setChatPinned(this, 0, true); + } else { + _list->pinned()->setPinned(this, false); + } + + owner().notifySettings().apply(this, data.vnotify_settings()); + + if (const auto draft = data.vdraft()) { + draft->match([&](const MTPDdraftMessage &data) { + Data::ApplyPeerCloudDraft( + &session(), + channel()->id, + _rootId, + data); + }, [](const MTPDdraftMessageEmpty&) {}); + } + + _replies->setInboxReadTill( + data.vread_inbox_max_id().v, + data.vunread_count().v); + _replies->setOutboxReadTill(data.vread_outbox_max_id().v); + applyTopicTopMessage(data.vtop_message().v); + unreadMentions().setCount(data.vunread_mentions_count().v); + unreadReactions().setCount(data.vunread_reactions_count().v); + } } void ForumTopic::applyCreator(PeerId creatorId) { diff --git a/Telegram/SourceFiles/data/data_replies_list.cpp b/Telegram/SourceFiles/data/data_replies_list.cpp index 40ba50eb7..f28152393 100644 --- a/Telegram/SourceFiles/data/data_replies_list.cpp +++ b/Telegram/SourceFiles/data/data_replies_list.cpp @@ -661,25 +661,6 @@ void RepliesList::loadAfter() { bool RepliesList::processMessagesIsEmpty(const MTPmessages_Messages &result) { const auto guard = gsl::finally([&] { _listChanges.fire({}); }); - const auto fullCount = result.match([&]( - const MTPDmessages_messagesNotModified &) { - LOG(("API Error: received messages.messagesNotModified! " - "(HistoryWidget::messagesReceived)")); - return 0; - }, [&](const MTPDmessages_messages &data) { - return int(data.vmessages().v.size()); - }, [&](const MTPDmessages_messagesSlice &data) { - return data.vcount().v; - }, [&](const MTPDmessages_channelMessages &data) { - if (_history->peer->isChannel()) { - _history->peer->asChannel()->ptsReceived(data.vpts().v); - } else { - LOG(("API Error: received messages.channelMessages when " - "no channel was passed! (HistoryWidget::messagesReceived)")); - } - return data.vcount().v; - }); - auto &owner = _history->owner(); const auto list = result.match([&]( const MTPDmessages_messagesNotModified &) { @@ -691,6 +672,27 @@ bool RepliesList::processMessagesIsEmpty(const MTPmessages_Messages &result) { owner.processChats(data.vchats()); return data.vmessages().v; }); + + const auto fullCount = result.match([&]( + const MTPDmessages_messagesNotModified &) { + LOG(("API Error: received messages.messagesNotModified! " + "(HistoryWidget::messagesReceived)")); + return 0; + }, [&](const MTPDmessages_messages &data) { + return int(data.vmessages().v.size()); + }, [&](const MTPDmessages_messagesSlice &data) { + return data.vcount().v; + }, [&](const MTPDmessages_channelMessages &data) { + if (const auto channel = _history->peer->asChannel()) { + channel->ptsReceived(data.vpts().v); + channel->processTopics(data.vtopics()); + } else { + LOG(("API Error: received messages.channelMessages when " + "no channel was passed! (HistoryWidget::messagesReceived)")); + } + return data.vcount().v; + }); + if (list.isEmpty()) { return true; } diff --git a/Telegram/SourceFiles/data/data_search_controller.cpp b/Telegram/SourceFiles/data/data_search_controller.cpp index 893188b9c..08fae4983 100644 --- a/Telegram/SourceFiles/data/data_search_controller.cpp +++ b/Telegram/SourceFiles/data/data_search_controller.cpp @@ -137,9 +137,10 @@ SearchResult ParseSearchResult( } break; case mtpc_messages_channelMessages: { - auto &d = data.c_messages_channelMessages(); - if (auto channel = peer->asChannel()) { + const auto &d = data.c_messages_channelMessages(); + if (const auto channel = peer->asChannel()) { channel->ptsReceived(d.vpts().v); + channel->processTopics(d.vtopics()); } else { LOG(("API Error: received messages.channelMessages when " "no channel was passed! (ParseSearchResult)")); diff --git a/Telegram/SourceFiles/data/data_session.cpp b/Telegram/SourceFiles/data/data_session.cpp index 1f3214bfb..26180efa3 100644 --- a/Telegram/SourceFiles/data/data_session.cpp +++ b/Telegram/SourceFiles/data/data_session.cpp @@ -2155,6 +2155,7 @@ void Session::processExistingMessages( data.match([&](const MTPDmessages_channelMessages &data) { if (channel) { channel->ptsReceived(data.vpts().v); + channel->processTopics(data.vtopics()); } else { LOG(("App Error: received messages.channelMessages!")); } diff --git a/Telegram/SourceFiles/data/data_web_page.cpp b/Telegram/SourceFiles/data/data_web_page.cpp index 99b8439f2..64957ae02 100644 --- a/Telegram/SourceFiles/data/data_web_page.cpp +++ b/Telegram/SourceFiles/data/data_web_page.cpp @@ -295,6 +295,7 @@ void WebPageData::ApplyChanges( const MTPDmessages_channelMessages &data) { if (channel) { channel->ptsReceived(data.vpts().v); + channel->processTopics(data.vtopics()); } else { LOG(("API Error: received messages.channelMessages " "when no channel was passed! (WebPageData::ApplyChanges)")); diff --git a/Telegram/SourceFiles/dialogs/dialogs_widget.cpp b/Telegram/SourceFiles/dialogs/dialogs_widget.cpp index 7ac389b83..6bf5291d2 100644 --- a/Telegram/SourceFiles/dialogs/dialogs_widget.cpp +++ b/Telegram/SourceFiles/dialogs/dialogs_widget.cpp @@ -1643,6 +1643,7 @@ void Widget::searchReceived( if (const auto peer = searchInPeer()) { if (const auto channel = peer->asChannel()) { channel->ptsReceived(data.vpts().v); + channel->processTopics(data.vtopics()); } else { LOG(("API Error: " "received messages.channelMessages when no channel " diff --git a/Telegram/SourceFiles/history/history_inner_widget.cpp b/Telegram/SourceFiles/history/history_inner_widget.cpp index c719a3ad3..be79da7b6 100644 --- a/Telegram/SourceFiles/history/history_inner_widget.cpp +++ b/Telegram/SourceFiles/history/history_inner_widget.cpp @@ -560,7 +560,7 @@ bool HistoryInner::hasSelectRestriction() const { } void HistoryInner::messagesReceived( - PeerData *peer, + not_null peer, const QVector &messages) { if (_history->peer == peer) { _history->addOlderSlice(messages); @@ -575,7 +575,9 @@ void HistoryInner::messagesReceived( } } -void HistoryInner::messagesReceivedDown(PeerData *peer, const QVector &messages) { +void HistoryInner::messagesReceivedDown( + not_null peer, + const QVector &messages) { if (_history->peer == peer) { const auto oldLoaded = _migrated && _history->isEmpty() diff --git a/Telegram/SourceFiles/history/history_inner_widget.h b/Telegram/SourceFiles/history/history_inner_widget.h index a84bb95b9..43ff3fb45 100644 --- a/Telegram/SourceFiles/history/history_inner_widget.h +++ b/Telegram/SourceFiles/history/history_inner_widget.h @@ -99,8 +99,12 @@ public: Ui::ChatPaintContext preparePaintContext(const QRect &clip) const; - void messagesReceived(PeerData *peer, const QVector &messages); - void messagesReceivedDown(PeerData *peer, const QVector &messages); + void messagesReceived( + not_null peer, + const QVector &messages); + void messagesReceivedDown( + not_null peer, + const QVector &messages); [[nodiscard]] TextForMimeData getSelectedText() const; diff --git a/Telegram/SourceFiles/history/history_unread_things.cpp b/Telegram/SourceFiles/history/history_unread_things.cpp index 928a74123..4cca97437 100644 --- a/Telegram/SourceFiles/history/history_unread_things.cpp +++ b/Telegram/SourceFiles/history/history_unread_things.cpp @@ -142,6 +142,7 @@ void Proxy::addSlice(const MTPmessages_Messages &slice, int alreadyLoaded) { }, [&](const MTPDmessages_channelMessages &data) { if (const auto channel = history->peer->asChannel()) { channel->ptsReceived(data.vpts().v); + channel->processTopics(data.vtopics()); } else { LOG(("API Error: received messages.channelMessages when " "no channel was passed! (Proxy::addSlice)")); diff --git a/Telegram/SourceFiles/history/history_widget.cpp b/Telegram/SourceFiles/history/history_widget.cpp index 2c0973114..92e113775 100644 --- a/Telegram/SourceFiles/history/history_widget.cpp +++ b/Telegram/SourceFiles/history/history_widget.cpp @@ -3021,7 +3021,10 @@ void HistoryWidget::messagesFailed(const MTP::Error &error, int requestId) { } } -void HistoryWidget::messagesReceived(PeerData *peer, const MTPmessages_Messages &messages, int requestId) { +void HistoryWidget::messagesReceived( + not_null peer, + const MTPmessages_Messages &messages, + int requestId) { Expects(_history != nullptr); bool toMigrated = (peer == _peer->migrateFrom()); @@ -3057,8 +3060,9 @@ void HistoryWidget::messagesReceived(PeerData *peer, const MTPmessages_Messages } break; case mtpc_messages_channelMessages: { auto &d(messages.c_messages_channelMessages()); - if (peer && peer->isChannel()) { - peer->asChannel()->ptsReceived(d.vpts().v); + if (const auto channel = peer->asChannel()) { + channel->ptsReceived(d.vpts().v); + channel->processTopics(d.vtopics()); } else { LOG(("API Error: received messages.channelMessages when no channel was passed! (HistoryWidget::messagesReceived)")); } @@ -5668,7 +5672,9 @@ std::optional HistoryWidget::unreadBarTop() const { return std::nullopt; } -void HistoryWidget::addMessagesToFront(PeerData *peer, const QVector &messages) { +void HistoryWidget::addMessagesToFront( + not_null peer, + const QVector &messages) { _list->messagesReceived(peer, messages); if (!_firstLoadRequest) { updateHistoryGeometry(); @@ -5677,7 +5683,7 @@ void HistoryWidget::addMessagesToFront(PeerData *peer, const QVector } void HistoryWidget::addMessagesToBack( - PeerData *peer, + not_null peer, const QVector &messages) { const auto checkForUnreadStart = [&] { if (_history->unreadBar() || !_history->trackUnreadMessages()) { diff --git a/Telegram/SourceFiles/history/history_widget.h b/Telegram/SourceFiles/history/history_widget.h index d8b236c81..bda30bfec 100644 --- a/Telegram/SourceFiles/history/history_widget.h +++ b/Telegram/SourceFiles/history/history_widget.h @@ -522,10 +522,10 @@ private: void checkPreview(); void requestPreview(); void gotPreview(QString links, const MTPMessageMedia &media, mtpRequestId req); - void messagesReceived(PeerData *peer, const MTPmessages_Messages &messages, int requestId); + void messagesReceived(not_null peer, const MTPmessages_Messages &messages, int requestId); void messagesFailed(const MTP::Error &error, int requestId); - void addMessagesToFront(PeerData *peer, const QVector &messages); - void addMessagesToBack(PeerData *peer, const QVector &messages); + void addMessagesToFront(not_null peer, const QVector &messages); + void addMessagesToBack(not_null peer, const QVector &messages); void updateHistoryGeometry(bool initial = false, bool loadedDown = false, const ScrollChange &change = { ScrollChangeNone, 0 }); void updateListSize();