Add BackgroundType::get_link() method.

GitOrigin-RevId: be2522d94742608e61adbef223a9b161e87b6aa3
This commit is contained in:
levlam 2019-12-22 04:34:39 +03:00
parent e1f1253e21
commit 8be871bad7
3 changed files with 81 additions and 61 deletions

View File

@ -361,46 +361,18 @@ void BackgroundManager::get_backgrounds(Promise<Unit> &&promise) {
Result<string> BackgroundManager::get_background_url(const string &name,
td_api::object_ptr<td_api::BackgroundType> background_type) const {
TRY_RESULT(type, get_background_type(background_type.get()));
vector<string> modes;
if (type.is_blurred) {
modes.emplace_back("blur");
}
if (type.is_moving) {
modes.emplace_back("motion");
}
string mode = implode(modes, '+');
string url = PSTRING() << G()->shared_config().get_option_string("t_me_url", "https://t.me/") << "bg/";
switch (type.type) {
case BackgroundType::Type::Wallpaper:
url += name;
if (!mode.empty()) {
url += "?mode=";
url += mode;
}
return url;
case BackgroundType::Type::Pattern:
url += name;
url += "?intensity=";
url += to_string(type.intensity);
url += "&bg_color=";
url += type.get_color_hex_string();
if (!mode.empty()) {
url += "&mode=";
url += mode;
}
return url;
case BackgroundType::Type::Solid:
url += type.get_color_hex_string();
return url;
case BackgroundType::Type::Gradient:
url += type.gradient.get_colors_hex_string();
return url;
default:
UNREACHABLE();
return url;
auto url = PSTRING() << G()->shared_config().get_option_string("t_me_url", "https://t.me/") << "bg/";
auto link = type.get_link();
if (type.is_server()) {
url += name;
if (!link.empty()) {
url += '?';
url += link;
}
} else {
url += link;
}
return url;
}
void BackgroundManager::reload_background_from_server(
@ -612,6 +584,7 @@ BackgroundId BackgroundManager::set_background(const td_api::InputBackground *in
promise.set_value(Unit());
return background_id;
}
CHECK(type.is_server());
if (input_background == nullptr) {
promise.set_error(Status::Error(400, "Input background must be non-empty"));
@ -812,7 +785,7 @@ void BackgroundManager::remove_background(BackgroundId background_id, Promise<Un
std::move(promise));
});
if (background->type.type == BackgroundType::Type::Solid || background->type.type == BackgroundType::Type::Gradient) {
if (!background->type.is_server()) {
return query_promise.set_value(Unit());
}

View File

@ -22,33 +22,76 @@ string GradientInfo::get_colors_hex_string() const {
return PSTRING() << get_color_hex_string(top_color) << '-' << get_color_hex_string(bottom_color);
}
bool operator==(const GradientInfo &lhs, const GradientInfo &rhs) {
return lhs.top_color == rhs.top_color && lhs.bottom_color == rhs.bottom_color;
}
string BackgroundType::get_color_hex_string() const {
return td::get_color_hex_string(color);
}
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.color == rhs.color && lhs.intensity == rhs.intensity;
string BackgroundType::get_link() const {
string mode;
if (is_blurred) {
mode = "blur";
}
if (is_moving) {
if (!mode.empty()) {
mode += '+';
}
mode += "motion";
}
switch (type) {
case BackgroundType::Type::Wallpaper: {
if (!mode.empty()) {
return PSTRING() << "mode=" << mode;
}
return string();
}
case BackgroundType::Type::Pattern: {
string link = PSTRING() << "intensity=" << intensity << "&bg_color=" << get_color_hex_string();
if (!mode.empty()) {
link += "&mode=";
link += mode;
}
return link;
}
case BackgroundType::Type::Solid:
return get_color_hex_string();
case BackgroundType::Type::Gradient:
return gradient.get_colors_hex_string();
default:
UNREACHABLE();
return string();
}
}
StringBuilder &operator<<(StringBuilder &string_builder, const BackgroundType &type) {
switch (type.type) {
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.color == rhs.color && lhs.intensity == rhs.intensity && lhs.gradient == rhs.gradient;
}
static StringBuilder &operator<<(StringBuilder &string_builder, const BackgroundType::Type &type) {
switch (type) {
case BackgroundType::Type::Wallpaper:
return string_builder << "type Wallpaper[" << (type.is_blurred ? "blurred" : "") << ' '
<< (type.is_moving ? "moving" : "") << ']';
return string_builder << "Wallpaper";
case BackgroundType::Type::Pattern:
return string_builder << "type Pattern[" << (type.is_moving ? "moving" : "") << ' ' << type.get_color_hex_string()
<< ' ' << type.intensity << ']';
return string_builder << "Pattern";
case BackgroundType::Type::Solid:
return string_builder << "type Solid[" << type.get_color_hex_string() << ']';
return string_builder << "Solid";
case BackgroundType::Type::Gradient:
return string_builder << "type Gradient[" << type.gradient.get_colors_hex_string() << ']';
return string_builder << "Gradient";
default:
UNREACHABLE();
return string_builder;
}
}
StringBuilder &operator<<(StringBuilder &string_builder, const BackgroundType &type) {
return string_builder << "type " << type.type << '[' << type.get_link() << ']';
}
static bool is_valid_color(int32 color) {
return 0 <= color && color <= 0xFFFFFF;
}
@ -171,17 +214,13 @@ telegram_api::object_ptr<telegram_api::wallPaperSettings> get_input_wallpaper_se
if (type.intensity) {
flags |= telegram_api::wallPaperSettings::INTENSITY_MASK;
}
switch (type.type) {
case BackgroundType::Type::Wallpaper:
case BackgroundType::Type::Pattern:
return telegram_api::make_object<telegram_api::wallPaperSettings>(flags, false /*ignored*/, false /*ignored*/,
type.color, 0, type.intensity, 0);
case BackgroundType::Type::Solid:
case BackgroundType::Type::Gradient:
default:
UNREACHABLE();
return nullptr;
if (type.is_server()) {
return telegram_api::make_object<telegram_api::wallPaperSettings>(flags, false /*ignored*/, false /*ignored*/,
type.color, 0, type.intensity, 0);
}
UNREACHABLE();
return nullptr;
}
} // namespace td

View File

@ -28,6 +28,8 @@ struct GradientInfo {
string get_colors_hex_string() const;
};
bool operator==(const GradientInfo &lhs, const GradientInfo &rhs);
struct BackgroundType {
enum class Type : int32 { Wallpaper, Pattern, Solid, Gradient };
Type type = Type::Solid;
@ -49,6 +51,12 @@ struct BackgroundType {
BackgroundType(GradientInfo gradient) : type(Type::Gradient), gradient(gradient) {
}
bool is_server() const {
return type == Type::Wallpaper || type == Type::Pattern;
}
string get_link() const;
string get_color_hex_string() const;
};