Support stickers in setChatPhoto.

This commit is contained in:
levlam 2023-01-23 13:53:23 +03:00
parent 7d2442f57f
commit a7e4c54f63
5 changed files with 81 additions and 43 deletions

View File

@ -6587,7 +6587,8 @@ setChatTitle chat_id:int53 title:string = Ok;
//@description Changes the photo of a chat. Supported only for basic groups, supergroups and channels. Requires can_change_info administrator right
//@chat_id Chat identifier
//@photo New chat photo; pass null to delete the chat photo
setChatPhoto chat_id:int53 photo:InputChatPhoto = Ok;
//@sticker Sticker-based version of the chat photo; pass null if none or an existing photo is set
setChatPhoto chat_id:int53 photo:InputChatPhoto sticker:chatPhotoSticker = Ok;
//@description Changes the message auto-delete or self-destruct (for secret chats) time in a chat. Requires change_info administrator right in basic groups, supergroups and channels
//-Message auto-delete time can't be changed in a chat with the current user (Saved Messages) and the chat 777000 (Telegram).

View File

@ -1315,7 +1315,8 @@ class EditDialogPhotoQuery final : public Td::ResultHandler {
if (file_id_.is_valid() && !was_uploaded_) {
VLOG(file_references) << "Receive " << status << " for " << file_id_;
td_->file_manager_->delete_file_reference(file_id_, file_reference_);
td_->messages_manager_->upload_dialog_photo(dialog_id_, file_id_, false, 0.0, false, std::move(promise_), {-1});
td_->messages_manager_->upload_dialog_photo(dialog_id_, file_id_, nullptr, false, 0.0, false,
std::move(promise_), {-1});
return;
} else {
LOG(ERROR) << "Receive file reference error, but file_id = " << file_id_
@ -9490,6 +9491,7 @@ void MessagesManager::on_upload_dialog_photo(FileId file_id, tl_object_ptr<teleg
}
DialogId dialog_id = it->second.dialog_id;
auto sticker_photo_size = std::move(it->second.sticker_photo_size);
double main_frame_timestamp = it->second.main_frame_timestamp;
bool is_animation = it->second.is_animation;
bool is_reupload = it->second.is_reupload;
@ -9512,7 +9514,8 @@ void MessagesManager::on_upload_dialog_photo(FileId file_id, tl_object_ptr<teleg
// delete file reference and forcely reupload the file
auto file_reference = FileManager::extract_file_reference(file_view.main_remote_location().as_input_document());
td_->file_manager_->delete_file_reference(file_id, file_reference);
upload_dialog_photo(dialog_id, file_id, is_animation, main_frame_timestamp, true, std::move(promise), {-1});
upload_dialog_photo(dialog_id, file_id, std::move(sticker_photo_size), is_animation, main_frame_timestamp, true,
std::move(promise), {-1});
} else {
CHECK(file_view.get_type() == FileType::Photo);
auto input_photo = file_view.main_remote_location().as_input_photo();
@ -9537,9 +9540,15 @@ void MessagesManager::on_upload_dialog_photo(FileId file_id, tl_object_ptr<teleg
flags |= telegram_api::inputChatUploadedPhoto::FILE_MASK;
photo_input_file = std::move(input_file);
}
auto video_emoji_markup =
sticker_photo_size != nullptr ? sticker_photo_size->get_input_video_size_object(td_) : nullptr;
if (video_emoji_markup != nullptr) {
flags |= telegram_api::inputChatUploadedPhoto::VIDEO_EMOJI_MARKUP_MASK;
}
auto input_chat_photo = make_tl_object<telegram_api::inputChatUploadedPhoto>(
flags, std::move(photo_input_file), std::move(video_input_file), main_frame_timestamp, nullptr);
flags, std::move(photo_input_file), std::move(video_input_file), main_frame_timestamp,
std::move(video_emoji_markup));
send_edit_dialog_photo_query(dialog_id, file_id, std::move(input_chat_photo), std::move(promise));
}
@ -34547,6 +34556,7 @@ void MessagesManager::on_updated_dialog_folder_id(DialogId dialog_id, uint64 gen
}
void MessagesManager::set_dialog_photo(DialogId dialog_id, const tl_object_ptr<td_api::InputChatPhoto> &input_photo,
const td_api::object_ptr<td_api::chatPhotoSticker> &sticker,
Promise<Unit> &&promise) {
if (!have_dialog_force(dialog_id, "set_dialog_photo")) {
return promise.set_error(Status::Error(400, "Chat not found"));
@ -34624,6 +34634,8 @@ void MessagesManager::set_dialog_photo(DialogId dialog_id, const tl_object_ptr<t
return promise.set_error(Status::Error(400, "Wrong main frame timestamp specified"));
}
TRY_RESULT_PROMISE(promise, sticker_photo_size, StickerPhotoSize::get_sticker_photo_size(td_, sticker));
auto file_type = is_animation ? FileType::Animation : FileType::Photo;
auto r_file_id = td_->file_manager_->get_input_file_id(file_type, *input_file, dialog_id, true, false);
if (r_file_id.is_error()) {
@ -34637,8 +34649,8 @@ void MessagesManager::set_dialog_photo(DialogId dialog_id, const tl_object_ptr<t
return;
}
upload_dialog_photo(dialog_id, td_->file_manager_->dup_file_id(file_id, "set_dialog_photo"), is_animation,
main_frame_timestamp, false, std::move(promise));
upload_dialog_photo(dialog_id, td_->file_manager_->dup_file_id(file_id, "set_dialog_photo"),
std::move(sticker_photo_size), is_animation, main_frame_timestamp, false, std::move(promise));
}
void MessagesManager::send_edit_dialog_photo_query(DialogId dialog_id, FileId file_id,
@ -34648,15 +34660,17 @@ void MessagesManager::send_edit_dialog_photo_query(DialogId dialog_id, FileId fi
td_->create_handler<EditDialogPhotoQuery>(std::move(promise))->send(dialog_id, file_id, std::move(input_chat_photo));
}
void MessagesManager::upload_dialog_photo(DialogId dialog_id, FileId file_id, bool is_animation,
void MessagesManager::upload_dialog_photo(DialogId dialog_id, FileId file_id,
unique_ptr<StickerPhotoSize> sticker_photo_size, bool is_animation,
double main_frame_timestamp, bool is_reupload, Promise<Unit> &&promise,
vector<int> bad_parts) {
CHECK(file_id.is_valid());
LOG(INFO) << "Ask to upload chat photo " << file_id;
bool is_inserted = being_uploaded_dialog_photos_
.emplace(file_id, UploadedDialogPhotoInfo{dialog_id, main_frame_timestamp, is_animation,
is_reupload, std::move(promise)})
.second;
bool is_inserted =
being_uploaded_dialog_photos_
.emplace(file_id, UploadedDialogPhotoInfo{dialog_id, std::move(sticker_photo_size), main_frame_timestamp,
is_animation, is_reupload, std::move(promise)})
.second;
CHECK(is_inserted);
// TODO use force_reupload if is_reupload
td_->file_manager_->resume_upload(file_id, std::move(bad_parts), upload_dialog_photo_callback_, 32, 0);

View File

@ -58,6 +58,7 @@
#include "td/telegram/SecretChatId.h"
#include "td/telegram/SecretInputMedia.h"
#include "td/telegram/ServerMessageId.h"
#include "td/telegram/StickerPhotoSize.h"
#include "td/telegram/td_api.h"
#include "td/telegram/telegram_api.h"
#include "td/telegram/UserId.h"
@ -534,7 +535,7 @@ class MessagesManager final : public Actor {
void add_dialog_to_list(DialogId dialog_id, DialogListId dialog_list_id, Promise<Unit> &&promise);
void set_dialog_photo(DialogId dialog_id, const tl_object_ptr<td_api::InputChatPhoto> &input_photo,
Promise<Unit> &&promise);
const td_api::object_ptr<td_api::chatPhotoSticker> &sticker, Promise<Unit> &&promise);
void set_dialog_title(DialogId dialog_id, const string &title, Promise<Unit> &&promise);
@ -989,8 +990,9 @@ class MessagesManager final : public Actor {
void on_update_scope_mention_notifications(NotificationSettingsScope scope, bool disable_mention_notifications);
void upload_dialog_photo(DialogId dialog_id, FileId file_id, bool is_animation, double main_frame_timestamp,
bool is_reupload, Promise<Unit> &&promise, vector<int> bad_parts = {});
void upload_dialog_photo(DialogId dialog_id, FileId file_id, unique_ptr<StickerPhotoSize> sticker_photo_size,
bool is_animation, double main_frame_timestamp, bool is_reupload, Promise<Unit> &&promise,
vector<int> bad_parts = {});
void on_binlog_events(vector<BinlogEvent> &&events);
@ -3476,14 +3478,16 @@ class MessagesManager final : public Actor {
struct UploadedDialogPhotoInfo {
DialogId dialog_id;
unique_ptr<StickerPhotoSize> sticker_photo_size;
double main_frame_timestamp;
bool is_animation;
bool is_reupload;
Promise<Unit> promise;
UploadedDialogPhotoInfo(DialogId dialog_id, double main_frame_timestamp, bool is_animation, bool is_reupload,
Promise<Unit> promise)
UploadedDialogPhotoInfo(DialogId dialog_id, unique_ptr<StickerPhotoSize> sticker_photo_size,
double main_frame_timestamp, bool is_animation, bool is_reupload, Promise<Unit> promise)
: dialog_id(dialog_id)
, sticker_photo_size(std::move(sticker_photo_size))
, main_frame_timestamp(main_frame_timestamp)
, is_animation(is_animation)
, is_reupload(is_reupload)

View File

@ -6147,7 +6147,7 @@ void Td::on_request(uint64 id, td_api::setChatTitle &request) {
void Td::on_request(uint64 id, const td_api::setChatPhoto &request) {
CREATE_OK_REQUEST_PROMISE();
messages_manager_->set_dialog_photo(DialogId(request.chat_id_), request.photo_, std::move(promise));
messages_manager_->set_dialog_photo(DialogId(request.chat_id_), request.photo_, request.sticker_, std::move(promise));
}
void Td::on_request(uint64 id, const td_api::setChatMessageAutoDeleteTime &request) {

View File

@ -872,20 +872,24 @@ class CliClient final : public Actor {
}
}
struct ChatPhotoStickerType {
struct ChatPhotoSticker {
int64 sticker_set_id = 0;
int64 sticker_id = 0;
operator td_api::object_ptr<td_api::ChatPhotoStickerType>() const {
operator td_api::object_ptr<td_api::chatPhotoSticker>() const {
if (sticker_set_id != 0) {
return td_api::make_object<td_api::chatPhotoStickerTypeRegularOrMask>(sticker_set_id, sticker_id);
return td_api::make_object<td_api::chatPhotoSticker>(
td_api::make_object<td_api::chatPhotoStickerTypeRegularOrMask>(sticker_set_id, sticker_id),
as_background_fill(0x7FFFFFFF));
} else {
return td_api::make_object<td_api::chatPhotoStickerTypeCustomEmoji>(sticker_id);
return td_api::make_object<td_api::chatPhotoSticker>(
td_api::make_object<td_api::chatPhotoStickerTypeCustomEmoji>(sticker_id),
as_background_fill({0x000000, 0xFF0000, 0x00FF00, 0x0000FF}));
}
}
};
void get_args(string &args, ChatPhotoStickerType &arg) const {
void get_args(string &args, ChatPhotoSticker &arg) const {
string sticker_set_id;
string sticker_id;
std::tie(sticker_set_id, sticker_id) = split(args, get_delimiter(args));
@ -4464,27 +4468,47 @@ class CliClient final : public Actor {
} else if (op == "scpe") {
ChatId chat_id;
get_args(args, chat_id);
send_request(td_api::make_object<td_api::setChatPhoto>(chat_id, nullptr));
send_request(td_api::make_object<td_api::setChatPhoto>(chat_id, nullptr, nullptr));
} else if (op == "scpp") {
ChatId chat_id;
int64 photo_id;
get_args(args, chat_id, photo_id);
send_request(td_api::make_object<td_api::setChatPhoto>(
chat_id, td_api::make_object<td_api::inputChatPhotoPrevious>(photo_id)));
chat_id, td_api::make_object<td_api::inputChatPhotoPrevious>(photo_id), nullptr));
} else if (op == "scp") {
ChatId chat_id;
string photo_path;
get_args(args, chat_id, photo_path);
send_request(td_api::make_object<td_api::setChatPhoto>(
chat_id, td_api::make_object<td_api::inputChatPhotoStatic>(as_input_file(photo_path))));
} else if (op == "scpa" || op == "scpv") {
chat_id, td_api::make_object<td_api::inputChatPhotoStatic>(as_input_file(photo_path)), nullptr));
} else if (op == "scpa") {
ChatId chat_id;
string animation;
string main_frame_timestamp;
get_args(args, chat_id, animation, main_frame_timestamp);
send_request(
td_api::make_object<td_api::setChatPhoto>(chat_id,
td_api::make_object<td_api::inputChatPhotoAnimation>(
as_input_file(animation), to_double(main_frame_timestamp)),
nullptr));
} else if (op == "scps") {
ChatId chat_id;
string photo_path;
ChatPhotoSticker sticker;
get_args(args, chat_id, photo_path, sticker);
send_request(td_api::make_object<td_api::setChatPhoto>(
chat_id, td_api::make_object<td_api::inputChatPhotoAnimation>(as_input_file(animation),
to_double(main_frame_timestamp))));
chat_id, td_api::make_object<td_api::inputChatPhotoStatic>(as_input_file(photo_path)), sticker));
} else if (op == "scpas") {
ChatId chat_id;
string animation;
string main_frame_timestamp;
ChatPhotoSticker sticker;
get_args(args, chat_id, animation, main_frame_timestamp, sticker);
send_request(
td_api::make_object<td_api::setChatPhoto>(chat_id,
td_api::make_object<td_api::inputChatPhotoAnimation>(
as_input_file(animation), to_double(main_frame_timestamp)),
sticker));
} else if (op == "scmt") {
ChatId chat_id;
int32 auto_delete_time;
@ -4897,26 +4921,21 @@ class CliClient final : public Actor {
td_api::make_object<td_api::setProfilePhoto>(td_api::make_object<td_api::inputChatPhotoAnimation>(
as_input_file(animation), to_double(main_frame_timestamp)),
nullptr, op == "sppaf"));
} else if (op == "sppps" || op == "spppsf") {
} else if (op == "spps" || op == "sppsf") {
string photo;
ChatPhotoStickerType type;
get_args(args, photo, type);
ChatPhotoSticker sticker;
get_args(args, photo, sticker);
send_request(td_api::make_object<td_api::setProfilePhoto>(
td_api::make_object<td_api::inputChatPhotoStatic>(as_input_file(photo)),
td_api::make_object<td_api::chatPhotoSticker>(type,
td_api::make_object<td_api::backgroundFillSolid>(0x7FFFFFFF)),
op == "spppcef"));
td_api::make_object<td_api::inputChatPhotoStatic>(as_input_file(photo)), sticker, op == "sppsf"));
} else if (op == "sppas" || op == "sppasf") {
string animation;
string main_frame_timestamp;
ChatPhotoStickerType type;
get_args(args, animation, main_frame_timestamp, type);
send_request(td_api::make_object<td_api::setProfilePhoto>(
td_api::make_object<td_api::inputChatPhotoAnimation>(as_input_file(animation),
to_double(main_frame_timestamp)),
td_api::make_object<td_api::chatPhotoSticker>(type,
td_api::make_object<td_api::backgroundFillSolid>(0x7FFFFFFF)),
op == "sppacef"));
ChatPhotoSticker sticker;
get_args(args, animation, main_frame_timestamp, sticker);
send_request(
td_api::make_object<td_api::setProfilePhoto>(td_api::make_object<td_api::inputChatPhotoAnimation>(
as_input_file(animation), to_double(main_frame_timestamp)),
sticker, op == "sppasf"));
} else if (op == "suppp") {
UserId user_id;
string photo;