Added initial ability to claim refund credits from history entries list.

This commit is contained in:
23rd 2024-05-28 02:22:32 +03:00 committed by John Preston
parent 885dcf0b28
commit be099880d8
5 changed files with 74 additions and 7 deletions

View file

@ -8,15 +8,14 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "api/api_credits.h"
#include "apiwrap.h"
#include "api/api_updates.h"
#include "base/unixtime.h"
#include "data/data_peer.h"
#include "data/data_photo.h"
#include "data/data_session.h"
#include "data/data_user.h"
#include "main/main_app_config.h"
#include "main/main_session.h"
#if _DEBUG
#include "base/random.h"
#endif
namespace Api {
namespace {
@ -199,4 +198,24 @@ rpl::producer<not_null<PeerData*>> PremiumPeerBot(
};
}
void CreditsRefund(
not_null<PeerData*> peer,
const QString &entryId,
Fn<void()> done,
Fn<void(QString)> fail) {
const auto user = peer->asUser();
if (!user) {
return;
}
peer->session().api().request(MTPpayments_RefundStarsCharge(
user->inputUser,
MTP_string(entryId)
)).done([=](const MTPUpdates &result) {
peer->session().api().updates().applyUpdates(result);
done();
}).fail([=](const MTP::Error &error) {
fail(error.type());
}).send();
}
} // namespace Api

View file

@ -68,6 +68,12 @@ private:
};
void CreditsRefund(
not_null<PeerData*> peer,
const QString &entryId,
Fn<void()> done,
Fn<void(QString)> fail);
[[nodiscard]] rpl::producer<not_null<PeerData*>> PremiumPeerBot(
not_null<Main::Session*> session);

View file

@ -19,6 +19,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "info/channel_statistics/boosts/giveaway/boost_badge.h"
#include "lang/lang_keys.h"
#include "main/main_session.h"
#include "main/session/session_show.h"
#include "ui/effects/credits_graphics.h"
#include "ui/effects/outline_segments.h" // Ui::UnreadStoryOutlineGradient.
#include "ui/effects/toggle_arrow.h"
@ -841,6 +842,10 @@ public:
void rowClicked(not_null<PeerListRow*> row) override;
void loadMoreRows() override;
base::unique_qptr<Ui::PopupMenu> rowContextMenu(
QWidget *parent,
not_null<PeerListRow*> row) override;
[[nodiscard]] bool skipRequest() const;
void requestNext();
@ -930,6 +935,29 @@ void CreditsController::rowClicked(not_null<PeerListRow*> row) {
}
}
base::unique_qptr<Ui::PopupMenu> CreditsController::rowContextMenu(
QWidget *parent,
not_null<PeerListRow*> row) {
const auto entry = static_cast<const CreditsRow*>(row.get())->entry();
if (!entry.bareId) {
return nullptr;
}
auto menu = base::make_unique_q<Ui::PopupMenu>(
parent,
st::defaultPopupMenu);
const auto peer = row->peer();
const auto callback = crl::guard(parent, [=, id = entry.id] {
const auto show = delegate()->peerListUiShow();
Api::CreditsRefund(
peer,
id,
[=] { show->showToast(tr::lng_report_spam_done(tr::now)); },
[=](const QString &error) { show->showToast(error); });
});
menu->addAction(tr::lng_channel_earn_history_return(tr::now), callback);
return menu;
}
rpl::producer<bool> CreditsController::allLoadedValue() const {
return _allLoaded.value();
}
@ -1086,6 +1114,7 @@ void AddBoostsList(
}
void AddCreditsHistoryList(
std::shared_ptr<Main::SessionShow> show,
const Data::CreditsStatusSlice &firstSlice,
not_null<Ui::VerticalLayout*> container,
Fn<void(const Data::CreditsHistoryEntry &)> callback,
@ -1094,13 +1123,18 @@ void AddCreditsHistoryList(
bool in,
bool out) {
struct State final {
State(CreditsDescriptor d) : controller(std::move(d)) {
State(
CreditsDescriptor d,
std::shared_ptr<Main::SessionShow> show)
: delegate(std::move(show))
, controller(std::move(d)) {
}
PeerListContentDelegateSimple delegate;
PeerListContentDelegateShow delegate;
CreditsController controller;
};
auto d = CreditsDescriptor{ firstSlice, callback, bot, icon, in, out };
const auto state = container->lifetime().make_state<State>(std::move(d));
const auto state = container->lifetime().make_state<State>(
CreditsDescriptor{ firstSlice, callback, bot, icon, in, out },
show);
state->delegate.setContent(container->add(
object_ptr<PeerListContent>(container, &state->controller)));

View file

@ -23,6 +23,10 @@ struct RecentPostId;
struct SupergroupStatistics;
} // namespace Data
namespace Main {
class SessionShow;
} // namespace Main
namespace Info::Statistics {
void AddPublicForwards(
@ -47,6 +51,7 @@ void AddBoostsList(
rpl::producer<QString> title);
void AddCreditsHistoryList(
std::shared_ptr<Main::SessionShow> show,
const Data::CreditsStatusSlice &firstSlice,
not_null<Ui::VerticalLayout*> container,
Fn<void(const Data::CreditsHistoryEntry &)> entryClickedCallback,

View file

@ -239,6 +239,7 @@ void Credits::setupHistory(not_null<Ui::VerticalLayout*> container) {
};
Info::Statistics::AddCreditsHistoryList(
controller->uiShow(),
fullSlice,
fullWrap->entity(),
entryClicked,
@ -247,6 +248,7 @@ void Credits::setupHistory(not_null<Ui::VerticalLayout*> container) {
true,
true);
Info::Statistics::AddCreditsHistoryList(
controller->uiShow(),
inSlice,
inWrap->entity(),
entryClicked,
@ -255,6 +257,7 @@ void Credits::setupHistory(not_null<Ui::VerticalLayout*> container) {
true,
false);
Info::Statistics::AddCreditsHistoryList(
controller->uiShow(),
outSlice,
outWrap->entity(),
std::move(entryClicked),