From 1056a5cc8e0edf30fbddb5499ecf1b018b72cb27 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Fri, 17 Nov 2023 05:57:50 +0300 Subject: [PATCH] Added API support to resolve story statistics. --- Telegram/SourceFiles/api/api_statistics.cpp | 74 +++++++++++++++++---- Telegram/SourceFiles/api/api_statistics.h | 4 ++ Telegram/SourceFiles/data/data_statistics.h | 3 + 3 files changed, 69 insertions(+), 12 deletions(-) diff --git a/Telegram/SourceFiles/api/api_statistics.cpp b/Telegram/SourceFiles/api/api_statistics.cpp index 70226fa06..bc3e415cd 100644 --- a/Telegram/SourceFiles/api/api_statistics.cpp +++ b/Telegram/SourceFiles/api/api_statistics.cpp @@ -437,6 +437,14 @@ MessageStatistics::MessageStatistics( , _fullId(fullId) { } +MessageStatistics::MessageStatistics( + not_null channel, + FullStoryId storyId) +: StatisticsRequestSender(channel) +, _publicForwards(channel, {}) +, _storyId(storyId) { +} + Data::PublicForwardsSlice MessageStatistics::firstSlice() const { return _firstSlice; } @@ -518,18 +526,60 @@ void MessageStatistics::request(Fn done) { }).send(); }; - makeRequest(MTPstats_GetMessageStats( - MTP_flags(MTPstats_GetMessageStats::Flags(0)), - channel()->inputChannel, - MTP_int(_fullId.msg.bare) - )).done([=](const MTPstats_MessageStats &result) { - const auto &data = result.data(); - requestPrivateForwards( - StatisticalGraphFromTL(data.vviews_graph()), - StatisticalGraphFromTL(data.vreactions_by_emotion_graph())); - }).fail([=](const MTP::Error &error) { - requestPrivateForwards({}, {}); - }).send(); + const auto requestStoryPrivateForwards = [=]( + const Data::StatisticalGraph &messageGraph, + const Data::StatisticalGraph &reactionsGraph) { + api().request(MTPstories_GetStoriesByID( + channel()->input, + MTP_vector(1, MTP_int(_storyId.story))) + ).done([=](const MTPstories_Stories &result) { + const auto &storyItem = result.data().vstories().v.front(); + storyItem.match([&](const MTPDstoryItem &data) { + if (!data.vviews()) { + return; + } + const auto &tlViews = data.vviews()->data(); + done({ + .messageInteractionGraph = messageGraph, + .reactionsByEmotionGraph = reactionsGraph, + .publicForwards = -1, + .privateForwards = tlViews.vforwards_count().value_or(0), + .views = tlViews.vviews_count().v, + .reactions = tlViews.vreactions_count().value_or(0), + }); + }, [](const auto &) { + }); + }).fail([=](const MTP::Error &error) { + }).send(); + }; + + if (_storyId) { + makeRequest(MTPstats_GetStoryStats( + MTP_flags(MTPstats_GetStoryStats::Flags(0)), + channel()->input, + MTP_int(_storyId.story) + )).done([=](const MTPstats_StoryStats &result) { + const auto &data = result.data(); + requestStoryPrivateForwards( + StatisticalGraphFromTL(data.vviews_graph()), + StatisticalGraphFromTL(data.vreactions_by_emotion_graph())); + }).fail([=](const MTP::Error &error) { + requestStoryPrivateForwards({}, {}); + }).send(); + } else { + makeRequest(MTPstats_GetMessageStats( + MTP_flags(MTPstats_GetMessageStats::Flags(0)), + channel()->inputChannel, + MTP_int(_fullId.msg.bare) + )).done([=](const MTPstats_MessageStats &result) { + const auto &data = result.data(); + requestPrivateForwards( + StatisticalGraphFromTL(data.vviews_graph()), + StatisticalGraphFromTL(data.vreactions_by_emotion_graph())); + }).fail([=](const MTP::Error &error) { + requestPrivateForwards({}, {}); + }).send(); + } } Boosts::Boosts(not_null peer) diff --git a/Telegram/SourceFiles/api/api_statistics.h b/Telegram/SourceFiles/api/api_statistics.h index dddf3654c..4df9ef4fb 100644 --- a/Telegram/SourceFiles/api/api_statistics.h +++ b/Telegram/SourceFiles/api/api_statistics.h @@ -86,6 +86,9 @@ public: explicit MessageStatistics( not_null channel, FullMsgId fullId); + explicit MessageStatistics( + not_null channel, + FullStoryId storyId); void request(Fn done); @@ -94,6 +97,7 @@ public: private: PublicForwards _publicForwards; const FullMsgId _fullId; + const FullStoryId _storyId; Data::PublicForwardsSlice _firstSlice; diff --git a/Telegram/SourceFiles/data/data_statistics.h b/Telegram/SourceFiles/data/data_statistics.h index 50b0694dd..7eb8f0a4d 100644 --- a/Telegram/SourceFiles/data/data_statistics.h +++ b/Telegram/SourceFiles/data/data_statistics.h @@ -118,11 +118,14 @@ struct MessageStatistics final { int views = 0; int reactions = 0; }; +// At the moment, the structures are identical. +using StoryStatistics = MessageStatistics; struct AnyStatistics final { Data::ChannelStatistics channel; Data::SupergroupStatistics supergroup; Data::MessageStatistics message; + Data::StoryStatistics story; }; struct PublicForwardsSlice final {