Added API class for requesting full statistic of single message.

This commit is contained in:
23rd 2023-10-09 17:10:43 +03:00 committed by John Preston
parent 9c1ef76e49
commit 3fa168cee0
3 changed files with 98 additions and 0 deletions

View file

@ -388,4 +388,70 @@ void PublicForwards::request(
}).send();
}
MessageStatistics::MessageStatistics(
not_null<ChannelData*> 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<void(Data::MessageStatistics)> 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

View file

@ -69,4 +69,26 @@ private:
};
class MessageStatistics final {
public:
explicit MessageStatistics(
not_null<ChannelData*> channel,
FullMsgId fullId);
void request(Fn<void(Data::MessageStatistics)> done);
[[nodiscard]] PublicForwards::Slice firstSlice() const;
private:
PublicForwards _publicForwards;
const not_null<ChannelData*> _channel;
const FullMsgId _fullId;
PublicForwards::Slice _firstSlice;
mtpRequestId _requestId = 0;
MTP::Sender _api;
};
} // namespace Api

View file

@ -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