Fixed Api requests for statistics with non-default DC.

This commit is contained in:
23rd 2023-11-11 14:58:44 +03:00
parent 468d4c5e4f
commit 1e6cf839e2
7 changed files with 41 additions and 61 deletions

View file

@ -187,40 +187,37 @@ namespace {
} // namespace } // namespace
Statistics::Statistics(not_null<ApiWrap*> api) Statistics::Statistics(not_null<ChannelData*> channel)
: _api(&api->instance()) { : _channel(channel)
, _api(&channel->session().api().instance()) {
} }
rpl::producer<rpl::no_value, QString> Statistics::request( rpl::producer<rpl::no_value, QString> Statistics::request() {
not_null<PeerData*> peer) {
return [=](auto consumer) { return [=](auto consumer) {
auto lifetime = rpl::lifetime(); 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( _api.request(MTPstats_GetBroadcastStats(
MTP_flags(MTPstats_GetBroadcastStats::Flags(0)), MTP_flags(MTPstats_GetBroadcastStats::Flags(0)),
channel->inputChannel _channel->inputChannel
)).done([=](const MTPstats_BroadcastStats &result) { )).done([=](const MTPstats_BroadcastStats &result) {
_channelStats = ChannelStatisticsFromTL(result.data()); _channelStats = ChannelStatisticsFromTL(result.data());
consumer.put_done(); consumer.put_done();
}).fail([=](const MTP::Error &error) { }).fail([=](const MTP::Error &error) {
consumer.put_error_copy(error.type()); consumer.put_error_copy(error.type());
}).send(); }).toDC(MTP::ShiftDcId(dcId, MTP::kStatsDcShift)).send();
} else { } else {
_api.request(MTPstats_GetMegagroupStats( _api.request(MTPstats_GetMegagroupStats(
MTP_flags(MTPstats_GetMegagroupStats::Flags(0)), MTP_flags(MTPstats_GetMegagroupStats::Flags(0)),
channel->inputChannel _channel->inputChannel
)).done([=](const MTPstats_MegagroupStats &result) { )).done([=](const MTPstats_MegagroupStats &result) {
_supergroupStats = SupergroupStatisticsFromTL(result.data()); _supergroupStats = SupergroupStatisticsFromTL(result.data());
peer->owner().processUsers(result.data().vusers()); _channel->owner().processUsers(result.data().vusers());
consumer.put_done(); consumer.put_done();
}).fail([=](const MTP::Error &error) { }).fail([=](const MTP::Error &error) {
consumer.put_error_copy(error.type()); consumer.put_error_copy(error.type());
}).send(); }).toDC(MTP::ShiftDcId(dcId, MTP::kStatsDcShift)).send();
} }
return lifetime; return lifetime;
@ -228,15 +225,11 @@ rpl::producer<rpl::no_value, QString> Statistics::request(
} }
Statistics::GraphResult Statistics::requestZoom( Statistics::GraphResult Statistics::requestZoom(
not_null<PeerData*> peer,
const QString &token, const QString &token,
float64 x) { float64 x) {
return [=](auto consumer) { return [=](auto consumer) {
auto lifetime = rpl::lifetime(); auto lifetime = rpl::lifetime();
const auto channel = peer->asChannel(); const auto dcId = _channel->owner().statsDcId(_channel);
if (!channel) {
return lifetime;
}
const auto wasEmpty = _zoomDeque.empty(); const auto wasEmpty = _zoomDeque.empty();
_zoomDeque.push_back([=] { _zoomDeque.push_back([=] {
_api.request(MTPstats_LoadAsyncGraph( _api.request(MTPstats_LoadAsyncGraph(
@ -256,7 +249,7 @@ Statistics::GraphResult Statistics::requestZoom(
} }
}).fail([=](const MTP::Error &error) { }).fail([=](const MTP::Error &error) {
consumer.put_error_copy(error.type()); consumer.put_error_copy(error.type());
}).send(); }).toDC(MTP::ShiftDcId(dcId, MTP::kStatsDcShift)).send();
}); });
if (wasEmpty) { if (wasEmpty) {
_zoomDeque.front()(); _zoomDeque.front()();
@ -266,32 +259,6 @@ Statistics::GraphResult Statistics::requestZoom(
}; };
} }
Statistics::GraphResult Statistics::requestMessage(
not_null<PeerData*> 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 { Data::ChannelStatistics Statistics::channelStats() const {
return _channelStats; return _channelStats;
} }
@ -314,6 +281,7 @@ void PublicForwards::request(
if (_requestId) { if (_requestId) {
return; return;
} }
const auto dcId = _channel->owner().statsDcId(_channel);
const auto offsetPeer = _channel->owner().peer(token.fullId.peer); const auto offsetPeer = _channel->owner().peer(token.fullId.peer);
const auto tlOffsetPeer = offsetPeer const auto tlOffsetPeer = offsetPeer
? offsetPeer->input ? offsetPeer->input
@ -396,7 +364,7 @@ void PublicForwards::request(
}); });
}).fail([=] { }).fail([=] {
_requestId = 0; _requestId = 0;
}).send(); }).toDC(MTP::ShiftDcId(dcId, MTP::kStatsDcShift)).send();
} }
MessageStatistics::MessageStatistics( MessageStatistics::MessageStatistics(
@ -416,6 +384,7 @@ void MessageStatistics::request(Fn<void(Data::MessageStatistics)> done) {
if (_channel->isMegagroup()) { if (_channel->isMegagroup()) {
return; return;
} }
const auto dcId = _channel->owner().statsDcId(_channel);
const auto requestFirstPublicForwards = [=]( const auto requestFirstPublicForwards = [=](
const Data::StatisticalGraph &messageGraph, const Data::StatisticalGraph &messageGraph,
@ -484,7 +453,7 @@ void MessageStatistics::request(Fn<void(Data::MessageStatistics)> done) {
StatisticalGraphFromTL(result.data().vviews_graph())); StatisticalGraphFromTL(result.data().vviews_graph()));
}).fail([=](const MTP::Error &error) { }).fail([=](const MTP::Error &error) {
requestPrivateForwards({}); requestPrivateForwards({});
}).send(); }).toDC(MTP::ShiftDcId(dcId, MTP::kStatsDcShift)).send();
} }

