Add class StickerType.

This commit is contained in:
levlam 2022-07-14 15:02:55 +03:00
parent a896edcd14
commit b447c32fe9
6 changed files with 108 additions and 19 deletions

View File

@ -434,6 +434,7 @@ set(TDLIB_SOURCE
td/telegram/StateManager.cpp
td/telegram/StickerFormat.cpp
td/telegram/StickersManager.cpp
td/telegram/StickerType.cpp
td/telegram/StorageManager.cpp
td/telegram/SuggestedAction.cpp
td/telegram/Td.cpp
@ -681,6 +682,7 @@ set(TDLIB_SOURCE
td/telegram/StickerFormat.h
td/telegram/StickerSetId.h
td/telegram/StickersManager.h
td/telegram/StickerType.h
td/telegram/StorageManager.h
td/telegram/SuggestedAction.h
td/telegram/Td.h

View File

@ -0,0 +1,50 @@
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
//
// 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/StickerType.h"
namespace td {
StickerType get_sticker_type(const td_api::object_ptr<td_api::StickerType> &type) {
if (type == nullptr) {
return StickerType::Regular;
}
switch (type->get_id()) {
case td_api::stickerTypeRegular::ID:
return StickerType::Regular;
case td_api::stickerTypeMask::ID:
return StickerType::Mask;
default:
UNREACHABLE();
return StickerType::Regular;
}
}
td_api::object_ptr<td_api::StickerType> get_sticker_type_object(StickerType sticker_type) {
switch (sticker_type) {
case StickerType::Regular:
return td_api::make_object<td_api::stickerTypeRegular>();
case StickerType::Mask:
return td_api::make_object<td_api::stickerTypeMask>();
default:
UNREACHABLE();
return nullptr;
}
}
StringBuilder &operator<<(StringBuilder &string_builder, StickerType sticker_type) {
switch (sticker_type) {
case StickerType::Regular:
return string_builder << "Regular";
case StickerType::Mask:
return string_builder << "Mask";
default:
UNREACHABLE();
return string_builder;
}
}
} // namespace td

25
td/telegram/StickerType.h Normal file
View File

@ -0,0 +1,25 @@
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
//
// 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)
//
#pragma once
#include "td/telegram/td_api.h"
#include "td/utils/common.h"
#include "td/utils/StringBuilder.h"
namespace td {
// update store_sticker/store_sticker_set when this type changes
enum class StickerType : int32 { Regular, Mask };
StickerType get_sticker_type(const td_api::object_ptr<td_api::StickerType> &type);
td_api::object_ptr<td_api::StickerType> get_sticker_type_object(StickerType sticker_type);
StringBuilder &operator<<(StringBuilder &string_builder, StickerType sticker_type);
} // namespace td

View File

@ -1897,8 +1897,9 @@ tl_object_ptr<td_api::sticker> StickersManager::get_sticker_object(FileId file_i
? td_->file_manager_->get_file_object(sticker->premium_animation_file_id)
: nullptr;
return td_api::make_object<td_api::sticker>(
sticker->set_id.get(), width, height, sticker->alt, get_sticker_format_object(sticker->format), nullptr,
std::move(mask_position), get_sticker_minithumbnail(sticker->minithumbnail, sticker->set_id, document_id, zoom),
sticker->set_id.get(), width, height, sticker->alt, get_sticker_format_object(sticker->format),
get_sticker_type_object(sticker->type), std::move(mask_position),
get_sticker_minithumbnail(sticker->minithumbnail, sticker->set_id, document_id, zoom),
std::move(thumbnail_object), std::move(premium_animation_object), td_->file_manager_->get_file_object(file_id));
}
@ -2034,8 +2035,9 @@ tl_object_ptr<td_api::stickerSet> StickersManager::get_sticker_set_object(Sticke
get_sticker_minithumbnail(sticker_set->minithumbnail, sticker_set->id, -2,
get_sticker_set_minithumbnail_zoom(sticker_set)),
sticker_set->is_installed && !sticker_set->is_archived, sticker_set->is_archived, sticker_set->is_official,
get_sticker_format_object(sticker_set->sticker_format), nullptr, sticker_set->is_viewed, std::move(stickers),
std::move(emojis));
get_sticker_format_object(sticker_set->sticker_format),
get_sticker_type_object(sticker_set->is_masks ? StickerType::Mask : StickerType::Regular), sticker_set->is_viewed,
std::move(stickers), std::move(emojis));
}
tl_object_ptr<td_api::stickerSets> StickersManager::get_sticker_sets_object(int32 total_count,
@ -2108,7 +2110,8 @@ tl_object_ptr<td_api::stickerSetInfo> StickersManager::get_sticker_set_info_obje
get_sticker_minithumbnail(sticker_set->minithumbnail, sticker_set->id, -3,
get_sticker_set_minithumbnail_zoom(sticker_set)),
sticker_set->is_installed && !sticker_set->is_archived, sticker_set->is_archived, sticker_set->is_official,
get_sticker_format_object(sticker_set->sticker_format), nullptr, sticker_set->is_viewed,
get_sticker_format_object(sticker_set->sticker_format),
get_sticker_type_object(sticker_set->is_masks ? StickerType::Mask : StickerType::Regular), sticker_set->is_viewed,
sticker_set->was_loaded ? actual_count : max(actual_count, sticker_set->sticker_count), std::move(stickers));
}
@ -2252,8 +2255,8 @@ FileId StickersManager::on_get_sticker(unique_ptr<Sticker> new_sticker, bool rep
if (s->format != new_sticker->format && new_sticker->format != StickerFormat::Unknown) {
s->format = new_sticker->format;
}
if (s->is_mask != new_sticker->is_mask && new_sticker->is_mask) {
s->is_mask = new_sticker->is_mask;
if (s->type != new_sticker->type && new_sticker->type != StickerType::Regular) {
s->type = new_sticker->type;
}
if (s->point != new_sticker->point && new_sticker->point != -1) {
s->point = new_sticker->point;
@ -2728,7 +2731,9 @@ void StickersManager::create_sticker(FileId file_id, FileId premium_animation_fi
s->set_id = on_get_input_sticker_set(file_id, std::move(sticker->stickerset_), load_data_multipromise_ptr);
s->alt = std::move(sticker->alt_);
s->is_mask = (sticker->flags_ & telegram_api::documentAttributeSticker::MASK_MASK) != 0;
if ((sticker->flags_ & telegram_api::documentAttributeSticker::MASK_MASK) != 0) {
s->type = StickerType::Mask;
}
if ((sticker->flags_ & telegram_api::documentAttributeSticker::MASK_COORDS_MASK) != 0) {
CHECK(sticker->mask_coords_ != nullptr);
int32 point = sticker->mask_coords_->n_;
@ -5897,7 +5902,7 @@ tl_object_ptr<telegram_api::inputStickerSetItem> StickersManager::get_input_stic
auto input_document = file_view.main_remote_location().as_input_document();
tl_object_ptr<telegram_api::maskCoords> mask_coords;
if (sticker->type_ != nullptr && sticker->type_->get_id() == td_api::stickerTypeMask::ID) {
if (get_sticker_type(sticker->type_) == StickerType::Mask) {
auto mask_position = sticker->mask_position_.get();
if (mask_position != nullptr && mask_position->point_ != nullptr) {
auto point = [mask_point_id = mask_position->point_->get_id()] {
@ -6019,8 +6024,8 @@ void StickersManager::create_new_sticker_set(UserId user_id, string &title, stri
if (is_sticker_format_animated(sticker_format) && is_url) {
return promise.set_error(Status::Error(400, "Animated stickers can't be uploaded by URL"));
}
sticker_formats.insert(sticker->format_->get_id());
sticker_types.insert(sticker->type_ == nullptr ? td_api::stickerTypeRegular::ID : sticker->type_->get_id());
sticker_formats.insert(static_cast<int32>(get_sticker_format(sticker->format_)) + 1);
sticker_types.insert(static_cast<int32>(get_sticker_type(sticker->type_)) + 1);
file_ids.push_back(file_id);
if (is_url) {
@ -6208,8 +6213,7 @@ void StickersManager::on_new_stickers_uploaded(int64 random_id, Result<Unit> res
auto &promise = pending_new_sticker_set->promise;
TRY_RESULT_PROMISE(promise, input_user, td_->contacts_manager_->get_input_user(pending_new_sticker_set->user_id));
bool is_masks = pending_new_sticker_set->stickers[0]->type_ != nullptr &&
pending_new_sticker_set->stickers[0]->type_->get_id() == td_api::stickerTypeMask::ID;
bool is_masks = get_sticker_type(pending_new_sticker_set->stickers[0]->type_) == StickerType::Mask;
StickerFormat sticker_format = pending_new_sticker_set->sticker_format;
auto sticker_count = pending_new_sticker_set->stickers.size();

View File

@ -17,6 +17,7 @@
#include "td/telegram/SpecialStickerSetType.h"
#include "td/telegram/StickerFormat.h"
#include "td/telegram/StickerSetId.h"
#include "td/telegram/StickerType.h"
#include "td/telegram/td_api.h"
#include "td/telegram/telegram_api.h"
@ -362,7 +363,7 @@ class StickersManager final : public Actor {
FileId premium_animation_file_id;
FileId file_id;
StickerFormat format = StickerFormat::Unknown;
bool is_mask = false;
StickerType type = StickerType::Regular;
int32 point = -1;
double x_shift = 0;
double y_shift = 0;
@ -594,7 +595,7 @@ class StickersManager final : public Actor {
void on_load_recent_stickers_finished(bool is_attached, vector<FileId> &&recent_sticker_ids,
bool from_database = false);
td_api::object_ptr<td_api::updateInstalledStickerSets> get_update_installed_sticker_sets_object(int is_mask) const;
td_api::object_ptr<td_api::updateInstalledStickerSets> get_update_installed_sticker_sets_object(int is_masks) const;
void send_update_installed_sticker_sets(bool from_database = false);

View File

@ -34,8 +34,9 @@ void StickersManager::store_sticker(FileId file_id, bool in_sticker_set, StorerT
bool is_tgs = sticker->format == StickerFormat::Tgs;
bool is_webm = sticker->format == StickerFormat::Webm;
bool has_premium_animation = sticker->premium_animation_file_id.is_valid();
bool is_mask = sticker->type == StickerType::Mask;
BEGIN_STORE_FLAGS();
STORE_FLAG(sticker->is_mask);
STORE_FLAG(is_mask);
STORE_FLAG(has_sticker_set_access_hash);
STORE_FLAG(in_sticker_set);
STORE_FLAG(is_tgs);
@ -56,7 +57,7 @@ void StickersManager::store_sticker(FileId file_id, bool in_sticker_set, StorerT
store(sticker->s_thumbnail, storer);
store(sticker->m_thumbnail, storer);
store(file_id, storer);
if (sticker->is_mask) {
if (is_mask) {
store(sticker->point, storer);
store(sticker->x_shift, storer);
store(sticker->y_shift, storer);
@ -83,8 +84,9 @@ FileId StickersManager::parse_sticker(bool in_sticker_set, ParserT &parser) {
bool is_tgs;
bool is_webm;
bool has_premium_animation;
bool is_mask;
BEGIN_PARSE_FLAGS();
PARSE_FLAG(sticker->is_mask);
PARSE_FLAG(is_mask);
PARSE_FLAG(has_sticker_set_access_hash);
PARSE_FLAG(in_sticker_set_stored);
PARSE_FLAG(is_tgs);
@ -99,6 +101,11 @@ FileId StickersManager::parse_sticker(bool in_sticker_set, ParserT &parser) {
} else {
sticker->format = StickerFormat::Webp;
}
if (is_mask) {
sticker->type = StickerType::Mask;
} else {
sticker->type = StickerType::Regular;
}
if (in_sticker_set_stored != in_sticker_set) {
Slice data = parser.template fetch_string_raw<Slice>(parser.get_left_len());
for (auto c : data) {
@ -131,7 +138,7 @@ FileId StickersManager::parse_sticker(bool in_sticker_set, ParserT &parser) {
parse(thumbnail, parser);
add_sticker_thumbnail(sticker.get(), thumbnail);
parse(sticker->file_id, parser);
if (sticker->is_mask) {
if (is_mask) {
parse(sticker->point, parser);
parse(sticker->x_shift, parser);
parse(sticker->y_shift, parser);