Add class BackgroundInfo.

This commit is contained in:
levlam 2023-04-10 16:32:59 +03:00
parent 10562f5bac
commit 64b2cb4e12
7 changed files with 141 additions and 46 deletions

View File

@ -293,6 +293,7 @@ set(TDLIB_SOURCE
td/telegram/AuthManager.cpp
td/telegram/AutoDownloadSettings.cpp
td/telegram/AutosaveManager.cpp
td/telegram/BackgroundInfo.cpp
td/telegram/BackgroundManager.cpp
td/telegram/BackgroundType.cpp
td/telegram/BotCommand.cpp
@ -526,6 +527,7 @@ set(TDLIB_SOURCE
td/telegram/AutoDownloadSettings.h
td/telegram/AutosaveManager.h
td/telegram/BackgroundId.h
td/telegram/BackgroundInfo.h
td/telegram/BackgroundManager.h
td/telegram/BackgroundType.h
td/telegram/BotCommand.h
@ -776,6 +778,7 @@ set(TDLIB_SOURCE
td/telegram/AnimationsManager.hpp
td/telegram/AudiosManager.hpp
td/telegram/AuthManager.hpp
td/telegram/BackgroundInfo.hpp
td/telegram/BackgroundType.hpp
td/telegram/DialogNotificationSettings.hpp
td/telegram/DialogFilter.hpp

View File

@ -0,0 +1,25 @@
//
// 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/BackgroundInfo.h"
#include "td/telegram/BackgroundManager.h"
#include "td/telegram/Td.h"
namespace td {
BackgroundInfo::BackgroundInfo(Td *td, telegram_api::object_ptr<telegram_api::WallPaper> &&wallpaper_ptr) {
auto background =
td->background_manager_->on_get_background(BackgroundId(), string(), std::move(wallpaper_ptr), false);
background_id_ = background.first;
background_type_ = std::move(background.second);
}
td_api::object_ptr<td_api::background> BackgroundInfo::get_background_object(const Td *td) const {
return td->background_manager_->get_background_object(background_id_, false, &background_type_);
}
} // namespace td

View File

@ -0,0 +1,56 @@
//
// 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/BackgroundId.h"
#include "td/telegram/BackgroundType.h"
#include "td/utils/common.h"
#include "td/utils/StringBuilder.h"
namespace td {
class Td;
class BackgroundInfo {
BackgroundId background_id_;
BackgroundType background_type_;
friend StringBuilder &operator<<(StringBuilder &string_builder, const BackgroundInfo &background_info);
public:
BackgroundInfo() : background_id_(), background_type_() {
}
BackgroundInfo(Td *td, telegram_api::object_ptr<telegram_api::WallPaper> &&wallpaper_ptr);
td_api::object_ptr<td_api::background> get_background_object(const Td *td) const;
bool is_valid() const {
return background_id_.is_valid();
}
bool operator==(const BackgroundInfo &other) const {
return background_id_ == other.background_id_ && background_type_ == other.background_type_;
}
bool operator!=(const BackgroundInfo &other) const {
return !(*this == other);
}
template <class StorerT>
void store(StorerT &storer) const;
template <class ParserT>
void parse(ParserT &parser);
};
inline StringBuilder &operator<<(StringBuilder &string_builder, const BackgroundInfo &background_info) {
return string_builder << background_info.background_id_ << " with type " << background_info.background_type_;
}
} // namespace td

View File

@ -0,0 +1,30 @@
//
// 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/BackgroundInfo.h"
#include "td/telegram/BackgroundManager.h"
#include "td/telegram/BackgroundType.hpp"
#include "td/telegram/Td.h"
#include "td/utils/tl_helpers.h"
namespace td {
template <class StorerT>
void BackgroundInfo::store(StorerT &storer) const {
storer.context()->td().get_actor_unsafe()->background_manager_->store_background(background_id_, storer);
td::store(background_type_, storer);
}
template <class ParserT>
void BackgroundInfo::parse(ParserT &parser) {
parser.context()->td().get_actor_unsafe()->background_manager_->parse_background(background_id_, parser);
td::parse(background_type_, parser);
}
} // namespace td

View File

@ -11,9 +11,8 @@
#include "td/telegram/AudiosManager.h"
#include "td/telegram/AudiosManager.hpp"
#include "td/telegram/AuthManager.h"
#include "td/telegram/BackgroundId.h"
#include "td/telegram/BackgroundManager.h"
#include "td/telegram/BackgroundType.h"
#include "td/telegram/BackgroundInfo.h"
#include "td/telegram/BackgroundInfo.hpp"
#include "td/telegram/CallDiscardReason.h"
#include "td/telegram/ChannelId.h"
#include "td/telegram/ChatId.h"
@ -902,12 +901,11 @@ class MessageWebViewWriteAccessAllowed final : public MessageContent {
class MessageSetBackground final : public MessageContent {
public:
MessageId old_message_id;
BackgroundId background_id;
BackgroundType background_type;
BackgroundInfo background_info;
MessageSetBackground() = default;
MessageSetBackground(MessageId old_message_id, BackgroundId background_id, BackgroundType background_type)
: old_message_id(old_message_id), background_id(background_id), background_type(std::move(background_type)) {
MessageSetBackground(MessageId old_message_id, BackgroundInfo background_info)
: old_message_id(old_message_id), background_info(std::move(background_info)) {
}
MessageContentType get_type() const final {
@ -1304,8 +1302,7 @@ static void store(const MessageContent *content, StorerT &storer) {
if (has_message_id) {
store(m->old_message_id, storer);
}
storer.context()->td().get_actor_unsafe()->background_manager_->store_background(m->background_id, storer);
store(m->background_type, storer);
store(m->background_info, storer);
break;
}
default:
@ -1835,8 +1832,7 @@ static void parse(unique_ptr<MessageContent> &content, ParserT &parser) {
if (has_message_id) {
parse(m->old_message_id, parser);
}
parser.context()->td().get_actor_unsafe()->background_manager_->parse_background(m->background_id, parser);
parse(m->background_type, parser);
parse(m->background_info, parser);
content = std::move(m);
break;
}
@ -4038,8 +4034,7 @@ void merge_message_contents(Td *td, const MessageContent *old_content, MessageCo
case MessageContentType::SetBackground: {
const auto *old_ = static_cast<const MessageSetBackground *>(old_content);
const auto *new_ = static_cast<const MessageSetBackground *>(new_content);
if (old_->old_message_id != new_->old_message_id || old_->background_id != new_->background_id ||
old_->background_type != new_->background_type) {
if (old_->old_message_id != new_->old_message_id || old_->background_info != new_->background_info) {
need_update = true;
}
break;
@ -5527,12 +5522,11 @@ unique_ptr<MessageContent> get_action_message_content(Td *td, tl_object_ptr<tele
}
case telegram_api::messageActionSetChatWallPaper::ID: {
auto action = move_tl_object_as<telegram_api::messageActionSetChatWallPaper>(action_ptr);
auto background =
td->background_manager_->on_get_background(BackgroundId(), string(), std::move(action->wallpaper_), false);
if (!background.first.is_valid()) {
BackgroundInfo background_info(td, std::move(action->wallpaper_));
if (!background_info.is_valid()) {
break;
}
return make_unique<MessageSetBackground>(MessageId(), background.first, background.second);
return make_unique<MessageSetBackground>(MessageId(), std::move(background_info));
}
case telegram_api::messageActionSetSameChatWallPaper::ID: {
if (reply_in_dialog_id.is_valid() && reply_in_dialog_id != owner_dialog_id) {
@ -5542,12 +5536,11 @@ unique_ptr<MessageContent> get_action_message_content(Td *td, tl_object_ptr<tele
reply_in_dialog_id = DialogId();
}
auto action = move_tl_object_as<telegram_api::messageActionSetSameChatWallPaper>(action_ptr);
auto background =
td->background_manager_->on_get_background(BackgroundId(), string(), std::move(action->wallpaper_), false);
if (!background.first.is_valid()) {
BackgroundInfo background_info(td, std::move(action->wallpaper_));
if (!background_info.is_valid()) {
break;
}
return make_unique<MessageSetBackground>(reply_to_message_id, background.first, background.second);
return make_unique<MessageSetBackground>(reply_to_message_id, std::move(background_info));
}
default:
UNREACHABLE();
@ -5878,9 +5871,8 @@ tl_object_ptr<td_api::MessageContent> get_message_content_object(const MessageCo
}
case MessageContentType::SetBackground: {
const auto *m = static_cast<const MessageSetBackground *>(content);
return td_api::make_object<td_api::messageChatSetBackground>(
m->old_message_id.get(),
td->background_manager_->get_background_object(m->background_id, false, &m->background_type));
return td_api::make_object<td_api::messageChatSetBackground>(m->old_message_id.get(),
m->background_info.get_background_object(td));
}
default:
UNREACHABLE();

View File

@ -7,8 +7,7 @@
#include "td/telegram/ThemeManager.h"
#include "td/telegram/AuthManager.h"
#include "td/telegram/BackgroundManager.h"
#include "td/telegram/BackgroundType.hpp"
#include "td/telegram/BackgroundInfo.hpp"
#include "td/telegram/Global.h"
#include "td/telegram/logevent/LogEvent.h"
#include "td/telegram/net/NetQueryCreator.h"
@ -54,9 +53,8 @@ class GetChatThemesQuery final : public Td::ResultHandler {
bool operator==(const ThemeManager::ThemeSettings &lhs, const ThemeManager::ThemeSettings &rhs) {
return lhs.accent_color == rhs.accent_color && lhs.message_accent_color == rhs.message_accent_color &&
lhs.background_id == rhs.background_id && lhs.background_type == rhs.background_type &&
lhs.base_theme == rhs.base_theme && lhs.message_colors == rhs.message_colors &&
lhs.animate_message_colors == rhs.animate_message_colors;
lhs.background_info == rhs.background_info && lhs.base_theme == rhs.base_theme &&
lhs.message_colors == rhs.message_colors && lhs.animate_message_colors == rhs.animate_message_colors;
}
bool operator!=(const ThemeManager::ThemeSettings &lhs, const ThemeManager::ThemeSettings &rhs) {
@ -67,7 +65,7 @@ template <class StorerT>
void ThemeManager::ThemeSettings::store(StorerT &storer) const {
using td::store;
bool has_message_accent_color = message_accent_color != accent_color;
bool has_background = background_id.is_valid();
bool has_background = background_info.is_valid();
BEGIN_STORE_FLAGS();
STORE_FLAG(animate_message_colors);
STORE_FLAG(has_message_accent_color);
@ -78,8 +76,7 @@ void ThemeManager::ThemeSettings::store(StorerT &storer) const {
store(message_accent_color, storer);
}
if (has_background) {
storer.context()->td().get_actor_unsafe()->background_manager_->store_background(background_id, storer);
store(background_type, storer);
store(background_info, storer);
}
store(base_theme, storer);
store(message_colors, storer);
@ -102,8 +99,7 @@ void ThemeManager::ThemeSettings::parse(ParserT &parser) {
message_accent_color = accent_color;
}
if (has_background) {
parser.context()->td().get_actor_unsafe()->background_manager_->parse_background(background_id, parser);
parse(background_type, parser);
parse(background_info, parser);
}
parse(base_theme, parser);
parse(message_colors, parser);
@ -304,9 +300,8 @@ td_api::object_ptr<td_api::themeSettings> ThemeManager::get_theme_settings_objec
// ignore settings.base_theme for now
return td_api::make_object<td_api::themeSettings>(
settings.accent_color,
td_->background_manager_->get_background_object(settings.background_id, false, &settings.background_type),
std::move(fill), settings.animate_message_colors, settings.message_accent_color);
settings.accent_color, settings.background_info.get_background_object(td_), std::move(fill),
settings.animate_message_colors, settings.message_accent_color);
}
td_api::object_ptr<td_api::chatTheme> ThemeManager::get_chat_theme_object(const ChatTheme &theme) const {
@ -415,14 +410,10 @@ ThemeManager::ThemeSettings ThemeManager::get_chat_theme_settings(
telegram_api::object_ptr<telegram_api::themeSettings> settings) {
ThemeSettings result;
if (settings != nullptr && !settings->message_colors_.empty() && settings->message_colors_.size() <= 4) {
auto background =
td_->background_manager_->on_get_background(BackgroundId(), string(), std::move(settings->wallpaper_), false);
result.accent_color = settings->accent_color_;
bool has_outbox_accent_color = (settings->flags_ & telegram_api::themeSettings::OUTBOX_ACCENT_COLOR_MASK) != 0;
result.message_accent_color = (has_outbox_accent_color ? settings->outbox_accent_color_ : result.accent_color);
result.background_id = background.first;
result.background_type = std::move(background.second);
result.background_info = BackgroundInfo(td_, std::move(settings->wallpaper_));
result.base_theme = get_base_theme(settings->base_theme_);
result.message_colors = std::move(settings->message_colors_);
result.animate_message_colors = settings->message_colors_animated_;

View File

@ -6,8 +6,7 @@
//
#pragma once
#include "td/telegram/BackgroundId.h"
#include "td/telegram/BackgroundType.h"
#include "td/telegram/BackgroundInfo.h"
#include "td/telegram/td_api.h"
#include "td/telegram/telegram_api.h"
@ -43,8 +42,7 @@ class ThemeManager final : public Actor {
struct ThemeSettings {
int32 accent_color = 0;
int32 message_accent_color = 0;
BackgroundId background_id;
BackgroundType background_type;
BackgroundInfo background_info;
BaseTheme base_theme = BaseTheme::Classic;
vector<int32> message_colors;
bool animate_message_colors = false;