View file

@ -11,7 +11,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "data/data_statistics.h" #include "data/data_statistics.h"
#include "mtproto/sender.h" #include "mtproto/sender.h"
class ApiWrap;
class ChannelData; class ChannelData;
class PeerData; class PeerData;
@ -19,23 +18,19 @@ namespace Api {
class Statistics final { class Statistics final {
public: public:
explicit Statistics(not_null<ApiWrap*> api); explicit Statistics(not_null<ChannelData*> channel);
[[nodiscard]] rpl::producer<rpl::no_value, QString> request( [[nodiscard]] rpl::producer<rpl::no_value, QString> request();
not_null<PeerData*> peer);
using GraphResult = rpl::producer<Data::StatisticalGraph, QString>; using GraphResult = rpl::producer<Data::StatisticalGraph, QString>;
[[nodiscard]] GraphResult requestZoom( [[nodiscard]] GraphResult requestZoom(
not_null<PeerData*> peer,
const QString &token, const QString &token,
float64 x); float64 x);
[[nodiscard]] GraphResult requestMessage(
not_null<PeerData*> peer,
MsgId msgId);
[[nodiscard]] Data::ChannelStatistics channelStats() const; [[nodiscard]] Data::ChannelStatistics channelStats() const;
[[nodiscard]] Data::SupergroupStatistics supergroupStats() const; [[nodiscard]] Data::SupergroupStatistics supergroupStats() const;
private: private:
const not_null<ChannelData*> _channel;
Data::ChannelStatistics _channelStats; Data::ChannelStatistics _channelStats;
Data::SupergroupStatistics _supergroupStats; Data::SupergroupStatistics _supergroupStats;
MTP::Sender _api; MTP::Sender _api;

View file

@ -1120,6 +1120,10 @@ void ApplyChannelUpdate(
channel, channel,
update.vnotify_settings()); update.vnotify_settings());
if (update.vstats_dc()) {
channel->owner().applyStatsDcId(channel, update.vstats_dc()->v);
}
if (const auto sendAs = update.vdefault_send_as()) { if (const auto sendAs = update.vdefault_send_as()) {
session->sendAsPeers().setChosen(channel, peerFromMTP(*sendAs)); session->sendAsPeers().setChosen(channel, peerFromMTP(*sendAs));
} else { } else {

View file

@ -4532,6 +4532,15 @@ uint64 Session::wallpapersHash() const {
return _wallpapersHash; return _wallpapersHash;
} }
MTP::DcId Session::statsDcId(not_null<ChannelData*> channel) {
const auto it = _channelStatsDcIds.find(channel);
return (it == end(_channelStatsDcIds)) ? MTP::DcId(0) : it->second;
}
void Session::applyStatsDcId(not_null<ChannelData*> channel, MTP::DcId dcId) {
_channelStatsDcIds[channel] = dcId;
}
void Session::webViewResultSent(WebViewResultSent &&sent) { void Session::webViewResultSent(WebViewResultSent &&sent) {
return _webViewResultSent.fire(std::move(sent)); return _webViewResultSent.fire(std::move(sent));
} }

View file

@ -730,6 +730,9 @@ public:
[[nodiscard]] auto peerDecorationsUpdated() const [[nodiscard]] auto peerDecorationsUpdated() const
-> rpl::producer<not_null<PeerData*>>; -> rpl::producer<not_null<PeerData*>>;
void applyStatsDcId(not_null<ChannelData*>, MTP::DcId);
[[nodiscard]] MTP::DcId statsDcId(not_null<ChannelData*>);
void clearLocalStorage(); void clearLocalStorage();
private: private:
@ -1014,6 +1017,8 @@ private:
base::flat_map<not_null<UserData*>, TimeId> _watchingForOffline; base::flat_map<not_null<UserData*>, TimeId> _watchingForOffline;
base::Timer _watchForOfflineTimer; base::Timer _watchForOfflineTimer;
base::flat_map<not_null<ChannelData*>, MTP::DcId> _channelStatsDcIds;
rpl::event_stream<WebViewResultSent> _webViewResultSent; rpl::event_stream<WebViewResultSent> _webViewResultSent;
rpl::event_stream<not_null<PeerData*>> _peerDecorationsUpdated; rpl::event_stream<not_null<PeerData*>> _peerDecorationsUpdated;

View file

@ -91,7 +91,6 @@ void ProcessZoom(
widget->zoomRequests( widget->zoomRequests(
) | rpl::start_with_next([=](float64 x) { ) | rpl::start_with_next([=](float64 x) {
d.api->requestZoom( d.api->requestZoom(
d.peer,
zoomToken, zoomToken,
x x
) | rpl::start_with_next_error_done([=]( ) | rpl::start_with_next_error_done([=](
@ -144,7 +143,6 @@ void FillStatistic(
m); m);
descriptor.api->requestZoom( descriptor.api->requestZoom(
descriptor.peer,
graphData.zoomToken, graphData.zoomToken,
0 0
) | rpl::start_with_next_error_done([=, graphPtr = &graphData]( ) | rpl::start_with_next_error_done([=, graphPtr = &graphData](
@ -524,7 +522,7 @@ void InnerWidget::load() {
const auto descriptor = Descriptor{ const auto descriptor = Descriptor{
_peer, _peer,
lifetime().make_state<Api::Statistics>(&_peer->session().api()), lifetime().make_state<Api::Statistics>(_peer->asChannel()),
_controller->uiShow()->toastParent(), _controller->uiShow()->toastParent(),
}; };
@ -543,7 +541,6 @@ void InnerWidget::load() {
) | rpl::take(1) | rpl::start_with_next([=] { ) | rpl::take(1) | rpl::start_with_next([=] {
if (!_contextId) { if (!_contextId) {
descriptor.api->request( descriptor.api->request(
descriptor.peer
) | rpl::start_with_done([=] { ) | rpl::start_with_done([=] {
_state.stats = Data::AnyStatistics{ _state.stats = Data::AnyStatistics{
descriptor.api->channelStats(), descriptor.api->channelStats(),
@ -575,7 +572,7 @@ void InnerWidget::fill() {
const auto inner = this; const auto inner = this;
const auto descriptor = Descriptor{ const auto descriptor = Descriptor{
_peer, _peer,
lifetime().make_state<Api::Statistics>(&_peer->session().api()), lifetime().make_state<Api::Statistics>(_peer->asChannel()),
_controller->uiShow()->toastParent(), _controller->uiShow()->toastParent(),
}; };
if (_state.stats.message) { if (_state.stats.message) {

View file

@ -47,6 +47,7 @@ constexpr auto kUpdaterDcShift = 0x03;
constexpr auto kExportDcShift = 0x04; constexpr auto kExportDcShift = 0x04;
constexpr auto kExportMediaDcShift = 0x05; constexpr auto kExportMediaDcShift = 0x05;
constexpr auto kGroupCallStreamDcShift = 0x06; constexpr auto kGroupCallStreamDcShift = 0x06;
constexpr auto kStatsDcShift = 0x06;
constexpr auto kMaxMediaDcCount = 0x10; constexpr auto kMaxMediaDcCount = 0x10;
constexpr auto kBaseDownloadDcShift = 0x10; constexpr auto kBaseDownloadDcShift = 0x10;
constexpr auto kBaseUploadDcShift = 0x20; constexpr auto kBaseUploadDcShift = 0x20;