tdlight/td/telegram/StickerPhotoSize.cpp

187 lines
7.2 KiB
C++
Raw Normal View History

//
2024-01-01 01:07:21 +01:00
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include "td/telegram/StickerPhotoSize.h"
#include "td/telegram/StickersManager.h"
#include "td/telegram/Td.h"
2023-01-31 11:33:30 +01:00
#include "td/utils/logging.h"
namespace td {
2023-01-23 11:02:10 +01:00
Result<unique_ptr<StickerPhotoSize>> StickerPhotoSize::get_sticker_photo_size(
2023-01-21 00:07:42 +01:00
Td *td, const td_api::object_ptr<td_api::chatPhotoSticker> &sticker) {
if (sticker == nullptr) {
2023-01-29 22:35:14 +01:00
return Status::Error(400, "Sticker must not be null");
}
2023-01-21 00:07:42 +01:00
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());
2023-01-23 11:02:10 +01:00
result->type_ = Type::Sticker;
2023-01-23 10:05:08 +01:00
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());
2023-01-23 11:02:10 +01:00
result->type_ = Type::CustomEmoji;
2023-01-23 10:05:08 +01:00
result->custom_emoji_id_ = CustomEmojiId(type->custom_emoji_id_);
if (!td->stickers_manager_->have_custom_emoji(result->custom_emoji_id_)) {
return Status::Error(400, "Custom emoji not found");
}
break;
}
}
auto fill = sticker->background_fill_.get();
switch (fill->get_id()) {
case td_api::backgroundFillSolid::ID: {
auto solid = static_cast<const td_api::backgroundFillSolid *>(fill);
2023-01-23 10:05:08 +01:00
result->background_colors_.push_back(solid->color_);
break;
}
case td_api::backgroundFillGradient::ID: {
auto gradient = static_cast<const td_api::backgroundFillGradient *>(fill);
2023-01-23 10:05:08 +01:00
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) {
2023-01-21 00:07:42 +01:00
return Status::Error(400, "Invalid number of colors specified");
}
2023-01-23 10:05:08 +01:00
result->background_colors_ = freeform->colors_;
break;
}
default:
UNREACHABLE();
break;
}
2023-01-23 10:05:08 +01:00
for (auto &color : result->background_colors_) {
color &= 0xFFFFFF;
}
2023-01-21 00:07:42 +01:00
return std::move(result);
}
2023-01-23 11:02:10 +01:00
telegram_api::object_ptr<telegram_api::VideoSize> StickerPhotoSize::get_input_video_size_object(Td *td) const {
switch (type_) {
case Type::Sticker:
return telegram_api::make_object<telegram_api::videoSizeStickerMarkup>(
2023-01-23 11:02:10 +01:00
td->stickers_manager_->get_input_sticker_set(sticker_set_id_), sticker_id_,
vector<int32>(background_colors_));
case Type::CustomEmoji:
return telegram_api::make_object<telegram_api::videoSizeEmojiMarkup>(custom_emoji_id_.get(),
vector<int32>(background_colors_));
default:
UNREACHABLE();
return nullptr;
}
}
2023-01-23 09:51:00 +01:00
2023-01-23 11:02:10 +01:00
unique_ptr<StickerPhotoSize> StickerPhotoSize::get_sticker_photo_size(
Td *td, telegram_api::object_ptr<telegram_api::VideoSize> &&size_ptr) {
2023-01-23 09:51:00 +01:00
CHECK(size_ptr != nullptr);
auto result = make_unique<StickerPhotoSize>();
bool is_valid = false;
switch (size_ptr->get_id()) {
case telegram_api::videoSizeEmojiMarkup::ID: {
auto size = move_tl_object_as<telegram_api::videoSizeEmojiMarkup>(size_ptr);
2023-01-23 11:02:10 +01:00
result->type_ = Type::CustomEmoji;
2023-01-23 10:05:08 +01:00
result->custom_emoji_id_ = CustomEmojiId(size->emoji_id_);
result->background_colors_ = std::move(size->background_colors_);
is_valid = result->custom_emoji_id_.is_valid();
2023-01-23 09:51:00 +01:00
break;
}
case telegram_api::videoSizeStickerMarkup::ID: {
auto size = move_tl_object_as<telegram_api::videoSizeStickerMarkup>(size_ptr);
2023-01-23 11:02:10 +01:00
result->type_ = Type::Sticker;
2023-01-23 10:05:08 +01:00
result->sticker_set_id_ = td->stickers_manager_->add_sticker_set(std::move(size->stickerset_));
result->sticker_id_ = size->sticker_id_;
result->background_colors_ = std::move(size->background_colors_);
is_valid = result->sticker_set_id_.is_valid() && result->sticker_id_ != 0;
2023-01-23 09:51:00 +01:00
break;
}
default:
UNREACHABLE();
}
2023-01-23 10:05:08 +01:00
if (!is_valid || result->background_colors_.empty() || result->background_colors_.size() > 4) {
2023-01-23 09:51:00 +01:00
LOG(ERROR) << "Receive invalid " << *result;
return {};
}
2023-01-23 10:05:08 +01:00
for (auto &color : result->background_colors_) {
2023-01-23 09:51:00 +01:00
color &= 0xFFFFFF;
}
2023-02-04 07:55:11 +01:00
return result;
2023-01-23 09:51:00 +01:00
}
td_api::object_ptr<td_api::chatPhotoSticker> StickerPhotoSize::get_chat_photo_sticker_object() const {
td_api::object_ptr<td_api::ChatPhotoStickerType> sticker_type;
2023-01-23 10:05:08 +01:00
switch (type_) {
2023-01-23 11:02:10 +01:00
case Type::Sticker:
2023-01-23 10:05:08 +01:00
sticker_type = td_api::make_object<td_api::chatPhotoStickerTypeRegularOrMask>(sticker_set_id_.get(), sticker_id_);
break;
2023-01-23 11:02:10 +01:00
case Type::CustomEmoji:
2023-01-23 10:05:08 +01:00
sticker_type = td_api::make_object<td_api::chatPhotoStickerTypeCustomEmoji>(custom_emoji_id_.get());
break;
default:
UNREACHABLE();
return nullptr;
}
CHECK(sticker_type != nullptr);
auto background_fill = [&](vector<int32> colors) -> td_api::object_ptr<td_api::BackgroundFill> {
switch (colors.size()) {
case 1:
return td_api::make_object<td_api::backgroundFillSolid>(colors[0]);
case 2:
return td_api::make_object<td_api::backgroundFillGradient>(colors[0], colors[1], 0);
case 3:
case 4:
return td_api::make_object<td_api::backgroundFillFreeformGradient>(std::move(colors));
default:
UNREACHABLE();
return nullptr;
}
2023-01-23 10:05:08 +01:00
}(background_colors_);
return td_api::make_object<td_api::chatPhotoSticker>(std::move(sticker_type), std::move(background_fill));
}
bool operator==(const StickerPhotoSize &lhs, const StickerPhotoSize &rhs) {
2023-01-23 10:05:08 +01:00
return lhs.type_ == rhs.type_ && lhs.sticker_set_id_ == rhs.sticker_set_id_ && lhs.sticker_id_ == rhs.sticker_id_ &&
lhs.custom_emoji_id_ == rhs.custom_emoji_id_ && lhs.background_colors_ == rhs.background_colors_;
}
bool operator!=(const StickerPhotoSize &lhs, const StickerPhotoSize &rhs) {
return !(lhs == rhs);
}
StringBuilder &operator<<(StringBuilder &string_builder, const StickerPhotoSize &sticker_photo_size) {
2023-01-23 10:05:08 +01:00
switch (sticker_photo_size.type_) {
case StickerPhotoSize::Type::Sticker:
2023-01-23 10:05:08 +01:00
return string_builder << sticker_photo_size.sticker_id_ << " from " << sticker_photo_size.sticker_set_id_
<< " on " << sticker_photo_size.background_colors_;
case StickerPhotoSize::Type::CustomEmoji:
2023-01-23 10:05:08 +01:00
return string_builder << sticker_photo_size.custom_emoji_id_ << " on " << sticker_photo_size.background_colors_;
default:
UNREACHABLE();
return string_builder;
}
}
} // namespace td