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,91 +70,90 @@ 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);
|
|
||||||
|
_topPromotionNextRequestTime = result.match([&](const auto &data) {
|
||||||
|
return data.vexpires().v;
|
||||||
|
});
|
||||||
|
topPromotionDelayed(
|
||||||
|
base::unixtime::now(),
|
||||||
|
_topPromotionNextRequestTime);
|
||||||
|
|
||||||
|
result.match([&](const MTPDhelp_promoDataEmpty &data) {
|
||||||
|
setTopPromoted(nullptr, QString(), QString());
|
||||||
|
}, [&](const MTPDhelp_promoData &data) {
|
||||||
|
_session->data().processChats(data.vchats());
|
||||||
|
_session->data().processUsers(data.vusers());
|
||||||
|
|
||||||
|
auto changedPendingSuggestions = false;
|
||||||
|
auto pendingSuggestions = ranges::views::all(
|
||||||
|
data.vpending_suggestions().v
|
||||||
|
) | ranges::views::transform([](const auto &suggestion) {
|
||||||
|
return qs(suggestion);
|
||||||
|
}) | ranges::to_vector;
|
||||||
|
if (!ranges::equal(_pendingSuggestions, pendingSuggestions)) {
|
||||||
|
_pendingSuggestions = std::move(pendingSuggestions);
|
||||||
|
changedPendingSuggestions = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto changedDismissedSuggestions = false;
|
||||||
|
for (const auto &suggestion : data.vdismissed_suggestions().v) {
|
||||||
|
changedDismissedSuggestions
|
||||||
|
|= _dismissedSuggestions.emplace(qs(suggestion)).second;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (const auto peer = data.vpeer()) {
|
||||||
|
const auto peerId = peerFromMTP(*peer);
|
||||||
|
const auto history = _session->data().history(peerId);
|
||||||
|
setTopPromoted(
|
||||||
|
history,
|
||||||
|
data.vpsa_type().value_or_empty(),
|
||||||
|
data.vpsa_message().value_or_empty());
|
||||||
|
} else {
|
||||||
|
setTopPromoted(nullptr, QString(), QString());
|
||||||
|
}
|
||||||
|
|
||||||
|
auto changedCustom = false;
|
||||||
|
auto custom = data.vcustom_pending_suggestion()
|
||||||
|
? std::make_optional(
|
||||||
|
CustomFromTL(
|
||||||
|
_session,
|
||||||
|
*data.vcustom_pending_suggestion()))
|
||||||
|
: std::nullopt;
|
||||||
|
if (_custom != custom) {
|
||||||
|
_custom = std::move(custom);
|
||||||
|
changedCustom = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (changedPendingSuggestions
|
||||||
|
|| changedDismissedSuggestions
|
||||||
|
|| changedCustom) {
|
||||||
|
_refreshed.fire({});
|
||||||
|
}
|
||||||
|
});
|
||||||
}).fail([=] {
|
}).fail([=] {
|
||||||
_topPromotionRequestId = 0;
|
_topPromotionRequestId = 0;
|
||||||
const auto now = base::unixtime::now();
|
const auto now = base::unixtime::now();
|
||||||
const auto next = _topPromotionNextRequestTime = now
|
const auto next = _topPromotionNextRequestTime = now
|
||||||
+ kTopPromotionInterval;
|
+ kTopPromotionInterval;
|
||||||
if (!_topPromotionTimer.isActive()) {
|
if (!_topPromotionTimer.isActive()) {
|
||||||
getTopPromotionDelayed(now, next);
|
topPromotionDelayed(now, next);
|
||||||
}
|
}
|
||||||
}).send();
|
}).send();
|
||||||
}
|
}
|
||||||
|
|
||||||
void PromoSuggestions::getTopPromotionDelayed(TimeId now, TimeId next) {
|
void PromoSuggestions::topPromotionDelayed(TimeId now, TimeId next) {
|
||||||
_topPromotionTimer.callOnce(std::min(
|
_topPromotionTimer.callOnce(std::min(
|
||||||
std::max(next - now, kTopPromotionMinDelay),
|
std::max(next - now, kTopPromotionMinDelay),
|
||||||
kTopPromotionInterval) * crl::time(1000));
|
kTopPromotionInterval) * crl::time(1000));
|
||||||
};
|
};
|
||||||
|
|
||||||
void PromoSuggestions::topPromotionDone(const MTPhelp_PromoData &proxy) {
|
|
||||||
_topPromotionNextRequestTime = proxy.match([&](const auto &data) {
|
|
||||||
return data.vexpires().v;
|
|
||||||
});
|
|
||||||
getTopPromotionDelayed(
|
|
||||||
base::unixtime::now(),
|
|
||||||
_topPromotionNextRequestTime);
|
|
||||||
|
|
||||||
proxy.match([&](const MTPDhelp_promoDataEmpty &data) {
|
|
||||||
setTopPromoted(nullptr, QString(), QString());
|
|
||||||
}, [&](const MTPDhelp_promoData &data) {
|
|
||||||
_session->data().processChats(data.vchats());
|
|
||||||
_session->data().processUsers(data.vusers());
|
|
||||||
|
|
||||||
auto changedPendingSuggestions = false;
|
|
||||||
auto pendingSuggestions = ranges::views::all(
|
|
||||||
data.vpending_suggestions().v
|
|
||||||
) | ranges::views::transform([](const auto &suggestion) {
|
|
||||||
return qs(suggestion);
|
|
||||||
}) | ranges::to_vector;
|
|
||||||
if (!ranges::equal(_pendingSuggestions, pendingSuggestions)) {
|
|
||||||
_pendingSuggestions = std::move(pendingSuggestions);
|
|
||||||
changedPendingSuggestions = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto changedDismissedSuggestions = false;
|
|
||||||
for (const auto &suggestion : data.vdismissed_suggestions().v) {
|
|
||||||
changedDismissedSuggestions
|
|
||||||
|= _dismissedSuggestions.emplace(qs(suggestion)).second;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (const auto peer = data.vpeer()) {
|
|
||||||
const auto peerId = peerFromMTP(*peer);
|
|
||||||
const auto history = _session->data().history(peerId);
|
|
||||||
setTopPromoted(
|
|
||||||
history,
|
|
||||||
data.vpsa_type().value_or_empty(),
|
|
||||||
data.vpsa_message().value_or_empty());
|
|
||||||
} else {
|
|
||||||
setTopPromoted(nullptr, QString(), QString());
|
|
||||||
}
|
|
||||||
|
|
||||||
auto changedCustom = false;
|
|
||||||
auto custom = data.vcustom_pending_suggestion()
|
|
||||||
? std::make_optional(
|
|
||||||
CustomFromTL(_session, *data.vcustom_pending_suggestion()))
|
|
||||||
: std::nullopt;
|
|
||||||
if (_custom != custom) {
|
|
||||||
_custom = std::move(custom);
|
|
||||||
changedCustom = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (changedPendingSuggestions
|
|
||||||
|| changedDismissedSuggestions
|
|
||||||
|| changedCustom) {
|
|
||||||
_refreshed.fire({});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
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