Add td_api::setChatBackground.

This commit is contained in:
levlam 2023-04-11 17:46:55 +03:00
parent 0ce6dc6834
commit b130306ce7
7 changed files with 225 additions and 25 deletions

View File

@ -6900,6 +6900,13 @@ setChatMessageAutoDeleteTime chat_id:int53 message_auto_delete_time:int32 = Ok;
//@permissions New non-administrator members permissions in the chat
setChatPermissions chat_id:int53 permissions:chatPermissions = Ok;
//@description Changes the background in a specific chat. Supported only in private and secret chats with non-deleted users
//@chat_id Chat identifier
//@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_brightness Brightness of the background in dark themes, as a percentage; 1-100
setChatBackground chat_id:int53 background:InputBackground type:BackgroundType dark_theme_brightness:int32 = 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;
@ -7920,7 +7927,7 @@ getBackgroundUrl name:string type:BackgroundType = HttpUrl;
searchBackground name:string = Background;
//@description Changes the background selected by the user; adds background to the list of installed backgrounds
//@background The input background to use; pass null to create a new filled backgrounds or to remove the current background
//@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 use the default type of the remote background or to remove the current background
//@for_dark_theme Pass true if the background is changed for a dark theme
setBackground background:InputBackground type:BackgroundType for_dark_theme:Bool = Background;

View File

