Add synchronous method td_api::getThemeParametersJsonString.

This commit is contained in:
levlam 2022-03-29 20:22:38 +03:00
parent 0b8b413206
commit c088dd9366
7 changed files with 76 additions and 13 deletions

View File

@ -4791,6 +4791,9 @@ getJsonValue json:string = JsonValue;
//@description Converts a JsonValue object to corresponding JSON-serialized string. Can be called synchronously @json_value The JsonValue object
getJsonString json_value:JsonValue = Text;
//@description Converts a themeParameters object to corresponding JSON-serialized string. Can be called synchronously @theme Theme parameters to convert to JSON
getThemeParametersJsonString theme:themeParameters = Text;
//@description Changes the user answer to a poll. A poll in quiz mode can be answered only once
//@chat_id Identifier of the chat to which the poll belongs

View File

@ -16,6 +16,7 @@
#include "td/telegram/PasswordManager.h"
#include "td/telegram/ServerMessageId.h"
#include "td/telegram/Td.h"
#include "td/telegram/ThemeManager.h"
#include "td/telegram/UpdatesManager.h"
#include "td/utils/algorithm.h"
@ -1152,17 +1153,7 @@ void get_payment_form(Td *td, FullMessageId full_message_id, const td_api::objec
tl_object_ptr<telegram_api::dataJSON> theme_parameters;
if (theme != nullptr) {
theme_parameters = make_tl_object<telegram_api::dataJSON>(string());
theme_parameters->data_ = json_encode<string>(json_object([&theme](auto &o) {
auto get_color = [](int32 color) {
return static_cast<int64>(static_cast<uint32>(color) | 0x000000FF);
};
o("bg_color", get_color(theme->background_color_));
o("text_color", get_color(theme->text_color_));
o("hint_color", get_color(theme->hint_color_));
o("link_color", get_color(theme->link_color_));
o("button_color", get_color(theme->button_color_));
o("button_text_color", get_color(theme->button_text_color_));
}));
theme_parameters->data_ = ThemeManager::get_theme_parameters_json_string(theme, false);
}
td->create_handler<GetPaymentFormQuery>(std::move(promise))
->send(full_message_id.get_dialog_id(), server_message_id, std::move(theme_parameters));

View File

@ -2927,6 +2927,7 @@ bool Td::is_synchronous_request(int32 id) {
case td_api::getChatFilterDefaultIconName::ID:
case td_api::getJsonValue::ID:
case td_api::getJsonString::ID:
case td_api::getThemeParametersJsonString::ID:
case td_api::getPushReceiverId::ID:
case td_api::setLogStream::ID:
case td_api::getLogStream::ID:
@ -3158,6 +3159,7 @@ td_api::object_ptr<td_api::Object> Td::static_request(td_api::object_ptr<td_api:
case td_api::getChatFilterDefaultIconName::ID:
case td_api::getJsonValue::ID:
case td_api::getJsonString::ID:
case td_api::getThemeParametersJsonString::ID:
case td_api::testReturnError::ID:
return true;
default:
@ -7885,6 +7887,10 @@ void Td::on_request(uint64 id, const td_api::getJsonString &request) {
UNREACHABLE();
}
void Td::on_request(uint64 id, const td_api::getThemeParametersJsonString &request) {
UNREACHABLE();
}
void Td::on_request(uint64 id, const td_api::setLogStream &request) {
UNREACHABLE();
}
@ -8066,6 +8072,10 @@ td_api::object_ptr<td_api::Object> Td::do_static_request(const td_api::getJsonSt
return td_api::make_object<td_api::text>(get_json_string(request.json_value_.get()));
}
td_api::object_ptr<td_api::Object> Td::do_static_request(const td_api::getThemeParametersJsonString &request) {
return td_api::make_object<td_api::text>(ThemeManager::get_theme_parameters_json_string(request.theme_, true));
}
td_api::object_ptr<td_api::Object> Td::do_static_request(td_api::setLogStream &request) {
auto result = Logging::set_current_stream(std::move(request.log_stream_));
if (result.is_ok()) {

View File

@ -1305,6 +1305,8 @@ class Td final : public Actor {
void on_request(uint64 id, const td_api::getJsonString &request);
void on_request(uint64 id, const td_api::getThemeParametersJsonString &request);
void on_request(uint64 id, const td_api::setLogStream &request);
void on_request(uint64 id, const td_api::getLogStream &request);
@ -1353,6 +1355,7 @@ class Td final : public Actor {
static td_api::object_ptr<td_api::Object> do_static_request(const td_api::getChatFilterDefaultIconName &request);
static td_api::object_ptr<td_api::Object> do_static_request(td_api::getJsonValue &request);
static td_api::object_ptr<td_api::Object> do_static_request(const td_api::getJsonString &request);
static td_api::object_ptr<td_api::Object> do_static_request(const td_api::getThemeParametersJsonString &request);
static td_api::object_ptr<td_api::Object> do_static_request(td_api::setLogStream &request);
static td_api::object_ptr<td_api::Object> do_static_request(const td_api::getLogStream &request);
static td_api::object_ptr<td_api::Object> do_static_request(const td_api::setLogVerbosityLevel &request);

View File

@ -17,6 +17,7 @@
#include "td/utils/algorithm.h"
#include "td/utils/buffer.h"
#include "td/utils/emoji.h"
#include "td/utils/JsonBuilder.h"
#include "td/utils/logging.h"
#include "td/utils/Random.h"
#include "td/utils/Time.h"
@ -240,6 +241,53 @@ void ThemeManager::on_update_theme(telegram_api::object_ptr<telegram_api::theme>
promise.set_value(Unit());
}
namespace {
template <bool for_web_view>
static auto get_color_json(int32 color);
template <>
auto get_color_json<false>(int32 color) {
return static_cast<int64>(static_cast<uint32>(color) | 0x000000FF);
}
template <>
auto get_color_json<true>(int32 color) {
string res(7, '#');
const char *hex = "0123456789abcdef";
for (int i = 0; i < 3; i++) {
int32 num = (color >> (i * 8)) & 0xFF;
res[2 * i + 1] = hex[num >> 4];
res[2 * i + 2] = hex[num & 15];
}
return res;
}
template <bool for_web_view>
string get_theme_parameters_json_string_impl(const td_api::object_ptr<td_api::themeParameters> &theme) {
if (for_web_view && theme == nullptr) {
return "null";
}
return json_encode<string>(json_object([&theme](auto &o) {
auto get_color = &get_color_json<for_web_view>;
o("bg_color", get_color(theme->background_color_));
o("text_color", get_color(theme->text_color_));
o("hint_color", get_color(theme->hint_color_));
o("link_color", get_color(theme->link_color_));
o("button_color", get_color(theme->button_color_));
o("button_text_color", get_color(theme->button_text_color_));
}));
}
} // namespace
string ThemeManager::get_theme_parameters_json_string(const td_api::object_ptr<td_api::themeParameters> &theme,
bool for_web_view) {
if (for_web_view) {
return get_theme_parameters_json_string_impl<true>(theme);
} else {
return get_theme_parameters_json_string_impl<false>(theme);
}
}
td_api::object_ptr<td_api::themeSettings> ThemeManager::get_theme_settings_object(const ThemeSettings &settings) const {
auto fill = [colors = settings.message_colors]() mutable -> td_api::object_ptr<td_api::BackgroundFill> {
if (colors.size() >= 3) {

View File

@ -29,6 +29,9 @@ class ThemeManager final : public Actor {
void on_update_theme(telegram_api::object_ptr<telegram_api::theme> &&theme, Promise<Unit> &&promise);
static string get_theme_parameters_json_string(const td_api::object_ptr<td_api::themeParameters> &theme,
bool for_web_view);
void get_current_state(vector<td_api::object_ptr<td_api::Update>> &updates) const;
private:

View File

@ -1632,6 +1632,10 @@ class CliClient final : public Actor {
return td_api::make_object<td_api::messageSchedulingStateSendAtDate>(send_date);
}
static td_api::object_ptr<td_api::themeParameters> get_theme_parameters() {
return td_api::make_object<td_api::themeParameters>(0, -1, 256, 65536, 123456789, 65535);
}
static td_api::object_ptr<td_api::BackgroundFill> get_background_fill(int32 color) {
return td_api::make_object<td_api::backgroundFillSolid>(color);
}
@ -1982,8 +1986,7 @@ class CliClient final : public Actor {
ChatId chat_id;
MessageId message_id;
get_args(args, chat_id, message_id);
send_request(td_api::make_object<td_api::getPaymentForm>(
chat_id, message_id, td_api::make_object<td_api::themeParameters>(0, -1, 256, 65536, 123456789, 65535)));
send_request(td_api::make_object<td_api::getPaymentForm>(chat_id, message_id, get_theme_parameters()));
} else if (op == "voi") {
ChatId chat_id;
MessageId message_id;
@ -3337,6 +3340,8 @@ class CliClient final : public Actor {
object->members_.emplace_back(
td_api::make_object<td_api::jsonObjectMember>("a", td_api::make_object<td_api::jsonValueNull>()));
test_get_json_string(std::move(object));
} else if (op == "gtpjs") {
execute(td_api::make_object<td_api::getThemeParametersJsonString>(get_theme_parameters()));
} else if (op == "gac") {
send_request(td_api::make_object<td_api::getApplicationConfig>());
} else if (op == "sale") {