From aa7a0a6ee3132137d8a683bd1a4d27f51ad2d277 Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 10 Nov 2023 14:40:14 +0300 Subject: [PATCH] Allow to set chat background for both sides. --- td/generate/scheme/td_api.tl | 3 ++- td/telegram/BackgroundManager.cpp | 34 +++++++++++++++++-------------- td/telegram/BackgroundManager.h | 6 +++--- td/telegram/Td.cpp | 2 +- td/telegram/cli.cpp | 4 ++-- 5 files changed, 27 insertions(+), 22 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 6c51ac912..4ea2b7a61 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -7706,7 +7706,8 @@ setChatPermissions chat_id:int53 permissions:chatPermissions = Ok; //@background The input background to use; pass null to create a new filled background or to remove the current background //@type Background type; pass null to remove the current background //@dark_theme_dimming Dimming of the background in dark themes, as a percentage; 0-100 -setChatBackground chat_id:int53 background:InputBackground type:BackgroundType dark_theme_dimming:int32 = Ok; +//@only_for_self Pass true to set background only for self; pass false to set background for both chat users. Background can be set for both users only by Telegram Premium users +setChatBackground chat_id:int53 background:InputBackground type:BackgroundType dark_theme_dimming:int32 only_for_self:Bool = Ok; //@description Changes the chat theme. Supported only in private and secret chats @chat_id Chat identifier @theme_name Name of the new chat theme; pass an empty string to return the default theme setChatTheme chat_id:int53 theme_name:string = Ok; diff --git a/td/telegram/BackgroundManager.cpp b/td/telegram/BackgroundManager.cpp index b48c6d81d..6f1cc4256 100644 --- a/td/telegram/BackgroundManager.cpp +++ b/td/telegram/BackgroundManager.cpp @@ -111,7 +111,8 @@ class SetChatWallPaperQuery final : public Td::ResultHandler { } void send(DialogId dialog_id, telegram_api::object_ptr input_wallpaper, - telegram_api::object_ptr settings, MessageId old_message_id) { + telegram_api::object_ptr settings, MessageId old_message_id, + bool for_both) { dialog_id_ = dialog_id; is_remove_ = input_wallpaper == nullptr && settings == nullptr; if (is_remove_) { @@ -132,6 +133,9 @@ class SetChatWallPaperQuery final : public Td::ResultHandler { if (old_message_id.is_valid()) { flags |= telegram_api::messages_setChatWallPaper::ID_MASK; } + if (for_both) { + flags |= telegram_api::messages_setChatWallPaper::FOR_BOTH_MASK; + } send_query(G()->net_query_creator().create(telegram_api::messages_setChatWallPaper( flags, false /*ignored*/, false /*ignored*/, std::move(input_peer), std::move(input_wallpaper), std::move(settings), old_message_id.get_server_message_id().get()))); @@ -722,7 +726,7 @@ void BackgroundManager::set_background(const td_api::InputBackground *input_back void BackgroundManager::set_dialog_background(DialogId dialog_id, const td_api::InputBackground *input_background, const td_api::BackgroundType *background_type, int32 dark_theme_dimming, - Promise &&promise) { + bool for_both, Promise &&promise) { if (!td_->messages_manager_->have_dialog_force(dialog_id, "set_dialog_background")) { return promise.set_error(Status::Error(400, "Chat not found")); } @@ -756,11 +760,11 @@ void BackgroundManager::set_dialog_background(DialogId dialog_id, const td_api:: return promise.set_error(Status::Error(400, "Input background must be non-empty for the background type")); } if (background_type == nullptr) { - return send_set_dialog_background_query(dialog_id, nullptr, nullptr, MessageId(), std::move(promise)); + return send_set_dialog_background_query(dialog_id, nullptr, nullptr, MessageId(), for_both, std::move(promise)); } else { - return send_set_dialog_background_query(dialog_id, - telegram_api::make_object(0), - type.get_input_wallpaper_settings(), MessageId(), std::move(promise)); + return send_set_dialog_background_query( + dialog_id, telegram_api::make_object(0), + type.get_input_wallpaper_settings(), MessageId(), for_both, std::move(promise)); } } @@ -778,17 +782,17 @@ void BackgroundManager::set_dialog_background(DialogId dialog_id, const td_api:: auto it = file_id_to_background_id_.find(file_id); if (it != file_id_to_background_id_.end()) { - return do_set_dialog_background(dialog_id, it->second, type, std::move(promise)); + return do_set_dialog_background(dialog_id, it->second, type, for_both, std::move(promise)); } auto upload_promise = - PromiseCreator::lambda([actor_id = actor_id(this), dialog_id, type, promise = std::move(promise)]( + PromiseCreator::lambda([actor_id = actor_id(this), dialog_id, type, for_both, promise = std::move(promise)]( Result> &&result) mutable { if (result.is_error()) { return promise.set_error(result.move_as_error()); } send_closure(actor_id, &BackgroundManager::do_set_dialog_background, dialog_id, - BackgroundId(result.ok()->id_), std::move(type), std::move(promise)); + BackgroundId(result.ok()->id_), std::move(type), for_both, std::move(promise)); }); upload_background_file(file_id, type, dialog_id, false, std::move(upload_promise)); break; @@ -796,7 +800,7 @@ void BackgroundManager::set_dialog_background(DialogId dialog_id, const td_api:: case td_api::inputBackgroundRemote::ID: { auto background_remote = static_cast(input_background); return do_set_dialog_background(dialog_id, BackgroundId(background_remote->background_id_), std::move(type), - std::move(promise)); + for_both, std::move(promise)); } case td_api::inputBackgroundPrevious::ID: { auto background_previous = static_cast(input_background); @@ -806,7 +810,7 @@ void BackgroundManager::set_dialog_background(DialogId dialog_id, const td_api:: } return send_set_dialog_background_query( dialog_id, nullptr, background_type == nullptr ? nullptr : type.get_input_wallpaper_settings(), message_id, - std::move(promise)); + for_both, std::move(promise)); } default: UNREACHABLE(); @@ -814,7 +818,7 @@ void BackgroundManager::set_dialog_background(DialogId dialog_id, const td_api:: } void BackgroundManager::do_set_dialog_background(DialogId dialog_id, BackgroundId background_id, BackgroundType type, - Promise &&promise) { + bool for_both, Promise &&promise) { TRY_STATUS_PROMISE(promise, G()->close_status()); const auto *background = get_background(background_id); if (background == nullptr) { @@ -828,15 +832,15 @@ void BackgroundManager::do_set_dialog_background(DialogId dialog_id, BackgroundI send_set_dialog_background_query( dialog_id, telegram_api::make_object(background_id.get(), background->access_hash), - type.get_input_wallpaper_settings(), MessageId(), std::move(promise)); + type.get_input_wallpaper_settings(), MessageId(), for_both, std::move(promise)); } void BackgroundManager::send_set_dialog_background_query( DialogId dialog_id, telegram_api::object_ptr input_wallpaper, - telegram_api::object_ptr settings, MessageId old_message_id, + telegram_api::object_ptr settings, MessageId old_message_id, bool for_both, Promise &&promise) { td_->create_handler(std::move(promise)) - ->send(dialog_id, std::move(input_wallpaper), std::move(settings), old_message_id); + ->send(dialog_id, std::move(input_wallpaper), std::move(settings), old_message_id, for_both); } void BackgroundManager::set_background(BackgroundId background_id, BackgroundType type, bool for_dark_theme, diff --git a/td/telegram/BackgroundManager.h b/td/telegram/BackgroundManager.h index 9741d6b05..6c7f010dc 100644 --- a/td/telegram/BackgroundManager.h +++ b/td/telegram/BackgroundManager.h @@ -49,7 +49,7 @@ class BackgroundManager final : public Actor { void reset_backgrounds(Promise &&promise); void set_dialog_background(DialogId dialog_id, const td_api::InputBackground *input_background, - const td_api::BackgroundType *background_type, int32 dark_theme_dimming, + const td_api::BackgroundType *background_type, int32 dark_theme_dimming, bool for_both, Promise &&promise); td_api::object_ptr get_background_object(BackgroundId background_id, bool for_dark_theme, @@ -140,13 +140,13 @@ class BackgroundManager final : public Actor { Result prepare_input_file(const tl_object_ptr &input_file); - void do_set_dialog_background(DialogId dialog_id, BackgroundId background_id, BackgroundType type, + void do_set_dialog_background(DialogId dialog_id, BackgroundId background_id, BackgroundType type, bool for_both, Promise &&promise); void send_set_dialog_background_query(DialogId dialog_id, telegram_api::object_ptr input_wallpaper, telegram_api::object_ptr settings, - MessageId old_message_id, Promise &&promise); + MessageId old_message_id, bool for_both, Promise &&promise); void set_background(BackgroundId background_id, BackgroundType type, bool for_dark_theme, Promise> &&promise); diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index c97fa2cad..459ab5935 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -6496,7 +6496,7 @@ void Td::on_request(uint64 id, td_api::setChatBackground &request) { CHECK_IS_USER(); CREATE_OK_REQUEST_PROMISE(); background_manager_->set_dialog_background(DialogId(request.chat_id_), request.background_.get(), request.type_.get(), - request.dark_theme_dimming_, std::move(promise)); + request.dark_theme_dimming_, !request.only_for_self_, std::move(promise)); } void Td::on_request(uint64 id, td_api::setChatTheme &request) { diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index dd4fb8fe3..63c22bfa8 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -3101,14 +3101,14 @@ class CliClient final : public Actor { send_request(td_api::make_object(background_id)); } else if (op == "rbgs") { send_request(td_api::make_object()); - } else if (op == "scbg") { + } else if (op == "scbg" || op == "scbgs") { ChatId chat_id; InputBackground input_background; BackgroundType background_type; int32 dark_theme_dimming; get_args(args, chat_id, input_background, background_type, dark_theme_dimming); send_request(td_api::make_object(chat_id, input_background, background_type, - dark_theme_dimming)); + dark_theme_dimming, op == "scbgs")); } else if (op == "gcos") { send_request(td_api::make_object()); } else if (op == "gcoc") {