2023-04-26 20:05:12 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
|
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
|
|
*/
|
|
|
|
#include "api/api_statistics.h"
|
|
|
|
|
|
|
|
#include "apiwrap.h"
|
2023-04-26 20:58:03 +00:00
|
|
|
#include "data/data_channel.h"
|
2023-04-26 20:05:12 +00:00
|
|
|
#include "data/data_peer.h"
|
2023-10-09 02:09:23 +00:00
|
|
|
#include "data/data_session.h"
|
|
|
|
#include "history/history.h"
|
2023-04-26 20:05:12 +00:00
|
|
|
#include "main/main_session.h"
|
2023-05-02 14:33:17 +00:00
|
|
|
#include "statistics/statistics_data_deserialize.h"
|
2023-04-26 20:05:12 +00:00
|
|
|
|
|
|
|
namespace Api {
|
|
|
|
namespace {
|
2023-04-26 20:58:03 +00:00
|
|
|
|
2023-11-11 17:20:09 +00:00
|
|
|
constexpr auto kCheckRequestsTimer = 10 * crl::time(1000);
|
|
|
|
|
2023-05-02 14:33:17 +00:00
|
|
|
[[nodiscard]] Data::StatisticalGraph StatisticalGraphFromTL(
|
|
|
|
const MTPStatsGraph &tl) {
|
|
|
|
return tl.match([&](const MTPDstatsGraph &d) {
|
|
|
|
using namespace Statistic;
|
2023-07-27 04:03:53 +00:00
|
|
|
const auto zoomToken = d.vzoom_token().has_value()
|
|
|
|
? qs(*d.vzoom_token()).toUtf8()
|
|
|
|
: QByteArray();
|
2023-05-02 14:33:17 +00:00
|
|
|
return Data::StatisticalGraph{
|
|
|
|
StatisticalChartFromJSON(qs(d.vjson().data().vdata()).toUtf8()),
|
2023-07-27 04:03:53 +00:00
|
|
|
zoomToken,
|
2023-05-02 14:33:17 +00:00
|
|
|
};
|
|
|
|
}, [&](const MTPDstatsGraphAsync &data) {
|
2023-07-27 04:03:53 +00:00
|
|
|
return Data::StatisticalGraph{
|
|
|
|
.zoomToken = qs(data.vtoken()).toUtf8(),
|
|
|
|
};
|
2023-05-02 14:33:17 +00:00
|
|
|
}, [&](const MTPDstatsGraphError &data) {
|
2023-10-21 08:26:34 +00:00
|
|
|
return Data::StatisticalGraph{ .error = qs(data.verror()) };
|
2023-05-02 14:33:17 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-04-26 20:58:03 +00:00
|
|
|
[[nodiscard]] Data::StatisticalValue StatisticalValueFromTL(
|
|
|
|
const MTPStatsAbsValueAndPrev &tl) {
|
|
|
|
const auto current = tl.data().vcurrent().v;
|
|
|
|
const auto previous = tl.data().vprevious().v;
|
|
|
|
return Data::StatisticalValue{
|
|
|
|
.value = current,
|
|
|
|
.previousValue = previous,
|
|
|
|
.growthRatePercentage = previous
|
|
|
|
? std::abs((current - previous) / float64(previous) * 100.)
|
|
|
|
: 0,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] Data::ChannelStatistics ChannelStatisticsFromTL(
|
|
|
|
const MTPDstats_broadcastStats &data) {
|
|
|
|
const auto &tlUnmuted = data.venabled_notifications().data();
|
|
|
|
const auto unmuted = (!tlUnmuted.vtotal().v)
|
|
|
|
? 0.
|
|
|
|
: std::clamp(
|
|
|
|
tlUnmuted.vpart().v / tlUnmuted.vtotal().v * 100.,
|
|
|
|
0.,
|
|
|
|
100.);
|
|
|
|
using Recent = MTPMessageInteractionCounters;
|
|
|
|
auto recentMessages = ranges::views::all(
|
|
|
|
data.vrecent_message_interactions().v
|
|
|
|
) | ranges::views::transform([&](const Recent &tl) {
|
|
|
|
return Data::StatisticsMessageInteractionInfo{
|
|
|
|
.messageId = tl.data().vmsg_id().v,
|
|
|
|
.viewsCount = tl.data().vviews().v,
|
|
|
|
.forwardsCount = tl.data().vforwards().v,
|
|
|
|
};
|
|
|
|
}) | ranges::to_vector;
|
2023-05-02 14:33:17 +00:00
|
|
|
|
2023-04-26 20:58:03 +00:00
|
|
|
return {
|
|
|
|
.startDate = data.vperiod().data().vmin_date().v,
|
|
|
|
.endDate = data.vperiod().data().vmax_date().v,
|
|
|
|
|
|
|
|
.memberCount = StatisticalValueFromTL(data.vfollowers()),
|
|
|
|
.meanViewCount = StatisticalValueFromTL(data.vviews_per_post()),
|
|
|
|
.meanShareCount = StatisticalValueFromTL(data.vshares_per_post()),
|
|
|
|
|
|
|
|
.enabledNotificationsPercentage = unmuted,
|
|
|
|
|
2023-05-02 14:33:17 +00:00
|
|
|
.memberCountGraph = StatisticalGraphFromTL(
|
|
|
|
data.vgrowth_graph()),
|
|
|
|
|
|
|
|
.joinGraph = StatisticalGraphFromTL(
|
|
|
|
data.vfollowers_graph()),
|
|
|
|
|
|
|
|
.muteGraph = StatisticalGraphFromTL(
|
|
|
|
data.vmute_graph()),
|
|
|
|
|
|
|
|
.viewCountByHourGraph = StatisticalGraphFromTL(
|
|
|
|
data.vtop_hours_graph()),
|
|
|
|
|
|
|
|
.viewCountBySourceGraph = StatisticalGraphFromTL(
|
|
|
|
data.vviews_by_source_graph()),
|
|
|
|
|
|
|
|
.joinBySourceGraph = StatisticalGraphFromTL(
|
|
|
|
data.vnew_followers_by_source_graph()),
|
|
|
|
|
|
|
|
.languageGraph = StatisticalGraphFromTL(
|
|
|
|
data.vlanguages_graph()),
|
|
|
|
|
|
|
|
.messageInteractionGraph = StatisticalGraphFromTL(
|
|
|
|
data.vinteractions_graph()),
|
|
|
|
|
|
|
|
.instantViewInteractionGraph = StatisticalGraphFromTL(
|
|
|
|
data.viv_interactions_graph()),
|
|
|
|
|
2023-04-26 20:58:03 +00:00
|
|
|
.recentMessageInteractions = std::move(recentMessages),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-09-28 20:20:07 +00:00
|
|
|
[[nodiscard]] Data::SupergroupStatistics SupergroupStatisticsFromTL(
|
|
|
|
const MTPDstats_megagroupStats &data) {
|
|
|
|
using Senders = MTPStatsGroupTopPoster;
|
|
|
|
using Administrators = MTPStatsGroupTopAdmin;
|
|
|
|
using Inviters = MTPStatsGroupTopInviter;
|
|
|
|
|
|
|
|
auto topSenders = ranges::views::all(
|
|
|
|
data.vtop_posters().v
|
|
|
|
) | ranges::views::transform([&](const Senders &tl) {
|
|
|
|
return Data::StatisticsMessageSenderInfo{
|
|
|
|
.userId = UserId(tl.data().vuser_id().v),
|
|
|
|
.sentMessageCount = tl.data().vmessages().v,
|
|
|
|
.averageCharacterCount = tl.data().vavg_chars().v,
|
|
|
|
};
|
|
|
|
}) | ranges::to_vector;
|
|
|
|
auto topAdministrators = ranges::views::all(
|
|
|
|
data.vtop_admins().v
|
|
|
|
) | ranges::views::transform([&](const Administrators &tl) {
|
|
|
|
return Data::StatisticsAdministratorActionsInfo{
|
|
|
|
.userId = UserId(tl.data().vuser_id().v),
|
|
|
|
.deletedMessageCount = tl.data().vdeleted().v,
|
|
|
|
.bannedUserCount = tl.data().vkicked().v,
|
|
|
|
.restrictedUserCount = tl.data().vbanned().v,
|
|
|
|
};
|
|
|
|
}) | ranges::to_vector;
|
|
|
|
auto topInviters = ranges::views::all(
|
|
|
|
data.vtop_inviters().v
|
|
|
|
) | ranges::views::transform([&](const Inviters &tl) {
|
|
|
|
return Data::StatisticsInviterInfo{
|
|
|
|
.userId = UserId(tl.data().vuser_id().v),
|
|
|
|
.addedMemberCount = tl.data().vinvitations().v,
|
|
|
|
};
|
|
|
|
}) | ranges::to_vector;
|
|
|
|
|
|
|
|
return {
|
|
|
|
.startDate = data.vperiod().data().vmin_date().v,
|
|
|
|
.endDate = data.vperiod().data().vmax_date().v,
|
|
|
|
|
|
|
|
.memberCount = StatisticalValueFromTL(data.vmembers()),
|
|
|
|
.messageCount = StatisticalValueFromTL(data.vmessages()),
|
|
|
|
.viewerCount = StatisticalValueFromTL(data.vviewers()),
|
|
|
|
.senderCount = StatisticalValueFromTL(data.vposters()),
|
|
|
|
|
|
|
|
.memberCountGraph = StatisticalGraphFromTL(
|
|
|
|
data.vgrowth_graph()),
|
|
|
|
|
|
|
|
.joinGraph = StatisticalGraphFromTL(
|
|
|
|
data.vmembers_graph()),
|
|
|
|
|
|
|
|
.joinBySourceGraph = StatisticalGraphFromTL(
|
|
|
|
data.vnew_members_by_source_graph()),
|
|
|
|
|
|
|
|
.languageGraph = StatisticalGraphFromTL(
|
|
|
|
data.vlanguages_graph()),
|
|
|
|
|
|
|
|
.messageContentGraph = StatisticalGraphFromTL(
|
|
|
|
data.vmessages_graph()),
|
|
|
|
|
|
|
|
.actionGraph = StatisticalGraphFromTL(
|
|
|
|
data.vactions_graph()),
|
|
|
|
|
|
|
|
.dayGraph = StatisticalGraphFromTL(
|
|
|
|
data.vtop_hours_graph()),
|
|
|
|
|
|
|
|
.weekGraph = StatisticalGraphFromTL(
|
|
|
|
data.vweekdays_graph()),
|
|
|
|
|
|
|
|
.topSenders = std::move(topSenders),
|
|
|
|
.topAdministrators = std::move(topAdministrators),
|
|
|
|
.topInviters = std::move(topInviters),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-04-26 20:05:12 +00:00
|
|
|
} // namespace
|
|
|
|
|
2023-11-11 11:58:44 +00:00
|
|
|
Statistics::Statistics(not_null<ChannelData*> channel)
|
2023-11-11 17:20:09 +00:00
|
|
|
: StatisticsRequestSender(channel) {
|
|
|
|
}
|
|
|
|
|
|
|
|
StatisticsRequestSender::StatisticsRequestSender(not_null<ChannelData *> channel)
|
2023-11-11 11:58:44 +00:00
|
|
|
: _channel(channel)
|
2023-11-11 17:20:09 +00:00
|
|
|
, _api(&_channel->session().api().instance())
|
|
|
|
, _timer([=] { checkRequests(); }) {
|
|
|
|
}
|
|
|
|
|
|
|
|
StatisticsRequestSender::~StatisticsRequestSender() {
|
|
|
|
for (const auto &[dcId, ids] : _requests) {
|
|
|
|
for (const auto id : ids) {
|
|
|
|
_channel->session().api().unregisterStatsRequest(dcId, id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StatisticsRequestSender::checkRequests() {
|
|
|
|
for (auto i = begin(_requests); i != end(_requests);) {
|
|
|
|
for (auto j = begin(i->second); j != end(i->second);) {
|
|
|
|
if (_api.pending(*j)) {
|
|
|
|
++j;
|
|
|
|
} else {
|
|
|
|
_channel->session().api().unregisterStatsRequest(
|
|
|
|
i->first,
|
|
|
|
*j);
|
|
|
|
j = i->second.erase(j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i->second.empty()) {
|
|
|
|
i = _requests.erase(i);
|
|
|
|
} else {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (_requests.empty()) {
|
|
|
|
_timer.cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Request, typename, typename>
|
|
|
|
auto StatisticsRequestSender::makeRequest(Request &&request) {
|
|
|
|
const auto id = _api.allocateRequestId();
|
|
|
|
const auto dcId = _channel->owner().statsDcId(_channel);
|
|
|
|
if (dcId) {
|
|
|
|
_channel->session().api().registerStatsRequest(dcId, id);
|
|
|
|
_requests[dcId].emplace(id);
|
|
|
|
if (!_timer.isActive()) {
|
|
|
|
_timer.callEach(kCheckRequestsTimer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return std::move(_api.request(
|
|
|
|
std::forward<Request>(request)
|
|
|
|
).toDC(
|
|
|
|
dcId ? MTP::ShiftDcId(dcId, MTP::kStatsDcShift) : 0
|
|
|
|
).overrideId(id));
|
2023-04-26 20:05:12 +00:00
|
|
|
}
|
|
|
|
|
2023-11-11 11:58:44 +00:00
|
|
|
rpl::producer<rpl::no_value, QString> Statistics::request() {
|
2023-04-26 20:58:03 +00:00
|
|
|
return [=](auto consumer) {
|
|
|
|
auto lifetime = rpl::lifetime();
|
|
|
|
|
2023-11-11 17:20:09 +00:00
|
|
|
if (!channel()->isMegagroup()) {
|
|
|
|
makeRequest(MTPstats_GetBroadcastStats(
|
2023-04-26 20:58:03 +00:00
|
|
|
MTP_flags(MTPstats_GetBroadcastStats::Flags(0)),
|
2023-11-11 17:20:09 +00:00
|
|
|
channel()->inputChannel
|
2023-04-26 20:58:03 +00:00
|
|
|
)).done([=](const MTPstats_BroadcastStats &result) {
|
|
|
|
_channelStats = ChannelStatisticsFromTL(result.data());
|
|
|
|
consumer.put_done();
|
|
|
|
}).fail([=](const MTP::Error &error) {
|
|
|
|
consumer.put_error_copy(error.type());
|
2023-11-11 17:20:09 +00:00
|
|
|
}).send();
|
2023-09-28 20:20:07 +00:00
|
|
|
} else {
|
2023-11-11 17:20:09 +00:00
|
|
|
makeRequest(MTPstats_GetMegagroupStats(
|
2023-09-28 20:20:07 +00:00
|
|
|
MTP_flags(MTPstats_GetMegagroupStats::Flags(0)),
|
2023-11-11 17:20:09 +00:00
|
|
|
channel()->inputChannel
|
2023-09-28 20:20:07 +00:00
|
|
|
)).done([=](const MTPstats_MegagroupStats &result) {
|
2023-11-11 17:20:09 +00:00
|
|
|
const auto &data = result.data();
|
|
|
|
_supergroupStats = SupergroupStatisticsFromTL(data);
|
|
|
|
channel()->owner().processUsers(data.vusers());
|
2023-09-28 20:20:07 +00:00
|
|
|
consumer.put_done();
|
|
|
|
}).fail([=](const MTP::Error &error) {
|
|
|
|
consumer.put_error_copy(error.type());
|
2023-11-11 17:20:09 +00:00
|
|
|
}).send();
|
2023-04-26 20:58:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return lifetime;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-09-28 22:47:51 +00:00
|
|
|
Statistics::GraphResult Statistics::requestZoom(
|
2023-07-27 04:03:53 +00:00
|
|
|
const QString &token,
|
|
|
|
float64 x) {
|
|
|
|
return [=](auto consumer) {
|
|
|
|
auto lifetime = rpl::lifetime();
|
2023-10-11 04:36:05 +00:00
|
|
|
const auto wasEmpty = _zoomDeque.empty();
|
|
|
|
_zoomDeque.push_back([=] {
|
2023-11-11 17:20:09 +00:00
|
|
|
makeRequest(MTPstats_LoadAsyncGraph(
|
2023-10-11 04:36:05 +00:00
|
|
|
MTP_flags(x
|
|
|
|
? MTPstats_LoadAsyncGraph::Flag::f_x
|
|
|
|
: MTPstats_LoadAsyncGraph::Flag(0)),
|
|
|
|
MTP_string(token),
|
|
|
|
MTP_long(x)
|
|
|
|
)).done([=](const MTPStatsGraph &result) {
|
|
|
|
consumer.put_next(StatisticalGraphFromTL(result));
|
|
|
|
consumer.put_done();
|
|
|
|
if (!_zoomDeque.empty()) {
|
|
|
|
_zoomDeque.pop_front();
|
|
|
|
if (!_zoomDeque.empty()) {
|
|
|
|
_zoomDeque.front()();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).fail([=](const MTP::Error &error) {
|
|
|
|
consumer.put_error_copy(error.type());
|
2023-11-11 17:20:09 +00:00
|
|
|
}).send();
|
2023-10-11 04:36:05 +00:00
|
|
|
});
|
|
|
|
if (wasEmpty) {
|
|
|
|
_zoomDeque.front()();
|
|
|
|
}
|
2023-07-27 04:03:53 +00:00
|
|
|
|
|
|
|
return lifetime;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-04-26 20:58:03 +00:00
|
|
|
Data::ChannelStatistics Statistics::channelStats() const {
|
|
|
|
return _channelStats;
|
2023-04-26 20:05:12 +00:00
|
|
|
}
|
|
|
|
|
2023-09-28 20:20:07 +00:00
|
|
|
Data::SupergroupStatistics Statistics::supergroupStats() const {
|
|
|
|
return _supergroupStats;
|
|
|
|
}
|
|
|
|
|
2023-10-09 02:09:23 +00:00
|
|
|
PublicForwards::PublicForwards(
|
|
|
|
not_null<ChannelData*> channel,
|
|
|
|
FullMsgId fullId)
|
2023-11-11 17:20:09 +00:00
|
|
|
: StatisticsRequestSender(channel)
|
|
|
|
, _fullId(fullId) {
|
2023-10-09 02:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PublicForwards::request(
|
2023-10-11 02:10:09 +00:00
|
|
|
const Data::PublicForwardsSlice::OffsetToken &token,
|
|
|
|
Fn<void(Data::PublicForwardsSlice)> done) {
|
2023-10-09 02:09:23 +00:00
|
|
|
if (_requestId) {
|
|
|
|
return;
|
|
|
|
}
|
2023-11-11 17:20:09 +00:00
|
|
|
const auto offsetPeer = channel()->owner().peer(token.fullId.peer);
|
2023-10-09 02:09:23 +00:00
|
|
|
const auto tlOffsetPeer = offsetPeer
|
|
|
|
? offsetPeer->input
|
|
|
|
: MTP_inputPeerEmpty();
|
|
|
|
constexpr auto kLimit = tl::make_int(100);
|
2023-11-11 17:20:09 +00:00
|
|
|
_requestId = makeRequest(MTPstats_GetMessagePublicForwards(
|
|
|
|
channel()->inputChannel,
|
2023-10-09 02:09:23 +00:00
|
|
|
MTP_int(_fullId.msg),
|
|
|
|
MTP_int(token.rate),
|
|
|
|
tlOffsetPeer,
|
|
|
|
MTP_int(token.fullId.msg),
|
|
|
|
kLimit
|
2023-11-11 17:20:09 +00:00
|
|
|
)).done([=, channel = channel()](const MTPmessages_Messages &result) {
|
2023-10-09 02:09:23 +00:00
|
|
|
using Messages = QVector<FullMsgId>;
|
|
|
|
_requestId = 0;
|
|
|
|
|
2023-10-11 02:10:09 +00:00
|
|
|
auto nextToken = Data::PublicForwardsSlice::OffsetToken();
|
2023-10-09 02:09:23 +00:00
|
|
|
const auto process = [&](const MTPVector<MTPMessage> &messages) {
|
|
|
|
auto result = Messages();
|
|
|
|
for (const auto &message : messages.v) {
|
|
|
|
const auto msgId = IdFromMessage(message);
|
|
|
|
const auto peerId = PeerFromMessage(message);
|
|
|
|
const auto lastDate = DateFromMessage(message);
|
|
|
|
if (const auto peer = channel->owner().peerLoaded(peerId)) {
|
|
|
|
if (lastDate) {
|
|
|
|
channel->owner().addNewMessage(
|
|
|
|
message,
|
|
|
|
MessageFlags(),
|
|
|
|
NewMessageType::Existing);
|
|
|
|
nextToken.fullId = { peerId, msgId };
|
|
|
|
result.push_back(nextToken.fullId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto allLoaded = false;
|
|
|
|
auto fullCount = 0;
|
|
|
|
auto messages = result.match([&](const MTPDmessages_messages &data) {
|
|
|
|
channel->owner().processUsers(data.vusers());
|
|
|
|
channel->owner().processChats(data.vchats());
|
|
|
|
auto list = process(data.vmessages());
|
|
|
|
allLoaded = true;
|
|
|
|
fullCount = list.size();
|
|
|
|
return list;
|
|
|
|
}, [&](const MTPDmessages_messagesSlice &data) {
|
|
|
|
channel->owner().processUsers(data.vusers());
|
|
|
|
channel->owner().processChats(data.vchats());
|
|
|
|
auto list = process(data.vmessages());
|
|
|
|
|
|
|
|
if (const auto nextRate = data.vnext_rate()) {
|
|
|
|
const auto rateUpdated = (nextRate->v != token.rate);
|
|
|
|
if (rateUpdated) {
|
|
|
|
nextToken.rate = nextRate->v;
|
|
|
|
} else {
|
|
|
|
allLoaded = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fullCount = data.vcount().v;
|
|
|
|
return list;
|
|
|
|
}, [&](const MTPDmessages_channelMessages &data) {
|
|
|
|
channel->owner().processUsers(data.vusers());
|
|
|
|
channel->owner().processChats(data.vchats());
|
|
|
|
auto list = process(data.vmessages());
|
|
|
|
allLoaded = true;
|
|
|
|
fullCount = data.vcount().v;
|
|
|
|
return list;
|
|
|
|
}, [&](const MTPDmessages_messagesNotModified &) {
|
|
|
|
allLoaded = true;
|
|
|
|
return Messages();
|
|
|
|
});
|
|
|
|
|
|
|
|
_lastTotal = std::max(_lastTotal, fullCount);
|
|
|
|
done({
|
|
|
|
.list = std::move(messages),
|
|
|
|
.total = _lastTotal,
|
|
|
|
.allLoaded = allLoaded,
|
|
|
|
.token = nextToken,
|
|
|
|
});
|
|
|
|
}).fail([=] {
|
|
|
|
_requestId = 0;
|
2023-11-11 17:20:09 +00:00
|
|
|
}).send();
|
2023-10-09 02:09:23 +00:00
|
|
|
}
|
|
|
|
|
2023-10-09 14:10:43 +00:00
|
|
|
MessageStatistics::MessageStatistics(
|
|
|
|
not_null<ChannelData*> channel,
|
|
|
|
FullMsgId fullId)
|
2023-11-11 17:20:09 +00:00
|
|
|
: StatisticsRequestSender(channel)
|
|
|
|
, _publicForwards(channel, fullId)
|
|
|
|
, _fullId(fullId) {
|
2023-10-09 14:10:43 +00:00
|
|
|
}
|
|
|
|
|
2023-10-11 02:10:09 +00:00
|
|
|
Data::PublicForwardsSlice MessageStatistics::firstSlice() const {
|
2023-10-09 14:10:43 +00:00
|
|
|
return _firstSlice;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MessageStatistics::request(Fn<void(Data::MessageStatistics)> done) {
|
2023-11-11 17:20:09 +00:00
|
|
|
if (channel()->isMegagroup()) {
|
2023-10-09 14:10:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto requestFirstPublicForwards = [=](
|
|
|
|
const Data::StatisticalGraph &messageGraph,
|
|
|
|
const Data::StatisticsMessageInteractionInfo &info) {
|
2023-10-11 02:10:09 +00:00
|
|
|
_publicForwards.request({}, [=](Data::PublicForwardsSlice slice) {
|
2023-10-09 14:10:43 +00:00
|
|
|
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) {
|
2023-11-11 17:20:09 +00:00
|
|
|
api().request(MTPchannels_GetMessages(
|
|
|
|
channel()->inputChannel,
|
2023-10-22 18:33:58 +00:00
|
|
|
MTP_vector<MTPInputMessage>(
|
|
|
|
1,
|
|
|
|
MTP_inputMessageID(MTP_int(_fullId.msg))))
|
|
|
|
).done([=](const MTPmessages_Messages &result) {
|
|
|
|
const auto process = [&](const MTPVector<MTPMessage> &messages) {
|
|
|
|
const auto &message = messages.v.front();
|
|
|
|
return message.match([&](const MTPDmessage &data) {
|
|
|
|
return Data::StatisticsMessageInteractionInfo{
|
|
|
|
.messageId = IdFromMessage(message),
|
|
|
|
.viewsCount = data.vviews()
|
|
|
|
? data.vviews()->v
|
|
|
|
: 0,
|
|
|
|
.forwardsCount = data.vforwards()
|
|
|
|
? data.vforwards()->v
|
|
|
|
: 0,
|
|
|
|
};
|
|
|
|
}, [](const MTPDmessageEmpty &) {
|
|
|
|
return Data::StatisticsMessageInteractionInfo();
|
|
|
|
}, [](const MTPDmessageService &) {
|
|
|
|
return Data::StatisticsMessageInteractionInfo();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
auto info = result.match([&](const MTPDmessages_messages &data) {
|
|
|
|
return process(data.vmessages());
|
|
|
|
}, [&](const MTPDmessages_messagesSlice &data) {
|
|
|
|
return process(data.vmessages());
|
|
|
|
}, [&](const MTPDmessages_channelMessages &data) {
|
|
|
|
return process(data.vmessages());
|
|
|
|
}, [](const MTPDmessages_messagesNotModified &) {
|
|
|
|
return Data::StatisticsMessageInteractionInfo();
|
|
|
|
});
|
|
|
|
|
|
|
|
requestFirstPublicForwards(messageGraph, std::move(info));
|
2023-10-09 14:10:43 +00:00
|
|
|
}).fail([=](const MTP::Error &error) {
|
|
|
|
requestFirstPublicForwards(messageGraph, {});
|
|
|
|
}).send();
|
|
|
|
};
|
|
|
|
|
2023-11-11 17:20:09 +00:00
|
|
|
makeRequest(MTPstats_GetMessageStats(
|
2023-10-09 14:10:43 +00:00
|
|
|
MTP_flags(MTPstats_GetMessageStats::Flags(0)),
|
2023-11-11 17:20:09 +00:00
|
|
|
channel()->inputChannel,
|
2023-10-09 14:10:43 +00:00
|
|
|
MTP_int(_fullId.msg.bare)
|
|
|
|
)).done([=](const MTPstats_MessageStats &result) {
|
|
|
|
requestPrivateForwards(
|
|
|
|
StatisticalGraphFromTL(result.data().vviews_graph()));
|
|
|
|
}).fail([=](const MTP::Error &error) {
|
|
|
|
requestPrivateForwards({});
|
2023-11-11 17:20:09 +00:00
|
|
|
}).send();
|
2023-10-09 14:10:43 +00:00
|
|
|
}
|
|
|
|
|
2023-10-11 21:09:43 +00:00
|
|
|
Boosts::Boosts(not_null<PeerData*> peer)
|
|
|
|
: _peer(peer)
|
|
|
|
, _api(&peer->session().api().instance()) {
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<rpl::no_value, QString> Boosts::request() {
|
|
|
|
return [=](auto consumer) {
|
|
|
|
auto lifetime = rpl::lifetime();
|
|
|
|
const auto channel = _peer->asChannel();
|
|
|
|
if (!channel || channel->isMegagroup()) {
|
|
|
|
return lifetime;
|
|
|
|
}
|
|
|
|
|
2023-10-23 05:00:09 +00:00
|
|
|
_api.request(MTPpremium_GetBoostsStatus(
|
2023-10-11 21:09:43 +00:00
|
|
|
_peer->input
|
2023-10-23 05:00:09 +00:00
|
|
|
)).done([=](const MTPpremium_BoostsStatus &result) {
|
2023-10-11 21:09:43 +00:00
|
|
|
const auto &data = result.data();
|
|
|
|
const auto hasPremium = !!data.vpremium_audience();
|
|
|
|
const auto premiumMemberCount = hasPremium
|
|
|
|
? std::max(0, int(data.vpremium_audience()->data().vpart().v))
|
|
|
|
: 0;
|
|
|
|
const auto participantCount = hasPremium
|
|
|
|
? std::max(
|
|
|
|
int(data.vpremium_audience()->data().vtotal().v),
|
|
|
|
premiumMemberCount)
|
|
|
|
: 0;
|
|
|
|
const auto premiumMemberPercentage = (participantCount > 0)
|
|
|
|
? (100. * premiumMemberCount / participantCount)
|
|
|
|
: 0;
|
|
|
|
|
2023-11-09 19:03:17 +00:00
|
|
|
const auto slots = data.vmy_boost_slots();
|
2023-10-11 21:09:43 +00:00
|
|
|
_boostStatus.overview = Data::BoostsOverview{
|
2023-11-09 19:03:17 +00:00
|
|
|
.mine = slots ? int(slots->v.size()) : 0,
|
2023-10-11 21:09:43 +00:00
|
|
|
.level = std::max(data.vlevel().v, 0),
|
|
|
|
.boostCount = std::max(
|
|
|
|
data.vboosts().v,
|
|
|
|
data.vcurrent_level_boosts().v),
|
|
|
|
.currentLevelBoostCount = data.vcurrent_level_boosts().v,
|
|
|
|
.nextLevelBoostCount = data.vnext_level_boosts()
|
|
|
|
? data.vnext_level_boosts()->v
|
|
|
|
: 0,
|
|
|
|
.premiumMemberCount = premiumMemberCount,
|
|
|
|
.premiumMemberPercentage = premiumMemberPercentage,
|
|
|
|
};
|
|
|
|
_boostStatus.link = qs(data.vboost_url());
|
|
|
|
|
2023-11-08 11:54:45 +00:00
|
|
|
if (data.vprepaid_giveaways()) {
|
|
|
|
_boostStatus.prepaidGiveaway = ranges::views::all(
|
|
|
|
data.vprepaid_giveaways()->v
|
|
|
|
) | ranges::views::transform([](const MTPPrepaidGiveaway &r) {
|
|
|
|
return Data::BoostPrepaidGiveaway{
|
|
|
|
.months = r.data().vmonths().v,
|
|
|
|
.id = r.data().vid().v,
|
|
|
|
.quantity = r.data().vquantity().v,
|
|
|
|
.date = QDateTime::fromSecsSinceEpoch(
|
|
|
|
r.data().vdate().v),
|
|
|
|
};
|
|
|
|
}) | ranges::to_vector;
|
|
|
|
}
|
|
|
|
|
2023-11-02 12:28:17 +00:00
|
|
|
using namespace Data;
|
|
|
|
requestBoosts({ .gifts = false }, [=](BoostsListSlice &&slice) {
|
|
|
|
_boostStatus.firstSliceBoosts = std::move(slice);
|
|
|
|
requestBoosts({ .gifts = true }, [=](BoostsListSlice &&s) {
|
|
|
|
_boostStatus.firstSliceGifts = std::move(s);
|
|
|
|
consumer.put_done();
|
|
|
|
});
|
2023-10-11 21:09:43 +00:00
|
|
|
});
|
|
|
|
}).fail([=](const MTP::Error &error) {
|
|
|
|
consumer.put_error_copy(error.type());
|
|
|
|
}).send();
|
|
|
|
|
|
|
|
return lifetime;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void Boosts::requestBoosts(
|
|
|
|
const Data::BoostsListSlice::OffsetToken &token,
|
|
|
|
Fn<void(Data::BoostsListSlice)> done) {
|
|
|
|
if (_requestId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
constexpr auto kTlFirstSlice = tl::make_int(kFirstSlice);
|
|
|
|
constexpr auto kTlLimit = tl::make_int(kLimit);
|
2023-11-02 12:28:17 +00:00
|
|
|
const auto gifts = token.gifts;
|
2023-10-23 05:00:09 +00:00
|
|
|
_requestId = _api.request(MTPpremium_GetBoostsList(
|
2023-11-02 12:28:17 +00:00
|
|
|
gifts
|
|
|
|
? MTP_flags(MTPpremium_GetBoostsList::Flag::f_gifts)
|
|
|
|
: MTP_flags(0),
|
2023-10-11 21:09:43 +00:00
|
|
|
_peer->input,
|
|
|
|
MTP_string(token.next),
|
|
|
|
token.next.isEmpty() ? kTlFirstSlice : kTlLimit
|
2023-10-23 05:00:09 +00:00
|
|
|
)).done([=](const MTPpremium_BoostsList &result) {
|
2023-10-11 21:09:43 +00:00
|
|
|
_requestId = 0;
|
|
|
|
|
|
|
|
const auto &data = result.data();
|
|
|
|
_peer->owner().processUsers(data.vusers());
|
|
|
|
|
|
|
|
auto list = std::vector<Data::Boost>();
|
2023-10-23 05:00:09 +00:00
|
|
|
list.reserve(data.vboosts().v.size());
|
2023-11-08 20:54:52 +00:00
|
|
|
constexpr auto kMonthsDivider = int(30 * 86400);
|
2023-10-23 05:00:09 +00:00
|
|
|
for (const auto &boost : data.vboosts().v) {
|
2023-10-24 10:07:06 +00:00
|
|
|
const auto &data = boost.data();
|
|
|
|
const auto path = data.vused_gift_slug()
|
|
|
|
? (u"giftcode/"_q + qs(data.vused_gift_slug()->v))
|
|
|
|
: QString();
|
|
|
|
auto giftCodeLink = !path.isEmpty()
|
|
|
|
? Data::GiftCodeLink{
|
|
|
|
_peer->session().createInternalLink(path),
|
|
|
|
_peer->session().createInternalLinkFull(path),
|
|
|
|
qs(data.vused_gift_slug()->v),
|
|
|
|
}
|
|
|
|
: Data::GiftCodeLink();
|
2023-10-11 21:09:43 +00:00
|
|
|
list.push_back({
|
2023-10-24 10:07:06 +00:00
|
|
|
data.is_gift(),
|
|
|
|
data.is_giveaway(),
|
|
|
|
data.is_unclaimed(),
|
|
|
|
qs(data.vid()),
|
|
|
|
data.vuser_id().value_or_empty(),
|
|
|
|
data.vgiveaway_msg_id()
|
|
|
|
? FullMsgId{ _peer->id, data.vgiveaway_msg_id()->v }
|
|
|
|
: FullMsgId(),
|
|
|
|
QDateTime::fromSecsSinceEpoch(data.vdate().v),
|
2023-11-08 20:54:52 +00:00
|
|
|
QDateTime::fromSecsSinceEpoch(data.vexpires().v),
|
|
|
|
(data.vexpires().v - data.vdate().v) / kMonthsDivider,
|
2023-10-24 10:07:06 +00:00
|
|
|
std::move(giftCodeLink),
|
|
|
|
data.vmultiplier().value_or_empty(),
|
2023-10-11 21:09:43 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
done(Data::BoostsListSlice{
|
|
|
|
.list = std::move(list),
|
2023-11-02 12:28:17 +00:00
|
|
|
.multipliedTotal = data.vcount().v,
|
2023-10-23 05:00:09 +00:00
|
|
|
.allLoaded = (data.vcount().v == data.vboosts().v.size()),
|
2023-10-11 21:09:43 +00:00
|
|
|
.token = Data::BoostsListSlice::OffsetToken{
|
2023-11-02 12:28:17 +00:00
|
|
|
.next = data.vnext_offset()
|
2023-10-11 21:09:43 +00:00
|
|
|
? qs(*data.vnext_offset())
|
2023-11-02 12:28:17 +00:00
|
|
|
: QString(),
|
|
|
|
.gifts = gifts,
|
2023-10-11 21:09:43 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}).fail([=] {
|
|
|
|
_requestId = 0;
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
|
|
|
Data::BoostStatus Boosts::boostStatus() const {
|
|
|
|
return _boostStatus;
|
|
|
|
}
|
|
|
|
|
2023-04-26 20:05:12 +00:00
|
|
|
} // namespace Api
|