97 lines
		
	
	
	
		
			2.3 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			97 lines
		
	
	
	
		
			2.3 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| /*
 | |
| 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 "main/main_account.h"
 | |
| #include "base/call_delayed.h"
 | |
| #include "apiwrap.h"
 | |
| 
 | |
| namespace Main {
 | |
| namespace {
 | |
| 
 | |
| constexpr auto kRefreshTimeout = 3600 * crl::time(1000);
 | |
| 
 | |
| } // namespace
 | |
| 
 | |
| 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 || !_api) {
 | |
| 		return;
 | |
| 	}
 | |
| 	_requestId = _api->request(MTPhelp_GetAppConfig(
 | |
| 	)).done([=](const MTPJSONValue &result) {
 | |
| 		_requestId = 0;
 | |
| 		refreshDelayed();
 | |
| 		if (result.type() == mtpc_jsonObject) {
 | |
| 			for (const auto &element : result.c_jsonObject().vvalue().v) {
 | |
| 				element.match([&](const MTPDjsonObjectValue &data) {
 | |
| 					_data.emplace_or_assign(qs(data.vkey()), data.vvalue());
 | |
| 				});
 | |
| 			}
 | |
| 		}
 | |
| 		_refreshed.fire({});
 | |
| 	}).fail([=](const RPCError &error) {
 | |
| 		_requestId = 0;
 | |
| 		refreshDelayed();
 | |
| 	}).send();
 | |
| }
 | |
| 
 | |
| void AppConfig::refreshDelayed() {
 | |
| 	base::call_delayed(kRefreshTimeout, _account, [=] {
 | |
| 		refresh();
 | |
| 	});
 | |
| }
 | |
| 
 | |
| 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);
 | |
| 	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;
 | |
| 		});
 | |
| 	});
 | |
| }
 | |
| 
 | |
| } // namespace Main
 | 
