From 9fbdba456aff80d9424bfb6e59cb2367844f19a0 Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 28 Nov 2023 16:12:21 +0300 Subject: [PATCH] Add td_api::setProfileAccentColor. --- td/generate/scheme/td_api.tl | 5 +++ td/telegram/ContactsManager.cpp | 58 +++++++++++++++++++++++++++++++++ td/telegram/ContactsManager.h | 4 +++ td/telegram/Td.cpp | 8 +++++ td/telegram/Td.h | 2 ++ td/telegram/cli.cpp | 6 ++++ 6 files changed, 83 insertions(+) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index ebf83eef2..b148b80b9 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -8707,6 +8707,11 @@ deleteProfilePhoto profile_photo_id:int64 = Ok; //@background_custom_emoji_id Identifier of a custom emoji to be shown on the reply header background; 0 if none setAccentColor accent_color_id:int32 background_custom_emoji_id:int64 = Ok; +//@description Changes accent color and background custom emoji for profile of the current user; for Telegram Premium users only +//@profile_accent_color_id Identifier of the accent color to use for profile; pass -1 if none +//@profile_background_custom_emoji_id Identifier of a custom emoji to be shown in the on the user's profile photo background; 0 if none +setProfileAccentColor profile_accent_color_id:int32 profile_background_custom_emoji_id:int64 = Ok; + //@description Changes the first and last name of the current user @first_name The new value of the first name for the current user; 1-64 characters @last_name The new value of the optional last name for the current user; 0-64 characters setName first_name:string last_name:string = Ok; diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index c10c36b14..277c5a63c 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -812,6 +812,47 @@ class UpdateColorQuery final : public Td::ResultHandler { } }; +class UpdateProfileColorQuery final : public Td::ResultHandler { + Promise promise_; + AccentColorId accent_color_id_; + CustomEmojiId background_custom_emoji_id_; + + public: + explicit UpdateProfileColorQuery(Promise &&promise) : promise_(std::move(promise)) { + } + + void send(AccentColorId accent_color_id, CustomEmojiId background_custom_emoji_id) { + accent_color_id_ = accent_color_id; + background_custom_emoji_id_ = background_custom_emoji_id; + int32 flags = telegram_api::account_updateColor::FOR_PROFILE_MASK; + if (accent_color_id.is_valid()) { + flags |= telegram_api::account_updateColor::COLOR_MASK; + } + if (background_custom_emoji_id.is_valid()) { + flags |= telegram_api::account_updateColor::BACKGROUND_EMOJI_ID_MASK; + } + send_query(G()->net_query_creator().create( + telegram_api::account_updateColor(flags, false /*ignored*/, accent_color_id.get(), + background_custom_emoji_id.get()), + {{"me"}})); + } + + void on_result(BufferSlice packet) final { + auto result_ptr = fetch_result(packet); + if (result_ptr.is_error()) { + return on_error(result_ptr.move_as_error()); + } + + LOG(DEBUG) << "Receive result for UpdateColorQuery: " << result_ptr.ok(); + td_->contacts_manager_->on_update_profile_accent_color_success(accent_color_id_, background_custom_emoji_id_); + promise_.set_value(Unit()); + } + + void on_error(Status status) final { + promise_.set_error(std::move(status)); + } +}; + class UpdateProfileQuery final : public Td::ResultHandler { Promise promise_; int32 flags_; @@ -7796,6 +7837,11 @@ void ContactsManager::set_accent_color(AccentColorId accent_color_id, CustomEmoj td_->create_handler(std::move(promise))->send(accent_color_id, background_custom_emoji_id); } +void ContactsManager::set_profile_accent_color(AccentColorId accent_color_id, CustomEmojiId background_custom_emoji_id, + Promise &&promise) { + td_->create_handler(std::move(promise))->send(accent_color_id, background_custom_emoji_id); +} + void ContactsManager::set_name(const string &first_name, const string &last_name, Promise &&promise) { auto new_first_name = clean_name(first_name, MAX_NAME_LENGTH); auto new_last_name = clean_name(last_name, MAX_NAME_LENGTH); @@ -7855,6 +7901,18 @@ void ContactsManager::on_update_accent_color_success(AccentColorId accent_color_ update_user(u, user_id); } +void ContactsManager::on_update_profile_accent_color_success(AccentColorId accent_color_id, + CustomEmojiId background_custom_emoji_id) { + auto user_id = get_my_id(); + User *u = get_user_force(user_id, "on_update_profile_accent_color_success"); + if (u == nullptr) { + return; + } + on_update_user_profile_accent_color_id(u, user_id, accent_color_id); + on_update_user_profile_background_custom_emoji_id(u, user_id, background_custom_emoji_id); + update_user(u, user_id); +} + void ContactsManager::on_update_profile_success(int32 flags, const string &first_name, const string &last_name, const string &about) { CHECK(flags != 0); diff --git a/td/telegram/ContactsManager.h b/td/telegram/ContactsManager.h index c93c78d33..339e121fa 100644 --- a/td/telegram/ContactsManager.h +++ b/td/telegram/ContactsManager.h @@ -200,6 +200,7 @@ class ContactsManager final : public Actor { void on_update_profile_success(int32 flags, const string &first_name, const string &last_name, const string &about); void on_update_accent_color_success(AccentColorId accent_color_id, CustomEmojiId background_custom_emoji_id); + void on_update_profile_accent_color_success(AccentColorId accent_color_id, CustomEmojiId background_custom_emoji_id); void on_update_user_name(UserId user_id, string &&first_name, string &&last_name, Usernames &&usernames); void on_update_user_phone_number(UserId user_id, string &&phone_number); @@ -433,6 +434,9 @@ class ContactsManager final : public Actor { void set_accent_color(AccentColorId accent_color_id, CustomEmojiId background_custom_emoji_id, Promise &&promise); + void set_profile_accent_color(AccentColorId accent_color_id, CustomEmojiId background_custom_emoji_id, + Promise &&promise); + void set_name(const string &first_name, const string &last_name, Promise &&promise); void set_bio(const string &bio, Promise &&promise); diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index fa9b5d476..e846b6abe 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -7622,6 +7622,14 @@ void Td::on_request(uint64 id, const td_api::setAccentColor &request) { CustomEmojiId(request.background_custom_emoji_id_), std::move(promise)); } +void Td::on_request(uint64 id, const td_api::setProfileAccentColor &request) { + CHECK_IS_USER(); + CREATE_OK_REQUEST_PROMISE(); + contacts_manager_->set_profile_accent_color(AccentColorId(request.profile_accent_color_id_), + CustomEmojiId(request.profile_background_custom_emoji_id_), + std::move(promise)); +} + void Td::on_request(uint64 id, td_api::setSupergroupUsername &request) { CHECK_IS_USER(); CLEAN_INPUT_STRING(request.username_); diff --git a/td/telegram/Td.h b/td/telegram/Td.h index b116f51cb..e39200476 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -1293,6 +1293,8 @@ class Td final : public Actor { void on_request(uint64 id, const td_api::setAccentColor &request); + void on_request(uint64 id, const td_api::setProfileAccentColor &request); + void on_request(uint64 id, td_api::setSupergroupUsername &request); void on_request(uint64 id, td_api::toggleSupergroupUsernameIsActive &request); diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index 0a0cf5944..deec9b8d5 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -5871,6 +5871,12 @@ class CliClient final : public Actor { CustomEmojiId background_custom_emoji_id; get_args(args, accent_color_id, background_custom_emoji_id); send_request(td_api::make_object(accent_color_id, background_custom_emoji_id)); + } else if (op == "spac") { + int32 profile_accent_color_id; + CustomEmojiId profile_background_custom_emoji_id; + get_args(args, profile_accent_color_id, profile_background_custom_emoji_id); + send_request(td_api::make_object(profile_accent_color_id, + profile_background_custom_emoji_id)); } else if (op == "gns") { int64 notification_sound_id; get_args(args, notification_sound_id);