From 3fa168cee00ca966d8a12e515ac1d29acb53a6f0 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Mon, 9 Oct 2023 17:10:43 +0300 Subject: [PATCH] Added API class for requesting full statistic of single message. --- Telegram/SourceFiles/api/api_statistics.cpp | 66 +++++++++++++++++++++ Telegram/SourceFiles/api/api_statistics.h | 22 +++++++ Telegram/SourceFiles/data/data_statistics.h | 10 ++++ 3 files changed, 98 insertions(+) diff --git a/Telegram/SourceFiles/api/api_statistics.cpp b/Telegram/SourceFiles/api/api_statistics.cpp index 5ca74ab1c..933b7118e 100644 --- a/Telegram/SourceFiles/api/api_statistics.cpp +++ b/Telegram/SourceFiles/api/api_statistics.cpp @@ -388,4 +388,70 @@ void PublicForwards::request( }).send(); } +MessageStatistics::MessageStatistics( + not_null channel, + FullMsgId fullId) +: _publicForwards(channel, fullId) +, _channel(channel) +, _fullId(fullId) +, _api(&channel->session().api().instance()) { +} + +PublicForwards::Slice MessageStatistics::firstSlice() const { + return _firstSlice; +} + +void MessageStatistics::request(Fn done) { + if (_channel->isMegagroup()) { + return; + } + + const auto requestFirstPublicForwards = [=]( + const Data::StatisticalGraph &messageGraph, + const Data::StatisticsMessageInteractionInfo &info) { + _publicForwards.request({}, [=](PublicForwards::Slice slice) { + const auto total = slice.total; + _firstSlice = std::move(slice); + done({ + .messageInteractionGraph = messageGraph, + .publicForwards = total, + .privateForwards = info.forwardsCount - total, + .views = info.viewsCount, + }); + }); + }; + + const auto requestPrivateForwards = [=]( + const Data::StatisticalGraph &messageGraph) { + _api.request(MTPstats_GetBroadcastStats( + MTP_flags(MTPstats_GetBroadcastStats::Flags(0)), + _channel->inputChannel + )).done([=](const MTPstats_BroadcastStats &result) { + const auto channelStats = ChannelStatisticsFromTL(result.data()); + auto info = Data::StatisticsMessageInteractionInfo(); + for (const auto &r : channelStats.recentMessageInteractions) { + if (r.messageId == _fullId.msg) { + info = r; + break; + } + } + requestFirstPublicForwards(messageGraph, info); + }).fail([=](const MTP::Error &error) { + requestFirstPublicForwards(messageGraph, {}); + }).send(); + }; + + _api.request(MTPstats_GetMessageStats( + MTP_flags(MTPstats_GetMessageStats::Flags(0)), + _channel->inputChannel, + MTP_int(_fullId.msg.bare) + )).done([=](const MTPstats_MessageStats &result) { + requestPrivateForwards( + StatisticalGraphFromTL(result.data().vviews_graph())); + }).fail([=](const MTP::Error &error) { + requestPrivateForwards({}); + }).send(); + +} + } // namespace Api diff --git a/Telegram/SourceFiles/api/api_statistics.h b/Telegram/SourceFiles/api/api_statistics.h index e3a282c96..d5d9583d4 100644 --- a/Telegram/SourceFiles/api/api_statistics.h +++ b/Telegram/SourceFiles/api/api_statistics.h @@ -69,4 +69,26 @@ private: }; +class MessageStatistics final { +public: + explicit MessageStatistics( + not_null channel, + FullMsgId fullId); + + void request(Fn done); + + [[nodiscard]] PublicForwards::Slice firstSlice() const; + +private: + PublicForwards _publicForwards; + const not_null _channel; + const FullMsgId _fullId; + + PublicForwards::Slice _firstSlice; + + mtpRequestId _requestId = 0; + MTP::Sender _api; + +}; + } // namespace Api diff --git a/Telegram/SourceFiles/data/data_statistics.h b/Telegram/SourceFiles/data/data_statistics.h index f27ab52d5..88a85e22c 100644 --- a/Telegram/SourceFiles/data/data_statistics.h +++ b/Telegram/SourceFiles/data/data_statistics.h @@ -103,4 +103,14 @@ struct SupergroupStatistics final { }; +struct MessageStatistics final { + explicit operator bool() const { + return !messageInteractionGraph.chart.empty(); + } + Data::StatisticalGraph messageInteractionGraph; + int publicForwards = 0; + int privateForwards = 0; + int views = 0; +}; + } // namespace Data