@ -8,6 +8,7 @@
#include "td/telegram/AuthManager.h"
#include "td/telegram/BackgroundType.hpp"
#include "td/telegram/ContactsManager.h"
#include "td/telegram/DialogId.h"
#include "td/telegram/Document.h"
#include "td/telegram/DocumentsManager.h"
@ -16,9 +17,11 @@
#include "td/telegram/files/FileManager.h"
#include "td/telegram/files/FileType.h"
#include "td/telegram/Global.h"
#include "td/telegram/MessagesManager.h"
#include "td/telegram/PhotoFormat.h"
#include "td/telegram/Td.h"
#include "td/telegram/TdDb.h"
#include "td/telegram/UpdatesManager.h"
#include "td/db/SqliteKeyValueAsync.h"
@ -96,6 +99,46 @@ class GetBackgroundsQuery final : public Td::ResultHandler {
}
};
class SetChatWallPaperQuery final : public Td::ResultHandler {
Promise<Unit> promise_;
public:
explicit SetChatWallPaperQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
}
void send(DialogId dialog_id, telegram_api::object_ptr<telegram_api::InputWallPaper> input_wallpaper,
telegram_api::object_ptr<telegram_api::wallPaperSettings> settings) {
int32 flags = 0;
auto input_peer = td_->messages_manager_->get_input_peer(dialog_id, AccessRights::Write);
if (input_peer == nullptr) {
return on_error(Status::Error(400, "Can't access the chat"));
}
if (input_wallpaper != nullptr) {
flags |= telegram_api::messages_setChatWallPaper::WALLPAPER_MASK;
}
if (settings != nullptr) {
flags |= telegram_api::messages_setChatWallPaper::SETTINGS_MASK;
}
send_query(G()->net_query_creator().create(telegram_api::messages_setChatWallPaper(
flags, std::move(input_peer), std::move(input_wallpaper), std::move(settings), 0)));
}
void on_result(BufferSlice packet) final {
auto result_ptr = fetch_result<telegram_api::messages_setChatWallPaper>(packet);
if (result_ptr.is_error()) {
return on_error(result_ptr.move_as_error());
}
auto ptr = result_ptr.move_as_ok();
LOG(INFO) << "Receive result for SetChatWallPaperQuery: " << to_string(ptr);
td_->updates_manager_->on_get_updates(std::move(ptr), std::move(promise_));
}
void on_error(Status status) final {
promise_.set_error(std::move(status));
}
};
class InstallBackgroundQuery final : public Td::ResultHandler {
Promise<Unit> promise_;
@ -127,6 +170,7 @@ class UploadBackgroundQuery final : public Td::ResultHandler {
Promise<td_api::object_ptr<td_api::background>> promise_;
FileId file_id_;
BackgroundType type_;
DialogId dialog_id_;
bool for_dark_theme_;
public:
@ -135,12 +179,16 @@ class UploadBackgroundQuery final : public Td::ResultHandler {
}
void send(FileId file_id, tl_object_ptr<telegram_api::InputFile> &&input_file, const BackgroundType &type,
bool for_dark_theme) {
DialogId dialog_id, bool for_dark_theme) {
CHECK(input_file != nullptr);
file_id_ = file_id;
type_ = type;
dialog_id_ = dialog_id;
for_dark_theme_ = for_dark_theme;
int32 flags = 0;
if (dialog_id.is_valid()) {
flags |= telegram_api::account_uploadWallPaper::FOR_CHAT_MASK;
}
send_query(G()->net_query_creator().create(telegram_api::account_uploadWallPaper(
flags, false /*ignored*/, std::move(input_file), type_.get_mime_type(), type_.get_input_wallpaper_settings())));
}
@ -151,8 +199,8 @@ class UploadBackgroundQuery final : public Td::ResultHandler {
return on_error(result_ptr.move_as_error());
}
td_->background_manager_->on_uploaded_background_file(file_id_, type_, for_dark_theme_, result_ptr.move_as_ok(),
std::move(promise_));
td_->background_manager_->on_uploaded_background_file(file_id_, type_, dialog_id_, for_dark_theme_,
result_ptr.move_as_ok(), std::move(promise_));
}
void on_error(Status status) final {
@ -627,11 +675,7 @@ void BackgroundManager::set_background(const td_api::InputBackground *input_back
CHECK(background_type != nullptr);
auto background_local = static_cast<const td_api::inputBackgroundLocal *>(input_background);
auto r_file_id = prepare_input_file(background_local->background_);
if (r_file_id.is_error()) {
return promise.set_error(r_file_id.move_as_error());
}
auto file_id = r_file_id.move_as_ok();
TRY_RESULT_PROMISE(promise, file_id, prepare_input_file(background_local->background_));
LOG(INFO) << "Receive file " << file_id << " for input background";
CHECK(file_id.is_valid());
@ -640,7 +684,7 @@ void BackgroundManager::set_background(const td_api::InputBackground *input_back
return set_background(it->second, type, for_dark_theme, std::move(promise));
}
upload_background_file(file_id, type, for_dark_theme, std::move(promise));
upload_background_file(file_id, type, DialogId(), for_dark_theme, std::move(promise));
break;
}
case td_api::inputBackgroundRemote::ID: {
@ -653,6 +697,114 @@ 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_brightness, Promise<Unit> &&promise) {
if (!td_->messages_manager_->have_dialog_force(dialog_id, "set_dialog_background")) {
return promise.set_error(Status::Error(400, "Chat not found"));
}
if (!td_->messages_manager_->have_input_peer(dialog_id, AccessRights::Write)) {
return promise.set_error(Status::Error(400, "Can't access the chat"));
}
switch (dialog_id.get_type()) {
case DialogType::User:
break;
case DialogType::Chat:
case DialogType::Channel:
return promise.set_error(Status::Error(400, "Can't change background in the chat"));
case DialogType::SecretChat: {
auto user_id = td_->contacts_manager_->get_secret_chat_user_id(dialog_id.get_secret_chat_id());
if (!user_id.is_valid()) {
return promise.set_error(Status::Error(400, "Can't access the user"));
}
dialog_id = DialogId(user_id);
break;
}
case DialogType::None:
default:
UNREACHABLE();
}
TRY_RESULT_PROMISE(promise, type, BackgroundType::get_background_type(background_type, dark_theme_brightness));
if (input_background == nullptr) {
if (type.has_file()) {
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, std::move(promise));
} else {
return send_set_dialog_background_query(dialog_id,
telegram_api::make_object<telegram_api::inputWallPaperNoFile>(0),
type.get_input_wallpaper_settings(), std::move(promise));
}
}
switch (input_background->get_id()) {
case td_api::inputBackgroundLocal::ID: {
if (!type.has_file()) {
return promise.set_error(Status::Error(400, "Can't specify local file for the background type"));
}
CHECK(background_type != nullptr);
auto background_local = static_cast<const td_api::inputBackgroundLocal *>(input_background);
TRY_RESULT_PROMISE(promise, file_id, prepare_input_file(background_local->background_));
LOG(INFO) << "Receive file " << file_id << " for input background";
CHECK(file_id.is_valid());
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));
}
auto upload_promise =
PromiseCreator::lambda([actor_id = actor_id(this), dialog_id, type, promise = std::move(promise)](
Result<td_api::object_ptr<td_api::background>> &&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));
});
upload_background_file(file_id, type, dialog_id, false, std::move(upload_promise));
break;
}
case td_api::inputBackgroundRemote::ID: {
auto background_remote = static_cast<const td_api::inputBackgroundRemote *>(input_background);
return do_set_dialog_background(dialog_id, BackgroundId(background_remote->background_id_), std::move(type),
std::move(promise));
}
default:
UNREACHABLE();
}
}
void BackgroundManager::do_set_dialog_background(DialogId dialog_id, BackgroundId background_id, BackgroundType type,
Promise<Unit> &&promise) {
TRY_STATUS_PROMISE(promise, G()->close_status());
const auto *background = get_background(background_id);
if (background == nullptr) {
return promise.set_error(Status::Error(400, "Background to set not found"));
}
if (!type.has_file()) {
type = background->type;
} else if (!background->type.has_equal_type(type)) {
return promise.set_error(Status::Error(400, "Background type mismatch"));
}
send_set_dialog_background_query(
dialog_id, telegram_api::make_object<telegram_api::inputWallPaper>(background_id.get(), background->access_hash),
type.get_input_wallpaper_settings(), std::move(promise));
}
void BackgroundManager::send_set_dialog_background_query(
DialogId dialog_id, telegram_api::object_ptr<telegram_api::InputWallPaper> input_wallpaper,
telegram_api::object_ptr<telegram_api::wallPaperSettings> settings, Promise<Unit> &&promise) {
td_->create_handler<SetChatWallPaperQuery>(std::move(promise))
->send(dialog_id, std::move(input_wallpaper), std::move(settings));
}
void BackgroundManager::set_background(BackgroundId background_id, BackgroundType type, bool for_dark_theme,
Promise<td_api::object_ptr<td_api::background>> &&promise) {
LOG(INFO) << "Set " << background_id << " with " << type;
@ -760,11 +912,13 @@ void BackgroundManager::save_local_backgrounds(bool for_dark_theme) {
}
}
void BackgroundManager::upload_background_file(FileId file_id, const BackgroundType &type, bool for_dark_theme,
void BackgroundManager::upload_background_file(FileId file_id, const BackgroundType &type, DialogId dialog_id,
bool for_dark_theme,
Promise<td_api::object_ptr<td_api::background>> &&promise) {
auto upload_file_id = td_->file_manager_->dup_file_id(file_id, "upload_background_file");
bool is_inserted =
being_uploaded_files_.emplace(upload_file_id, UploadedFileInfo(type, for_dark_theme, std::move(promise))).second;
bool is_inserted = being_uploaded_files_
.emplace(upload_file_id, UploadedFileInfo(type, dialog_id, for_dark_theme, std::move(promise)))
.second;
CHECK(is_inserted);
LOG(INFO) << "Ask to upload background file " << upload_file_id;
td_->file_manager_->upload(upload_file_id, upload_background_file_callback_, 1, 0);
@ -777,12 +931,13 @@ void BackgroundManager::on_upload_background_file(FileId file_id, tl_object_ptr<
CHECK(it != being_uploaded_files_.end());
auto type = it->second.type_;
auto dialog_id = it->second.dialog_id_;
auto for_dark_theme = it->second.for_dark_theme_;
auto promise = std::move(it->second.promise_);
being_uploaded_files_.erase(it);
do_upload_background_file(file_id, type, for_dark_theme, std::move(input_file), std::move(promise));
do_upload_background_file(file_id, type, dialog_id, for_dark_theme, std::move(input_file), std::move(promise));
}
void BackgroundManager::on_upload_background_file_error(FileId file_id, Status status) {
@ -805,24 +960,31 @@ void BackgroundManager::on_upload_background_file_error(FileId file_id, Status s
status.message())); // TODO CHECK that status has always a code
}
void BackgroundManager::do_upload_background_file(FileId file_id, const BackgroundType &type, bool for_dark_theme,
void BackgroundManager::do_upload_background_file(FileId file_id, const BackgroundType &type, DialogId dialog_id,
bool for_dark_theme,
tl_object_ptr<telegram_api::InputFile> &&input_file,
Promise<td_api::object_ptr<td_api::background>> &&promise) {
TRY_STATUS_PROMISE(promise, G()->close_status());
if (input_file == nullptr) {
FileView file_view = td_->file_manager_->get_file_view(file_id);
file_id = file_view.get_main_file_id();
auto it = file_id_to_background_id_.find(file_id);
if (it != file_id_to_background_id_.end()) {
if (dialog_id.is_valid()) {
return promise.set_value(get_background_object(it->second, for_dark_theme, nullptr));
}
return set_background(it->second, type, for_dark_theme, std::move(promise));
}
return promise.set_error(Status::Error(500, "Failed to reupload background"));
}
td_->create_handler<UploadBackgroundQuery>(std::move(promise))
->send(file_id, std::move(input_file), type, for_dark_theme);
->send(file_id, std::move(input_file), type, dialog_id, for_dark_theme);
}
void BackgroundManager::on_uploaded_background_file(FileId file_id, const BackgroundType &type, bool for_dark_theme,
void BackgroundManager::on_uploaded_background_file(FileId file_id, const BackgroundType &type, DialogId dialog_id,
bool for_dark_theme,
telegram_api::object_ptr<telegram_api::WallPaper> wallpaper,
Promise<td_api::object_ptr<td_api::background>> &&promise) {
CHECK(wallpaper != nullptr);
@ -843,7 +1005,9 @@ void BackgroundManager::on_uploaded_background_file(FileId file_id, const Backgr
return promise.set_error(Status::Error(500, "Receive wrong uploaded background without file"));
}
LOG_STATUS(td_->file_manager_->merge(background->file_id, file_id));
set_background_id(background_id, type, for_dark_theme);
if (!dialog_id.is_valid()) {
set_background_id(background_id, type, for_dark_theme);
}
promise.set_value(get_background_object(background_id, for_dark_theme, nullptr));
}

View File

@ -8,6 +8,7 @@
#include "td/telegram/BackgroundId.h"
#include "td/telegram/BackgroundType.h"
#include "td/telegram/DialogId.h"
#include "td/telegram/files/FileId.h"
#include "td/telegram/files/FileSourceId.h"
#include "td/telegram/logevent/LogEvent.h"
@ -46,6 +47,10 @@ class BackgroundManager final : public Actor {
void reset_backgrounds(Promise<Unit> &&promise);
void set_dialog_background(DialogId dialog_id, const td_api::InputBackground *input_background,
const td_api::BackgroundType *background_type, int32 dark_theme_brightness,
Promise<Unit> &&promise);
td_api::object_ptr<td_api::background> get_background_object(BackgroundId background_id, bool for_dark_theme,
const BackgroundType *type) const;
@ -55,7 +60,7 @@ class BackgroundManager final : public Actor {
FileSourceId get_background_file_source_id(BackgroundId background_id, int64 access_hash);
void on_uploaded_background_file(FileId file_id, const BackgroundType &type, bool for_dark_theme,
void on_uploaded_background_file(FileId file_id, const BackgroundType &type, DialogId dialog_id, bool for_dark_theme,
telegram_api::object_ptr<telegram_api::WallPaper> wallpaper,
Promise<td_api::object_ptr<td_api::background>> &&promise);
@ -134,6 +139,14 @@ class BackgroundManager final : public Actor {
Result<FileId> prepare_input_file(const tl_object_ptr<td_api::InputFile> &input_file);
void do_set_dialog_background(DialogId dialog_id, BackgroundId background_id, BackgroundType type,
Promise<Unit> &&promise);
void send_set_dialog_background_query(DialogId dialog_id,
telegram_api::object_ptr<telegram_api::InputWallPaper> input_wallpaper,
telegram_api::object_ptr<telegram_api::wallPaperSettings> settings,
Promise<Unit> &&promise);
void set_background(BackgroundId background_id, BackgroundType type, bool for_dark_theme,
Promise<td_api::object_ptr<td_api::background>> &&promise);
@ -146,14 +159,14 @@ class BackgroundManager final : public Actor {
void on_reset_background(Result<Unit> &&result, Promise<Unit> &&promise);
void upload_background_file(FileId file_id, const BackgroundType &type, bool for_dark_theme,
void upload_background_file(FileId file_id, const BackgroundType &type, DialogId dialog_id, bool for_dark_theme,
Promise<td_api::object_ptr<td_api::background>> &&promise);
void on_upload_background_file(FileId file_id, tl_object_ptr<telegram_api::InputFile> input_file);
void on_upload_background_file_error(FileId file_id, Status status);
void do_upload_background_file(FileId file_id, const BackgroundType &type, bool for_dark_theme,
void do_upload_background_file(FileId file_id, const BackgroundType &type, DialogId dialog_id, bool for_dark_theme,
tl_object_ptr<telegram_api::InputFile> &&input_file,
Promise<td_api::object_ptr<td_api::background>> &&promise);
@ -180,12 +193,13 @@ class BackgroundManager final : public Actor {
struct UploadedFileInfo {
BackgroundType type_;
DialogId dialog_id_;
bool for_dark_theme_;
Promise<td_api::object_ptr<td_api::background>> promise_;
UploadedFileInfo(BackgroundType type, bool for_dark_theme,
UploadedFileInfo(BackgroundType type, DialogId dialog_id, bool for_dark_theme,
Promise<td_api::object_ptr<td_api::background>> &&promise)
: type_(type), for_dark_theme_(for_dark_theme), promise_(std::move(promise)) {
: type_(type), dialog_id_(dialog_id), for_dark_theme_(for_dark_theme), promise_(std::move(promise)) {
}
};
FlatHashMap<FileId, UploadedFileInfo, FileIdHash> being_uploaded_files_;

View File

@ -444,8 +444,6 @@ td_api::object_ptr<td_api::BackgroundType> BackgroundType::get_background_type_o
}
telegram_api::object_ptr<telegram_api::wallPaperSettings> BackgroundType::get_input_wallpaper_settings() const {
CHECK(has_file());
int32 flags = 0;
if (is_blurred_) {
flags |= telegram_api::wallPaperSettings::BLUR_MASK;

View File

@ -6239,6 +6239,13 @@ void Td::on_request(uint64 id, const td_api::setChatPermissions &request) {
messages_manager_->set_dialog_permissions(DialogId(request.chat_id_), request.permissions_, std::move(promise));
}
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_brightness_, std::move(promise));
}
void Td::on_request(uint64 id, td_api::setChatTheme &request) {
CHECK_IS_USER();
CLEAN_INPUT_STRING(request.theme_name_);

View File

@ -955,6 +955,8 @@ class Td final : public Actor {
void on_request(uint64 id, const td_api::setChatPermissions &request);
void on_request(uint64 id, td_api::setChatBackground &request);
void on_request(uint64 id, td_api::setChatTheme &request);
void on_request(uint64 id, td_api::setChatDraftMessage &request);

View File

@ -2792,6 +2792,14 @@ class CliClient final : public Actor {
send_request(td_api::make_object<td_api::removeBackground>(background_id));
} else if (op == "rbgs") {
send_request(td_api::make_object<td_api::resetBackgrounds>());
} else if (op == "scbg") {
ChatId chat_id;
InputBackground input_background;
BackgroundType background_type;
int32 dark_theme_brightness;
get_args(args, chat_id, input_background, background_type, dark_theme_brightness);
send_request(td_api::make_object<td_api::setChatBackground>(chat_id, input_background, background_type,
dark_theme_brightness));
} else if (op == "gcos") {
send_request(td_api::make_object<td_api::getCountries>());
} else if (op == "gcoc") {