2
0
mirror of https://github.com/telegramdesktop/tdesktop synced 2025-08-23 18:57:12 +00:00
tdesktop/Telegram/SourceFiles/main/main_app_config.cpp

354 lines
8.9 KiB
C++
Raw Normal View History

2019-08-01 15:50:24 +01:00
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "main/main_app_config.h"
#include "apiwrap.h"
#include "base/call_delayed.h"
#include "main/main_account.h"
#include "main/main_session.h"
#include "data/data_session.h"
#include "ui/chat/chat_style.h"
2019-08-01 15:50:24 +01:00
namespace Main {
namespace {
2019-08-08 16:27:30 +01:00
constexpr auto kRefreshTimeout = 3600 * crl::time(1000);
2019-08-01 15:50:24 +01:00
} // namespace
2019-11-27 12:45:23 +03:00
AppConfig::AppConfig(not_null<Account*> account) : _account(account) {
2020-06-30 13:01:59 +04:00
account->sessionChanges(
) | rpl::filter([=](Session *session) {
return (session != nullptr);
}) | rpl::start_with_next([=] {
2025-03-10 17:52:13 +04:00
_lastFrozenRefresh = 0;
2020-06-30 13:01:59 +04:00
refresh();
}, _lifetime);
2019-08-01 15:50:24 +01:00
}
AppConfig::~AppConfig() = default;
void AppConfig::start() {
_account->mtpMainSessionValue(
) | rpl::start_with_next([=](not_null<MTP::Instance*> instance) {
_api.emplace(instance);
refresh();
2025-03-10 17:52:13 +04:00
_frozenTrackLifetime = instance->frozenErrorReceived(
) | rpl::start_with_next([=] {
if (!get<int>(u"freeze_since_date"_q, 0)) {
const auto now = crl::now();
if (!_lastFrozenRefresh
|| now > _lastFrozenRefresh + kRefreshTimeout) {
_lastFrozenRefresh = now;
refresh();
}
}
});
}, _lifetime);
}
2024-09-30 14:45:55 +04:00
int AppConfig::quoteLengthMax() const {
return get<int>(u"quote_length_max"_q, 1024);
}
2024-10-14 18:31:35 +04:00
int AppConfig::stargiftConvertPeriodMax() const {
return get<int>(
u"stargifts_convert_period_max"_q,
_account->mtp().isTestMode() ? 300 : (90 * 86400));
}
2024-11-29 17:24:22 +04:00
const std::vector<QString> &AppConfig::startRefPrefixes() {
if (_startRefPrefixes.empty()) {
_startRefPrefixes = get<std::vector<QString>>(
u"starref_start_param_prefixes"_q,
std::vector<QString>());
}
return _startRefPrefixes;
}
bool AppConfig::starrefSetupAllowed() const {
return get<bool>(u"starref_program_allowed"_q, false);
}
bool AppConfig::starrefJoinAllowed() const {
return get<bool>(u"starref_connect_allowed"_q, false);
}
int AppConfig::starrefCommissionMin() const {
return get<int>(u"starref_min_commission_permille"_q, 1);
}
int AppConfig::starrefCommissionMax() const {
return get<int>(u"starref_max_commission_permille"_q, 900);
}
float64 AppConfig::starsWithdrawRate() const {
return get<float64>(u"stars_usd_withdraw_rate_x1000"_q, 1300) / 1000.;
}
bool AppConfig::paidMessagesAvailable() const {
return get<bool>(u"stars_paid_messages_available"_q, false);
}
int AppConfig::paidMessageStarsMax() const {
return get<int>(u"stars_paid_message_amount_max"_q, 10'000);
}
int AppConfig::paidMessageCommission() const {
return get<int>(u"stars_paid_message_commission_permille"_q, 850);
}
2025-03-05 14:57:17 +04:00
int AppConfig::pinnedGiftsLimit() const {
return get<int>(u"stargifts_pinned_to_top_limit"_q, 6);
}
2025-04-04 11:18:10 +04:00
bool AppConfig::confcallPrioritizeVP8() const {
AssertIsDebug();
return true;
}
2024-08-14 15:17:59 +02:00
void AppConfig::refresh(bool force) {
2019-11-27 12:45:23 +03:00
if (_requestId || !_api) {
2024-08-14 15:17:59 +02:00
if (force) {
_pendingRefresh = true;
}
2019-08-01 15:50:24 +01:00
return;
}
2024-08-14 15:17:59 +02:00
_pendingRefresh = false;
2019-11-27 12:45:23 +03:00
_requestId = _api->request(MTPhelp_GetAppConfig(
2023-03-01 11:55:33 +04:00
MTP_int(_hash)
)).done([=](const MTPhelp_AppConfig &result) {
2019-08-01 15:50:24 +01:00
_requestId = 0;
2023-03-01 11:55:33 +04:00
result.match([&](const MTPDhelp_appConfig &data) {
_hash = data.vhash().v;
const auto &config = data.vconfig();
if (config.type() != mtpc_jsonObject) {
LOG(("API Error: Unexpected config type."));
return;
}
2024-08-14 15:17:59 +02:00
auto was = ignoredRestrictionReasons();
_data.clear();
2023-03-01 11:55:33 +04:00
for (const auto &element : config.c_jsonObject().vvalue().v) {
2019-08-01 15:50:24 +01:00
element.match([&](const MTPDjsonObjectValue &data) {
_data.emplace_or_assign(qs(data.vkey()), data.vvalue());
});
}
2024-08-14 15:17:59 +02:00
updateIgnoredRestrictionReasons(std::move(was));
{
const auto dismissedSuggestions = get<std::vector<QString>>(
u"dismissed_suggestions"_q,
std::vector<QString>());
for (const auto &suggestion : dismissedSuggestions) {
_dismissedSuggestions.emplace(suggestion);
}
}
DEBUG_LOG(("getAppConfig result handled."));
2023-03-01 11:55:33 +04:00
_refreshed.fire({});
}, [](const MTPDhelp_appConfigNotModified &) {});
2024-08-14 15:17:59 +02:00
if (base::take(_pendingRefresh)) {
refresh();
} else {
refreshDelayed();
}
}).fail([=] {
2019-08-01 15:50:24 +01:00
_requestId = 0;
refreshDelayed();
}).send();
}
void AppConfig::refreshDelayed() {
2019-11-27 12:45:23 +03:00
base::call_delayed(kRefreshTimeout, _account, [=] {
2019-08-01 15:50:24 +01:00
refresh();
});
}
2024-08-14 15:17:59 +02:00
void AppConfig::updateIgnoredRestrictionReasons(std::vector<QString> was) {
_ignoreRestrictionReasons = get<std::vector<QString>>(
u"ignore_restriction_reasons"_q,
std::vector<QString>());
ranges::sort(_ignoreRestrictionReasons);
if (_ignoreRestrictionReasons != was) {
for (const auto &reason : _ignoreRestrictionReasons) {
const auto i = ranges::remove(was, reason);
if (i != end(was)) {
was.erase(i, end(was));
} else {
was.push_back(reason);
}
}
_ignoreRestrictionChanges.fire(std::move(was));
}
}
2019-11-27 12:45:23 +03:00
rpl::producer<> AppConfig::refreshed() const {
return _refreshed.events();
}
2020-07-03 20:53:24 +04:00
rpl::producer<> AppConfig::value() const {
return _refreshed.events_starting_with({});
}
2019-11-27 12:45:23 +03:00
template <typename Extractor>
auto AppConfig::getValue(const QString &key, Extractor &&extractor) const {
2019-08-01 15:50:24 +01:00
const auto i = _data.find(key);
2019-11-27 12:45:23 +03:00
return extractor((i != end(_data))
? i->second
: MTPJSONValue(MTP_jsonNull()));
}
2020-03-18 14:07:11 +04:00
bool AppConfig::getBool(const QString &key, bool fallback) const {
return getValue(key, [&](const MTPJSONValue &value) {
return value.match([&](const MTPDjsonBool &data) {
return mtpIsTrue(data.vvalue());
}, [&](const auto &data) {
return fallback;
});
});
}
2019-11-27 12:45:23 +03:00
double AppConfig::getDouble(const QString &key, double fallback) const {
return getValue(key, [&](const MTPJSONValue &value) {
return value.match([&](const MTPDjsonNumber &data) {
return data.vvalue().v;
}, [&](const auto &data) {
return fallback;
});
});
}
QString AppConfig::getString(
const QString &key,
const QString &fallback) const {
return getValue(key, [&](const MTPJSONValue &value) {
return value.match([&](const MTPDjsonString &data) {
return qs(data.vvalue());
}, [&](const auto &data) {
return fallback;
});
2019-08-01 15:50:24 +01:00
});
}
std::vector<QString> AppConfig::getStringArray(
const QString &key,
std::vector<QString> &&fallback) const {
return getValue(key, [&](const MTPJSONValue &value) {
return value.match([&](const MTPDjsonArray &data) {
auto result = std::vector<QString>();
2020-07-09 21:38:26 +04:00
result.reserve(data.vvalue().v.size());
for (const auto &entry : data.vvalue().v) {
if (entry.type() != mtpc_jsonString) {
return std::move(fallback);
}
result.push_back(qs(entry.c_jsonString().vvalue()));
}
return result;
}, [&](const auto &data) {
return std::move(fallback);
});
});
}
2024-07-12 12:23:47 +02:00
base::flat_map<QString, QString> AppConfig::getStringMap(
2020-07-09 21:38:26 +04:00
const QString &key,
2024-07-12 12:23:47 +02:00
base::flat_map<QString, QString> &&fallback) const {
2020-07-09 21:38:26 +04:00
return getValue(key, [&](const MTPJSONValue &value) {
2024-07-12 12:23:47 +02:00
return value.match([&](const MTPDjsonObject &data) {
auto result = base::flat_map<QString, QString>();
2020-07-09 21:38:26 +04:00
result.reserve(data.vvalue().v.size());
for (const auto &entry : data.vvalue().v) {
2024-07-12 12:23:47 +02:00
const auto &data = entry.data();
const auto &value = data.vvalue();
if (value.type() != mtpc_jsonString) {
2020-07-09 21:38:26 +04:00
return std::move(fallback);
}
2024-07-12 12:23:47 +02:00
result.emplace(
qs(data.vkey()),
qs(value.c_jsonString().vvalue()));
2020-07-09 21:38:26 +04:00
}
return result;
}, [&](const auto &data) {
return std::move(fallback);
});
});
}
std::vector<int> AppConfig::getIntArray(
const QString &key,
std::vector<int> &&fallback) const {
return getValue(key, [&](const MTPJSONValue &value) {
return value.match([&](const MTPDjsonArray &data) {
auto result = std::vector<int>();
result.reserve(data.vvalue().v.size());
for (const auto &entry : data.vvalue().v) {
if (entry.type() != mtpc_jsonNumber) {
return std::move(fallback);
}
result.push_back(
int(base::SafeRound(entry.c_jsonNumber().vvalue().v)));
}
return result;
}, [&](const auto &data) {
return std::move(fallback);
});
});
}
2020-07-03 20:53:24 +04:00
bool AppConfig::suggestionCurrent(const QString &key) const {
if (key == u"BIRTHDAY_CONTACTS_TODAY"_q) {
if (_dismissedSuggestions.contains(key)) {
return false;
} else {
const auto known
= _account->session().data().knownContactBirthdays();
if (!known) {
return true;
}
return !known->empty();
}
}
2020-07-03 20:53:24 +04:00
return !_dismissedSuggestions.contains(key)
&& ranges::contains(
get<std::vector<QString>>(
u"pending_suggestions"_q,
std::vector<QString>()),
key);
}
rpl::producer<> AppConfig::suggestionRequested(const QString &key) const {
return value(
) | rpl::filter([=] {
return suggestionCurrent(key);
});
}
void AppConfig::dismissSuggestion(const QString &key) {
Expects(_api.has_value());
2020-07-03 20:53:24 +04:00
if (!_dismissedSuggestions.emplace(key).second) {
return;
}
_api->request(MTPhelp_DismissSuggestion(
2021-02-12 14:59:51 +04:00
MTP_inputPeerEmpty(),
2020-07-03 20:53:24 +04:00
MTP_string(key)
)).send();
}
2024-03-29 15:30:50 +04:00
bool AppConfig::newRequirePremiumFree() const {
return get<bool>(
u"new_noncontact_peers_require_premium_without_ownpremium"_q,
false);
}
2019-08-01 15:50:24 +01:00
} // namespace Main