Suggest relevant min price for gift resale.

This commit is contained in:
John Preston 2025-07-14 13:28:57 +04:00
parent 0132436dc8
commit 79ea992a0f
4 changed files with 224 additions and 120 deletions

View file

@ -127,6 +127,7 @@ constexpr auto kUpgradeDoneToastDuration = 4 * crl::time(1000);
constexpr auto kGiftsPreloadTimeout = 3 * crl::time(1000);
constexpr auto kResaleGiftsPerPage = 50;
constexpr auto kFiltersCount = 4;
constexpr auto kResellPriceCacheLifetime = 60 * crl::time(1000);
using namespace HistoryView;
using namespace Info::PeerGifts;
@ -220,6 +221,33 @@ struct GiftDetails {
bool byStars = false;
};
struct SessionResalePrices {
explicit SessionResalePrices(not_null<Main::Session*> session)
: api(std::make_unique<Api::PremiumGiftCodeOptions>(session->user())) {
}
std::unique_ptr<Api::PremiumGiftCodeOptions> api;
base::flat_map<QString, int> prices;
std::vector<Fn<void()>> waiting;
rpl::lifetime requestLifetime;
crl::time lastReceived = 0;
};
[[nodiscard]] not_null<SessionResalePrices*> ResalePrices(
not_null<Main::Session*> session) {
static auto result = base::flat_map<
not_null<Main::Session*>,
std::unique_ptr<SessionResalePrices>>();
if (const auto i = result.find(session); i != end(result)) {
return i->second.get();
}
const auto i = result.emplace(
session,
std::make_unique<SessionResalePrices>(session)).first;
session->lifetime().add([session] { result.remove(session); });
return i->second.get();
}
class PeerRow final : public PeerListRow {
public:
using PeerListRow::PeerListRow;
@ -4381,6 +4409,55 @@ void ShowUniqueGiftWearBox(
}));
}
void PreloadUniqueGiftResellPrices(not_null<Main::Session*> session) {
const auto entry = ResalePrices(session);
const auto now = crl::now();
const auto makeRequest = entry->prices.empty()
|| (now - entry->lastReceived >= kResellPriceCacheLifetime);
if (!makeRequest || entry->requestLifetime) {
return;
}
const auto finish = [=] {
entry->requestLifetime.destroy();
entry->lastReceived = crl::now();
for (const auto &callback : base::take(entry->waiting)) {
callback();
}
};
entry->requestLifetime = entry->api->requestStarGifts(
) | rpl::start_with_error_done(finish, [=] {
const auto &gifts = entry->api->starGifts();
entry->prices.reserve(gifts.size());
for (auto &gift : gifts) {
if (!gift.resellTitle.isEmpty() && gift.starsResellMin > 0) {
entry->prices[gift.resellTitle] = gift.starsResellMin;
}
}
finish();
});
}
void InvokeWithUniqueGiftResellPrice(
not_null<Main::Session*> session,
const QString &title,
Fn<void(int)> callback) {
PreloadUniqueGiftResellPrices(session);
const auto finish = [=] {
const auto entry = ResalePrices(session);
Assert(entry->lastReceived != 0);
const auto i = entry->prices.find(title);
callback((i != end(entry->prices)) ? i->second : 0);
};
const auto entry = ResalePrices(session);
if (entry->lastReceived) {
finish();
} else {
entry->waiting.push_back(finish);
}
}
void UpdateGiftSellPrice(
std::shared_ptr<ChatHelpers::Show> show,
std::shared_ptr<Data::UniqueGift> unique,
@ -4422,15 +4499,13 @@ void UpdateGiftSellPrice(
}).send();
}
void ShowUniqueGiftSellBox(
void UniqueGiftSellBox(
not_null<Ui::GenericBox*> box,
std::shared_ptr<ChatHelpers::Show> show,
std::shared_ptr<Data::UniqueGift> unique,
Data::SavedStarGiftId savedId,
int price,
Settings::GiftWearBoxStyleOverride st) {
if (ShowResaleGiftLater(show, unique)) {
return;
}
show->show(Box([=](not_null<Ui::GenericBox*> box) {
box->setTitle(tr::lng_gift_sell_title());
box->setStyle(st.box ? *st.box : st::upgradeGiftBox);
box->setWidth(st::boxWideWidth);
@ -4462,7 +4537,7 @@ void ShowUniqueGiftSellBox(
wrap,
st::editTagField,
rpl::single(QString()),
QString::number(priceNow ? priceNow : minimal),
QString::number(priceNow ? priceNow : price ? price : minimal),
limit);
const auto field = owned.data();
wrap->widthValue() | rpl::start_with_next([=](int width) {
@ -4548,7 +4623,21 @@ void ShowUniqueGiftSellBox(
button->moveToLeft(padding.left(), padding.top());
}
}, box->lifetime());
}));
}
void ShowUniqueGiftSellBox(
std::shared_ptr<ChatHelpers::Show> show,
std::shared_ptr<Data::UniqueGift> unique,
Data::SavedStarGiftId savedId,
Settings::GiftWearBoxStyleOverride st) {
if (ShowResaleGiftLater(show, unique)) {
return;
}
const auto session = &show->session();
const auto &title = unique->title;
InvokeWithUniqueGiftResellPrice(session, title, [=](int price) {
show->show(Box(UniqueGiftSellBox, show, unique, savedId, price, st));
});
}
void GiftReleasedByHandler(not_null<PeerData*> peer) {

View file

@ -21,6 +21,7 @@ class SavedStarGiftId;
} // namespace Data
namespace Main {
class Session;
class SessionShow;
} // namespace Main
@ -71,6 +72,8 @@ void ShowUniqueGiftWearBox(
const Data::UniqueGift &gift,
Settings::GiftWearBoxStyleOverride st);
void PreloadUniqueGiftResellPrices(not_null<Main::Session*> session);
void UpdateGiftSellPrice(
std::shared_ptr<ChatHelpers::Show> show,
std::shared_ptr<Data::UniqueGift> unique,

View file

@ -9,6 +9,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "api/api_premium.h"
#include "apiwrap.h"
#include "boxes/star_gift_box.h"
#include "data/data_channel.h"
#include "data/data_credits.h"
#include "data/data_session.h"
@ -380,6 +381,7 @@ void InnerWidget::loadMore() {
_entries.clear();
}
_entries.reserve(_entries.size() + data.vgifts().v.size());
auto hasUnique = false;
for (const auto &gift : data.vgifts().v) {
if (auto parsed = Api::FromTL(_peer, gift)) {
auto descriptor = DescriptorForGift(_peer, *parsed);
@ -387,10 +389,15 @@ void InnerWidget::loadMore() {
.gift = std::move(*parsed),
.descriptor = std::move(descriptor),
});
hasUnique = (parsed->info.unique != nullptr);
}
}
refreshButtons();
refreshAbout();
if (hasUnique) {
Ui::PreloadUniqueGiftResellPrices(&_peer->session());
}
}).fail([=] {
_loadMoreRequestId = 0;
_allLoaded = true;

View file

@ -1250,12 +1250,13 @@ void GenericCreditsEntryBox(
EntryToSavedStarGiftId(session, e),
style);
};
const auto canResell = CanResellGift(session, e);
AddUniqueGiftCover(
content,
rpl::single(*uniqueGift),
{},
std::move(price),
CanResellGift(session, e) ? std::move(change) : Fn<void()>());
canResell ? std::move(change) : Fn<void()>());
AddSkip(content, st::defaultVerticalListSkip * 2);
@ -1263,6 +1264,10 @@ void GenericCreditsEntryBox(
const auto type = SavedStarGiftMenuType::View;
FillUniqueGiftMenu(show, menu, e, type, st);
});
if (canResell) {
Ui::PreloadUniqueGiftResellPrices(session);
}
} else if (const auto callback = Ui::PaintPreviewCallback(session, e)) {
const auto thumb = content->add(object_ptr<Ui::CenterWrap<>>(
content,