Add td_api::backgroundTypeChatTheme for boosted chats.

This commit is contained in:
levlam 2023-12-19 16:00:50 +03:00
parent b3ce27c4ea
commit 93aa765f44
7 changed files with 79 additions and 23 deletions

View File

@ -511,14 +511,14 @@ poll id:int64 question:string options:vector<pollOption> total_voter_count:int32
//@is_default True, if this is one of default backgrounds
//@is_dark True, if the background is dark and is recommended to be used with dark theme
//@name Unique background name
//@document Document with the background; may be null. Null only for filled backgrounds
//@document Document with the background; may be null. Null only for filled and chat theme backgrounds
//@type Type of the background
background id:int64 is_default:Bool is_dark:Bool name:string document:document type:BackgroundType = Background;
//@description Contains a list of backgrounds @backgrounds A list of backgrounds
backgrounds backgrounds:vector<background> = Backgrounds;
//@description Describes a background set for a specific chat @background The background @dark_theme_dimming Dimming of the background in dark themes, as a percentage; 0-100
//@description Describes a background set for a specific chat @background The background @dark_theme_dimming Dimming of the background in dark themes, as a percentage; 0-100. Applied only to Wallpaper and Fill types of background
chatBackground background:background dark_theme_dimming:int32 = ChatBackground;
@ -4814,6 +4814,9 @@ backgroundTypePattern fill:BackgroundFill intensity:int32 is_inverted:Bool is_mo
//@description A filled background @fill The background fill
backgroundTypeFill fill:BackgroundFill = BackgroundType;
//@description A background from a chat theme; can be used only as a chat background in channels @theme_name Name of the chat theme
backgroundTypeChatTheme theme_name:string = BackgroundType;
//@class InputBackground @description Contains information about background to set
@ -7896,9 +7899,9 @@ setChatPermissions chat_id:int53 permissions:chatPermissions = Ok;
//@description Sets the background in a specific chat. Supported only in private and secret chats with non-deleted users
//@chat_id Chat identifier
//@background The input background to use; pass null to create a new filled background
//@background The input background to use; pass null to create a new filled or chat theme background
//@type Background type; pass null to use default background type for the chosen background
//@dark_theme_dimming Dimming of the background in dark themes, as a percentage; 0-100
//@dark_theme_dimming Dimming of the background in dark themes, as a percentage; 0-100. Applied only to Wallpaper and Fill types of background
//@only_for_self Pass true to set background only for self; pass false to set background for both chat users. Background can be set for both users only by Telegram Premium users and if set background isn't of the type inputBackgroundPrevious
setChatBackground chat_id:int53 background:InputBackground type:BackgroundType dark_theme_dimming:int32 only_for_self:Bool = Ok;
@ -9131,7 +9134,7 @@ createInvoiceLink invoice:InputMessageContent = HttpUrl;
getSupportUser = User;
//@description Constructs a persistent HTTP URL for a background @name Background name @type Background type
//@description Constructs a persistent HTTP URL for a background @name Background name @type Background type; backgroundTypeChatTheme isn't supported
getBackgroundUrl name:string type:BackgroundType = HttpUrl;
//@description Searches for a background by its name @name The name of the background
@ -9139,7 +9142,7 @@ searchBackground name:string = Background;
//@description Sets default background for chats; adds the background to the list of installed backgrounds
//@background The input background to use; pass null to create a new filled background
//@type Background type; pass null to use the default type of the remote background
//@type Background type; pass null to use the default type of the remote background; backgroundTypeChatTheme isn't supported
//@for_dark_theme Pass true if the background is set for a dark theme
setDefaultBackground background:InputBackground type:BackgroundType for_dark_theme:Bool = Background;

View File

