diff --git a/Telegram/SourceFiles/api/api_statistics.cpp b/Telegram/SourceFiles/api/api_statistics.cpp index 383d0973cf..12c7a313ef 100644 --- a/Telegram/SourceFiles/api/api_statistics.cpp +++ b/Telegram/SourceFiles/api/api_statistics.cpp @@ -187,40 +187,37 @@ namespace { } // namespace -Statistics::Statistics(not_null api) -: _api(&api->instance()) { +Statistics::Statistics(not_null channel) +: _channel(channel) +, _api(&channel->session().api().instance()) { } -rpl::producer Statistics::request( - not_null peer) { +rpl::producer Statistics::request() { return [=](auto consumer) { auto lifetime = rpl::lifetime(); - const auto channel = peer->asChannel(); - if (!channel) { - return lifetime; - } - if (!channel->isMegagroup()) { + const auto dcId = _channel->owner().statsDcId(_channel); + if (!_channel->isMegagroup()) { _api.request(MTPstats_GetBroadcastStats( MTP_flags(MTPstats_GetBroadcastStats::Flags(0)), - channel->inputChannel + _channel->inputChannel )).done([=](const MTPstats_BroadcastStats &result) { _channelStats = ChannelStatisticsFromTL(result.data()); consumer.put_done(); }).fail([=](const MTP::Error &error) { consumer.put_error_copy(error.type()); - }).send(); + }).toDC(MTP::ShiftDcId(dcId, MTP::kStatsDcShift)).send(); } else { _api.request(MTPstats_GetMegagroupStats( MTP_flags(MTPstats_GetMegagroupStats::Flags(0)), - channel->inputChannel + _channel->inputChannel )).done([=](const MTPstats_MegagroupStats &result) { _supergroupStats = SupergroupStatisticsFromTL(result.data()); - peer->owner().processUsers(result.data().vusers()); + _channel->owner().processUsers(result.data().vusers()); consumer.put_done(); }).fail([=](const MTP::Error &error) { consumer.put_error_copy(error.type()); - }).send(); + }).toDC(MTP::ShiftDcId(dcId, MTP::kStatsDcShift)).send(); } return lifetime; @@ -228,15 +225,11 @@ rpl::producer Statistics::request( } Statistics::GraphResult Statistics::requestZoom( - not_null peer, const QString &token, float64 x) { return [=](auto consumer) { auto lifetime = rpl::lifetime(); - const auto channel = peer->asChannel(); - if (!channel) { - return lifetime; - } + const auto dcId = _channel->owner().statsDcId(_channel); const auto wasEmpty = _zoomDeque.empty(); _zoomDeque.push_back([=] { _api.request(MTPstats_LoadAsyncGraph( @@ -256,7 +249,7 @@ Statistics::GraphResult Statistics::requestZoom( } }).fail([=](const MTP::Error &error) { consumer.put_error_copy(error.type()); - }).send(); + }).toDC(MTP::ShiftDcId(dcId, MTP::kStatsDcShift)).send(); }); if (wasEmpty) { _zoomDeque.front()(); @@ -266,32 +259,6 @@ Statistics::GraphResult Statistics::requestZoom( }; } -Statistics::GraphResult Statistics::requestMessage( - not_null peer, - MsgId msgId) { - return [=](auto consumer) { - auto lifetime = rpl::lifetime(); - const auto channel = peer->asChannel(); - if (!channel) { - return lifetime; - } - - _api.request(MTPstats_GetMessageStats( - MTP_flags(MTPstats_GetMessageStats::Flags(0)), - channel->inputChannel, - MTP_int(msgId.bare) - )).done([=](const MTPstats_MessageStats &result) { - consumer.put_next( - StatisticalGraphFromTL(result.data().vviews_graph())); - consumer.put_done(); - }).fail([=](const MTP::Error &error) { - consumer.put_error_copy(error.type()); - }).send(); - - return lifetime; - }; -} - Data::ChannelStatistics Statistics::channelStats() const { return _channelStats; } @@ -314,6 +281,7 @@ void PublicForwards::request( if (_requestId) { return; } + const auto dcId = _channel->owner().statsDcId(_channel); const auto offsetPeer = _channel->owner().peer(token.fullId.peer); const auto tlOffsetPeer = offsetPeer ? offsetPeer->input @@ -396,7 +364,7 @@ void PublicForwards::request( }); }).fail([=] { _requestId = 0; - }).send(); + }).toDC(MTP::ShiftDcId(dcId, MTP::kStatsDcShift)).send(); } MessageStatistics::MessageStatistics( @@ -416,6 +384,7 @@ void MessageStatistics::request(Fn done) { if (_channel->isMegagroup()) { return; } + const auto dcId = _channel->owner().statsDcId(_channel); const auto requestFirstPublicForwards = [=]( const Data::StatisticalGraph &messageGraph, @@ -484,7 +453,7 @@ void MessageStatistics::request(Fn done) { StatisticalGraphFromTL(result.data().vviews_graph())); }).fail([=](const MTP::Error &error) { requestPrivateForwards({}); - }).send(); + }).toDC(MTP::ShiftDcId(dcId, MTP::kStatsDcShift)).send(); } diff --git a/Telegram/SourceFiles/api/api_statistics.h b/Telegram/SourceFiles/api/api_statistics.h index 460c9df9d4..14ec1ccf8e 100644 --- a/Telegram/SourceFiles/api/api_statistics.h +++ b/Telegram/SourceFiles/api/api_statistics.h @@ -11,7 +11,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "data/data_statistics.h" #include "mtproto/sender.h" -class ApiWrap; class ChannelData; class PeerData; @@ -19,23 +18,19 @@ namespace Api { class Statistics final { public: - explicit Statistics(not_null api); + explicit Statistics(not_null channel); - [[nodiscard]] rpl::producer request( - not_null peer); + [[nodiscard]] rpl::producer request(); using GraphResult = rpl::producer; [[nodiscard]] GraphResult requestZoom( - not_null peer, const QString &token, float64 x); - [[nodiscard]] GraphResult requestMessage( - not_null peer, - MsgId msgId); [[nodiscard]] Data::ChannelStatistics channelStats() const; [[nodiscard]] Data::SupergroupStatistics supergroupStats() const; private: + const not_null _channel; Data::ChannelStatistics _channelStats; Data::SupergroupStatistics _supergroupStats; MTP::Sender _api; diff --git a/Telegram/SourceFiles/data/data_channel.cpp b/Telegram/SourceFiles/data/data_channel.cpp index 3b26c1eb96..5daff7e176 100644 --- a/Telegram/SourceFiles/data/data_channel.cpp +++ b/Telegram/SourceFiles/data/data_channel.cpp @@ -1120,6 +1120,10 @@ void ApplyChannelUpdate( channel, update.vnotify_settings()); + if (update.vstats_dc()) { + channel->owner().applyStatsDcId(channel, update.vstats_dc()->v); + } + if (const auto sendAs = update.vdefault_send_as()) { session->sendAsPeers().setChosen(channel, peerFromMTP(*sendAs)); } else { diff --git a/Telegram/SourceFiles/data/data_session.cpp b/Telegram/SourceFiles/data/data_session.cpp index 162db84c2b..bfe8019de0 100644 --- a/Telegram/SourceFiles/data/data_session.cpp +++ b/Telegram/SourceFiles/data/data_session.cpp @@ -4532,6 +4532,15 @@ uint64 Session::wallpapersHash() const { return _wallpapersHash; } +MTP::DcId Session::statsDcId(not_null channel) { + const auto it = _channelStatsDcIds.find(channel); + return (it == end(_channelStatsDcIds)) ? MTP::DcId(0) : it->second; +} + +void Session::applyStatsDcId(not_null channel, MTP::DcId dcId) { + _channelStatsDcIds[channel] = dcId; +} + void Session::webViewResultSent(WebViewResultSent &&sent) { return _webViewResultSent.fire(std::move(sent)); } diff --git a/Telegram/SourceFiles/data/data_session.h b/Telegram/SourceFiles/data/data_session.h index c470e3a8b3..df6dc5019a 100644 --- a/Telegram/SourceFiles/data/data_session.h +++ b/Telegram/SourceFiles/data/data_session.h @@ -730,6 +730,9 @@ public: [[nodiscard]] auto peerDecorationsUpdated() const -> rpl::producer>; + void applyStatsDcId(not_null, MTP::DcId); + [[nodiscard]] MTP::DcId statsDcId(not_null); + void clearLocalStorage(); private: @@ -1014,6 +1017,8 @@ private: base::flat_map, TimeId> _watchingForOffline; base::Timer _watchForOfflineTimer; + base::flat_map, MTP::DcId> _channelStatsDcIds; + rpl::event_stream _webViewResultSent; rpl::event_stream> _peerDecorationsUpdated; diff --git a/Telegram/SourceFiles/info/statistics/info_statistics_inner_widget.cpp b/Telegram/SourceFiles/info/statistics/info_statistics_inner_widget.cpp index 19704c7797..237453d6e6 100644 --- a/Telegram/SourceFiles/info/statistics/info_statistics_inner_widget.cpp +++ b/Telegram/SourceFiles/info/statistics/info_statistics_inner_widget.cpp @@ -91,7 +91,6 @@ void ProcessZoom( widget->zoomRequests( ) | rpl::start_with_next([=](float64 x) { d.api->requestZoom( - d.peer, zoomToken, x ) | rpl::start_with_next_error_done([=]( @@ -144,7 +143,6 @@ void FillStatistic( m); descriptor.api->requestZoom( - descriptor.peer, graphData.zoomToken, 0 ) | rpl::start_with_next_error_done([=, graphPtr = &graphData]( @@ -524,7 +522,7 @@ void InnerWidget::load() { const auto descriptor = Descriptor{ _peer, - lifetime().make_state(&_peer->session().api()), + lifetime().make_state(_peer->asChannel()), _controller->uiShow()->toastParent(), }; @@ -543,7 +541,6 @@ void InnerWidget::load() { ) | rpl::take(1) | rpl::start_with_next([=] { if (!_contextId) { descriptor.api->request( - descriptor.peer ) | rpl::start_with_done([=] { _state.stats = Data::AnyStatistics{ descriptor.api->channelStats(), @@ -575,7 +572,7 @@ void InnerWidget::fill() { const auto inner = this; const auto descriptor = Descriptor{ _peer, - lifetime().make_state(&_peer->session().api()), + lifetime().make_state(_peer->asChannel()), _controller->uiShow()->toastParent(), }; if (_state.stats.message) { diff --git a/Telegram/SourceFiles/mtproto/core_types.h b/Telegram/SourceFiles/mtproto/core_types.h index 0437644be2..fba98aeeef 100644 --- a/Telegram/SourceFiles/mtproto/core_types.h +++ b/Telegram/SourceFiles/mtproto/core_types.h @@ -47,6 +47,7 @@ constexpr auto kUpdaterDcShift = 0x03; constexpr auto kExportDcShift = 0x04; constexpr auto kExportMediaDcShift = 0x05; constexpr auto kGroupCallStreamDcShift = 0x06; +constexpr auto kStatsDcShift = 0x06; constexpr auto kMaxMediaDcCount = 0x10; constexpr auto kBaseDownloadDcShift = 0x10; constexpr auto kBaseUploadDcShift = 0x20;