Add td_api::setUserPersonalProfilePhoto.

This commit is contained in:
levlam 2022-12-13 14:16:49 +03:00
parent 0db6fe533b
commit b2c838f7bb
6 changed files with 123 additions and 38 deletions

View File

@ -6244,6 +6244,9 @@ changeImportedContacts contacts:vector<contact> = ImportedContacts;
//@description Clears all imported contacts, contact list remains unchanged
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 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;

View File

@ -13,6 +13,7 @@
#include "td/telegram/FullMessageId.h"
#include "td/telegram/MessageContentType.h"
#include "td/telegram/PollId.h"
#include "td/telegram/UserId.h"
#include "td/utils/common.h"
#include "td/utils/HashTableUtils.h"
@ -49,6 +50,9 @@ class ChainId {
ChainId(const string &str) : id(Hash<string>()(str)) {
}
ChainId(UserId user_id) : ChainId(DialogId(user_id)) {
}
uint64 get() const {
return id;
}

View File

@ -437,19 +437,31 @@ class SearchDialogsNearbyQuery final : public Td::ResultHandler {
class UploadProfilePhotoQuery final : public Td::ResultHandler {
Promise<Unit> promise_;
UserId user_id_;
FileId file_id_;
public:
explicit UploadProfilePhotoQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
}
void send(FileId file_id, tl_object_ptr<telegram_api::InputFile> &&input_file, bool is_animation,
void send(UserId user_id, FileId file_id, tl_object_ptr<telegram_api::InputFile> &&input_file, bool is_animation,
double main_frame_timestamp) {
CHECK(input_file != nullptr);
CHECK(file_id.is_valid());
user_id_ = user_id;
file_id_ = file_id;
static_assert(telegram_api::photos_uploadProfilePhoto::VIDEO_MASK ==
telegram_api::photos_uploadContactProfilePhoto::VIDEO_MASK,
"");
static_assert(telegram_api::photos_uploadProfilePhoto::VIDEO_START_TS_MASK ==
telegram_api::photos_uploadContactProfilePhoto::VIDEO_START_TS_MASK,
"");
static_assert(
telegram_api::photos_uploadProfilePhoto::FILE_MASK == telegram_api::photos_uploadContactProfilePhoto::FILE_MASK,
"");
int32 flags = 0;
tl_object_ptr<telegram_api::InputFile> photo_input_file;
tl_object_ptr<telegram_api::InputFile> video_input_file;
@ -464,19 +476,35 @@ class UploadProfilePhotoQuery final : public Td::ResultHandler {
flags |= telegram_api::photos_uploadProfilePhoto::FILE_MASK;
photo_input_file = std::move(input_file);
}
send_query(G()->net_query_creator().create(
telegram_api::photos_uploadProfilePhoto(flags, false /*ignored*/, std::move(photo_input_file),
std::move(video_input_file), main_frame_timestamp),
{{"me"}}));
if (user_id == td_->contacts_manager_->get_my_id()) {
send_query(G()->net_query_creator().create(
telegram_api::photos_uploadProfilePhoto(flags, false /*ignored*/, std::move(photo_input_file),
std::move(video_input_file), main_frame_timestamp),
{{"me"}}));
} 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());
}
send_query(G()->net_query_creator().create(
telegram_api::photos_uploadContactProfilePhoto(flags, false /*ignored*/, false /*ignored*/,
r_input_user.move_as_ok(), std::move(photo_input_file),
std::move(video_input_file), main_frame_timestamp),
{{user_id}}));
}
}
void on_result(BufferSlice packet) final {
static_assert(std::is_same<telegram_api::photos_uploadProfilePhoto::ReturnType,
telegram_api::photos_uploadContactProfilePhoto::ReturnType>::value,
"");
auto result_ptr = fetch_result<telegram_api::photos_uploadProfilePhoto>(packet);
if (result_ptr.is_error()) {
return on_error(result_ptr.move_as_error());
}
td_->contacts_manager_->on_set_profile_photo(result_ptr.move_as_ok(), 0);
td_->contacts_manager_->on_set_profile_photo(user_id_, result_ptr.move_as_ok(), 0);
td_->file_manager_->delete_partial_remote_location(file_id_);
@ -486,7 +514,6 @@ class UploadProfilePhotoQuery final : public Td::ResultHandler {
void on_error(Status status) final {
promise_.set_error(std::move(status));
td_->file_manager_->delete_partial_remote_location(file_id_);
td_->updates_manager_->get_difference("UploadProfilePhotoQuery");
}
};
@ -516,7 +543,8 @@ class UpdateProfilePhotoQuery final : public Td::ResultHandler {
return on_error(result_ptr.move_as_error());
}
td_->contacts_manager_->on_set_profile_photo(result_ptr.move_as_ok(), old_photo_id_);
td_->contacts_manager_->on_set_profile_photo(td_->contacts_manager_->get_my_id(), result_ptr.move_as_ok(),
old_photo_id_);
promise_.set_value(Unit());
}
@ -6874,6 +6902,12 @@ FileId ContactsManager::get_profile_photo_file_id(int64 photo_id) const {
void ContactsManager::set_profile_photo(const td_api::object_ptr<td_api::InputChatPhoto> &input_photo,
Promise<Unit> &&promise) {
set_profile_photo_impl(get_my_id(), input_photo, std::move(promise));
}
void ContactsManager::set_profile_photo_impl(UserId user_id,
const td_api::object_ptr<td_api::InputChatPhoto> &input_photo,
Promise<Unit> &&promise) {
if (input_photo == nullptr) {
return promise.set_error(Status::Error(400, "New profile photo must be non-empty"));
}
@ -6883,9 +6917,12 @@ void ContactsManager::set_profile_photo(const td_api::object_ptr<td_api::InputCh
bool is_animation = false;
switch (input_photo->get_id()) {
case td_api::inputChatPhotoPrevious::ID: {
if (user_id != get_my_id()) {
return promise.set_error(Status::Error(400, "Can't use inputChatPhotoPrevious"));
}
auto photo = static_cast<const td_api::inputChatPhotoPrevious *>(input_photo.get());
auto photo_id = photo->chat_photo_id_;
auto *u = get_user(get_my_id());
auto *u = get_user(user_id);
if (u != nullptr && u->photo.id > 0 && photo_id == u->photo.id) {
return promise.set_value(Unit());
}
@ -6894,8 +6931,8 @@ void ContactsManager::set_profile_photo(const td_api::object_ptr<td_api::InputCh
if (!file_id.is_valid()) {
return promise.set_error(Status::Error(400, "Unknown profile photo ID specified"));
}
return send_update_profile_photo_query(td_->file_manager_->dup_file_id(file_id, "set_profile_photo"), photo_id,
std::move(promise));
return send_update_profile_photo_query(td_->file_manager_->dup_file_id(file_id, "set_profile_photo_impl"),
photo_id, std::move(promise));
}
case td_api::inputChatPhotoStatic::ID: {
auto photo = static_cast<const td_api::inputChatPhotoStatic *>(input_photo.get());
@ -6920,7 +6957,7 @@ void ContactsManager::set_profile_photo(const td_api::object_ptr<td_api::InputCh
}
auto file_type = is_animation ? FileType::Animation : FileType::Photo;
auto r_file_id = td_->file_manager_->get_input_file_id(file_type, *input_file, DialogId(get_my_id()), false, false);
auto r_file_id = td_->file_manager_->get_input_file_id(file_type, *input_file, DialogId(user_id), false, false);
if (r_file_id.is_error()) {
// TODO promise.set_error(std::move(status));
return promise.set_error(Status::Error(400, r_file_id.error().message()));
@ -6928,26 +6965,44 @@ void ContactsManager::set_profile_photo(const td_api::object_ptr<td_api::InputCh
FileId file_id = r_file_id.ok();
CHECK(file_id.is_valid());
upload_profile_photo(td_->file_manager_->dup_file_id(file_id, "set_profile_photo"), is_animation,
upload_profile_photo(user_id, td_->file_manager_->dup_file_id(file_id, "set_profile_photo_impl"), is_animation,
main_frame_timestamp, std::move(promise));
}
void ContactsManager::set_user_profile_photo(UserId user_id,
const td_api::object_ptr<td_api::InputChatPhoto> &input_photo,
Promise<Unit> &&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());
}
if (!is_user_contact(user_id)) {
return promise.set_error(Status::Error(400, "User isn't a contact"));
}
if (user_id == get_my_id()) {
return promise.set_error(Status::Error(400, "Can't set personal photo to self"));
}
set_profile_photo_impl(user_id, input_photo, std::move(promise));
}
void ContactsManager::send_update_profile_photo_query(FileId file_id, int64 old_photo_id, Promise<Unit> &&promise) {
FileView file_view = td_->file_manager_->get_file_view(file_id);
td_->create_handler<UpdateProfilePhotoQuery>(std::move(promise))
->send(file_id, old_photo_id, file_view.main_remote_location().as_input_photo());
}
void ContactsManager::upload_profile_photo(FileId file_id, bool is_animation, double main_frame_timestamp,
Promise<Unit> &&promise, int reupload_count, vector<int> bad_parts) {
void ContactsManager::upload_profile_photo(UserId user_id, FileId file_id, bool is_animation,
double main_frame_timestamp, Promise<Unit> &&promise, int reupload_count,
vector<int> bad_parts) {
CHECK(file_id.is_valid());
bool is_inserted = uploaded_profile_photos_
.emplace(file_id, UploadedProfilePhoto{main_frame_timestamp, is_animation, reupload_count,
std::move(promise)})
.emplace(file_id, UploadedProfilePhoto{user_id, 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
<< " with bad parts " << bad_parts;
<< " for user " << user_id << " with bad parts " << bad_parts;
// TODO use force_reupload if reupload_count >= 1, replace reupload_count with is_reupload
td_->file_manager_->resume_upload(file_id, std::move(bad_parts), upload_profile_photo_callback_, 32, 0);
}
@ -12778,21 +12833,24 @@ void ContactsManager::on_ignored_restriction_reasons_changed() {
});
}
void ContactsManager::on_set_profile_photo(tl_object_ptr<telegram_api::photos_photo> &&photo, int64 old_photo_id) {
void ContactsManager::on_set_profile_photo(UserId user_id, tl_object_ptr<telegram_api::photos_photo> &&photo,
int64 old_photo_id) {
LOG(INFO) << "Changed profile photo to " << to_string(photo);
UserId my_user_id = get_my_id();
delete_profile_photo_from_cache(my_user_id, old_photo_id, false);
add_set_profile_photo_to_cache(my_user_id,
get_photo(td_->file_manager_.get(), std::move(photo->photo_), DialogId(my_user_id)));
User *u = get_user(my_user_id);
if (u != nullptr) {
update_user(u, my_user_id);
bool is_my = (user_id == get_my_id());
if (is_my) {
delete_profile_photo_from_cache(user_id, old_photo_id, false);
}
auto *user_full = get_user_full(my_user_id);
add_set_profile_photo_to_cache(user_id,
get_photo(td_->file_manager_.get(), std::move(photo->photo_), DialogId(user_id)));
User *u = get_user(user_id);
if (u != nullptr) {
update_user(u, user_id);
}
auto *user_full = get_user_full(user_id);
if (user_full != nullptr) {
update_user_full(user_full, my_user_id, "on_set_profile_photo");
update_user_full(user_full, user_id, "on_set_profile_photo");
}
// if cache was correctly updated, this should produce no updates
@ -17559,6 +17617,7 @@ void ContactsManager::on_upload_profile_photo(FileId file_id, tl_object_ptr<tele
auto it = uploaded_profile_photos_.find(file_id);
CHECK(it != uploaded_profile_photos_.end());
UserId user_id = it->second.user_id;
double main_frame_timestamp = it->second.main_frame_timestamp;
bool is_animation = it->second.is_animation;
int32 reupload_count = it->second.reupload_count;
@ -17566,8 +17625,8 @@ void ContactsManager::on_upload_profile_photo(FileId file_id, tl_object_ptr<tele
uploaded_profile_photos_.erase(it);
LOG(INFO) << "Uploaded " << (is_animation ? "animated" : "static") << " profile photo " << file_id
<< " with reupload_count = " << reupload_count;
LOG(INFO) << "Uploaded " << (is_animation ? "animated" : "static") << " profile photo " << file_id << " for "
<< user_id << " with reupload_count = " << reupload_count;
FileView file_view = td_->file_manager_->get_file_view(file_id);
if (file_view.has_remote_location() && input_file == nullptr) {
if (file_view.main_remote_location().is_web()) {
@ -17589,13 +17648,14 @@ void ContactsManager::on_upload_profile_photo(FileId file_id, tl_object_ptr<tele
is_animation ? FileManager::extract_file_reference(file_view.main_remote_location().as_input_document())
: FileManager::extract_file_reference(file_view.main_remote_location().as_input_photo());
td_->file_manager_->delete_file_reference(file_id, file_reference);
upload_profile_photo(file_id, is_animation, main_frame_timestamp, std::move(promise), reupload_count + 1, {-1});
upload_profile_photo(user_id, file_id, is_animation, main_frame_timestamp, std::move(promise), reupload_count + 1,
{-1});
return;
}
CHECK(input_file != nullptr);
td_->create_handler<UploadProfilePhotoQuery>(std::move(promise))
->send(file_id, std::move(input_file), is_animation, main_frame_timestamp);
->send(user_id, file_id, std::move(input_file), is_animation, main_frame_timestamp);
}
void ContactsManager::on_upload_profile_photo_error(FileId file_id, Status status) {

View File

@ -188,7 +188,8 @@ class ContactsManager final : public Actor {
void on_update_user_common_chat_count(UserId user_id, int32 common_chat_count);
void on_update_user_need_phone_number_privacy_exception(UserId user_id, bool need_phone_number_privacy_exception);
void on_set_profile_photo(tl_object_ptr<telegram_api::photos_photo> &&photo, int64 old_photo_id);
void on_set_profile_photo(UserId user_id, tl_object_ptr<telegram_api::photos_photo> &&photo, int64 old_photo_id);
void on_delete_profile_photo(int64 profile_photo_id, Promise<Unit> promise);
void on_ignored_restriction_reasons_changed();
@ -363,6 +364,9 @@ class ContactsManager final : public Actor {
void set_profile_photo(const td_api::object_ptr<td_api::InputChatPhoto> &input_photo, Promise<Unit> &&promise);
void set_user_profile_photo(UserId user_id, const td_api::object_ptr<td_api::InputChatPhoto> &input_photo,
Promise<Unit> &&promise);
void send_update_profile_photo_query(FileId file_id, int64 old_photo_id, Promise<Unit> &&promise);
void delete_profile_photo(int64 profile_photo_id, Promise<Unit> &&promise);
@ -1336,8 +1340,11 @@ class ContactsManager final : public Actor {
const char *source);
void apply_pending_user_photo(User *u, UserId user_id);
void upload_profile_photo(FileId file_id, bool is_animation, double main_frame_timestamp, Promise<Unit> &&promise,
int reupload_count = 0, vector<int> bad_parts = {});
void set_profile_photo_impl(UserId user_id, const td_api::object_ptr<td_api::InputChatPhoto> &input_photo,
Promise<Unit> &&promise);
void upload_profile_photo(UserId user_id, FileId file_id, bool is_animation, double main_frame_timestamp,
Promise<Unit> &&promise, int reupload_count = 0, vector<int> bad_parts = {});
void on_upload_profile_photo(FileId file_id, tl_object_ptr<telegram_api::InputFile> input_file);
void on_upload_profile_photo_error(FileId file_id, Status status);
@ -1862,13 +1869,16 @@ class ContactsManager final : public Actor {
std::shared_ptr<UploadProfilePhotoCallback> upload_profile_photo_callback_;
struct UploadedProfilePhoto {
UserId user_id;
double main_frame_timestamp;
bool is_animation;
int reupload_count;
Promise<Unit> promise;
UploadedProfilePhoto(double main_frame_timestamp, bool is_animation, int32 reupload_count, Promise<Unit> promise)
: main_frame_timestamp(main_frame_timestamp)
UploadedProfilePhoto(UserId user_id, double main_frame_timestamp, bool is_animation, int32 reupload_count,
Promise<Unit> promise)
: user_id(user_id)
, main_frame_timestamp(main_frame_timestamp)
, is_animation(is_animation)
, reupload_count(reupload_count)
, promise(std::move(promise)) {

View File

@ -6823,6 +6823,12 @@ void Td::on_request(uint64 id, const td_api::clearImportedContacts &request) {
contacts_manager_->clear_imported_contacts(std::move(promise));
}
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));
}
void Td::on_request(uint64 id, td_api::searchUserByPhoneNumber &request) {
CHECK_IS_USER();
CLEAN_INPUT_STRING(request.phone_number_);

View File

@ -1052,6 +1052,8 @@ class Td final : public Actor {
void on_request(uint64 id, const td_api::clearImportedContacts &request);
void on_request(uint64 id, td_api::setUserPersonalProfilePhoto &request);
void on_request(uint64 id, td_api::searchUserByPhoneNumber &request);
void on_request(uint64 id, const td_api::sharePhoneNumber &request);