2
0
mirror of https://github.com/telegramdesktop/tdesktop synced 2025-09-01 15:05:56 +00:00

Use app config value for QR login.

This commit is contained in:
John Preston
2019-11-27 12:45:23 +03:00
parent 55f83129b7
commit e6c86b19db
25 changed files with 191 additions and 91 deletions

View File

@@ -7,7 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "main/main_app_config.h"
#include "main/main_session.h"
#include "main/main_account.h"
#include "base/call_delayed.h"
#include "apiwrap.h"
@@ -18,15 +18,25 @@ constexpr auto kRefreshTimeout = 3600 * crl::time(1000);
} // namespace
AppConfig::AppConfig(not_null<Session*> session) : _session(session) {
AppConfig::AppConfig(not_null<Account*> account) : _account(account) {
account->mtpValue(
) | rpl::start_with_next([=](MTP::Instance *instance) {
if (instance) {
_api.emplace(instance);
refresh();
} else {
_api.reset();
_requestId = 0;
}
}, _lifetime);
refresh();
}
void AppConfig::refresh() {
if (_requestId) {
if (_requestId || !_api) {
return;
}
_requestId = _session->api().request(MTPhelp_GetAppConfig(
_requestId = _api->request(MTPhelp_GetAppConfig(
)).done([=](const MTPJSONValue &result) {
_requestId = 0;
refreshDelayed();
@@ -37,6 +47,7 @@ void AppConfig::refresh() {
});
}
}
_refreshed.fire({});
}).fail([=](const RPCError &error) {
_requestId = 0;
refreshDelayed();
@@ -44,20 +55,42 @@ void AppConfig::refresh() {
}
void AppConfig::refreshDelayed() {
base::call_delayed(kRefreshTimeout, _session, [=] {
base::call_delayed(kRefreshTimeout, _account, [=] {
refresh();
});
}
double AppConfig::getDouble(const QString &key, double fallback) const {
rpl::producer<> AppConfig::refreshed() const {
return _refreshed.events();
}
template <typename Extractor>
auto AppConfig::getValue(const QString &key, Extractor &&extractor) const {
const auto i = _data.find(key);
if (i == end(_data)) {
return fallback;
}
return i->second.match([&](const MTPDjsonNumber &data) {
return data.vvalue().v;
}, [&](const auto &data) {
return fallback;
return extractor((i != end(_data))
? i->second
: MTPJSONValue(MTP_jsonNull()));
}
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;
});
});
}