From 9171588824f67e0a278d3d014e6a5cf2ad8ff300 Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 18 Dec 2023 15:51:11 +0300 Subject: [PATCH] Add class PeerColor. --- CMakeLists.txt | 4 +++- td/telegram/ContactsManager.cpp | 40 +++++++++++++-------------------- td/telegram/PeerColor.cpp | 33 +++++++++++++++++++++++++++ td/telegram/PeerColor.h | 24 ++++++++++++++++++++ td/telegram/WebPageBlock.cpp | 6 ++--- 5 files changed, 78 insertions(+), 29 deletions(-) create mode 100644 td/telegram/PeerColor.cpp create mode 100644 td/telegram/PeerColor.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 3177b3b01..1f5441918 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -441,14 +441,15 @@ set(TDLIB_SOURCE td/telegram/OrderInfo.cpp td/telegram/Payments.cpp td/telegram/PasswordManager.cpp + td/telegram/PeerColor.cpp td/telegram/PhoneNumberManager.cpp - td/telegram/PrivacyManager.cpp td/telegram/Photo.cpp td/telegram/PhotoSize.cpp td/telegram/PhotoSizeSource.cpp td/telegram/PollManager.cpp td/telegram/Premium.cpp td/telegram/PremiumGiftOption.cpp + td/telegram/PrivacyManager.cpp td/telegram/QueryCombiner.cpp td/telegram/QueryMerger.cpp td/telegram/ReactionManager.cpp @@ -749,6 +750,7 @@ set(TDLIB_SOURCE td/telegram/OrderInfo.h td/telegram/PasswordManager.h td/telegram/Payments.h + td/telegram/PeerColor.h td/telegram/PhoneNumberManager.h td/telegram/Photo.h td/telegram/PhotoFormat.h diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index 215756817..5f5a1f6af 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -37,6 +37,7 @@ #include "td/telegram/NotificationManager.h" #include "td/telegram/OptionManager.h" #include "td/telegram/PasswordManager.h" +#include "td/telegram/PeerColor.h" #include "td/telegram/Photo.h" #include "td/telegram/Photo.hpp" #include "td/telegram/PhotoSize.h" @@ -10950,21 +10951,12 @@ void ContactsManager::on_get_user(tl_object_ptr &&user_ptr, on_update_user_usernames(u, user_id, Usernames{std::move(user->username_), std::move(user->usernames_)}); } on_update_user_emoji_status(u, user_id, EmojiStatus(std::move(user->emoji_status_))); - on_update_user_accent_color_id( - u, user_id, - (user->color_ != nullptr && (user->color_->flags_ & telegram_api::peerColor::COLOR_MASK) != 0 - ? AccentColorId(user->color_->color_) - : AccentColorId())); - on_update_user_background_custom_emoji_id( - u, user_id, (user->color_ != nullptr ? CustomEmojiId(user->color_->background_emoji_id_) : CustomEmojiId())); - on_update_user_profile_accent_color_id( - u, user_id, - (user->profile_color_ != nullptr && (user->profile_color_->flags_ & telegram_api::peerColor::COLOR_MASK) != 0 - ? AccentColorId(user->profile_color_->color_) - : AccentColorId())); - on_update_user_profile_background_custom_emoji_id( - u, user_id, - (user->profile_color_ != nullptr ? CustomEmojiId(user->profile_color_->background_emoji_id_) : CustomEmojiId())); + PeerColor peer_color(user->color_); + on_update_user_accent_color_id(u, user_id, peer_color.accent_color_id_); + on_update_user_background_custom_emoji_id(u, user_id, peer_color.background_custom_emoji_id_); + PeerColor profile_peer_color(user->profile_color_); + on_update_user_profile_accent_color_id(u, user_id, profile_peer_color.accent_color_id_); + on_update_user_profile_background_custom_emoji_id(u, user_id, profile_peer_color.background_custom_emoji_id_); if (is_me_regular_user && is_received) { on_update_user_stories_hidden(u, user_id, stories_hidden); } @@ -19717,9 +19709,8 @@ void ContactsManager::on_get_channel(telegram_api::channel &channel, const char if (td_->auth_manager_->is_bot()) { min_channel->photo_.minithumbnail.clear(); } - if (channel.color_ != nullptr && (channel.color_->flags_ & telegram_api::peerColor::COLOR_MASK) != 0) { - min_channel->accent_color_id_ = AccentColorId(channel.color_->color_); - } + PeerColor peer_color(channel.color_); + min_channel->accent_color_id_ = peer_color.accent_color_id_; min_channel->title_ = std::move(channel.title_); min_channel->is_megagroup_ = is_megagroup; @@ -19793,13 +19784,12 @@ void ContactsManager::on_get_channel(telegram_api::channel &channel, const char on_update_channel_title(c, channel_id, std::move(channel.title_)); on_update_channel_photo(c, channel_id, std::move(channel.photo_)); - on_update_channel_accent_color_id( - c, channel_id, - (channel.color_ != nullptr && (channel.color_->flags_ & telegram_api::peerColor::COLOR_MASK) != 0 - ? AccentColorId(channel.color_->color_) - : AccentColorId())); - on_update_channel_background_custom_emoji_id( - c, channel_id, channel.color_ != nullptr ? CustomEmojiId(channel.color_->background_emoji_id_) : CustomEmojiId()); + PeerColor peer_color(channel.color_); + on_update_channel_accent_color_id(c, channel_id, peer_color.accent_color_id_); + on_update_channel_background_custom_emoji_id(c, channel_id, peer_color.background_custom_emoji_id_); + PeerColor profile_peer_color(channel.profile_color_); + on_update_channel_profile_accent_color_id(c, channel_id, profile_peer_color.accent_color_id_); + on_update_channel_profile_background_custom_emoji_id(c, channel_id, profile_peer_color.background_custom_emoji_id_); on_update_channel_status(c, channel_id, std::move(status)); on_update_channel_usernames( c, channel_id, diff --git a/td/telegram/PeerColor.cpp b/td/telegram/PeerColor.cpp new file mode 100644 index 000000000..c11ceb7c0 --- /dev/null +++ b/td/telegram/PeerColor.cpp @@ -0,0 +1,33 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023 +// +// 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/PeerColor.h" + +#include "td/utils/logging.h" + +namespace td { + +PeerColor::PeerColor(const telegram_api::object_ptr &peer_color) { + if (peer_color == nullptr) { + return; + } + if ((peer_color->flags_ & telegram_api::peerColor::COLOR_MASK) != 0) { + accent_color_id_ = AccentColorId(peer_color->color_); + if (!accent_color_id_.is_valid()) { + LOG(ERROR) << "Receive " << to_string(peer_color); + accent_color_id_ = AccentColorId(); + } + } + if ((peer_color->flags_ & telegram_api::peerColor::BACKGROUND_EMOJI_ID_MASK) != 0) { + background_custom_emoji_id_ = CustomEmojiId(peer_color->background_emoji_id_); + if (!background_custom_emoji_id_.is_valid()) { + LOG(ERROR) << "Receive " << to_string(peer_color); + background_custom_emoji_id_ = CustomEmojiId(); + } + } +} + +} // namespace td diff --git a/td/telegram/PeerColor.h b/td/telegram/PeerColor.h new file mode 100644 index 000000000..04d1083cf --- /dev/null +++ b/td/telegram/PeerColor.h @@ -0,0 +1,24 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023 +// +// 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/AccentColorId.h" +#include "td/telegram/CustomEmojiId.h" +#include "td/telegram/telegram_api.h" + +namespace td { + +class Td; + +struct PeerColor { + AccentColorId accent_color_id_; + CustomEmojiId background_custom_emoji_id_; + + explicit PeerColor(const telegram_api::object_ptr &peer_color); +}; + +} // namespace td diff --git a/td/telegram/WebPageBlock.cpp b/td/telegram/WebPageBlock.cpp index c48219177..11eda13fb 100644 --- a/td/telegram/WebPageBlock.cpp +++ b/td/telegram/WebPageBlock.cpp @@ -21,6 +21,7 @@ #include "td/telegram/files/FileId.h" #include "td/telegram/LinkManager.h" #include "td/telegram/Location.h" +#include "td/telegram/PeerColor.h" #include "td/telegram/Photo.h" #include "td/telegram/Photo.hpp" #include "td/telegram/PhotoFormat.h" @@ -2213,14 +2214,13 @@ unique_ptr get_web_page_block(Td *td, tl_object_ptrflags_ & telegram_api::channel::ACCESS_HASH_MASK) != 0; + PeerColor peer_color(channel->color_); return td::make_unique( std::move(channel->title_), get_dialog_photo(td->file_manager_.get(), DialogId(channel_id), has_access_hash ? channel->access_hash_ : 0, std::move(channel->photo_)), std::move(channel->username_), - channel->color_ != nullptr && (channel->color_->flags_ & telegram_api::peerColor::COLOR_MASK) != 0 - ? AccentColorId(channel->color_->color_) - : AccentColorId(channel_id), + peer_color.accent_color_id_.is_valid() ? peer_color.accent_color_id_ : AccentColorId(channel_id), channel_id); } } else {