mirror of
https://github.com/AyuGram/AyuGramDesktop.git
synced 2025-06-05 06:33:57 +02:00
Added api support of credits status and credits history.
This commit is contained in:
parent
f0a82de784
commit
2bf8cb84d0
3 changed files with 154 additions and 0 deletions
|
@ -8,10 +8,54 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||||
#include "api/api_credits.h"
|
#include "api/api_credits.h"
|
||||||
|
|
||||||
#include "apiwrap.h"
|
#include "apiwrap.h"
|
||||||
|
#include "base/unixtime.h"
|
||||||
#include "data/data_peer.h"
|
#include "data/data_peer.h"
|
||||||
|
#include "data/data_session.h"
|
||||||
#include "main/main_session.h"
|
#include "main/main_session.h"
|
||||||
|
|
||||||
namespace Api {
|
namespace Api {
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
[[nodiscard]] Data::CreditsHistoryEntry HistoryFromTL(
|
||||||
|
const MTPStarsTransaction &tl) {
|
||||||
|
using HistoryPeerTL = MTPDstarsTransactionPeer;
|
||||||
|
return Data::CreditsHistoryEntry{
|
||||||
|
.id = qs(tl.data().vid()),
|
||||||
|
.credits = tl.data().vstars().v,
|
||||||
|
.date = base::unixtime::parse(tl.data().vdate().v),
|
||||||
|
.peerType = tl.data().vpeer().match([](const HistoryPeerTL &) {
|
||||||
|
return Data::CreditsHistoryEntry::PeerType::Peer;
|
||||||
|
}, [](const MTPDstarsTransactionPeerPlayMarket &) {
|
||||||
|
return Data::CreditsHistoryEntry::PeerType::PlayMarket;
|
||||||
|
}, [](const MTPDstarsTransactionPeerFragment &) {
|
||||||
|
return Data::CreditsHistoryEntry::PeerType::Fragment;
|
||||||
|
}, [](const MTPDstarsTransactionPeerAppStore &) {
|
||||||
|
return Data::CreditsHistoryEntry::PeerType::AppStore;
|
||||||
|
}),
|
||||||
|
.peerId = tl.data().vpeer().match([](const HistoryPeerTL &p) {
|
||||||
|
return peerFromMTP(p.vpeer());
|
||||||
|
}, [](const auto &) {
|
||||||
|
return PeerId(0);
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] Data::CreditsStatusSlice StatusFromTL(
|
||||||
|
const MTPpayments_StarsStatus &status,
|
||||||
|
not_null<PeerData*> peer) {
|
||||||
|
peer->owner().processUsers(status.data().vusers());
|
||||||
|
peer->owner().processChats(status.data().vchats());
|
||||||
|
return Data::CreditsStatusSlice{
|
||||||
|
.list = ranges::views::all(
|
||||||
|
status.data().vhistory().v
|
||||||
|
) | ranges::views::transform(HistoryFromTL) | ranges::to_vector,
|
||||||
|
.balance = status.data().vbalance().v,
|
||||||
|
.allLoaded = status.data().vnext_offset().has_value(),
|
||||||
|
.token = qs(status.data().vnext_offset().value_or_empty()),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
CreditsTopupOptions::CreditsTopupOptions(not_null<PeerData*> peer)
|
CreditsTopupOptions::CreditsTopupOptions(not_null<PeerData*> peer)
|
||||||
: _peer(peer)
|
: _peer(peer)
|
||||||
|
@ -45,6 +89,58 @@ rpl::producer<rpl::no_value, QString> CreditsTopupOptions::request() {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CreditsStatus::CreditsStatus(not_null<PeerData*> peer)
|
||||||
|
: _peer(peer)
|
||||||
|
, _api(&peer->session().api().instance()) {
|
||||||
|
}
|
||||||
|
|
||||||
|
void CreditsStatus::request(
|
||||||
|
const Data::CreditsStatusSlice::OffsetToken &token,
|
||||||
|
Fn<void(Data::CreditsStatusSlice)> done) {
|
||||||
|
if (_requestId) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
using TLResult = MTPpayments_StarsStatus;
|
||||||
|
|
||||||
|
_requestId = _api.request(MTPpayments_GetStarsStatus(
|
||||||
|
_peer->isSelf() ? MTP_inputPeerSelf() : _peer->input
|
||||||
|
)).done([=](const TLResult &result) {
|
||||||
|
_requestId = 0;
|
||||||
|
done(StatusFromTL(result, _peer));
|
||||||
|
}).fail([=] {
|
||||||
|
_requestId = 0;
|
||||||
|
done({});
|
||||||
|
}).send();
|
||||||
|
}
|
||||||
|
|
||||||
|
CreditsHistory::CreditsHistory(not_null<PeerData*> peer, bool in, bool out)
|
||||||
|
: _peer(peer)
|
||||||
|
, _flags(HistoryTL::Flags(0)
|
||||||
|
| (in ? HistoryTL::Flag::f_inbound : HistoryTL::Flags(0))
|
||||||
|
| (out ? HistoryTL::Flag::f_outbound : HistoryTL::Flags(0)))
|
||||||
|
, _api(&peer->session().api().instance()) {
|
||||||
|
}
|
||||||
|
|
||||||
|
void CreditsHistory::request(
|
||||||
|
const Data::CreditsStatusSlice::OffsetToken &token,
|
||||||
|
Fn<void(Data::CreditsStatusSlice)> done) {
|
||||||
|
if (_requestId) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_requestId = _api.request(MTPpayments_GetStarsTransactions(
|
||||||
|
MTP_flags(_flags),
|
||||||
|
_peer->isSelf() ? MTP_inputPeerSelf() : _peer->input,
|
||||||
|
MTP_string(token)
|
||||||
|
)).done([=](const MTPpayments_StarsStatus &result) {
|
||||||
|
_requestId = 0;
|
||||||
|
done(StatusFromTL(result, _peer));
|
||||||
|
}).fail([=] {
|
||||||
|
_requestId = 0;
|
||||||
|
done({});
|
||||||
|
}).send();
|
||||||
|
}
|
||||||
|
|
||||||
Data::CreditTopupOptions CreditsTopupOptions::options() const {
|
Data::CreditTopupOptions CreditsTopupOptions::options() const {
|
||||||
return _options;
|
return _options;
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,4 +32,40 @@ private:
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class CreditsStatus final {
|
||||||
|
public:
|
||||||
|
CreditsStatus(not_null<PeerData*> peer);
|
||||||
|
|
||||||
|
void request(
|
||||||
|
const Data::CreditsStatusSlice::OffsetToken &token,
|
||||||
|
Fn<void(Data::CreditsStatusSlice)> done);
|
||||||
|
|
||||||
|
private:
|
||||||
|
const not_null<PeerData*> _peer;
|
||||||
|
|
||||||
|
mtpRequestId _requestId = 0;
|
||||||
|
|
||||||
|
MTP::Sender _api;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
class CreditsHistory final {
|
||||||
|
public:
|
||||||
|
CreditsHistory(not_null<PeerData*> peer, bool in, bool out);
|
||||||
|
|
||||||
|
void request(
|
||||||
|
const Data::CreditsStatusSlice::OffsetToken &token,
|
||||||
|
Fn<void(Data::CreditsStatusSlice)> done);
|
||||||
|
|
||||||
|
private:
|
||||||
|
using HistoryTL = MTPpayments_GetStarsTransactions;
|
||||||
|
const not_null<PeerData*> _peer;
|
||||||
|
const HistoryTL::Flags _flags;
|
||||||
|
|
||||||
|
mtpRequestId _requestId = 0;
|
||||||
|
|
||||||
|
MTP::Sender _api;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
} // namespace Api
|
} // namespace Api
|
||||||
|
|
|
@ -18,4 +18,26 @@ struct CreditTopupOption final {
|
||||||
|
|
||||||
using CreditTopupOptions = std::vector<CreditTopupOption>;
|
using CreditTopupOptions = std::vector<CreditTopupOption>;
|
||||||
|
|
||||||
|
struct CreditsHistoryEntry final {
|
||||||
|
enum class PeerType {
|
||||||
|
Peer,
|
||||||
|
AppStore,
|
||||||
|
PlayMarket,
|
||||||
|
Fragment,
|
||||||
|
};
|
||||||
|
QString id;
|
||||||
|
uint64 credits = 0;
|
||||||
|
QDateTime date;
|
||||||
|
PeerType peerType;
|
||||||
|
PeerId peerId = PeerId(0);
|
||||||
|
};
|
||||||
|
|
||||||
|
struct CreditsStatusSlice final {
|
||||||
|
using OffsetToken = QString;
|
||||||
|
std::vector<CreditsHistoryEntry> list;
|
||||||
|
uint64 balance = 0;
|
||||||
|
bool allLoaded = false;
|
||||||
|
OffsetToken token;
|
||||||
|
};
|
||||||
|
|
||||||
} // namespace Data
|
} // namespace Data
|
||||||
|
|
Loading…
Add table
Reference in a new issue