@ -683,9 +683,12 @@ void BackgroundManager::set_background(const td_api::InputBackground *input_back
TRY_RESULT_PROMISE(promise, type, BackgroundType::get_background_type(background_type, 0));
if (input_background == nullptr) {
if (background_type == nullptr || type.has_file()) {
if (type.has_file() || background_type == nullptr) {
return promise.set_error(Status::Error(400, "Input background must be non-empty for the background type"));
}
if (background_type->get_id() == td_api::backgroundTypeChatTheme::ID) {
return promise.set_error(Status::Error(400, "Background type isn't supported"));
}
auto background_id = add_local_background(type);
set_background_id(background_id, type, for_dark_theme);
@ -745,8 +748,9 @@ Result<DialogId> BackgroundManager::get_background_dialog(DialogId dialog_id) {
case DialogType::User:
return dialog_id;
case DialogType::Chat:
case DialogType::Channel:
return Status::Error(400, "Can't change background in the chat");
case DialogType::Channel:
return dialog_id;
case DialogType::SecretChat: {
auto user_id = td_->contacts_manager_->get_secret_chat_user_id(dialog_id.get_secret_chat_id());
if (!user_id.is_valid()) {

View File

@ -50,6 +50,9 @@ BackgroundFill::BackgroundFill(const telegram_api::wallPaperSettings *settings)
}
auto flags = settings->flags_;
if ((flags & telegram_api::wallPaperSettings::EMOTICON_MASK) != 0) {
LOG(ERROR) << "Receive filled background with " << to_string(*settings);
}
if ((flags & telegram_api::wallPaperSettings::BACKGROUND_COLOR_MASK) != 0) {
top_color_ = settings->background_color_;
if (!validate_alpha_color(top_color_)) {
@ -194,12 +197,12 @@ Result<BackgroundFill> BackgroundFill::get_background_fill(Slice name) {
string BackgroundFill::get_link(bool is_first) const {
switch (get_type()) {
case BackgroundFill::Type::Solid:
case Type::Solid:
return get_color_hex_string(top_color_);
case BackgroundFill::Type::Gradient:
case Type::Gradient:
return PSTRING() << get_color_hex_string(top_color_) << '-' << get_color_hex_string(bottom_color_)
<< (is_first ? '?' : '&') << "rotation=" << rotation_angle_;
case BackgroundFill::Type::FreeformGradient: {
case Type::FreeformGradient: {
SliceBuilder sb;
sb << get_color_hex_string(top_color_) << '~' << get_color_hex_string(bottom_color_) << '~'
<< get_color_hex_string(third_color_);
@ -305,6 +308,8 @@ string BackgroundType::get_link(bool is_first) const {
}
case Type::Fill:
return fill_.get_link(is_first);
case Type::ChatTheme:
return string();
default:
UNREACHABLE();
return string();
@ -313,7 +318,7 @@ string BackgroundType::get_link(bool is_first) const {
bool operator==(const BackgroundType &lhs, const BackgroundType &rhs) {
return lhs.type_ == rhs.type_ && lhs.is_blurred_ == rhs.is_blurred_ && lhs.is_moving_ == rhs.is_moving_ &&
lhs.intensity_ == rhs.intensity_ && lhs.fill_ == rhs.fill_;
lhs.intensity_ == rhs.intensity_ && lhs.fill_ == rhs.fill_ && lhs.theme_name_ == rhs.theme_name_;
}
StringBuilder &operator<<(StringBuilder &string_builder, const BackgroundType &type) {
@ -328,6 +333,9 @@ StringBuilder &operator<<(StringBuilder &string_builder, const BackgroundType &t
case BackgroundType::Type::Fill:
string_builder << "Fill";
break;
case BackgroundType::Type::ChatTheme:
string_builder << "ChatTheme";
break;
default:
UNREACHABLE();
break;
@ -363,6 +371,10 @@ Result<BackgroundType> BackgroundType::get_background_type(const td_api::Backgro
TRY_RESULT(background_fill, BackgroundFill::get_background_fill(fill_type->fill_.get()));
return BackgroundType(std::move(background_fill), dark_theme_dimming);
}
case td_api::backgroundTypeChatTheme::ID: {
auto chat_theme_type = static_cast<const td_api::backgroundTypeChatTheme *>(background_type);
return BackgroundType(chat_theme_type->theme_name_);
}
default:
UNREACHABLE();
return BackgroundType();
@ -378,7 +390,7 @@ bool BackgroundType::is_background_name_local(Slice name) {
return name.size() <= 13u || name.find('?') <= 13u || !is_base64url_characters(name.substr(0, name.find('?')));
}
BackgroundType::BackgroundType(bool is_fill, bool is_pattern,
BackgroundType::BackgroundType(bool has_no_file, bool is_pattern,
telegram_api::object_ptr<telegram_api::wallPaperSettings> settings) {
if (settings != nullptr && (settings->flags_ & telegram_api::wallPaperSettings::INTENSITY_MASK) != 0) {
intensity_ = settings->intensity_;
@ -387,10 +399,15 @@ BackgroundType::BackgroundType(bool is_fill, bool is_pattern,
intensity_ = is_pattern ? 50 : 0;
}
}
if (is_fill) {
type_ = Type::Fill;
if (has_no_file) {
CHECK(settings != nullptr);
if ((settings->flags_ & telegram_api::wallPaperSettings::EMOTICON_MASK) != 0) {
type_ = Type::ChatTheme;
theme_name_ = std::move(settings->emoticon_);
} else {
type_ = Type::Fill;
fill_ = BackgroundFill(settings.get());
}
} else if (is_pattern) {
type_ = Type::Pattern;
if (settings != nullptr) {
@ -408,11 +425,11 @@ BackgroundType::BackgroundType(bool is_fill, bool is_pattern,
td_api::object_ptr<td_api::BackgroundFill> BackgroundFill::get_background_fill_object() const {
switch (get_type()) {
case BackgroundFill::Type::Solid:
case Type::Solid:
return td_api::make_object<td_api::backgroundFillSolid>(top_color_);
case BackgroundFill::Type::Gradient:
case Type::Gradient:
return td_api::make_object<td_api::backgroundFillGradient>(top_color_, bottom_color_, rotation_angle_);
case BackgroundFill::Type::FreeformGradient: {
case Type::FreeformGradient: {
vector<int32> colors{top_color_, bottom_color_, third_color_, fourth_color_};
if (colors.back() == -1) {
colors.pop_back();
@ -434,6 +451,8 @@ td_api::object_ptr<td_api::BackgroundType> BackgroundType::get_background_type_o
fill_.get_background_fill_object(), intensity_ < 0 ? -intensity_ : intensity_, intensity_ < 0, is_moving_);
case Type::Fill:
return td_api::make_object<td_api::backgroundTypeFill>(fill_.get_background_fill_object());
case Type::ChatTheme:
return td_api::make_object<td_api::backgroundTypeChatTheme>(theme_name_);
default:
UNREACHABLE();
return nullptr;
@ -464,12 +483,15 @@ telegram_api::object_ptr<telegram_api::wallPaperSettings> BackgroundType::get_in
default:
UNREACHABLE();
}
if (!theme_name_.empty()) {
flags |= telegram_api::wallPaperSettings::EMOTICON_MASK;
}
if (intensity_ != 0) {
flags |= telegram_api::wallPaperSettings::INTENSITY_MASK;
}
return telegram_api::make_object<telegram_api::wallPaperSettings>(
flags, is_blurred_, is_moving_, fill_.top_color_, fill_.bottom_color_, fill_.third_color_, fill_.fourth_color_,
intensity_, fill_.rotation_angle_, string());
intensity_, fill_.rotation_angle_, theme_name_);
}
} // namespace td

View File

@ -64,12 +64,13 @@ class BackgroundFill {
bool operator==(const BackgroundFill &lhs, const BackgroundFill &rhs);
class BackgroundType {
enum class Type : int32 { Wallpaper, Pattern, Fill };
enum class Type : int32 { Wallpaper, Pattern, Fill, ChatTheme };
Type type_ = Type::Fill;
bool is_blurred_ = false;
bool is_moving_ = false;
int32 intensity_ = 0;
BackgroundFill fill_;
string theme_name_;
friend bool operator==(const BackgroundType &lhs, const BackgroundType &rhs);
@ -84,11 +85,13 @@ class BackgroundType {
BackgroundType(BackgroundFill &&fill, int32 dark_theme_dimming)
: type_(Type::Fill), intensity_(dark_theme_dimming), fill_(std::move(fill)) {
}
explicit BackgroundType(string theme_name) : type_(Type::ChatTheme), theme_name_(std::move(theme_name)) {
}
public:
BackgroundType() = default;
BackgroundType(bool is_fill, bool is_pattern, telegram_api::object_ptr<telegram_api::wallPaperSettings> settings);
BackgroundType(bool has_no_file, bool is_pattern, telegram_api::object_ptr<telegram_api::wallPaperSettings> settings);
static Result<BackgroundType> get_background_type(const td_api::BackgroundType *background_type,
int32 dark_theme_dimming);

View File

@ -20,6 +20,7 @@ void BackgroundType::store(StorerT &storer) const {
auto fill_type = fill_.get_type();
bool is_gradient = fill_type == BackgroundFill::Type::Gradient;
bool is_freeform_gradient = fill_type == BackgroundFill::Type::FreeformGradient;
bool has_theme_name = !theme_name_.empty();
BEGIN_STORE_FLAGS();
STORE_FLAG(is_blurred_);
STORE_FLAG(is_moving_);
@ -27,6 +28,7 @@ void BackgroundType::store(StorerT &storer) const {
STORE_FLAG(has_intensity);
STORE_FLAG(is_gradient);
STORE_FLAG(is_freeform_gradient);
STORE_FLAG(has_theme_name);
END_STORE_FLAGS();
store(type_, storer);
if (is_freeform_gradient) {
@ -44,6 +46,9 @@ void BackgroundType::store(StorerT &storer) const {
if (has_intensity) {
store(intensity_, storer);
}
if (has_theme_name) {
store(theme_name_, storer);
}
}
template <class ParserT>
@ -53,6 +58,7 @@ void BackgroundType::parse(ParserT &parser) {
bool has_intensity;
bool is_gradient;
bool is_freeform_gradient;
bool has_theme_name;
BEGIN_PARSE_FLAGS();
PARSE_FLAG(is_blurred_);
PARSE_FLAG(is_moving_);
@ -60,6 +66,7 @@ void BackgroundType::parse(ParserT &parser) {
PARSE_FLAG(has_intensity);
PARSE_FLAG(is_gradient);
PARSE_FLAG(is_freeform_gradient);
PARSE_FLAG(has_theme_name);
END_PARSE_FLAGS();
parse(type_, parser);
if (is_freeform_gradient) {
@ -79,6 +86,9 @@ void BackgroundType::parse(ParserT &parser) {
if (has_intensity) {
parse(intensity_, parser);
}
if (has_theme_name) {
parse(theme_name_, parser);
}
}
} // namespace td

View File

@ -2498,6 +2498,9 @@ Result<string> LinkManager::get_background_url(const string &name,
if (background_type == nullptr) {
return Status::Error(400, "Type must be non-empty");
}
if (background_type->get_id() == td_api::backgroundTypeChatTheme::ID) {
return Status::Error(400, "Background has no link");
}
TRY_RESULT(type, BackgroundType::get_background_type(background_type.get(), 0));
auto url = PSTRING() << get_t_me_url() << "bg/";
auto link = type.get_link();

View File

@ -1150,9 +1150,10 @@ class CliClient final : public Actor {
}
struct BackgroundType {
enum class Type : int32 { Null, Wallpaper, SolidPattern, GradientPattern, Fill };
enum class Type : int32 { Null, Wallpaper, SolidPattern, GradientPattern, Fill, ChatTheme };
Type type = Type::Null;
vector<int32> colors;
string theme_name;
operator td_api::object_ptr<td_api::BackgroundType>() const {
switch (type) {
@ -1172,6 +1173,8 @@ class CliClient final : public Actor {
return as_gradient_background(colors[0], colors[1]);
}
return as_freeform_gradient_background(colors);
case Type::ChatTheme:
return as_chat_theme_background(theme_name);
default:
UNREACHABLE();
return nullptr;
@ -1190,6 +1193,9 @@ class CliClient final : public Actor {
arg.type = BackgroundType::Type::SolidPattern;
} else if (args == "gp") {
arg.type = BackgroundType::Type::GradientPattern;
} else if (args[0] == 't') {
arg.type = BackgroundType::Type::ChatTheme;
arg.theme_name = args.substr(1);
} else {
arg.type = BackgroundType::Type::Fill;
arg.colors = to_integers<int32>(args);
@ -2257,6 +2263,10 @@ class CliClient final : public Actor {
return td_api::make_object<td_api::backgroundTypeFill>(as_background_fill(std::move(colors)));
}
static td_api::object_ptr<td_api::BackgroundType> as_chat_theme_background(const string &theme_name) {
return td_api::make_object<td_api::backgroundTypeChatTheme>(theme_name);
}
td_api::object_ptr<td_api::phoneNumberAuthenticationSettings> as_phone_number_authentication_settings() const {
return td_api::make_object<td_api::phoneNumberAuthenticationSettings>(false, true, false, false, nullptr,
vector<string>(authentication_tokens_));
@ -3117,6 +3127,7 @@ class CliClient final : public Actor {
send_get_background_url(as_freeform_gradient_background({0xFEDCBA, 0x222222}));
send_get_background_url(as_freeform_gradient_background({0xFEDCBA, 0x111111, 0x222222}));
send_get_background_url(as_freeform_gradient_background({0xABCDEF, 0xFEDCBA, 0x111111, 0x222222}));
send_get_background_url(as_chat_theme_background(args));
} else {
op_not_found_count++;
}