Improve get_sticker_photo_size.

This commit is contained in:
levlam 2023-01-21 02:07:42 +03:00
parent eade454791
commit d6502824b3
4 changed files with 64 additions and 56 deletions

View File

@ -448,7 +448,7 @@ class UploadProfilePhotoQuery final : public Td::ResultHandler {
}
void send(UserId user_id, FileId file_id, tl_object_ptr<telegram_api::InputFile> &&input_file,
const StickerPhotoSize &sticker_photo_size, bool is_fallback, bool only_suggest, bool is_animation,
unique_ptr<StickerPhotoSize> sticker_photo_size, bool is_fallback, bool only_suggest, bool is_animation,
double main_frame_timestamp) {
CHECK(input_file != nullptr);
CHECK(file_id.is_valid());
@ -7055,14 +7055,14 @@ 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,
const td_api::object_ptr<td_api::chatPhotoSticker> &custom_emoji,
bool is_fallback, Promise<Unit> &&promise) {
set_profile_photo_impl(get_my_id(), input_photo, custom_emoji, is_fallback, false, std::move(promise));
const td_api::object_ptr<td_api::chatPhotoSticker> &sticker, bool is_fallback,
Promise<Unit> &&promise) {
set_profile_photo_impl(get_my_id(), input_photo, sticker, is_fallback, false, std::move(promise));
}
void ContactsManager::set_profile_photo_impl(UserId user_id,
const td_api::object_ptr<td_api::InputChatPhoto> &input_photo,
const td_api::object_ptr<td_api::chatPhotoSticker> &custom_emoji,
const td_api::object_ptr<td_api::chatPhotoSticker> &sticker,
bool is_fallback, bool only_suggest, Promise<Unit> &&promise) {
if (input_photo == nullptr) {
return promise.set_error(Status::Error(400, "New profile photo must be non-empty"));
@ -7076,8 +7076,8 @@ void ContactsManager::set_profile_photo_impl(UserId user_id,
if (user_id != get_my_id()) {
return promise.set_error(Status::Error(400, "Can't use inputChatPhotoPrevious"));
}
if (custom_emoji != nullptr) {
return promise.set_error(Status::Error(400, "Can't use custom emoji with a previous photo"));
if (sticker != nullptr) {
return promise.set_error(Status::Error(400, "Can't use sticker with a previous photo"));
}
auto photo = static_cast<const td_api::inputChatPhotoPrevious *>(input_photo.get());
auto photo_id = photo->chat_photo_id_;
@ -7116,6 +7116,8 @@ void ContactsManager::set_profile_photo_impl(UserId user_id,
return promise.set_error(Status::Error(400, "Wrong main frame timestamp specified"));
}
TRY_RESULT_PROMISE(promise, sticker_photo_size, 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, DialogId(user_id), false, false);
if (r_file_id.is_error()) {
@ -7125,7 +7127,6 @@ void ContactsManager::set_profile_photo_impl(UserId user_id,
FileId file_id = r_file_id.ok();
CHECK(file_id.is_valid());
auto sticker_photo_size = get_sticker_photo_size(td_, custom_emoji);
upload_profile_photo(user_id, td_->file_manager_->dup_file_id(file_id, "set_profile_photo_impl"),
std::move(sticker_photo_size), is_fallback, only_suggest, is_animation, main_frame_timestamp,
std::move(promise));
@ -7162,10 +7163,10 @@ 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, StickerPhotoSize sticker_photo_size,
bool is_fallback, bool only_suggest, 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,
unique_ptr<StickerPhotoSize> sticker_photo_size, bool is_fallback,
bool only_suggest, 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{user_id, std::move(sticker_photo_size), is_fallback,
@ -17948,8 +17949,8 @@ void ContactsManager::on_upload_profile_photo(FileId file_id, tl_object_ptr<tele
CHECK(input_file != nullptr);
td_->create_handler<UploadProfilePhotoQuery>(std::move(promise))
->send(user_id, file_id, std::move(input_file), sticker_photo_size, is_fallback, only_suggest, is_animation,
main_frame_timestamp);
->send(user_id, file_id, std::move(input_file), std::move(sticker_photo_size), is_fallback, only_suggest,
is_animation, main_frame_timestamp);
}
void ContactsManager::on_upload_profile_photo_error(FileId file_id, Status status) {

View File

@ -367,7 +367,7 @@ class ContactsManager final : public Actor {
FileId get_profile_photo_file_id(int64 photo_id) const;
void set_profile_photo(const td_api::object_ptr<td_api::InputChatPhoto> &input_photo,
const td_api::object_ptr<td_api::chatPhotoSticker> &custom_emoji, bool is_fallback,
const td_api::object_ptr<td_api::chatPhotoSticker> &sticker, bool is_fallback,
Promise<Unit> &&promise);
void set_user_profile_photo(UserId user_id, const td_api::object_ptr<td_api::InputChatPhoto> &input_photo,
@ -1353,12 +1353,12 @@ 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<td_api::InputChatPhoto> &input_photo,
const td_api::object_ptr<td_api::chatPhotoSticker> &custom_emoji, bool is_fallback,
const td_api::object_ptr<td_api::chatPhotoSticker> &sticker, bool is_fallback,
bool only_suggest, Promise<Unit> &&promise);
void upload_profile_photo(UserId user_id, FileId file_id, StickerPhotoSize sticker_photo_size, bool is_fallback,
bool only_suggest, bool is_animation, double main_frame_timestamp, Promise<Unit> &&promise,
int reupload_count = 0, vector<int> bad_parts = {});
void upload_profile_photo(UserId user_id, FileId file_id, unique_ptr<StickerPhotoSize> sticker_photo_size,
bool is_fallback, bool only_suggest, 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);
@ -1897,7 +1897,7 @@ class ContactsManager final : public Actor {
struct UploadedProfilePhoto {
UserId user_id;
StickerPhotoSize sticker_photo_size;
unique_ptr<StickerPhotoSize> sticker_photo_size;
bool is_fallback;
bool only_suggest;
double main_frame_timestamp;
@ -1905,10 +1905,11 @@ class ContactsManager final : public Actor {
int reupload_count;
Promise<Unit> promise;
UploadedProfilePhoto(UserId user_id, StickerPhotoSize sticker_photo_size, bool is_fallback, bool only_suggest,
double main_frame_timestamp, bool is_animation, int32 reupload_count, Promise<Unit> promise)
UploadedProfilePhoto(UserId user_id, unique_ptr<StickerPhotoSize> sticker_photo_size, bool is_fallback,
bool only_suggest, double main_frame_timestamp, bool is_animation, int32 reupload_count,
Promise<Unit> promise)
: user_id(user_id)
, sticker_photo_size(sticker_photo_size)
, sticker_photo_size(std::move(sticker_photo_size))
, is_fallback(is_fallback)
, only_suggest(only_suggest)
, main_frame_timestamp(main_frame_timestamp)

View File

@ -11,28 +11,35 @@
namespace td {
StickerPhotoSize get_sticker_photo_size(Td *td, const td_api::object_ptr<td_api::chatPhotoSticker> &sticker) {
if (sticker == nullptr || sticker->type_ == nullptr || sticker->background_fill_ == nullptr) {
return {};
Result<unique_ptr<StickerPhotoSize>> get_sticker_photo_size(
Td *td, const td_api::object_ptr<td_api::chatPhotoSticker> &sticker) {
if (sticker == nullptr) {
return nullptr;
}
StickerPhotoSize result;
if (sticker->type_ == nullptr) {
return Status::Error(400, "Type must be non-null");
}
if (sticker->background_fill_ == nullptr) {
return Status::Error(400, "Background must be non-null");
}
auto result = make_unique<StickerPhotoSize>();
switch (sticker->type_->get_id()) {
case td_api::chatPhotoStickerTypeRegularOrMask::ID: {
auto type = static_cast<const td_api::chatPhotoStickerTypeRegularOrMask *>(sticker->type_.get());
result.type = StickerPhotoSize::Type::Sticker;
result.sticker_set_id = StickerSetId(type->sticker_set_id_);
result.sticker_id = type->sticker_id_;
//if (!td->stickers_manager_->have_sticker(result.sticker_set_id, result.sticker_id)) {
// return {};
result->type = StickerPhotoSize::Type::Sticker;
result->sticker_set_id = StickerSetId(type->sticker_set_id_);
result->sticker_id = type->sticker_id_;
//if (!td->stickers_manager_->have_sticker(result->sticker_set_id, result->sticker_id)) {
// return Status::Error(400, "Sticker not found");
//}
break;
}
case td_api::chatPhotoStickerTypeCustomEmoji::ID: {
auto type = static_cast<const td_api::chatPhotoStickerTypeCustomEmoji *>(sticker->type_.get());
result.type = StickerPhotoSize::Type::CustomEmoji;
result.custom_emoji_id = CustomEmojiId(type->custom_emoji_id_);
//if (!td->stickers_manager_->have_custom_emoji_id(result.custom_emoji_id)) {
// return {};
result->type = StickerPhotoSize::Type::CustomEmoji;
result->custom_emoji_id = CustomEmojiId(type->custom_emoji_id_);
//if (!td->stickers_manager_->have_custom_emoji_id(result->custom_emoji_id)) {
// return Status::Error(400, "Custom emoji not found");
//}
break;
}
@ -41,49 +48,46 @@ StickerPhotoSize get_sticker_photo_size(Td *td, const td_api::object_ptr<td_api:
switch (fill->get_id()) {
case td_api::backgroundFillSolid::ID: {
auto solid = static_cast<const td_api::backgroundFillSolid *>(fill);
result.background_colors.push_back(solid->color_);
result->background_colors.push_back(solid->color_);
break;
}
case td_api::backgroundFillGradient::ID: {
auto gradient = static_cast<const td_api::backgroundFillGradient *>(fill);
result.background_colors.push_back(gradient->top_color_);
result.background_colors.push_back(gradient->bottom_color_);
result->background_colors.push_back(gradient->top_color_);
result->background_colors.push_back(gradient->bottom_color_);
break;
}
case td_api::backgroundFillFreeformGradient::ID: {
auto freeform = static_cast<const td_api::backgroundFillFreeformGradient *>(fill);
if (freeform->colors_.size() != 3 && freeform->colors_.size() != 4) {
return {};
return Status::Error(400, "Invalid number of colors specified");
}
result.background_colors = freeform->colors_;
result->background_colors = freeform->colors_;
break;
}
default:
UNREACHABLE();
break;
}
for (auto &color : result.background_colors) {
for (auto &color : result->background_colors) {
color &= 0xFFFFFF;
}
return result;
return std::move(result);
}
telegram_api::object_ptr<telegram_api::VideoSize> get_input_video_size_object(
Td *td, const StickerPhotoSize &sticker_photo_size) {
switch (sticker_photo_size.type) {
Td *td, const unique_ptr<StickerPhotoSize> &sticker_photo_size) {
if (sticker_photo_size == nullptr) {
return nullptr;
}
switch (sticker_photo_size->type) {
case StickerPhotoSize::Type::Sticker:
if (!sticker_photo_size.sticker_set_id.is_valid()) {
return nullptr;
}
return telegram_api::make_object<telegram_api::videoSizeStickerMarkup>(
td->stickers_manager_->get_input_sticker_set(sticker_photo_size.sticker_set_id),
sticker_photo_size.sticker_id, vector<int32>(sticker_photo_size.background_colors));
td->stickers_manager_->get_input_sticker_set(sticker_photo_size->sticker_set_id),
sticker_photo_size->sticker_id, vector<int32>(sticker_photo_size->background_colors));
case StickerPhotoSize::Type::CustomEmoji:
if (!sticker_photo_size.custom_emoji_id.is_valid()) {
return nullptr;
}
return telegram_api::make_object<telegram_api::videoSizeEmojiMarkup>(
sticker_photo_size.custom_emoji_id.get(), vector<int32>(sticker_photo_size.background_colors));
sticker_photo_size->custom_emoji_id.get(), vector<int32>(sticker_photo_size->background_colors));
default:
UNREACHABLE();
return nullptr;

View File

@ -12,6 +12,7 @@
#include "td/telegram/telegram_api.h"
#include "td/utils/common.h"
#include "td/utils/Status.h"
#include "td/utils/StringBuilder.h"
namespace td {
@ -27,10 +28,11 @@ struct StickerPhotoSize {
vector<int32> background_colors;
};
StickerPhotoSize get_sticker_photo_size(Td *td, const td_api::object_ptr<td_api::chatPhotoSticker> &chat_photo_sticker);
Result<unique_ptr<StickerPhotoSize>> get_sticker_photo_size(
Td *td, const td_api::object_ptr<td_api::chatPhotoSticker> &chat_photo_sticker);
telegram_api::object_ptr<telegram_api::VideoSize> get_input_video_size_object(
Td *td, const StickerPhotoSize &sticker_photo_size);
Td *td, const unique_ptr<StickerPhotoSize> &sticker_photo_size);
bool operator==(const StickerPhotoSize &lhs, const StickerPhotoSize &rhs);
bool operator!=(const StickerPhotoSize &lhs, const StickerPhotoSize &rhs);