mirror of
https://github.com/AyuGram/AyuGramDesktop.git
synced 2025-06-07 23:53:58 +02:00
Slightly improved code style in PromoSuggestions.
This commit is contained in:
parent
197f6b05ae
commit
24a3a41cd6
2 changed files with 65 additions and 67 deletions
|
@ -56,7 +56,7 @@ void PromoSuggestions::refreshTopPromotion() {
|
||||||
? _topPromotionNextRequestTime
|
? _topPromotionNextRequestTime
|
||||||
: now;
|
: now;
|
||||||
if (_topPromotionRequestId) {
|
if (_topPromotionRequestId) {
|
||||||
getTopPromotionDelayed(now, next);
|
topPromotionDelayed(now, next);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const auto key = [&]() -> std::pair<QString, uint32> {
|
const auto key = [&]() -> std::pair<QString, uint32> {
|
||||||
|
@ -70,40 +70,22 @@ void PromoSuggestions::refreshTopPromotion() {
|
||||||
return { proxy.host, proxy.port };
|
return { proxy.host, proxy.port };
|
||||||
}();
|
}();
|
||||||
if (_topPromotionKey == key && now < next) {
|
if (_topPromotionKey == key && now < next) {
|
||||||
getTopPromotionDelayed(now, next);
|
topPromotionDelayed(now, next);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
_topPromotionKey = key;
|
_topPromotionKey = key;
|
||||||
_topPromotionRequestId = _session->api().request(MTPhelp_GetPromoData(
|
_topPromotionRequestId = _session->api().request(MTPhelp_GetPromoData(
|
||||||
)).done([=](const MTPhelp_PromoData &result) {
|
)).done([=](const MTPhelp_PromoData &result) {
|
||||||
_topPromotionRequestId = 0;
|
_topPromotionRequestId = 0;
|
||||||
topPromotionDone(result);
|
|
||||||
}).fail([=] {
|
|
||||||
_topPromotionRequestId = 0;
|
|
||||||
const auto now = base::unixtime::now();
|
|
||||||
const auto next = _topPromotionNextRequestTime = now
|
|
||||||
+ kTopPromotionInterval;
|
|
||||||
if (!_topPromotionTimer.isActive()) {
|
|
||||||
getTopPromotionDelayed(now, next);
|
|
||||||
}
|
|
||||||
}).send();
|
|
||||||
}
|
|
||||||
|
|
||||||
void PromoSuggestions::getTopPromotionDelayed(TimeId now, TimeId next) {
|
_topPromotionNextRequestTime = result.match([&](const auto &data) {
|
||||||
_topPromotionTimer.callOnce(std::min(
|
|
||||||
std::max(next - now, kTopPromotionMinDelay),
|
|
||||||
kTopPromotionInterval) * crl::time(1000));
|
|
||||||
};
|
|
||||||
|
|
||||||
void PromoSuggestions::topPromotionDone(const MTPhelp_PromoData &proxy) {
|
|
||||||
_topPromotionNextRequestTime = proxy.match([&](const auto &data) {
|
|
||||||
return data.vexpires().v;
|
return data.vexpires().v;
|
||||||
});
|
});
|
||||||
getTopPromotionDelayed(
|
topPromotionDelayed(
|
||||||
base::unixtime::now(),
|
base::unixtime::now(),
|
||||||
_topPromotionNextRequestTime);
|
_topPromotionNextRequestTime);
|
||||||
|
|
||||||
proxy.match([&](const MTPDhelp_promoDataEmpty &data) {
|
result.match([&](const MTPDhelp_promoDataEmpty &data) {
|
||||||
setTopPromoted(nullptr, QString(), QString());
|
setTopPromoted(nullptr, QString(), QString());
|
||||||
}, [&](const MTPDhelp_promoData &data) {
|
}, [&](const MTPDhelp_promoData &data) {
|
||||||
_session->data().processChats(data.vchats());
|
_session->data().processChats(data.vchats());
|
||||||
|
@ -140,7 +122,9 @@ void PromoSuggestions::topPromotionDone(const MTPhelp_PromoData &proxy) {
|
||||||
auto changedCustom = false;
|
auto changedCustom = false;
|
||||||
auto custom = data.vcustom_pending_suggestion()
|
auto custom = data.vcustom_pending_suggestion()
|
||||||
? std::make_optional(
|
? std::make_optional(
|
||||||
CustomFromTL(_session, *data.vcustom_pending_suggestion()))
|
CustomFromTL(
|
||||||
|
_session,
|
||||||
|
*data.vcustom_pending_suggestion()))
|
||||||
: std::nullopt;
|
: std::nullopt;
|
||||||
if (_custom != custom) {
|
if (_custom != custom) {
|
||||||
_custom = std::move(custom);
|
_custom = std::move(custom);
|
||||||
|
@ -153,7 +137,22 @@ void PromoSuggestions::topPromotionDone(const MTPhelp_PromoData &proxy) {
|
||||||
_refreshed.fire({});
|
_refreshed.fire({});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}).fail([=] {
|
||||||
|
_topPromotionRequestId = 0;
|
||||||
|
const auto now = base::unixtime::now();
|
||||||
|
const auto next = _topPromotionNextRequestTime = now
|
||||||
|
+ kTopPromotionInterval;
|
||||||
|
if (!_topPromotionTimer.isActive()) {
|
||||||
|
topPromotionDelayed(now, next);
|
||||||
}
|
}
|
||||||
|
}).send();
|
||||||
|
}
|
||||||
|
|
||||||
|
void PromoSuggestions::topPromotionDelayed(TimeId now, TimeId next) {
|
||||||
|
_topPromotionTimer.callOnce(std::min(
|
||||||
|
std::max(next - now, kTopPromotionMinDelay),
|
||||||
|
kTopPromotionInterval) * crl::time(1000));
|
||||||
|
};
|
||||||
|
|
||||||
rpl::producer<> PromoSuggestions::value() const {
|
rpl::producer<> PromoSuggestions::value() const {
|
||||||
return _refreshed.events_starting_with({});
|
return _refreshed.events_starting_with({});
|
||||||
|
|
|
@ -51,8 +51,7 @@ private:
|
||||||
const QString &type,
|
const QString &type,
|
||||||
const QString &message);
|
const QString &message);
|
||||||
|
|
||||||
void getTopPromotionDelayed(TimeId now, TimeId next);
|
void topPromotionDelayed(TimeId now, TimeId next);
|
||||||
void topPromotionDone(const MTPhelp_PromoData &proxy);
|
|
||||||
|
|
||||||
const not_null<Main::Session*> _session;
|
const not_null<Main::Session*> _session;
|
||||||
base::flat_set<QString> _dismissedSuggestions;
|
base::flat_set<QString> _dismissedSuggestions;
|
||||||
|
|
Loading…
Add table
Reference in a new issue