tdesktop/Telegram/SourceFiles/main/main_app_config.cpp

195 lines
4.9 KiB
C++
Raw Normal View History

2019-08-01 14:50:24 +00: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"
2019-11-27 09:45:23 +00:00
#include "main/main_account.h"
2019-09-26 10:55:35 +00:00
#include "base/call_delayed.h"
2019-08-01 14:50:24 +00:00
#include "apiwrap.h"
namespace Main {
namespace {
2019-08-08 15:27:30 +00:00
constexpr auto kRefreshTimeout = 3600 * crl::time(1000);
2019-08-01 14:50:24 +00:00
} // namespace
2019-11-27 09:45:23 +00:00
AppConfig::AppConfig(not_null<Account*> account) : _account(account) {
2020-06-30 09:01:59 +00:00
account->sessionChanges(
) | rpl::filter([=](Session *session) {
return (session != nullptr);
}) | rpl::start_with_next([=] {
refresh();
}, _lifetime);
2019-08-01 14:50:24 +00:00
}
void AppConfig::start() {
_account->mtpMainSessionValue(
) | rpl::start_with_next([=](not_null<MTP::Instance*> instance) {
_api.emplace(instance);
refresh();
}, _lifetime);
}
2019-08-01 14:50:24 +00:00
void AppConfig::refresh() {
2019-11-27 09:45:23 +00:00
if (_requestId || !_api) {
2019-08-01 14:50:24 +00:00
return;
}
2019-11-27 09:45:23 +00:00
_requestId = _api->request(MTPhelp_GetAppConfig(
2019-08-01 14:50:24 +00:00
)).done([=](const MTPJSONValue &result) {
_requestId = 0;
refreshDelayed();
if (result.type() == mtpc_jsonObject) {
_data.clear();
2019-08-01 14:50:24 +00:00
for (const auto &element : result.c_jsonObject().vvalue().v) {
element.match([&](const MTPDjsonObjectValue &data) {
_data.emplace_or_assign(qs(data.vkey()), data.vvalue());
});
}
DEBUG_LOG(("getAppConfig result handled."));
2019-08-01 14:50:24 +00:00
}
2019-11-27 09:45:23 +00:00
_refreshed.fire({});
}).fail([=] {
2019-08-01 14:50:24 +00:00
_requestId = 0;
refreshDelayed();
}).send();
}
void AppConfig::refreshDelayed() {
2019-11-27 09:45:23 +00:00
base::call_delayed(kRefreshTimeout, _account, [=] {
2019-08-01 14:50:24 +00:00
refresh();
});
}
2019-11-27 09:45:23 +00:00
rpl::producer<> AppConfig::refreshed() const {
return _refreshed.events();
}
2020-07-03 16:53:24 +00:00
rpl::producer<> AppConfig::value() const {
return _refreshed.events_starting_with({});
}
2019-11-27 09:45:23 +00:00
template <typename Extractor>
auto AppConfig::getValue(const QString &key, Extractor &&extractor) const {
2019-08-01 14:50:24 +00:00
const auto i = _data.find(key);
2019-11-27 09:45:23 +00:00
return extractor((i != end(_data))
? i->second
: MTPJSONValue(MTP_jsonNull()));
}
2020-03-18 10:07:11 +00: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 09:45:23 +00: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 14:50:24 +00: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 17:38:26 +00: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);
});
});
}
2020-07-09 17:38:26 +00:00
std::vector<std::map<QString, QString>> AppConfig::getStringMapArray(
const QString &key,
std::vector<std::map<QString, QString>> &&fallback) const {
return getValue(key, [&](const MTPJSONValue &value) {
return value.match([&](const MTPDjsonArray &data) {
auto result = std::vector<std::map<QString, QString>>();
result.reserve(data.vvalue().v.size());
for (const auto &entry : data.vvalue().v) {
if (entry.type() != mtpc_jsonObject) {
return std::move(fallback);
}
auto element = std::map<QString, QString>();
for (const auto &field : entry.c_jsonObject().vvalue().v) {
const auto &data = field.c_jsonObjectValue();
if (data.vvalue().type() != mtpc_jsonString) {
return std::move(fallback);
}
element.emplace(
qs(data.vkey()),
qs(data.vvalue().c_jsonString().vvalue()));
}
result.push_back(std::move(element));
}
return result;
}, [&](const auto &data) {
return std::move(fallback);
});
});
}
2020-07-03 16:53:24 +00:00
bool AppConfig::suggestionCurrent(const QString &key) const {
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 16:53:24 +00:00
if (!_dismissedSuggestions.emplace(key).second) {
return;
}
_api->request(MTPhelp_DismissSuggestion(
2021-02-12 10:59:51 +00:00
MTP_inputPeerEmpty(),
2020-07-03 16:53:24 +00:00
MTP_string(key)
)).send();
}
2019-08-01 14:50:24 +00:00
} // namespace Main