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"
|
|
|
|
|
|
|
|
#include "apiwrap.h"
|
2023-10-26 19:40:53 +00:00
|
|
|
#include "base/call_delayed.h"
|
|
|
|
#include "main/main_account.h"
|
|
|
|
#include "ui/chat/chat_style.h"
|
2019-08-01 14:50:24 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-10-26 19:40:53 +00:00
|
|
|
AppConfig::~AppConfig() = default;
|
|
|
|
|
2022-01-13 16:39:44 +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(
|
2023-03-01 07:55:33 +00:00
|
|
|
MTP_int(_hash)
|
|
|
|
)).done([=](const MTPhelp_AppConfig &result) {
|
2019-08-01 14:50:24 +00:00
|
|
|
_requestId = 0;
|
|
|
|
refreshDelayed();
|
2023-03-01 07:55:33 +00: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;
|
|
|
|
}
|
2019-12-09 13:57:33 +00:00
|
|
|
_data.clear();
|
2023-03-01 07:55:33 +00:00
|
|
|
for (const auto &element : config.c_jsonObject().vvalue().v) {
|
2019-08-01 14:50:24 +00:00
|
|
|
element.match([&](const MTPDjsonObjectValue &data) {
|
|
|
|
_data.emplace_or_assign(qs(data.vkey()), data.vvalue());
|
|
|
|
});
|
|
|
|
}
|
2023-10-26 19:40:53 +00:00
|
|
|
parseColorIndices();
|
2020-02-12 15:36:05 +00:00
|
|
|
DEBUG_LOG(("getAppConfig result handled."));
|
2023-03-01 07:55:33 +00:00
|
|
|
_refreshed.fire({});
|
|
|
|
}, [](const MTPDhelp_appConfigNotModified &) {});
|
2021-11-26 20:46:53 +00:00
|
|
|
}).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
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-12-09 13:57:33 +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());
|
2019-12-09 13:57:33 +00:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-10-26 19:40:53 +00:00
|
|
|
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 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) {
|
2022-01-13 16:39:44 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2023-10-26 19:40:53 +00:00
|
|
|
void AppConfig::parseColorIndices() {
|
|
|
|
constexpr auto parseColor = [](const MTPJSONValue &color) {
|
|
|
|
if (color.type() != mtpc_jsonString) {
|
|
|
|
LOG(("API Error: Bad type for color element."));
|
|
|
|
return uint32();
|
|
|
|
}
|
|
|
|
const auto value = color.c_jsonString().vvalue().v;
|
|
|
|
if (value.size() != 6) {
|
|
|
|
LOG(("API Error: Bad length for color element: %1"
|
|
|
|
).arg(qs(value)));
|
|
|
|
return uint32();
|
|
|
|
}
|
|
|
|
const auto hex = [](char ch) {
|
|
|
|
return (ch >= 'a' && ch <= 'f')
|
|
|
|
? (ch - 'a' + 10)
|
|
|
|
: (ch >= 'A' && ch <= 'F')
|
|
|
|
? (ch - 'A' + 10)
|
|
|
|
: (ch >= '0' && ch <= '9')
|
|
|
|
? (ch - '0')
|
|
|
|
: 0;
|
|
|
|
};
|
|
|
|
auto result = (uint32(1) << 24);
|
|
|
|
for (auto i = 0; i != 6; ++i) {
|
|
|
|
result |= (uint32(hex(value[i])) << ((5 - i) * 4));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ParsedColor {
|
|
|
|
uint8 colorIndex = Ui::kColorIndexCount;
|
|
|
|
std::array<uint32, Ui::kColorPatternsCount> colors;
|
|
|
|
|
|
|
|
explicit operator bool() const {
|
|
|
|
return colorIndex < Ui::kColorIndexCount;
|
|
|
|
}
|
|
|
|
};
|
2023-10-28 19:22:46 +00:00
|
|
|
const auto parseColors = [&](const MTPJSONObjectValue &element) {
|
2023-10-26 19:40:53 +00:00
|
|
|
const auto &data = element.data();
|
|
|
|
if (data.vvalue().type() != mtpc_jsonArray) {
|
|
|
|
LOG(("API Error: Bad value for peer_colors element."));
|
|
|
|
return ParsedColor();
|
|
|
|
}
|
|
|
|
const auto &list = data.vvalue().c_jsonArray().vvalue().v;
|
|
|
|
if (list.empty() || list.size() > Ui::kColorPatternsCount) {
|
|
|
|
LOG(("API Error: Bad count for peer_colors element: %1"
|
|
|
|
).arg(list.size()));
|
|
|
|
return ParsedColor();
|
|
|
|
}
|
|
|
|
const auto index = data.vkey().v.toInt();
|
|
|
|
if (index < Ui::kSimpleColorIndexCount
|
|
|
|
|| index >= Ui::kColorIndexCount) {
|
|
|
|
LOG(("API Error: Bad index for peer_colors element: %1"
|
|
|
|
).arg(qs(data.vkey().v)));
|
|
|
|
return ParsedColor();
|
|
|
|
}
|
|
|
|
auto result = ParsedColor{ .colorIndex = uint8(index) };
|
|
|
|
auto fill = result.colors.data();
|
|
|
|
for (const auto &color : list) {
|
|
|
|
*fill++ = parseColor(color);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
2023-10-28 19:22:46 +00:00
|
|
|
const auto checkColorsObjectType = [&](const MTPJSONValue &value) {
|
2023-10-26 19:40:53 +00:00
|
|
|
if (value.type() != mtpc_jsonObject) {
|
|
|
|
if (value.type() != mtpc_jsonArray
|
|
|
|
|| !value.c_jsonArray().vvalue().v.empty()) {
|
|
|
|
LOG(("API Error: Bad value for [dark_]peer_colors."));
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto colors = std::make_shared<
|
|
|
|
std::array<Ui::ColorIndexData, Ui::kColorIndexCount>>();
|
|
|
|
getValue(u"peer_colors"_q, [&](const MTPJSONValue &value) {
|
|
|
|
if (!checkColorsObjectType(value)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (const auto &element : value.c_jsonObject().vvalue().v) {
|
|
|
|
if (const auto parsed = parseColors(element)) {
|
|
|
|
auto &fields = (*colors)[parsed.colorIndex];
|
|
|
|
fields.dark = fields.light = parsed.colors;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
getValue(u"dark_peer_colors"_q, [&](const MTPJSONValue &value) {
|
|
|
|
if (!checkColorsObjectType(value)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (const auto &element : value.c_jsonObject().vvalue().v) {
|
|
|
|
if (const auto parsed = parseColors(element)) {
|
|
|
|
(*colors)[parsed.colorIndex].dark = parsed.colors;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!_colorIndicesCurrent) {
|
|
|
|
_colorIndicesCurrent = std::make_unique<Ui::ColorIndicesCompressed>(
|
|
|
|
Ui::ColorIndicesCompressed{ std::move(colors) });
|
|
|
|
_colorIndicesChanged.fire({});
|
|
|
|
} else if (*_colorIndicesCurrent->colors != *colors) {
|
|
|
|
_colorIndicesCurrent->colors = std::move(colors);
|
|
|
|
_colorIndicesChanged.fire({});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto AppConfig::colorIndicesValue() const
|
|
|
|
-> rpl::producer<Ui::ColorIndicesCompressed> {
|
|
|
|
return rpl::single(_colorIndicesCurrent
|
|
|
|
? *_colorIndicesCurrent
|
|
|
|
: Ui::ColorIndicesCompressed()
|
|
|
|
) | rpl::then(_colorIndicesChanged.events() | rpl::map([=] {
|
|
|
|
return *_colorIndicesCurrent;
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2019-08-01 14:50:24 +00:00
|
|
|
} // namespace Main
|