From 1ee924aeb2caad5e65b8657c9356cb561159c322 Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 15 Dec 2022 18:12:30 +0300 Subject: [PATCH] Add suggestUserPersonalProfilePhoto. --- td/generate/scheme/td_api.tl | 3 +++ td/telegram/ContactsManager.cpp | 40 +++++++++++++++++++-------------- td/telegram/ContactsManager.h | 12 +++++----- td/telegram/Td.cpp | 8 ++++++- td/telegram/Td.h | 2 ++ td/telegram/cli.cpp | 28 +++++++++++++++++++++++ 6 files changed, 70 insertions(+), 23 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 03d9d80f8..a0f4394e1 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -7018,6 +7018,9 @@ clearImportedContacts = Ok; //@description Changes a personal profile photo of a contact user @user_id User identifier @photo Profile photo to set; inputChatPhotoPrevious isn't supported in this function setUserPersonalProfilePhoto user_id:int53 photo:InputChatPhoto = Ok; +//@description Suggests a personal profile photo to a user with common messages @user_id User identifier @photo Profile photo to suggest; inputChatPhotoPrevious isn't supported in this function +suggestUserPersonalProfilePhoto user_id:int53 photo:InputChatPhoto = Ok; + //@description Searches a user by their phone number. Returns a 404 error if the user can't be found @phone_number Phone number to search for searchUserByPhoneNumber phone_number:string = User; diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index 8aa733cd2..7fba616ab 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -446,7 +446,7 @@ class UploadProfilePhotoQuery final : public Td::ResultHandler { } void send(UserId user_id, FileId file_id, tl_object_ptr &&input_file, bool is_fallback, - bool is_animation, double main_frame_timestamp) { + bool only_suggest, bool is_animation, double main_frame_timestamp) { CHECK(input_file != nullptr); CHECK(file_id.is_valid()); @@ -487,7 +487,11 @@ class UploadProfilePhotoQuery final : public Td::ResultHandler { std::move(video_input_file), main_frame_timestamp), {{"me"}})); } else { - flags |= telegram_api::photos_uploadContactProfilePhoto::SAVE_MASK; + if (only_suggest) { + flags |= telegram_api::photos_uploadContactProfilePhoto::SUGGEST_MASK; + } else { + flags |= telegram_api::photos_uploadContactProfilePhoto::SAVE_MASK; + } auto r_input_user = td_->contacts_manager_->get_input_user(user_id); if (r_input_user.is_error()) { return on_error(r_input_user.move_as_error()); @@ -6924,12 +6928,12 @@ FileId ContactsManager::get_profile_photo_file_id(int64 photo_id) const { void ContactsManager::set_profile_photo(const td_api::object_ptr &input_photo, bool is_fallback, Promise &&promise) { - set_profile_photo_impl(get_my_id(), input_photo, is_fallback, std::move(promise)); + set_profile_photo_impl(get_my_id(), input_photo, is_fallback, false, std::move(promise)); } void ContactsManager::set_profile_photo_impl(UserId user_id, const td_api::object_ptr &input_photo, - bool is_fallback, Promise &&promise) { + bool is_fallback, bool only_suggest, Promise &&promise) { if (input_photo == nullptr) { return promise.set_error(Status::Error(400, "New profile photo must be non-empty")); } @@ -6988,12 +6992,12 @@ void ContactsManager::set_profile_photo_impl(UserId user_id, CHECK(file_id.is_valid()); upload_profile_photo(user_id, td_->file_manager_->dup_file_id(file_id, "set_profile_photo_impl"), is_fallback, - is_animation, main_frame_timestamp, std::move(promise)); + only_suggest, is_animation, main_frame_timestamp, std::move(promise)); } void ContactsManager::set_user_profile_photo(UserId user_id, const td_api::object_ptr &input_photo, - Promise &&promise) { + bool only_suggest, Promise &&promise) { auto r_input_user = get_input_user(user_id); if (r_input_user.is_error()) { return promise.set_error(r_input_user.move_as_error()); @@ -7005,7 +7009,7 @@ void ContactsManager::set_user_profile_photo(UserId user_id, return promise.set_error(Status::Error(400, "Can't set personal photo to self")); } - set_profile_photo_impl(user_id, input_photo, false, std::move(promise)); + set_profile_photo_impl(user_id, input_photo, false, only_suggest, std::move(promise)); } void ContactsManager::send_update_profile_photo_query(FileId file_id, int64 old_photo_id, bool is_fallback, @@ -7015,14 +7019,15 @@ void ContactsManager::send_update_profile_photo_query(FileId file_id, int64 old_ ->send(file_id, old_photo_id, is_fallback, file_view.main_remote_location().as_input_photo()); } -void ContactsManager::upload_profile_photo(UserId user_id, FileId file_id, bool is_fallback, bool is_animation, - double main_frame_timestamp, Promise &&promise, int reupload_count, - vector bad_parts) { +void ContactsManager::upload_profile_photo(UserId user_id, FileId file_id, bool is_fallback, bool only_suggest, + bool is_animation, double main_frame_timestamp, Promise &&promise, + int reupload_count, vector bad_parts) { CHECK(file_id.is_valid()); - bool is_inserted = uploaded_profile_photos_ - .emplace(file_id, UploadedProfilePhoto{user_id, is_fallback, main_frame_timestamp, - is_animation, reupload_count, std::move(promise)}) - .second; + bool is_inserted = + uploaded_profile_photos_ + .emplace(file_id, UploadedProfilePhoto{user_id, is_fallback, only_suggest, main_frame_timestamp, is_animation, + reupload_count, std::move(promise)}) + .second; CHECK(is_inserted); LOG(INFO) << "Ask to upload " << (is_animation ? "animated" : "static") << " profile photo " << file_id << " for user " << user_id << " with bad parts " << bad_parts; @@ -17679,6 +17684,7 @@ void ContactsManager::on_upload_profile_photo(FileId file_id, tl_object_ptrsecond.user_id; bool is_fallback = it->second.is_fallback; + bool only_suggest = it->second.only_suggest; double main_frame_timestamp = it->second.main_frame_timestamp; bool is_animation = it->second.is_animation; int32 reupload_count = it->second.reupload_count; @@ -17709,14 +17715,14 @@ void ContactsManager::on_upload_profile_photo(FileId file_id, tl_object_ptrfile_manager_->delete_file_reference(file_id, file_reference); - upload_profile_photo(user_id, file_id, is_fallback, is_animation, main_frame_timestamp, std::move(promise), - reupload_count + 1, {-1}); + upload_profile_photo(user_id, file_id, is_fallback, only_suggest, is_animation, main_frame_timestamp, + std::move(promise), reupload_count + 1, {-1}); return; } CHECK(input_file != nullptr); td_->create_handler(std::move(promise)) - ->send(user_id, file_id, std::move(input_file), is_fallback, is_animation, main_frame_timestamp); + ->send(user_id, file_id, std::move(input_file), is_fallback, only_suggest, is_animation, main_frame_timestamp); } void ContactsManager::on_upload_profile_photo_error(FileId file_id, Status status) { diff --git a/td/telegram/ContactsManager.h b/td/telegram/ContactsManager.h index 1b00a3974..37b203001 100644 --- a/td/telegram/ContactsManager.h +++ b/td/telegram/ContactsManager.h @@ -367,7 +367,7 @@ class ContactsManager final : public Actor { Promise &&promise); void set_user_profile_photo(UserId user_id, const td_api::object_ptr &input_photo, - Promise &&promise); + bool only_suggest, Promise &&promise); void send_update_profile_photo_query(FileId file_id, int64 old_photo_id, bool is_fallback, Promise &&promise); @@ -1345,9 +1345,9 @@ class ContactsManager final : public Actor { void apply_pending_user_photo(User *u, UserId user_id); void set_profile_photo_impl(UserId user_id, const td_api::object_ptr &input_photo, - bool is_fallback, Promise &&promise); + bool is_fallback, bool only_suggest, Promise &&promise); - void upload_profile_photo(UserId user_id, FileId file_id, bool is_fallback, bool is_animation, + void upload_profile_photo(UserId user_id, FileId file_id, bool is_fallback, bool only_suggest, bool is_animation, double main_frame_timestamp, Promise &&promise, int reupload_count = 0, vector bad_parts = {}); @@ -1877,15 +1877,17 @@ class ContactsManager final : public Actor { struct UploadedProfilePhoto { UserId user_id; bool is_fallback; + bool only_suggest; double main_frame_timestamp; bool is_animation; int reupload_count; Promise promise; - UploadedProfilePhoto(UserId user_id, bool is_fallback, double main_frame_timestamp, bool is_animation, - int32 reupload_count, Promise promise) + UploadedProfilePhoto(UserId user_id, bool is_fallback, bool only_suggest, double main_frame_timestamp, + bool is_animation, int32 reupload_count, Promise promise) : user_id(user_id) , is_fallback(is_fallback) + , only_suggest(only_suggest) , main_frame_timestamp(main_frame_timestamp) , is_animation(is_animation) , reupload_count(reupload_count) diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 6ff37ca55..21d3b5839 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -6826,7 +6826,13 @@ void Td::on_request(uint64 id, const td_api::clearImportedContacts &request) { void Td::on_request(uint64 id, td_api::setUserPersonalProfilePhoto &request) { CHECK_IS_USER(); CREATE_OK_REQUEST_PROMISE(); - contacts_manager_->set_user_profile_photo(UserId(request.user_id_), request.photo_, std::move(promise)); + contacts_manager_->set_user_profile_photo(UserId(request.user_id_), request.photo_, false, std::move(promise)); +} + +void Td::on_request(uint64 id, td_api::suggestUserPersonalProfilePhoto &request) { + CHECK_IS_USER(); + CREATE_OK_REQUEST_PROMISE(); + contacts_manager_->set_user_profile_photo(UserId(request.user_id_), request.photo_, true, std::move(promise)); } void Td::on_request(uint64 id, td_api::searchUserByPhoneNumber &request) { diff --git a/td/telegram/Td.h b/td/telegram/Td.h index 0d82f7613..659b0e10d 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -1054,6 +1054,8 @@ class Td final : public Actor { void on_request(uint64 id, td_api::setUserPersonalProfilePhoto &request); + void on_request(uint64 id, td_api::suggestUserPersonalProfilePhoto &request); + void on_request(uint64 id, td_api::searchUserByPhoneNumber &request); void on_request(uint64 id, const td_api::sharePhoneNumber &request); diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index daa24e290..8ff2bef12 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -4815,6 +4815,34 @@ class CliClient final : public Actor { td_api::make_object(td_api::make_object( as_input_file(animation), to_double(main_frame_timestamp)), op == "sppaf")); + } else if (op == "suppp") { + UserId user_id; + string photo; + get_args(args, user_id, photo); + send_request(td_api::make_object( + user_id, td_api::make_object(as_input_file(photo)))); + } else if (op == "supppa") { + UserId user_id; + string animation; + string main_frame_timestamp; + get_args(args, user_id, animation, main_frame_timestamp); + send_request(td_api::make_object( + user_id, td_api::make_object(as_input_file(animation), + to_double(main_frame_timestamp)))); + } else if (op == "suuppp") { + UserId user_id; + string photo; + get_args(args, user_id, photo); + send_request(td_api::make_object( + user_id, td_api::make_object(as_input_file(photo)))); + } else if (op == "suupppa") { + UserId user_id; + string animation; + string main_frame_timestamp; + get_args(args, user_id, animation, main_frame_timestamp); + send_request(td_api::make_object( + user_id, td_api::make_object(as_input_file(animation), + to_double(main_frame_timestamp)))); } else if (op == "sh") { const string &prefix = args; send_request(td_api::make_object(prefix, 10));