Skip to content

Commit

Permalink
f Migrated API for channel statistics from MTP to TDLib.
Browse files Browse the repository at this point in the history
  • Loading branch information
john-preston committed Sep 11, 2024
1 parent e9e2361 commit 796caaa
Showing 1 changed file with 75 additions and 73 deletions.
148 changes: 75 additions & 73 deletions Telegram/SourceFiles/api/api_statistics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ For license and copyright information please follow this link:
namespace Api {
namespace {

using namespace Tdb;

#if 0 // mtp
[[nodiscard]] Data::StatisticalValue StatisticalValueFromTL(
const MTPStatsAbsValueAndPrev &tl) {
Expand Down Expand Up @@ -201,7 +203,7 @@ namespace {
#endif

[[nodiscard]] Data::StatisticalValue StatisticalValueFromTL(
const Tdb::TLstatisticalValue &tl) {
const TLstatisticalValue &tl) {
return Data::StatisticalValue{
.value = tl.data().vvalue().v,
.previousValue = tl.data().vprevious_value().v,
Expand All @@ -210,9 +212,9 @@ namespace {
}

[[nodiscard]] Data::ChannelStatistics ChannelStatisticsFromTL(
const Tdb::TLDchatStatisticsChannel &data) {
const TLDchatStatisticsChannel &data) {
const auto unmuted = data.venabled_notifications_percentage().v;
using Recent = Tdb::TLchatStatisticsInteractionInfo;
using Recent = TLchatStatisticsInteractionInfo;
auto recentMessages = ranges::views::all(
data.vrecent_interactions().v
) | ranges::views::transform([&](const Recent &interaction) {
Expand All @@ -222,9 +224,9 @@ namespace {
.forwardsCount = data.vforward_count().v,
};
data.vobject_type().match([&](
const Tdb::TLDchatStatisticsObjectTypeMessage &data) {
const TLDchatStatisticsObjectTypeMessage &data) {
result.messageId = data.vmessage_id().v;
}, [&](const Tdb::TLDchatStatisticsObjectTypeStory &data) {
}, [&](const TLDchatStatisticsObjectTypeStory &data) {
result.storyId = data.vstory_id().v;
});
return result;
Expand Down Expand Up @@ -284,10 +286,10 @@ namespace {
}

[[nodiscard]] Data::SupergroupStatistics SupergroupStatisticsFromTL(
const Tdb::TLDchatStatisticsSupergroup &data) {
using Senders = Tdb::TLchatStatisticsMessageSenderInfo;
using Administrators = Tdb::TLchatStatisticsAdministratorActionsInfo;
using Inviters = Tdb::TLchatStatisticsInviterInfo;
const TLDchatStatisticsSupergroup &data) {
using Senders = TLchatStatisticsMessageSenderInfo;
using Administrators = TLchatStatisticsAdministratorActionsInfo;
using Inviters = TLchatStatisticsInviterInfo;

auto topSenders = ranges::views::all(
data.vtop_senders().v
Expand Down Expand Up @@ -391,17 +393,17 @@ rpl::producer<rpl::no_value, QString> Statistics::request() {
}).send();
}
#endif
api().request(Tdb::TLgetChatStatistics(
api().request(TLgetChatStatistics(
peerToTdbChat(channel()->id),
Tdb::tl_bool(false)
)).done([=](const Tdb::TLchatStatistics &result) {
result.match([&](const Tdb::TLDchatStatisticsChannel &data) {
tl_bool(false)
)).done([=](const TLchatStatistics &result) {
result.match([&](const TLDchatStatisticsChannel &data) {
_channelStats = ChannelStatisticsFromTL(data);
}, [&](const Tdb::TLDchatStatisticsSupergroup &data) {
}, [&](const TLDchatStatisticsSupergroup &data) {
_supergroupStats = SupergroupStatisticsFromTL(data);
});
consumer.put_done();
}).fail([=](const Tdb::Error &error) {
}).fail([=](const Error &error) {
consumer.put_error_copy(error.message);
}).send();

Expand Down Expand Up @@ -436,11 +438,11 @@ Statistics::GraphResult Statistics::requestZoom(
consumer.put_error_copy(error.type());
}).send();
#endif
api().request(Tdb::TLgetStatisticalGraph(
api().request(TLgetStatisticalGraph(
peerToTdbChat(channel()->id),
Tdb::tl_string(token),
Tdb::tl_int53(x)
)).done([=](const Tdb::TLstatisticalGraph &result) {
tl_string(token),
tl_int53(x)
)).done([=](const TLstatisticalGraph &result) {
consumer.put_next(StatisticalGraphFromTL(result));
consumer.put_done();
if (!_zoomDeque.empty()) {
Expand All @@ -449,7 +451,7 @@ Statistics::GraphResult Statistics::requestZoom(
_zoomDeque.front()();
}
}
}).fail([=](const Tdb::Error &error) {
}).fail([=](const Error &error) {
consumer.put_error_copy(error.message);
}).send();
});
Expand Down Expand Up @@ -486,12 +488,12 @@ void PublicForwards::request(

// todo stories
constexpr auto kLimit = tl::make_int(100);
_requestId = api().request(Tdb::TLgetMessagePublicForwards(
_requestId = api().request(TLgetMessagePublicForwards(
peerToTdbChat(channel->id),
Tdb::tl_int53(_fullId.messageId.msg.bare),
Tdb::tl_string(token),
tl_int53(_fullId.messageId.msg.bare),
tl_string(token),
kLimit
)).done([=](const Tdb::TLpublicForwards &result) {
)).done([=](const TLpublicForwards &result) {
using Messages = QVector<Data::RecentPostId>;
_requestId = 0;

Expand All @@ -500,13 +502,13 @@ void PublicForwards::request(
auto recentList = Messages();
recentList.reserve(data.vforwards().v.size());
for (const auto &forward : result.data().vforwards().v) {
forward.match([&](const Tdb::TLDpublicForwardMessage &data) {
forward.match([&](const TLDpublicForwardMessage &data) {
recentList.push_back({
.messageId = channel->owner().processMessage(
data.vmessage(),
NewMessageType::Existing)->fullId(),
});
}, [&](const Tdb::TLDpublicForwardStory &data) {
}, [&](const TLDpublicForwardStory &data) {
const auto story = channel->owner().stories().applySingle(
data.vstory());
if (story) {
Expand Down Expand Up @@ -704,10 +706,10 @@ void MessageStatistics::request(Fn<void(Data::MessageStatistics)> done) {
requestFirstPublicForwards(messageGraph, reactionsGraph, {});
}).send();
#endif
api().request(Tdb::TLgetMessage(
api().request(TLgetMessage(
peerToTdbChat(channel()->id),
Tdb::tl_int53(_fullId.msg.bare)
)).done([=](const Tdb::TLDmessage &data) {
tl_int53(_fullId.msg.bare)
)).done([=](const TLDmessage &data) {
auto info = Data::StatisticsMessageInteractionInfo{
.messageId = data.vid().v,
.viewsCount = data.vinteraction_info()
Expand All @@ -722,19 +724,19 @@ void MessageStatistics::request(Fn<void(Data::MessageStatistics)> done) {
messageGraph,
reactionsGraph,
std::move(info));
}).fail([=](const Tdb::Error &error) {
}).fail([=](const Error &error) {
requestFirstPublicForwards(messageGraph, reactionsGraph, {});
}).send();
};

const auto requestStoryPrivateForwards = [=](
const Data::StatisticalGraph &messageGraph,
const Data::StatisticalGraph &reactionsGraph) {
api().request(Tdb::TLgetStory(
api().request(TLgetStory(
peerToTdbChat(channel()->id),
Tdb::tl_int32(_storyId.story),
Tdb::tl_bool(false)
)).done([=](const Tdb::TLstory &result) {
tl_int32(_storyId.story),
tl_bool(false)
)).done([=](const TLstory &result) {
auto info = Data::StatisticsMessageInteractionInfo();
const auto &data = result.data();
if (const auto interaction = data.vinteraction_info()) {
Expand All @@ -750,32 +752,32 @@ void MessageStatistics::request(Fn<void(Data::MessageStatistics)> done) {
messageGraph,
reactionsGraph,
std::move(info));
}).fail([=](const Tdb::Error &error) {
}).fail([=](const Error &error) {
requestFirstPublicForwards(messageGraph, reactionsGraph, {});
}).send();
};
if (_storyId) {
api().request(Tdb::TLgetStoryStatistics(
api().request(TLgetStoryStatistics(
peerToTdbChat(channel()->id),
Tdb::tl_int32(_storyId.story),
Tdb::tl_bool(false)
)).done([=](const Tdb::TLDstoryStatistics &data) {
tl_int32(_storyId.story),
tl_bool(false)
)).done([=](const TLDstoryStatistics &data) {
requestStoryPrivateForwards(
StatisticalGraphFromTL(data.vstory_interaction_graph()),
StatisticalGraphFromTL(data.vstory_reaction_graph()));
}).fail([=](const Tdb::Error &error) {
}).fail([=](const Error &error) {
requestStoryPrivateForwards({}, {});
}).send();
} else {
api().request(Tdb::TLgetMessageStatistics(
api().request(TLgetMessageStatistics(
peerToTdbChat(channel()->id),
Tdb::tl_int53(_fullId.msg.bare),
Tdb::tl_bool(false)
)).done([=](const Tdb::TLDmessageStatistics &data) {
tl_int53(_fullId.msg.bare),
tl_bool(false)
)).done([=](const TLDmessageStatistics &data) {
requestPrivateForwards(
StatisticalGraphFromTL(data.vmessage_interaction_graph()),
StatisticalGraphFromTL(data.vmessage_reaction_graph()));
}).fail([=](const Tdb::Error &error) {
}).fail([=](const Error &error) {
requestPrivateForwards({}, {});
}).send();
}
Expand Down Expand Up @@ -917,9 +919,9 @@ rpl::producer<rpl::no_value, QString> Boosts::request() {
consumer.put_error_copy(error.type());
}).send();
#endif
_api.request(Tdb::TLgetChatBoostStatus(
_api.request(TLgetChatBoostStatus(
peerToTdbChat(_peer->id)
)).done([=](const Tdb::TLDchatBoostStatus &data) {
)).done([=](const TLDchatBoostStatus &data) {
_boostStatus.overview = Data::BoostsOverview{
.mine = bool(data.vapplied_slot_ids().v.size()),
.level = std::max(data.vlevel().v, 0),
Expand All @@ -937,7 +939,7 @@ rpl::producer<rpl::no_value, QString> Boosts::request() {
_boostStatus.prepaidGiveaway = ranges::views::all(
data.vprepaid_giveaways().v
) | ranges::views::transform([](
const Tdb::TLprepaidPremiumGiveaway &r) {
const TLprepaidPremiumGiveaway &r) {
return Data::BoostPrepaidGiveaway{
.months = r.data().vmonth_count().v,
.id = uint64(r.data().vid().v),
Expand All @@ -955,7 +957,7 @@ rpl::producer<rpl::no_value, QString> Boosts::request() {
consumer.put_done();
});
});
}).fail([=](const Tdb::Error &error) {
}).fail([=](const Error &error) {
consumer.put_error_copy(error.message);
}).send();

Expand All @@ -972,12 +974,12 @@ void Boosts::requestBoosts(
constexpr auto kTlFirstSlice = tl::make_int(kFirstSlice);
constexpr auto kTlLimit = tl::make_int(kLimit);
const auto gifts = token.gifts;
_requestId = _api.request(Tdb::TLgetChatBoosts(
_requestId = _api.request(TLgetChatBoosts(
peerToTdbChat(_peer->id),
Tdb::tl_bool(gifts),
Tdb::tl_string(token.next),
tl_bool(gifts),
tl_string(token.next),
token.next.isEmpty() ? kTlFirstSlice : kTlLimit
)).done([=](const Tdb::TLDfoundChatBoosts &data) {
)).done([=](const TLDfoundChatBoosts &data) {
_requestId = 0;

auto list = std::vector<Data::Boost>();
Expand All @@ -990,7 +992,7 @@ void Boosts::requestBoosts(
return UserId(d.vuser_id().v);
});
const auto giftCodeLink = data.vsource().match([](
const Tdb::TLDchatBoostSourcePremium &) {
const TLDchatBoostSourcePremium &) {
return Data::GiftCodeLink();
}, [&](const auto &d) {
const auto tlGiftCode = d.vgift_code().v.toUtf8();
Expand All @@ -1004,13 +1006,13 @@ void Boosts::requestBoosts(
};
});
const auto isUnclaimed = data.vsource().match([](
const Tdb::TLDchatBoostSourceGiveaway &d) {
const TLDchatBoostSourceGiveaway &d) {
return d.vis_unclaimed().v;
}, [](const auto &) {
return false;
});
const auto giveawayMessage = data.vsource().match([&](
const Tdb::TLDchatBoostSourceGiveaway &d) {
const TLDchatBoostSourceGiveaway &d) {
return FullMsgId{ _peer->id, d.vgiveaway_message_id().v };
}, [](const auto &) {
return FullMsgId();
Expand Down Expand Up @@ -1116,10 +1118,10 @@ rpl::producer<rpl::no_value, QString> ChannelEarnStatistics::request() {
return [=](auto consumer) {
auto lifetime = rpl::lifetime();

api().request(Tdb::TLgetChatRevenueStatistics(
api().request(TLgetChatRevenueStatistics(
peerToTdbChat(channel()->id),
Tdb::tl_bool(false)
)).done([=](const Tdb::TLchatRevenueStatistics &result) {
tl_bool(false)
)).done([=](const TLchatRevenueStatistics &result) {
const auto &data = result.data();
const auto &amount = data.vrevenue_amount().data();
_data = Data::EarnStatistics{
Expand All @@ -1135,17 +1137,17 @@ rpl::producer<rpl::no_value, QString> ChannelEarnStatistics::request() {
requestHistory({}, [=](Data::EarnHistorySlice &&slice) {
_data.firstHistorySlice = std::move(slice);

api().request(Tdb::TLgetSupergroupFullInfo(
Tdb::tl_int53(peerToChannel(channel()->id).bare)
)).done([=](const Tdb::TLsupergroupFullInfo &result) {
api().request(TLgetSupergroupFullInfo(
tl_int53(peerToChannel(channel()->id).bare)
)).done([=](const TLsupergroupFullInfo &result) {
_data.switchedOff
= !result.data().vcan_have_sponsored_messages().v;
consumer.put_done();
}).fail([=](const Tdb::Error &error) {
}).fail([=](const Error &error) {
consumer.put_error_copy(error.message);
}).send();
});
}).fail([=](const Tdb::Error &error) {
}).fail([=](const Error &error) {
consumer.put_error_copy(error.message);
}).send();
#if 0 // mtp
Expand Down Expand Up @@ -1256,11 +1258,11 @@ void ChannelEarnStatistics::requestHistory(
.token = Data::EarnHistorySlice::OffsetToken(nextToken),
});
#endif
_requestId = api().request(Tdb::TLgetChatRevenueTransactions(
_requestId = api().request(TLgetChatRevenueTransactions(
peerToTdbChat(channel()->id),
Tdb::tl_int32(token),
tl_int32(token),
(!token) ? kTlFirstSlice : kTlLimit
)).done([=](const Tdb::TLchatRevenueTransactions &result) {
)).done([=](const TLchatRevenueTransactions &result) {
_requestId = 0;

const auto &data = result.data();
Expand All @@ -1275,27 +1277,27 @@ void ChannelEarnStatistics::requestHistory(
});
auto &entry = list.back();
d.vtype().match([&](
const Tdb::TLDchatRevenueTransactionTypeEarnings &d) {
const TLDchatRevenueTransactionTypeEarnings &d) {
entry.type = Data::EarnHistoryEntry::Type::In;
entry.date = base::unixtime::parse(d.vstart_date().v);
entry.dateTo = base::unixtime::parse(d.vend_date().v);
}, [&](const Tdb::TLDchatRevenueTransactionTypeWithdrawal &d) {
}, [&](const TLDchatRevenueTransactionTypeWithdrawal &d) {
entry.type = Data::EarnHistoryEntry::Type::Out;
entry.amount = (std::numeric_limits<Data::EarnInt>::max()
- entry.amount
+ 1);
entry.date = base::unixtime::parse(d.vwithdrawal_date().v);
// entry.provider = d.vprovider().v;
d.vstate().match([&](const Tdb::TLDrevenueWithdrawalStateFailed &d) {
d.vstate().match([&](const TLDrevenueWithdrawalStateFailed &d) {
entry.status = Data::EarnHistoryEntry::Status::Failed;
}, [&](const Tdb::TLDrevenueWithdrawalStatePending &d) {
}, [&](const TLDrevenueWithdrawalStatePending &d) {
entry.status = Data::EarnHistoryEntry::Status::Pending;
}, [&](const Tdb::TLDrevenueWithdrawalStateSucceeded &d) {
}, [&](const TLDrevenueWithdrawalStateSucceeded &d) {
entry.successDate = base::unixtime::parse(d.vdate().v);
entry.successLink = d.vurl().v;
entry.status = Data::EarnHistoryEntry::Status::Success;
});
}, [&](const Tdb::TLDchatRevenueTransactionTypeRefund &d) {
}, [&](const TLDchatRevenueTransactionTypeRefund &d) {
entry.type = Data::EarnHistoryEntry::Type::Return;
entry.date = base::unixtime::parse(d.vrefund_date().v);
// entry.provider = d.vprovider().v;
Expand Down

0 comments on commit 796caaa

Please sign in to comment.