2019-05-07 04:51:56 +02:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2019-05-07 04:51:56 +02:00
|
|
|
//
|
|
|
|
// 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/BackgroundManager.h"
|
|
|
|
|
2019-05-10 18:04:14 +02:00
|
|
|
#include "td/telegram/AuthManager.h"
|
2019-05-10 16:58:44 +02:00
|
|
|
#include "td/telegram/BackgroundType.hpp"
|
2019-05-07 04:51:56 +02:00
|
|
|
#include "td/telegram/DialogId.h"
|
|
|
|
#include "td/telegram/Document.h"
|
|
|
|
#include "td/telegram/DocumentsManager.h"
|
2019-05-10 16:58:44 +02:00
|
|
|
#include "td/telegram/DocumentsManager.hpp"
|
2019-05-07 04:51:56 +02:00
|
|
|
#include "td/telegram/FileReferenceManager.h"
|
|
|
|
#include "td/telegram/files/FileManager.h"
|
|
|
|
#include "td/telegram/files/FileType.h"
|
|
|
|
#include "td/telegram/Global.h"
|
2022-04-09 22:21:07 +02:00
|
|
|
#include "td/telegram/PhotoFormat.h"
|
2019-05-07 04:51:56 +02:00
|
|
|
#include "td/telegram/Td.h"
|
2019-05-10 16:58:44 +02:00
|
|
|
#include "td/telegram/TdDb.h"
|
2021-09-18 23:47:05 +02:00
|
|
|
#include "td/telegram/TdParameters.h"
|
2019-05-07 04:51:56 +02:00
|
|
|
|
2019-07-19 04:34:49 +02:00
|
|
|
#include "td/db/SqliteKeyValueAsync.h"
|
|
|
|
|
2021-01-01 13:59:53 +01:00
|
|
|
#include "td/utils/algorithm.h"
|
2021-05-21 00:49:59 +02:00
|
|
|
#include "td/utils/base64.h"
|
2019-05-07 04:51:56 +02:00
|
|
|
#include "td/utils/buffer.h"
|
|
|
|
#include "td/utils/common.h"
|
2019-07-23 00:50:12 +02:00
|
|
|
#include "td/utils/format.h"
|
2019-05-14 16:26:13 +02:00
|
|
|
#include "td/utils/logging.h"
|
2019-05-07 04:51:56 +02:00
|
|
|
#include "td/utils/misc.h"
|
2019-07-23 00:50:12 +02:00
|
|
|
#include "td/utils/Slice.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2019-05-14 16:26:13 +02:00
|
|
|
#include "td/utils/tl_helpers.h"
|
2019-05-07 04:51:56 +02:00
|
|
|
|
2022-06-02 15:19:16 +02:00
|
|
|
#include <algorithm>
|
|
|
|
|
2019-05-07 04:51:56 +02:00
|
|
|
namespace td {
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class GetBackgroundQuery final : public Td::ResultHandler {
|
2019-05-07 17:21:57 +02:00
|
|
|
Promise<Unit> promise_;
|
|
|
|
BackgroundId background_id_;
|
2019-07-19 04:34:49 +02:00
|
|
|
string background_name_;
|
2019-05-07 17:21:57 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
explicit GetBackgroundQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
2019-07-19 04:34:49 +02:00
|
|
|
void send(BackgroundId background_id, const string &background_name,
|
|
|
|
telegram_api::object_ptr<telegram_api::InputWallPaper> &&input_wallpaper) {
|
2019-05-07 17:21:57 +02:00
|
|
|
background_id_ = background_id;
|
2019-07-19 04:34:49 +02:00
|
|
|
background_name_ = background_name;
|
2020-03-15 22:17:11 +01:00
|
|
|
send_query(G()->net_query_creator().create(telegram_api::account_getWallPaper(std::move(input_wallpaper))));
|
2019-05-07 17:21:57 +02:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2019-05-07 17:21:57 +02:00
|
|
|
auto result_ptr = fetch_result<telegram_api::account_getWallPaper>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2019-05-07 17:21:57 +02:00
|
|
|
}
|
|
|
|
|
2021-11-08 13:20:38 +01:00
|
|
|
td_->background_manager_->on_get_background(background_id_, background_name_, result_ptr.move_as_ok(), true);
|
2019-05-07 17:21:57 +02:00
|
|
|
|
|
|
|
promise_.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2020-09-24 15:08:04 +02:00
|
|
|
LOG(INFO) << "Receive error for GetBackgroundQuery for " << background_id_ << "/" << background_name_ << ": "
|
|
|
|
<< status;
|
2019-05-07 17:21:57 +02:00
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class GetBackgroundsQuery final : public Td::ResultHandler {
|
2019-05-07 04:51:56 +02:00
|
|
|
Promise<telegram_api::object_ptr<telegram_api::account_WallPapers>> promise_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit GetBackgroundsQuery(Promise<telegram_api::object_ptr<telegram_api::account_WallPapers>> &&promise)
|
|
|
|
: promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void send() {
|
2020-03-15 22:17:11 +01:00
|
|
|
send_query(G()->net_query_creator().create(telegram_api::account_getWallPapers(0)));
|
2019-05-07 04:51:56 +02:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2019-05-07 04:51:56 +02:00
|
|
|
auto result_ptr = fetch_result<telegram_api::account_getWallPapers>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2019-05-07 04:51:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
promise_.set_value(result_ptr.move_as_ok());
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2019-05-07 04:51:56 +02:00
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class InstallBackgroundQuery final : public Td::ResultHandler {
|
2019-05-10 14:36:37 +02:00
|
|
|
Promise<Unit> promise_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit InstallBackgroundQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
2021-06-09 18:01:16 +02:00
|
|
|
void send(telegram_api::object_ptr<telegram_api::InputWallPaper> input_wallpaper, const BackgroundType &type) {
|
|
|
|
send_query(G()->net_query_creator().create(
|
2021-06-09 20:19:08 +02:00
|
|
|
telegram_api::account_installWallPaper(std::move(input_wallpaper), type.get_input_wallpaper_settings())));
|
2019-05-10 14:36:37 +02:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2019-05-10 14:36:37 +02:00
|
|
|
auto result_ptr = fetch_result<telegram_api::account_installWallPaper>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2019-05-10 14:36:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
LOG_IF(INFO, !result_ptr.ok()) << "Receive false from account.installWallPaper";
|
|
|
|
promise_.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2019-05-10 14:36:37 +02:00
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class UploadBackgroundQuery final : public Td::ResultHandler {
|
2022-11-17 16:48:48 +01:00
|
|
|
Promise<td_api::object_ptr<td_api::background>> promise_;
|
2019-05-10 14:36:37 +02:00
|
|
|
FileId file_id_;
|
|
|
|
BackgroundType type_;
|
2019-06-18 00:39:57 +02:00
|
|
|
bool for_dark_theme_;
|
2019-05-10 14:36:37 +02:00
|
|
|
|
|
|
|
public:
|
2022-11-17 16:48:48 +01:00
|
|
|
explicit UploadBackgroundQuery(Promise<td_api::object_ptr<td_api::background>> &&promise)
|
|
|
|
: promise_(std::move(promise)) {
|
2019-05-10 14:36:37 +02:00
|
|
|
}
|
|
|
|
|
2019-06-18 00:39:57 +02:00
|
|
|
void send(FileId file_id, tl_object_ptr<telegram_api::InputFile> &&input_file, const BackgroundType &type,
|
|
|
|
bool for_dark_theme) {
|
2019-05-10 14:36:37 +02:00
|
|
|
CHECK(input_file != nullptr);
|
|
|
|
file_id_ = file_id;
|
|
|
|
type_ = type;
|
2019-06-18 00:39:57 +02:00
|
|
|
for_dark_theme_ = for_dark_theme;
|
2021-06-09 19:58:39 +02:00
|
|
|
send_query(G()->net_query_creator().create(telegram_api::account_uploadWallPaper(
|
2021-08-24 16:13:51 +02:00
|
|
|
std::move(input_file), type_.get_mime_type(), type_.get_input_wallpaper_settings())));
|
2019-05-10 14:36:37 +02:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2019-05-10 14:36:37 +02:00
|
|
|
auto result_ptr = fetch_result<telegram_api::account_uploadWallPaper>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2019-05-10 14:36:37 +02:00
|
|
|
}
|
|
|
|
|
2021-11-08 13:20:38 +01:00
|
|
|
td_->background_manager_->on_uploaded_background_file(file_id_, type_, for_dark_theme_, result_ptr.move_as_ok(),
|
|
|
|
std::move(promise_));
|
2019-05-10 14:36:37 +02:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2019-05-10 14:36:37 +02:00
|
|
|
CHECK(file_id_.is_valid());
|
|
|
|
if (begins_with(status.message(), "FILE_PART_") && ends_with(status.message(), "_MISSING")) {
|
2021-11-08 13:20:38 +01:00
|
|
|
// TODO td_->background_manager_->on_upload_background_file_part_missing(file_id_, to_integer<int32>(status.message().substr(10)));
|
2019-05-10 14:36:37 +02:00
|
|
|
// return;
|
|
|
|
} else {
|
|
|
|
if (status.code() != 429 && status.code() < 500 && !G()->close_flag()) {
|
2021-11-08 13:20:38 +01:00
|
|
|
td_->file_manager_->delete_partial_remote_location(file_id_);
|
2019-05-10 14:36:37 +02:00
|
|
|
}
|
|
|
|
}
|
2021-11-08 13:20:38 +01:00
|
|
|
td_->file_manager_->cancel_upload(file_id_);
|
2019-05-10 14:36:37 +02:00
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class UnsaveBackgroundQuery final : public Td::ResultHandler {
|
2019-05-10 22:55:26 +02:00
|
|
|
Promise<Unit> promise_;
|
|
|
|
|
|
|
|
public:
|
2021-06-07 19:46:57 +02:00
|
|
|
explicit UnsaveBackgroundQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
|
2019-05-10 22:55:26 +02:00
|
|
|
}
|
|
|
|
|
2021-06-07 19:46:57 +02:00
|
|
|
void send(telegram_api::object_ptr<telegram_api::InputWallPaper> input_wallpaper) {
|
2020-03-15 22:17:11 +01:00
|
|
|
send_query(G()->net_query_creator().create(telegram_api::account_saveWallPaper(
|
2021-06-07 19:46:57 +02:00
|
|
|
std::move(input_wallpaper), true, telegram_api::make_object<telegram_api::wallPaperSettings>())));
|
2019-05-10 22:55:26 +02:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2019-05-10 22:55:26 +02:00
|
|
|
auto result_ptr = fetch_result<telegram_api::account_saveWallPaper>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2019-05-10 22:55:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool result = result_ptr.move_as_ok();
|
2019-05-12 03:10:18 +02:00
|
|
|
LOG(INFO) << "Receive result for save background: " << result;
|
2019-05-10 22:55:26 +02:00
|
|
|
promise_.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2020-03-30 23:35:58 +02:00
|
|
|
if (!G()->is_expected_error(status)) {
|
2019-05-10 22:55:26 +02:00
|
|
|
LOG(ERROR) << "Receive error for save background: " << status;
|
|
|
|
}
|
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class ResetBackgroundsQuery final : public Td::ResultHandler {
|
2019-05-12 03:10:18 +02:00
|
|
|
Promise<Unit> promise_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit ResetBackgroundsQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void send() {
|
2020-03-15 22:17:11 +01:00
|
|
|
send_query(G()->net_query_creator().create(telegram_api::account_resetWallPapers()));
|
2019-05-12 03:10:18 +02:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2019-05-12 03:10:18 +02:00
|
|
|
auto result_ptr = fetch_result<telegram_api::account_resetWallPapers>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2019-05-12 03:10:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool result = result_ptr.move_as_ok();
|
|
|
|
LOG(INFO) << "Receive result for reset backgrounds: " << result;
|
|
|
|
promise_.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2020-03-30 23:35:58 +02:00
|
|
|
if (!G()->is_expected_error(status)) {
|
2019-05-12 03:10:18 +02:00
|
|
|
LOG(ERROR) << "Receive error for reset backgrounds: " << status;
|
|
|
|
}
|
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class BackgroundManager::UploadBackgroundFileCallback final : public FileManager::UploadCallback {
|
2019-05-10 14:36:37 +02:00
|
|
|
public:
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_upload_ok(FileId file_id, tl_object_ptr<telegram_api::InputFile> input_file) final {
|
2019-05-10 14:36:37 +02:00
|
|
|
send_closure_later(G()->background_manager(), &BackgroundManager::on_upload_background_file, file_id,
|
|
|
|
std::move(input_file));
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_upload_encrypted_ok(FileId file_id, tl_object_ptr<telegram_api::InputEncryptedFile> input_file) final {
|
2019-05-10 14:36:37 +02:00
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_upload_secure_ok(FileId file_id, tl_object_ptr<telegram_api::InputSecureFile> input_file) final {
|
2019-05-10 14:36:37 +02:00
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_upload_error(FileId file_id, Status error) final {
|
2019-05-10 14:36:37 +02:00
|
|
|
send_closure_later(G()->background_manager(), &BackgroundManager::on_upload_background_file_error, file_id,
|
|
|
|
std::move(error));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-05-07 04:51:56 +02:00
|
|
|
BackgroundManager::BackgroundManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) {
|
2019-05-10 14:36:37 +02:00
|
|
|
upload_background_file_callback_ = std::make_shared<UploadBackgroundFileCallback>();
|
2019-05-07 04:51:56 +02:00
|
|
|
}
|
|
|
|
|
2019-07-18 21:12:46 +02:00
|
|
|
template <class StorerT>
|
|
|
|
void BackgroundManager::Background::store(StorerT &storer) const {
|
|
|
|
bool has_file_id = file_id.is_valid();
|
|
|
|
BEGIN_STORE_FLAGS();
|
|
|
|
STORE_FLAG(is_creator);
|
|
|
|
STORE_FLAG(is_default);
|
|
|
|
STORE_FLAG(is_dark);
|
|
|
|
STORE_FLAG(has_file_id);
|
2021-06-10 02:57:34 +02:00
|
|
|
STORE_FLAG(has_new_local_id);
|
2019-07-18 21:12:46 +02:00
|
|
|
END_STORE_FLAGS();
|
|
|
|
td::store(id, storer);
|
|
|
|
td::store(access_hash, storer);
|
|
|
|
td::store(name, storer);
|
|
|
|
if (has_file_id) {
|
|
|
|
storer.context()->td().get_actor_unsafe()->documents_manager_->store_document(file_id, storer);
|
|
|
|
}
|
|
|
|
td::store(type, storer);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ParserT>
|
|
|
|
void BackgroundManager::Background::parse(ParserT &parser) {
|
|
|
|
bool has_file_id;
|
|
|
|
BEGIN_PARSE_FLAGS();
|
|
|
|
PARSE_FLAG(is_creator);
|
|
|
|
PARSE_FLAG(is_default);
|
|
|
|
PARSE_FLAG(is_dark);
|
|
|
|
PARSE_FLAG(has_file_id);
|
2021-06-10 02:57:34 +02:00
|
|
|
PARSE_FLAG(has_new_local_id);
|
2019-07-18 21:12:46 +02:00
|
|
|
END_PARSE_FLAGS();
|
|
|
|
td::parse(id, parser);
|
|
|
|
td::parse(access_hash, parser);
|
|
|
|
td::parse(name, parser);
|
|
|
|
if (has_file_id) {
|
|
|
|
file_id = parser.context()->td().get_actor_unsafe()->documents_manager_->parse_document(parser);
|
|
|
|
} else {
|
|
|
|
file_id = FileId();
|
|
|
|
}
|
|
|
|
td::parse(type, parser);
|
|
|
|
}
|
|
|
|
|
2019-05-10 16:58:44 +02:00
|
|
|
class BackgroundManager::BackgroundLogEvent {
|
|
|
|
public:
|
2019-07-18 21:12:46 +02:00
|
|
|
Background background_;
|
2019-05-10 16:58:44 +02:00
|
|
|
BackgroundType set_type_;
|
|
|
|
|
|
|
|
template <class StorerT>
|
|
|
|
void store(StorerT &storer) const {
|
2019-07-18 21:12:46 +02:00
|
|
|
td::store(background_, storer);
|
2019-05-10 16:58:44 +02:00
|
|
|
td::store(set_type_, storer);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ParserT>
|
|
|
|
void parse(ParserT &parser) {
|
2019-07-18 21:12:46 +02:00
|
|
|
td::parse(background_, parser);
|
2019-05-10 16:58:44 +02:00
|
|
|
td::parse(set_type_, parser);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-06-10 17:58:19 +02:00
|
|
|
class BackgroundManager::BackgroundsLogEvent {
|
|
|
|
public:
|
|
|
|
vector<Background> backgrounds_;
|
|
|
|
|
|
|
|
template <class StorerT>
|
|
|
|
void store(StorerT &storer) const {
|
|
|
|
td::store(backgrounds_, storer);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ParserT>
|
|
|
|
void parse(ParserT &parser) {
|
|
|
|
td::parse(backgrounds_, parser);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-05-10 16:58:44 +02:00
|
|
|
void BackgroundManager::start_up() {
|
2021-06-10 02:57:34 +02:00
|
|
|
max_local_background_id_ = BackgroundId(to_integer<int64>(G()->td_db()->get_binlog_pmc()->get("max_bg_id")));
|
|
|
|
|
|
|
|
// first parse all log events and fix max_local_background_id_ value
|
|
|
|
bool has_selected_background[2] = {false, false};
|
|
|
|
BackgroundLogEvent selected_background_log_event[2];
|
2019-06-18 00:39:57 +02:00
|
|
|
for (int i = 0; i < 2; i++) {
|
2019-09-08 22:40:30 +02:00
|
|
|
bool for_dark_theme = i != 0;
|
2020-09-22 01:15:09 +02:00
|
|
|
auto log_event_string = G()->td_db()->get_binlog_pmc()->get(get_background_database_key(for_dark_theme));
|
|
|
|
if (!log_event_string.empty()) {
|
2021-06-10 02:57:34 +02:00
|
|
|
has_selected_background[i] = true;
|
|
|
|
log_event_parse(selected_background_log_event[i], log_event_string).ensure();
|
|
|
|
const Background &background = selected_background_log_event[i].background_;
|
|
|
|
if (background.has_new_local_id && background.id.is_local() && !background.type.has_file() &&
|
|
|
|
background.id.get() > max_local_background_id_.get()) {
|
|
|
|
set_max_local_background_id(background.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-10 17:58:19 +02:00
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
bool for_dark_theme = i != 0;
|
|
|
|
auto log_event_string = G()->td_db()->get_binlog_pmc()->get(get_local_backgrounds_database_key(for_dark_theme));
|
|
|
|
if (!log_event_string.empty()) {
|
|
|
|
BackgroundsLogEvent log_event;
|
|
|
|
log_event_parse(log_event, log_event_string).ensure();
|
|
|
|
for (const auto &background : log_event.backgrounds_) {
|
|
|
|
CHECK(background.has_new_local_id);
|
|
|
|
CHECK(background.id.is_valid());
|
|
|
|
CHECK(background.id.is_local());
|
|
|
|
CHECK(!background.type.has_file());
|
|
|
|
CHECK(!background.file_id.is_valid());
|
|
|
|
if (background.id.get() > max_local_background_id_.get()) {
|
|
|
|
set_max_local_background_id(background.id);
|
|
|
|
}
|
2021-08-27 11:10:51 +02:00
|
|
|
add_background(background, true);
|
2021-06-10 17:58:19 +02:00
|
|
|
local_background_ids_[for_dark_theme].push_back(background.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// then add selected backgrounds fixing their ID
|
2021-06-10 02:57:34 +02:00
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
bool for_dark_theme = i != 0;
|
|
|
|
if (has_selected_background[i]) {
|
|
|
|
Background &background = selected_background_log_event[i].background_;
|
|
|
|
|
|
|
|
bool need_resave = false;
|
|
|
|
if (!background.has_new_local_id && !background.type.has_file()) {
|
|
|
|
background.has_new_local_id = true;
|
|
|
|
background.id = get_next_local_background_id();
|
|
|
|
need_resave = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK(background.id.is_valid());
|
|
|
|
if (background.file_id.is_valid() != background.type.has_file()) {
|
|
|
|
LOG(ERROR) << "Failed to load " << background.id << " of " << background.type;
|
|
|
|
need_resave = true;
|
|
|
|
} else {
|
|
|
|
set_background_id_[for_dark_theme] = background.id;
|
|
|
|
set_background_type_[for_dark_theme] = selected_background_log_event[i].set_type_;
|
|
|
|
|
2021-08-27 11:10:51 +02:00
|
|
|
add_background(background, false);
|
2019-07-19 02:50:51 +02:00
|
|
|
}
|
2019-06-18 00:39:57 +02:00
|
|
|
|
2021-06-10 02:57:34 +02:00
|
|
|
if (need_resave) {
|
|
|
|
save_background_id(for_dark_theme);
|
|
|
|
}
|
2019-05-10 16:58:44 +02:00
|
|
|
}
|
2019-05-10 17:55:55 +02:00
|
|
|
|
2019-06-18 00:39:57 +02:00
|
|
|
send_update_selected_background(for_dark_theme);
|
|
|
|
}
|
2019-05-10 16:58:44 +02:00
|
|
|
}
|
|
|
|
|
2019-05-07 04:51:56 +02:00
|
|
|
void BackgroundManager::tear_down() {
|
|
|
|
parent_.reset();
|
|
|
|
}
|
|
|
|
|
2021-09-24 15:03:01 +02:00
|
|
|
void BackgroundManager::store_background(BackgroundId background_id, LogEventStorerCalcLength &storer) {
|
|
|
|
const auto *background = get_background(background_id);
|
|
|
|
CHECK(background != nullptr);
|
|
|
|
store(*background, storer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BackgroundManager::store_background(BackgroundId background_id, LogEventStorerUnsafe &storer) {
|
|
|
|
const auto *background = get_background(background_id);
|
|
|
|
CHECK(background != nullptr);
|
|
|
|
store(*background, storer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BackgroundManager::parse_background(BackgroundId &background_id, LogEventParser &parser) {
|
|
|
|
Background background;
|
|
|
|
parse(background, parser);
|
|
|
|
CHECK(background.has_new_local_id);
|
|
|
|
if (background.file_id.is_valid() != background.type.has_file() || !background.id.is_valid()) {
|
|
|
|
parser.set_error(PSTRING() << "Failed to load " << background.id);
|
|
|
|
background_id = BackgroundId();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (background.id.is_local() && !background.type.has_file() && background.id.get() > max_local_background_id_.get()) {
|
|
|
|
set_max_local_background_id(background.id);
|
|
|
|
}
|
|
|
|
background_id = background.id;
|
|
|
|
add_background(background, false);
|
|
|
|
}
|
|
|
|
|
2021-09-03 08:07:18 +02:00
|
|
|
void BackgroundManager::get_backgrounds(bool for_dark_theme,
|
|
|
|
Promise<td_api::object_ptr<td_api::backgrounds>> &&promise) {
|
|
|
|
pending_get_backgrounds_queries_.emplace_back(for_dark_theme, std::move(promise));
|
2019-05-07 04:51:56 +02:00
|
|
|
if (pending_get_backgrounds_queries_.size() == 1) {
|
|
|
|
auto request_promise = PromiseCreator::lambda(
|
|
|
|
[actor_id = actor_id(this)](Result<telegram_api::object_ptr<telegram_api::account_WallPapers>> result) {
|
|
|
|
send_closure(actor_id, &BackgroundManager::on_get_backgrounds, std::move(result));
|
|
|
|
});
|
|
|
|
|
|
|
|
td_->create_handler<GetBackgroundsQuery>(std::move(request_promise))->send();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-09 03:07:21 +02:00
|
|
|
void BackgroundManager::reload_background_from_server(
|
2019-07-19 04:34:49 +02:00
|
|
|
BackgroundId background_id, const string &background_name,
|
|
|
|
telegram_api::object_ptr<telegram_api::InputWallPaper> &&input_wallpaper, Promise<Unit> &&promise) const {
|
2021-10-07 15:36:21 +02:00
|
|
|
TRY_STATUS_PROMISE(promise, G()->close_status());
|
|
|
|
|
2019-07-19 04:34:49 +02:00
|
|
|
td_->create_handler<GetBackgroundQuery>(std::move(promise))
|
|
|
|
->send(background_id, background_name, std::move(input_wallpaper));
|
2019-05-07 17:21:57 +02:00
|
|
|
}
|
|
|
|
|
2019-05-09 03:07:21 +02:00
|
|
|
void BackgroundManager::reload_background(BackgroundId background_id, int64 access_hash, Promise<Unit> &&promise) {
|
|
|
|
reload_background_from_server(
|
2019-07-19 04:34:49 +02:00
|
|
|
background_id, string(),
|
|
|
|
telegram_api::make_object<telegram_api::inputWallPaper>(background_id.get(), access_hash), std::move(promise));
|
2019-05-09 03:07:21 +02:00
|
|
|
}
|
|
|
|
|
2019-12-22 20:32:01 +01:00
|
|
|
static bool is_background_name_local(Slice name) {
|
2021-05-21 00:49:59 +02:00
|
|
|
return name.size() <= 13u || name.find('?') <= 13u || !is_base64url_characters(name.substr(0, name.find('?')));
|
2019-12-22 20:32:01 +01:00
|
|
|
}
|
|
|
|
|
2021-05-26 19:17:05 +02:00
|
|
|
std::pair<BackgroundId, BackgroundType> BackgroundManager::search_background(const string &name,
|
|
|
|
Promise<Unit> &&promise) {
|
|
|
|
auto params_pos = name.find('?');
|
|
|
|
string slug = params_pos >= name.size() ? name : name.substr(0, params_pos);
|
|
|
|
auto it = name_to_background_id_.find(slug);
|
2019-05-07 17:21:57 +02:00
|
|
|
if (it != name_to_background_id_.end()) {
|
2021-05-26 19:17:05 +02:00
|
|
|
CHECK(!is_background_name_local(slug));
|
|
|
|
|
|
|
|
const auto *background = get_background(it->second);
|
|
|
|
CHECK(background != nullptr);
|
2019-05-07 17:21:57 +02:00
|
|
|
promise.set_value(Unit());
|
2021-05-26 19:17:05 +02:00
|
|
|
BackgroundType type = background->type;
|
|
|
|
type.apply_parameters_from_link(name);
|
|
|
|
return {it->second, std::move(type)};
|
2019-05-07 17:21:57 +02:00
|
|
|
}
|
|
|
|
|
2021-05-26 19:17:05 +02:00
|
|
|
if (slug.empty()) {
|
2019-05-10 15:06:53 +02:00
|
|
|
promise.set_error(Status::Error(400, "Background name must be non-empty"));
|
2021-05-26 19:17:05 +02:00
|
|
|
return {};
|
2019-05-10 15:06:53 +02:00
|
|
|
}
|
|
|
|
|
2021-05-26 19:17:05 +02:00
|
|
|
if (is_background_name_local(slug)) {
|
2021-06-10 03:44:39 +02:00
|
|
|
auto r_type = BackgroundType::get_local_background_type(name);
|
|
|
|
if (r_type.is_error()) {
|
|
|
|
promise.set_error(r_type.move_as_error());
|
2021-05-26 19:17:05 +02:00
|
|
|
return {};
|
2019-12-21 03:28:07 +01:00
|
|
|
}
|
2021-06-10 03:44:39 +02:00
|
|
|
auto background_id = add_local_background(r_type.ok());
|
2019-05-10 15:06:53 +02:00
|
|
|
promise.set_value(Unit());
|
2021-06-10 03:44:39 +02:00
|
|
|
return {background_id, r_type.ok()};
|
2019-05-10 15:06:53 +02:00
|
|
|
}
|
|
|
|
|
2021-05-26 19:17:05 +02:00
|
|
|
if (G()->parameters().use_file_db && loaded_from_database_backgrounds_.count(slug) == 0) {
|
|
|
|
auto &queries = being_loaded_from_database_backgrounds_[slug];
|
2019-07-19 04:34:49 +02:00
|
|
|
queries.push_back(std::move(promise));
|
|
|
|
if (queries.size() == 1) {
|
2021-05-26 19:17:05 +02:00
|
|
|
LOG(INFO) << "Trying to load background " << slug << " from database";
|
2019-07-19 04:34:49 +02:00
|
|
|
G()->td_db()->get_sqlite_pmc()->get(
|
2021-10-19 17:11:16 +02:00
|
|
|
get_background_name_database_key(slug), PromiseCreator::lambda([slug](string value) mutable {
|
2019-07-19 04:34:49 +02:00
|
|
|
send_closure(G()->background_manager(), &BackgroundManager::on_load_background_from_database,
|
2021-05-26 19:17:05 +02:00
|
|
|
std::move(slug), std::move(value));
|
2019-07-19 04:34:49 +02:00
|
|
|
}));
|
|
|
|
}
|
2021-05-26 19:17:05 +02:00
|
|
|
return {};
|
2019-07-19 04:34:49 +02:00
|
|
|
}
|
|
|
|
|
2021-05-26 19:17:05 +02:00
|
|
|
reload_background_from_server(BackgroundId(), slug, telegram_api::make_object<telegram_api::inputWallPaperSlug>(slug),
|
2019-05-09 03:07:21 +02:00
|
|
|
std::move(promise));
|
2021-05-26 19:17:05 +02:00
|
|
|
return {};
|
2019-05-07 17:21:57 +02:00
|
|
|
}
|
|
|
|
|
2019-07-19 04:34:49 +02:00
|
|
|
void BackgroundManager::on_load_background_from_database(string name, string value) {
|
2020-05-16 01:25:03 +02:00
|
|
|
if (G()->close_flag()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-19 04:34:49 +02:00
|
|
|
auto promises_it = being_loaded_from_database_backgrounds_.find(name);
|
|
|
|
CHECK(promises_it != being_loaded_from_database_backgrounds_.end());
|
|
|
|
auto promises = std::move(promises_it->second);
|
|
|
|
CHECK(!promises.empty());
|
|
|
|
being_loaded_from_database_backgrounds_.erase(promises_it);
|
|
|
|
|
|
|
|
loaded_from_database_backgrounds_.insert(name);
|
|
|
|
|
2019-12-22 20:32:01 +01:00
|
|
|
CHECK(!is_background_name_local(name));
|
2019-07-19 04:34:49 +02:00
|
|
|
if (name_to_background_id_.count(name) == 0 && !value.empty()) {
|
|
|
|
LOG(INFO) << "Successfully loaded background " << name << " of size " << value.size() << " from database";
|
|
|
|
Background background;
|
|
|
|
auto status = log_event_parse(background, value);
|
2021-06-09 19:48:15 +02:00
|
|
|
if (status.is_error() || !background.type.has_file() || !background.file_id.is_valid() ||
|
2019-07-19 04:34:49 +02:00
|
|
|
!background.id.is_valid()) {
|
2019-12-21 03:28:07 +01:00
|
|
|
LOG(ERROR) << "Can't load background " << name << ": " << status << ' ' << format::as_hex_dump<4>(Slice(value));
|
2019-07-19 04:34:49 +02:00
|
|
|
} else {
|
|
|
|
if (background.name != name) {
|
|
|
|
LOG(ERROR) << "Expected background " << name << ", but received " << background.name;
|
2021-10-19 17:11:16 +02:00
|
|
|
name_to_background_id_.emplace(std::move(name), background.id);
|
2019-07-19 04:34:49 +02:00
|
|
|
}
|
2021-08-27 11:10:51 +02:00
|
|
|
add_background(background, false);
|
2019-07-19 04:34:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-13 16:40:12 +02:00
|
|
|
set_promises(promises);
|
2019-07-19 04:34:49 +02:00
|
|
|
}
|
|
|
|
|
2019-06-18 00:39:57 +02:00
|
|
|
td_api::object_ptr<td_api::updateSelectedBackground> BackgroundManager::get_update_selected_background_object(
|
|
|
|
bool for_dark_theme) const {
|
|
|
|
return td_api::make_object<td_api::updateSelectedBackground>(
|
2021-08-24 16:13:51 +02:00
|
|
|
for_dark_theme,
|
|
|
|
get_background_object(set_background_id_[for_dark_theme], for_dark_theme, &set_background_type_[for_dark_theme]));
|
2019-05-10 17:55:55 +02:00
|
|
|
}
|
|
|
|
|
2019-06-18 00:39:57 +02:00
|
|
|
void BackgroundManager::send_update_selected_background(bool for_dark_theme) const {
|
|
|
|
send_closure(G()->td(), &Td::send_update, get_update_selected_background_object(for_dark_theme));
|
2019-05-10 17:55:55 +02:00
|
|
|
}
|
|
|
|
|
2019-05-10 14:36:37 +02:00
|
|
|
Result<FileId> BackgroundManager::prepare_input_file(const tl_object_ptr<td_api::InputFile> &input_file) {
|
2023-02-09 16:31:02 +01:00
|
|
|
TRY_RESULT(file_id, td_->file_manager_->get_input_file_id(FileType::Background, input_file, {}, false, false));
|
2019-05-10 14:36:37 +02:00
|
|
|
|
|
|
|
FileView file_view = td_->file_manager_->get_file_view(file_id);
|
|
|
|
if (file_view.is_encrypted()) {
|
|
|
|
return Status::Error(400, "Can't use encrypted file");
|
|
|
|
}
|
|
|
|
if (!file_view.has_local_location() && !file_view.has_generate_location()) {
|
|
|
|
return Status::Error(400, "Need local or generate location to upload background");
|
|
|
|
}
|
|
|
|
return std::move(file_id);
|
|
|
|
}
|
|
|
|
|
2021-06-10 02:57:34 +02:00
|
|
|
void BackgroundManager::set_max_local_background_id(BackgroundId background_id) {
|
|
|
|
CHECK(background_id.is_local());
|
|
|
|
CHECK(background_id.get() > max_local_background_id_.get());
|
|
|
|
max_local_background_id_ = background_id;
|
|
|
|
G()->td_db()->get_binlog_pmc()->set("max_bg_id", to_string(max_local_background_id_.get()));
|
|
|
|
}
|
|
|
|
|
|
|
|
BackgroundId BackgroundManager::get_next_local_background_id() {
|
|
|
|
set_max_local_background_id(BackgroundId(max_local_background_id_.get() + 1));
|
|
|
|
return max_local_background_id_;
|
|
|
|
}
|
|
|
|
|
2021-06-10 03:44:39 +02:00
|
|
|
BackgroundId BackgroundManager::add_local_background(const BackgroundType &type) {
|
2019-12-21 03:28:07 +01:00
|
|
|
Background background;
|
2021-06-10 02:57:34 +02:00
|
|
|
background.id = get_next_local_background_id();
|
2019-12-21 03:28:07 +01:00
|
|
|
background.is_creator = true;
|
2021-06-10 03:55:58 +02:00
|
|
|
background.is_default = false;
|
|
|
|
background.is_dark = type.is_dark();
|
2021-06-10 03:44:39 +02:00
|
|
|
background.type = type;
|
|
|
|
background.name = type.get_link();
|
2021-08-27 11:10:51 +02:00
|
|
|
add_background(background, true);
|
2019-12-21 03:28:07 +01:00
|
|
|
|
2021-06-10 02:57:34 +02:00
|
|
|
return background.id;
|
2019-12-21 03:28:07 +01:00
|
|
|
}
|
|
|
|
|
2022-11-17 16:48:48 +01:00
|
|
|
void BackgroundManager::set_background(const td_api::InputBackground *input_background,
|
|
|
|
const td_api::BackgroundType *background_type, bool for_dark_theme,
|
|
|
|
Promise<td_api::object_ptr<td_api::background>> &&promise) {
|
2021-06-09 18:01:16 +02:00
|
|
|
BackgroundType type;
|
|
|
|
if (background_type != nullptr) {
|
2021-06-09 20:19:08 +02:00
|
|
|
auto r_type = BackgroundType::get_background_type(background_type);
|
2021-06-09 18:01:16 +02:00
|
|
|
if (r_type.is_error()) {
|
2022-11-17 16:48:48 +01:00
|
|
|
return promise.set_error(r_type.move_as_error());
|
2021-06-09 18:01:16 +02:00
|
|
|
}
|
|
|
|
type = r_type.move_as_ok();
|
|
|
|
} else {
|
|
|
|
CHECK(!type.has_file());
|
2019-07-18 20:09:48 +02:00
|
|
|
}
|
|
|
|
|
2021-06-09 18:01:16 +02:00
|
|
|
if (input_background == nullptr) {
|
|
|
|
if (background_type == nullptr) {
|
|
|
|
set_background_id(BackgroundId(), BackgroundType(), for_dark_theme);
|
2022-11-17 16:48:48 +01:00
|
|
|
return promise.set_value(nullptr);
|
2021-06-09 18:01:16 +02:00
|
|
|
}
|
|
|
|
if (type.has_file()) {
|
2022-11-17 16:48:48 +01:00
|
|
|
return promise.set_error(Status::Error(400, "Input background must be non-empty for the background type"));
|
2021-06-09 18:01:16 +02:00
|
|
|
}
|
2019-05-10 14:36:37 +02:00
|
|
|
|
2021-06-10 03:44:39 +02:00
|
|
|
auto background_id = add_local_background(type);
|
2019-12-22 23:29:19 +01:00
|
|
|
set_background_id(background_id, type, for_dark_theme);
|
2021-06-10 17:58:19 +02:00
|
|
|
|
|
|
|
local_background_ids_[for_dark_theme].insert(local_background_ids_[for_dark_theme].begin(), background_id);
|
|
|
|
save_local_backgrounds(for_dark_theme);
|
|
|
|
|
2022-11-17 16:48:48 +01:00
|
|
|
return promise.set_value(get_background_object(background_id, for_dark_theme, nullptr));
|
2019-12-21 03:28:07 +01:00
|
|
|
}
|
2019-05-10 14:36:37 +02:00
|
|
|
|
|
|
|
switch (input_background->get_id()) {
|
|
|
|
case td_api::inputBackgroundLocal::ID: {
|
2021-06-09 18:01:16 +02:00
|
|
|
if (!type.has_file()) {
|
2022-11-17 16:48:48 +01:00
|
|
|
return promise.set_error(Status::Error(400, "Can't specify local file for the background type"));
|
2021-06-09 18:01:16 +02:00
|
|
|
}
|
|
|
|
CHECK(background_type != nullptr);
|
|
|
|
|
2019-05-10 14:36:37 +02:00
|
|
|
auto background_local = static_cast<const td_api::inputBackgroundLocal *>(input_background);
|
|
|
|
auto r_file_id = prepare_input_file(background_local->background_);
|
|
|
|
if (r_file_id.is_error()) {
|
2022-11-17 16:48:48 +01:00
|
|
|
return promise.set_error(r_file_id.move_as_error());
|
2019-05-10 14:36:37 +02:00
|
|
|
}
|
|
|
|
auto file_id = r_file_id.move_as_ok();
|
|
|
|
LOG(INFO) << "Receive file " << file_id << " for input background";
|
2022-02-09 22:59:52 +01:00
|
|
|
CHECK(file_id.is_valid());
|
2019-05-10 14:36:37 +02:00
|
|
|
|
|
|
|
auto it = file_id_to_background_id_.find(file_id);
|
|
|
|
if (it != file_id_to_background_id_.end()) {
|
2019-06-18 00:39:57 +02:00
|
|
|
return set_background(it->second, type, for_dark_theme, std::move(promise));
|
2019-05-10 14:36:37 +02:00
|
|
|
}
|
|
|
|
|
2019-06-18 00:39:57 +02:00
|
|
|
upload_background_file(file_id, type, for_dark_theme, std::move(promise));
|
2019-05-10 14:36:37 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case td_api::inputBackgroundRemote::ID: {
|
|
|
|
auto background_remote = static_cast<const td_api::inputBackgroundRemote *>(input_background);
|
2021-06-09 18:01:16 +02:00
|
|
|
return set_background(BackgroundId(background_remote->background_id_), std::move(type), for_dark_theme,
|
|
|
|
std::move(promise));
|
2019-05-10 14:36:37 +02:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-17 16:48:48 +01:00
|
|
|
void BackgroundManager::set_background(BackgroundId background_id, BackgroundType type, bool for_dark_theme,
|
|
|
|
Promise<td_api::object_ptr<td_api::background>> &&promise) {
|
2019-12-22 23:29:19 +01:00
|
|
|
LOG(INFO) << "Set " << background_id << " with " << type;
|
2021-05-26 19:17:05 +02:00
|
|
|
const auto *background = get_background(background_id);
|
2019-05-10 14:36:37 +02:00
|
|
|
if (background == nullptr) {
|
2022-11-17 16:48:48 +01:00
|
|
|
return promise.set_error(Status::Error(400, "Background to set not found"));
|
2019-05-10 14:36:37 +02:00
|
|
|
}
|
2021-06-09 18:01:16 +02:00
|
|
|
if (!type.has_file()) {
|
|
|
|
type = background->type;
|
2021-06-10 03:44:39 +02:00
|
|
|
} else if (!background->type.has_equal_type(type)) {
|
2022-11-17 16:48:48 +01:00
|
|
|
return promise.set_error(Status::Error(400, "Background type mismatch"));
|
2019-05-10 14:36:37 +02:00
|
|
|
}
|
2019-12-22 23:29:19 +01:00
|
|
|
if (set_background_id_[for_dark_theme] == background_id && set_background_type_[for_dark_theme] == type) {
|
2022-11-17 16:48:48 +01:00
|
|
|
return promise.set_value(get_background_object(background_id, for_dark_theme, nullptr));
|
2019-05-10 14:36:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
LOG(INFO) << "Install " << background_id << " with " << type;
|
2021-06-09 18:01:16 +02:00
|
|
|
|
|
|
|
if (!type.has_file()) {
|
|
|
|
set_background_id(background_id, type, for_dark_theme);
|
2022-11-17 16:48:48 +01:00
|
|
|
return promise.set_value(get_background_object(background_id, for_dark_theme, nullptr));
|
2021-06-09 18:01:16 +02:00
|
|
|
}
|
|
|
|
|
2019-06-18 00:39:57 +02:00
|
|
|
auto query_promise = PromiseCreator::lambda([actor_id = actor_id(this), background_id, type, for_dark_theme,
|
|
|
|
promise = std::move(promise)](Result<Unit> &&result) mutable {
|
|
|
|
send_closure(actor_id, &BackgroundManager::on_installed_background, background_id, type, for_dark_theme,
|
|
|
|
std::move(result), std::move(promise));
|
|
|
|
});
|
2019-05-11 22:27:17 +02:00
|
|
|
td_->create_handler<InstallBackgroundQuery>(std::move(query_promise))
|
2021-06-09 18:01:16 +02:00
|
|
|
->send(telegram_api::make_object<telegram_api::inputWallPaper>(background_id.get(), background->access_hash),
|
|
|
|
type);
|
2019-05-10 14:36:37 +02:00
|
|
|
}
|
|
|
|
|
2019-06-18 00:39:57 +02:00
|
|
|
void BackgroundManager::on_installed_background(BackgroundId background_id, BackgroundType type, bool for_dark_theme,
|
2022-11-17 16:48:48 +01:00
|
|
|
Result<Unit> &&result,
|
|
|
|
Promise<td_api::object_ptr<td_api::background>> &&promise) {
|
2019-05-11 22:27:17 +02:00
|
|
|
if (result.is_error()) {
|
|
|
|
return promise.set_error(result.move_as_error());
|
|
|
|
}
|
|
|
|
|
2021-08-24 16:13:51 +02:00
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < installed_backgrounds_.size(); i++) {
|
|
|
|
if (installed_backgrounds_[i].first == background_id) {
|
|
|
|
installed_backgrounds_[i].second = type;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i == installed_backgrounds_.size()) {
|
|
|
|
installed_backgrounds_.insert(installed_backgrounds_.begin(), {background_id, type});
|
2019-05-11 22:27:17 +02:00
|
|
|
}
|
2019-06-18 00:39:57 +02:00
|
|
|
set_background_id(background_id, type, for_dark_theme);
|
2022-11-17 16:48:48 +01:00
|
|
|
promise.set_value(get_background_object(background_id, for_dark_theme, nullptr));
|
2019-05-11 22:27:17 +02:00
|
|
|
}
|
|
|
|
|
2019-06-18 00:39:57 +02:00
|
|
|
string BackgroundManager::get_background_database_key(bool for_dark_theme) {
|
|
|
|
return for_dark_theme ? "bgd" : "bg";
|
2019-05-10 16:58:44 +02:00
|
|
|
}
|
|
|
|
|
2021-06-10 17:58:19 +02:00
|
|
|
string BackgroundManager::get_local_backgrounds_database_key(bool for_dark_theme) {
|
|
|
|
return for_dark_theme ? "bgsd" : "bgs";
|
|
|
|
}
|
|
|
|
|
|
|
|
void BackgroundManager::save_background_id(bool for_dark_theme) {
|
2019-06-18 00:39:57 +02:00
|
|
|
string key = get_background_database_key(for_dark_theme);
|
|
|
|
auto background_id = set_background_id_[for_dark_theme];
|
|
|
|
if (background_id.is_valid()) {
|
|
|
|
const Background *background = get_background(background_id);
|
2019-05-10 16:58:44 +02:00
|
|
|
CHECK(background != nullptr);
|
2020-09-22 01:15:09 +02:00
|
|
|
BackgroundLogEvent log_event{*background, set_background_type_[for_dark_theme]};
|
|
|
|
G()->td_db()->get_binlog_pmc()->set(key, log_event_store(log_event).as_slice().str());
|
2019-05-10 16:58:44 +02:00
|
|
|
} else {
|
|
|
|
G()->td_db()->get_binlog_pmc()->erase(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-18 00:39:57 +02:00
|
|
|
void BackgroundManager::set_background_id(BackgroundId background_id, const BackgroundType &type, bool for_dark_theme) {
|
|
|
|
if (background_id == set_background_id_[for_dark_theme] && set_background_type_[for_dark_theme] == type) {
|
2019-05-10 14:36:37 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-18 00:39:57 +02:00
|
|
|
set_background_id_[for_dark_theme] = background_id;
|
|
|
|
set_background_type_[for_dark_theme] = type;
|
2019-05-10 16:58:44 +02:00
|
|
|
|
2019-06-18 00:39:57 +02:00
|
|
|
save_background_id(for_dark_theme);
|
|
|
|
send_update_selected_background(for_dark_theme);
|
2019-05-10 14:36:37 +02:00
|
|
|
}
|
|
|
|
|
2021-06-10 17:58:19 +02:00
|
|
|
void BackgroundManager::save_local_backgrounds(bool for_dark_theme) {
|
|
|
|
string key = get_local_backgrounds_database_key(for_dark_theme);
|
|
|
|
auto &background_ids = local_background_ids_[for_dark_theme];
|
|
|
|
const size_t MAX_LOCAL_BACKGROUNDS = 100;
|
|
|
|
while (background_ids.size() > MAX_LOCAL_BACKGROUNDS) {
|
|
|
|
background_ids.pop_back();
|
|
|
|
}
|
|
|
|
if (!background_ids.empty()) {
|
|
|
|
BackgroundsLogEvent log_event;
|
|
|
|
log_event.backgrounds_ = transform(background_ids, [&](BackgroundId background_id) {
|
|
|
|
const Background *background = get_background(background_id);
|
|
|
|
CHECK(background != nullptr);
|
|
|
|
return *background;
|
|
|
|
});
|
|
|
|
G()->td_db()->get_binlog_pmc()->set(key, log_event_store(log_event).as_slice().str());
|
|
|
|
} else {
|
|
|
|
G()->td_db()->get_binlog_pmc()->erase(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-18 00:39:57 +02:00
|
|
|
void BackgroundManager::upload_background_file(FileId file_id, const BackgroundType &type, bool for_dark_theme,
|
2022-11-17 16:48:48 +01:00
|
|
|
Promise<td_api::object_ptr<td_api::background>> &&promise) {
|
2022-11-21 09:40:43 +01:00
|
|
|
auto upload_file_id = td_->file_manager_->dup_file_id(file_id, "upload_background_file");
|
2021-11-11 15:39:09 +01:00
|
|
|
bool is_inserted =
|
|
|
|
being_uploaded_files_.emplace(upload_file_id, UploadedFileInfo(type, for_dark_theme, std::move(promise))).second;
|
|
|
|
CHECK(is_inserted);
|
2019-05-10 14:36:37 +02:00
|
|
|
LOG(INFO) << "Ask to upload background file " << upload_file_id;
|
|
|
|
td_->file_manager_->upload(upload_file_id, upload_background_file_callback_, 1, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BackgroundManager::on_upload_background_file(FileId file_id, tl_object_ptr<telegram_api::InputFile> input_file) {
|
|
|
|
LOG(INFO) << "Background file " << file_id << " has been uploaded";
|
|
|
|
|
|
|
|
auto it = being_uploaded_files_.find(file_id);
|
|
|
|
CHECK(it != being_uploaded_files_.end());
|
|
|
|
|
2021-11-11 15:39:09 +01:00
|
|
|
auto type = it->second.type_;
|
|
|
|
auto for_dark_theme = it->second.for_dark_theme_;
|
|
|
|
auto promise = std::move(it->second.promise_);
|
2019-05-10 14:36:37 +02:00
|
|
|
|
|
|
|
being_uploaded_files_.erase(it);
|
|
|
|
|
2019-06-18 00:39:57 +02:00
|
|
|
do_upload_background_file(file_id, type, for_dark_theme, std::move(input_file), std::move(promise));
|
2019-05-10 14:36:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BackgroundManager::on_upload_background_file_error(FileId file_id, Status status) {
|
|
|
|
if (G()->close_flag()) {
|
|
|
|
// do not fail upload if closing
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(WARNING) << "Background file " << file_id << " has upload error " << status;
|
|
|
|
CHECK(status.is_error());
|
|
|
|
|
|
|
|
auto it = being_uploaded_files_.find(file_id);
|
|
|
|
CHECK(it != being_uploaded_files_.end());
|
|
|
|
|
2021-11-11 15:39:09 +01:00
|
|
|
auto promise = std::move(it->second.promise_);
|
2019-05-10 14:36:37 +02:00
|
|
|
|
|
|
|
being_uploaded_files_.erase(it);
|
|
|
|
|
|
|
|
promise.set_error(Status::Error(status.code() > 0 ? status.code() : 500,
|
|
|
|
status.message())); // TODO CHECK that status has always a code
|
|
|
|
}
|
|
|
|
|
2019-06-18 00:39:57 +02:00
|
|
|
void BackgroundManager::do_upload_background_file(FileId file_id, const BackgroundType &type, bool for_dark_theme,
|
2019-05-10 14:36:37 +02:00
|
|
|
tl_object_ptr<telegram_api::InputFile> &&input_file,
|
2022-11-17 16:48:48 +01:00
|
|
|
Promise<td_api::object_ptr<td_api::background>> &&promise) {
|
2019-05-10 14:36:37 +02:00
|
|
|
if (input_file == nullptr) {
|
|
|
|
FileView file_view = td_->file_manager_->get_file_view(file_id);
|
2022-07-03 11:01:08 +02:00
|
|
|
file_id = file_view.get_main_file_id();
|
2019-05-10 14:36:37 +02:00
|
|
|
auto it = file_id_to_background_id_.find(file_id);
|
|
|
|
if (it != file_id_to_background_id_.end()) {
|
2022-11-17 16:48:48 +01:00
|
|
|
return set_background(it->second, type, for_dark_theme, std::move(promise));
|
2019-05-10 14:36:37 +02:00
|
|
|
}
|
|
|
|
return promise.set_error(Status::Error(500, "Failed to reupload background"));
|
|
|
|
}
|
|
|
|
|
2019-06-18 00:39:57 +02:00
|
|
|
td_->create_handler<UploadBackgroundQuery>(std::move(promise))
|
|
|
|
->send(file_id, std::move(input_file), type, for_dark_theme);
|
2019-05-10 14:36:37 +02:00
|
|
|
}
|
|
|
|
|
2019-06-18 00:39:57 +02:00
|
|
|
void BackgroundManager::on_uploaded_background_file(FileId file_id, const BackgroundType &type, bool for_dark_theme,
|
2019-12-22 00:56:51 +01:00
|
|
|
telegram_api::object_ptr<telegram_api::WallPaper> wallpaper,
|
2022-11-17 16:48:48 +01:00
|
|
|
Promise<td_api::object_ptr<td_api::background>> &&promise) {
|
2019-05-10 14:36:37 +02:00
|
|
|
CHECK(wallpaper != nullptr);
|
|
|
|
|
2021-08-27 11:10:51 +02:00
|
|
|
auto added_background = on_get_background(BackgroundId(), string(), std::move(wallpaper), true);
|
2021-08-24 16:13:51 +02:00
|
|
|
auto background_id = added_background.first;
|
2019-05-10 14:36:37 +02:00
|
|
|
if (!background_id.is_valid()) {
|
|
|
|
td_->file_manager_->cancel_upload(file_id);
|
|
|
|
return promise.set_error(Status::Error(500, "Receive wrong uploaded background"));
|
|
|
|
}
|
2021-08-24 16:13:51 +02:00
|
|
|
LOG_IF(ERROR, added_background.second != type)
|
|
|
|
<< "Type of uploaded background has changed from " << type << " to " << added_background.second;
|
2019-05-10 14:36:37 +02:00
|
|
|
|
2021-05-26 19:17:05 +02:00
|
|
|
const auto *background = get_background(background_id);
|
2019-05-10 14:36:37 +02:00
|
|
|
CHECK(background != nullptr);
|
2019-12-22 00:56:51 +01:00
|
|
|
if (!background->file_id.is_valid()) {
|
|
|
|
td_->file_manager_->cancel_upload(file_id);
|
|
|
|
return promise.set_error(Status::Error(500, "Receive wrong uploaded background without file"));
|
|
|
|
}
|
2019-05-10 14:36:37 +02:00
|
|
|
LOG_STATUS(td_->file_manager_->merge(background->file_id, file_id));
|
2019-06-18 00:39:57 +02:00
|
|
|
set_background_id(background_id, type, for_dark_theme);
|
2022-11-17 16:48:48 +01:00
|
|
|
promise.set_value(get_background_object(background_id, for_dark_theme, nullptr));
|
2019-05-10 14:36:37 +02:00
|
|
|
}
|
|
|
|
|
2019-05-10 22:55:26 +02:00
|
|
|
void BackgroundManager::remove_background(BackgroundId background_id, Promise<Unit> &&promise) {
|
2021-05-26 19:17:05 +02:00
|
|
|
const auto *background = get_background(background_id);
|
2019-05-10 22:55:26 +02:00
|
|
|
if (background == nullptr) {
|
|
|
|
return promise.set_error(Status::Error(400, "Background not found"));
|
|
|
|
}
|
|
|
|
|
|
|
|
auto query_promise = PromiseCreator::lambda(
|
|
|
|
[actor_id = actor_id(this), background_id, promise = std::move(promise)](Result<Unit> &&result) mutable {
|
|
|
|
send_closure(actor_id, &BackgroundManager::on_removed_background, background_id, std::move(result),
|
|
|
|
std::move(promise));
|
|
|
|
});
|
|
|
|
|
2021-06-08 00:10:19 +02:00
|
|
|
if (!background->type.has_file()) {
|
2021-06-09 16:14:10 +02:00
|
|
|
if (!background->id.is_local()) {
|
2021-06-08 00:10:19 +02:00
|
|
|
return td_->create_handler<UnsaveBackgroundQuery>(std::move(query_promise))
|
|
|
|
->send(telegram_api::make_object<telegram_api::inputWallPaperNoFile>(background_id.get()));
|
|
|
|
} else {
|
|
|
|
return query_promise.set_value(Unit());
|
|
|
|
}
|
2019-05-10 22:55:26 +02:00
|
|
|
}
|
|
|
|
|
2021-06-07 19:46:57 +02:00
|
|
|
td_->create_handler<UnsaveBackgroundQuery>(std::move(query_promise))
|
|
|
|
->send(telegram_api::make_object<telegram_api::inputWallPaper>(background_id.get(), background->access_hash));
|
2019-05-10 22:55:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BackgroundManager::on_removed_background(BackgroundId background_id, Result<Unit> &&result,
|
|
|
|
Promise<Unit> &&promise) {
|
|
|
|
if (result.is_error()) {
|
|
|
|
return promise.set_error(result.move_as_error());
|
|
|
|
}
|
2021-08-24 16:13:51 +02:00
|
|
|
td::remove_if(installed_backgrounds_,
|
|
|
|
[background_id](const auto &background) { return background.first == background_id; });
|
2019-06-18 00:39:57 +02:00
|
|
|
if (background_id == set_background_id_[0]) {
|
|
|
|
set_background_id(BackgroundId(), BackgroundType(), false);
|
|
|
|
}
|
|
|
|
if (background_id == set_background_id_[1]) {
|
|
|
|
set_background_id(BackgroundId(), BackgroundType(), true);
|
2019-05-10 22:55:26 +02:00
|
|
|
}
|
2021-06-10 17:58:19 +02:00
|
|
|
if (background_id.is_local()) {
|
|
|
|
if (td::remove(local_background_ids_[0], background_id)) {
|
|
|
|
save_local_backgrounds(false);
|
|
|
|
}
|
|
|
|
if (td::remove(local_background_ids_[1], background_id)) {
|
|
|
|
save_local_backgrounds(true);
|
|
|
|
}
|
|
|
|
}
|
2019-05-10 22:55:26 +02:00
|
|
|
promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
2019-05-12 03:10:18 +02:00
|
|
|
void BackgroundManager::reset_backgrounds(Promise<Unit> &&promise) {
|
|
|
|
auto query_promise =
|
|
|
|
PromiseCreator::lambda([actor_id = actor_id(this), promise = std::move(promise)](Result<Unit> &&result) mutable {
|
|
|
|
send_closure(actor_id, &BackgroundManager::on_reset_background, std::move(result), std::move(promise));
|
|
|
|
});
|
|
|
|
|
|
|
|
td_->create_handler<ResetBackgroundsQuery>(std::move(query_promise))->send();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BackgroundManager::on_reset_background(Result<Unit> &&result, Promise<Unit> &&promise) {
|
|
|
|
if (result.is_error()) {
|
|
|
|
return promise.set_error(result.move_as_error());
|
|
|
|
}
|
2021-08-24 16:13:51 +02:00
|
|
|
installed_backgrounds_.clear();
|
2019-06-18 00:39:57 +02:00
|
|
|
set_background_id(BackgroundId(), BackgroundType(), false);
|
|
|
|
set_background_id(BackgroundId(), BackgroundType(), true);
|
2021-06-10 17:58:19 +02:00
|
|
|
if (!local_background_ids_[0].empty()) {
|
|
|
|
local_background_ids_[0].clear();
|
|
|
|
save_local_backgrounds(false);
|
|
|
|
}
|
|
|
|
if (!local_background_ids_[1].empty()) {
|
|
|
|
local_background_ids_[1].clear();
|
|
|
|
save_local_backgrounds(true);
|
|
|
|
}
|
|
|
|
|
2019-05-12 03:10:18 +02:00
|
|
|
promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
2021-08-27 11:10:51 +02:00
|
|
|
void BackgroundManager::add_background(const Background &background, bool replace_type) {
|
2019-12-22 23:29:19 +01:00
|
|
|
LOG(INFO) << "Add " << background.id << " of " << background.type;
|
|
|
|
|
2019-07-19 02:50:51 +02:00
|
|
|
CHECK(background.id.is_valid());
|
2022-02-11 17:27:32 +01:00
|
|
|
auto &result_ptr = backgrounds_[background.id];
|
|
|
|
if (result_ptr == nullptr) {
|
|
|
|
result_ptr = make_unique<Background>();
|
|
|
|
}
|
|
|
|
auto *result = result_ptr.get();
|
2019-07-19 02:50:51 +02:00
|
|
|
|
|
|
|
FileSourceId file_source_id;
|
|
|
|
auto it = background_id_to_file_source_id_.find(background.id);
|
|
|
|
if (it != background_id_to_file_source_id_.end()) {
|
|
|
|
CHECK(!result->id.is_valid());
|
|
|
|
file_source_id = it->second.second;
|
|
|
|
background_id_to_file_source_id_.erase(it);
|
|
|
|
}
|
|
|
|
|
2019-05-09 03:07:21 +02:00
|
|
|
if (!result->id.is_valid()) {
|
2019-07-19 02:50:51 +02:00
|
|
|
result->id = background.id;
|
2021-08-27 11:10:51 +02:00
|
|
|
result->type = background.type;
|
2019-07-19 02:50:51 +02:00
|
|
|
} else {
|
|
|
|
CHECK(result->id == background.id);
|
2021-08-27 11:10:51 +02:00
|
|
|
if (replace_type) {
|
|
|
|
result->type = background.type;
|
|
|
|
}
|
2019-07-19 02:50:51 +02:00
|
|
|
}
|
|
|
|
result->access_hash = background.access_hash;
|
|
|
|
result->is_creator = background.is_creator;
|
|
|
|
result->is_default = background.is_default;
|
|
|
|
result->is_dark = background.is_dark;
|
|
|
|
|
|
|
|
if (result->name != background.name) {
|
|
|
|
if (!result->name.empty()) {
|
|
|
|
LOG(ERROR) << "Background name has changed from " << result->name << " to " << background.name;
|
2019-07-19 04:34:49 +02:00
|
|
|
// keep correspondence from previous name to background ID
|
|
|
|
// it will not harm, because background names can't be reassigned
|
|
|
|
// name_to_background_id_.erase(result->name);
|
2019-07-19 02:50:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
result->name = background.name;
|
|
|
|
|
2019-12-22 20:32:01 +01:00
|
|
|
if (!is_background_name_local(result->name)) {
|
2019-07-19 02:50:51 +02:00
|
|
|
name_to_background_id_.emplace(result->name, result->id);
|
2019-07-19 04:34:49 +02:00
|
|
|
loaded_from_database_backgrounds_.erase(result->name); // don't needed anymore
|
2019-05-09 03:07:21 +02:00
|
|
|
}
|
|
|
|
}
|
2019-07-19 02:50:51 +02:00
|
|
|
|
|
|
|
if (result->file_id != background.file_id) {
|
|
|
|
if (result->file_id.is_valid()) {
|
2022-07-03 11:01:08 +02:00
|
|
|
if (!background.file_id.is_valid() ||
|
|
|
|
td_->file_manager_->get_file_view(result->file_id).get_main_file_id() !=
|
|
|
|
td_->file_manager_->get_file_view(background.file_id).get_main_file_id()) {
|
2019-07-19 02:50:51 +02:00
|
|
|
LOG(ERROR) << "Background file has changed from " << result->file_id << " to " << background.file_id;
|
|
|
|
file_id_to_background_id_.erase(result->file_id);
|
|
|
|
result->file_source_id = FileSourceId();
|
|
|
|
}
|
|
|
|
CHECK(!file_source_id.is_valid());
|
|
|
|
}
|
|
|
|
if (file_source_id.is_valid()) {
|
|
|
|
result->file_source_id = file_source_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
result->file_id = background.file_id;
|
|
|
|
|
|
|
|
if (result->file_id.is_valid()) {
|
|
|
|
if (!result->file_source_id.is_valid()) {
|
|
|
|
result->file_source_id =
|
|
|
|
td_->file_reference_manager_->create_background_file_source(result->id, result->access_hash);
|
|
|
|
}
|
|
|
|
for (auto file_id : Document(Document::Type::General, result->file_id).get_file_ids(td_)) {
|
|
|
|
td_->file_manager_->add_file_source(file_id, result->file_source_id);
|
|
|
|
}
|
|
|
|
|
2022-02-09 22:59:52 +01:00
|
|
|
file_id_to_background_id_.emplace(result->file_id, result->id);
|
|
|
|
}
|
2019-07-19 02:50:51 +02:00
|
|
|
} else {
|
|
|
|
// if file_source_id is valid, then this is a new background with result->file_id == FileId()
|
2019-12-22 19:01:51 +01:00
|
|
|
// then background.file_id == FileId(), then this is a fill background, which can't have file_source_id
|
2019-07-19 02:50:51 +02:00
|
|
|
CHECK(!file_source_id.is_valid());
|
|
|
|
}
|
2019-05-09 03:07:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
BackgroundManager::Background *BackgroundManager::get_background_ref(BackgroundId background_id) {
|
|
|
|
auto p = backgrounds_.find(background_id);
|
|
|
|
if (p == backgrounds_.end()) {
|
|
|
|
return nullptr;
|
|
|
|
} else {
|
2022-02-11 17:27:32 +01:00
|
|
|
return p->second.get();
|
2019-05-09 03:07:21 +02:00
|
|
|
}
|
2019-05-07 04:51:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const BackgroundManager::Background *BackgroundManager::get_background(BackgroundId background_id) const {
|
|
|
|
auto p = backgrounds_.find(background_id);
|
|
|
|
if (p == backgrounds_.end()) {
|
|
|
|
return nullptr;
|
|
|
|
} else {
|
2022-02-11 17:27:32 +01:00
|
|
|
return p->second.get();
|
2019-05-07 04:51:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-19 04:34:49 +02:00
|
|
|
string BackgroundManager::get_background_name_database_key(const string &name) {
|
|
|
|
return PSTRING() << "bgn" << name;
|
|
|
|
}
|
|
|
|
|
2021-08-24 16:13:51 +02:00
|
|
|
std::pair<BackgroundId, BackgroundType> BackgroundManager::on_get_background(
|
|
|
|
BackgroundId expected_background_id, const string &expected_background_name,
|
2021-08-27 11:10:51 +02:00
|
|
|
telegram_api::object_ptr<telegram_api::WallPaper> wallpaper_ptr, bool replace_type) {
|
2021-09-13 14:55:01 +02:00
|
|
|
if (wallpaper_ptr == nullptr) {
|
|
|
|
return {};
|
|
|
|
}
|
2019-12-22 00:56:51 +01:00
|
|
|
|
|
|
|
if (wallpaper_ptr->get_id() == telegram_api::wallPaperNoFile::ID) {
|
|
|
|
auto wallpaper = move_tl_object_as<telegram_api::wallPaperNoFile>(wallpaper_ptr);
|
|
|
|
|
2021-06-10 01:09:16 +02:00
|
|
|
if (wallpaper->settings_ == nullptr) {
|
2019-12-22 00:56:51 +01:00
|
|
|
LOG(ERROR) << "Receive wallPaperNoFile without settings: " << to_string(wallpaper);
|
2021-08-24 16:13:51 +02:00
|
|
|
return {};
|
2019-12-22 00:56:51 +01:00
|
|
|
}
|
|
|
|
|
2021-06-08 00:10:19 +02:00
|
|
|
auto background_id = BackgroundId(wallpaper->id_);
|
2021-09-13 14:55:01 +02:00
|
|
|
if (background_id.is_local()) {
|
2021-06-08 00:10:19 +02:00
|
|
|
LOG(ERROR) << "Receive " << to_string(wallpaper);
|
2021-08-24 16:13:51 +02:00
|
|
|
return {};
|
2021-06-08 00:10:19 +02:00
|
|
|
}
|
2021-09-13 14:55:01 +02:00
|
|
|
if (!background_id.is_valid()) {
|
|
|
|
background_id = get_next_local_background_id();
|
|
|
|
}
|
2019-12-22 00:56:51 +01:00
|
|
|
|
2021-06-08 00:10:19 +02:00
|
|
|
Background background;
|
|
|
|
background.id = background_id;
|
|
|
|
background.is_creator = false;
|
2021-11-01 19:53:23 +01:00
|
|
|
background.is_default = wallpaper->default_;
|
|
|
|
background.is_dark = wallpaper->dark_;
|
2021-06-10 01:09:16 +02:00
|
|
|
background.type = BackgroundType(true, false, std::move(wallpaper->settings_));
|
2021-06-08 00:10:19 +02:00
|
|
|
background.name = background.type.get_link();
|
2021-08-27 11:10:51 +02:00
|
|
|
add_background(background, replace_type);
|
2021-06-08 00:10:19 +02:00
|
|
|
|
2021-08-24 16:13:51 +02:00
|
|
|
return {background_id, background.type};
|
2019-12-22 00:56:51 +01:00
|
|
|
}
|
2019-05-07 04:51:56 +02:00
|
|
|
|
2019-12-22 00:56:51 +01:00
|
|
|
auto wallpaper = move_tl_object_as<telegram_api::wallPaper>(wallpaper_ptr);
|
2021-06-08 00:10:19 +02:00
|
|
|
auto background_id = BackgroundId(wallpaper->id_);
|
2021-06-10 02:57:34 +02:00
|
|
|
if (!background_id.is_valid() || background_id.is_local() || is_background_name_local(wallpaper->slug_)) {
|
2019-05-07 04:51:56 +02:00
|
|
|
LOG(ERROR) << "Receive " << to_string(wallpaper);
|
2021-08-24 16:13:51 +02:00
|
|
|
return {};
|
2019-05-07 04:51:56 +02:00
|
|
|
}
|
2021-06-08 00:10:19 +02:00
|
|
|
if (expected_background_id.is_valid() && background_id != expected_background_id) {
|
2019-05-07 17:21:57 +02:00
|
|
|
LOG(ERROR) << "Expected " << expected_background_id << ", but receive " << to_string(wallpaper);
|
|
|
|
}
|
2019-05-07 04:51:56 +02:00
|
|
|
|
|
|
|
int32 document_id = wallpaper->document_->get_id();
|
|
|
|
if (document_id == telegram_api::documentEmpty::ID) {
|
|
|
|
LOG(ERROR) << "Receive " << to_string(wallpaper);
|
2021-08-24 16:13:51 +02:00
|
|
|
return {};
|
2019-05-07 04:51:56 +02:00
|
|
|
}
|
|
|
|
CHECK(document_id == telegram_api::document::ID);
|
|
|
|
|
2021-11-01 19:53:23 +01:00
|
|
|
bool is_pattern = wallpaper->pattern_;
|
2019-05-07 04:51:56 +02:00
|
|
|
|
|
|
|
Document document = td_->documents_manager_->on_get_document(
|
|
|
|
telegram_api::move_object_as<telegram_api::document>(wallpaper->document_), DialogId(), nullptr,
|
|
|
|
Document::Type::General, true, is_pattern);
|
|
|
|
if (!document.file_id.is_valid()) {
|
|
|
|
LOG(ERROR) << "Receive wrong document in " << to_string(wallpaper);
|
2021-08-24 16:13:51 +02:00
|
|
|
return {};
|
2019-05-07 04:51:56 +02:00
|
|
|
}
|
2019-07-19 02:50:51 +02:00
|
|
|
CHECK(document.type == Document::Type::General); // guaranteed by is_background parameter to on_get_document
|
|
|
|
|
|
|
|
Background background;
|
2021-06-08 00:10:19 +02:00
|
|
|
background.id = background_id;
|
2019-07-19 02:50:51 +02:00
|
|
|
background.access_hash = wallpaper->access_hash_;
|
2021-11-01 19:53:23 +01:00
|
|
|
background.is_creator = wallpaper->creator_;
|
|
|
|
background.is_default = wallpaper->default_;
|
|
|
|
background.is_dark = wallpaper->dark_;
|
2021-06-10 01:09:16 +02:00
|
|
|
background.type = BackgroundType(false, is_pattern, std::move(wallpaper->settings_));
|
2019-07-19 02:50:51 +02:00
|
|
|
background.name = std::move(wallpaper->slug_);
|
|
|
|
background.file_id = document.file_id;
|
2021-08-27 11:10:51 +02:00
|
|
|
add_background(background, replace_type);
|
2019-05-07 17:21:57 +02:00
|
|
|
|
2019-07-19 04:34:49 +02:00
|
|
|
if (!expected_background_name.empty() && background.name != expected_background_name) {
|
|
|
|
LOG(ERROR) << "Expected background " << expected_background_name << ", but receive " << background.name;
|
2021-06-08 00:10:19 +02:00
|
|
|
name_to_background_id_.emplace(expected_background_name, background_id);
|
2019-07-19 04:34:49 +02:00
|
|
|
}
|
|
|
|
|
2021-05-21 00:49:59 +02:00
|
|
|
if (G()->parameters().use_file_db) {
|
2021-06-08 00:10:19 +02:00
|
|
|
LOG(INFO) << "Save " << background_id << " to database with name " << background.name;
|
2021-05-21 00:49:59 +02:00
|
|
|
CHECK(!is_background_name_local(background.name));
|
2019-07-19 04:34:49 +02:00
|
|
|
G()->td_db()->get_sqlite_pmc()->set(get_background_name_database_key(background.name),
|
|
|
|
log_event_store(background).as_slice().str(), Auto());
|
|
|
|
}
|
|
|
|
|
2021-08-24 16:13:51 +02:00
|
|
|
return {background_id, background.type};
|
2019-05-07 04:51:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BackgroundManager::on_get_backgrounds(Result<telegram_api::object_ptr<telegram_api::account_WallPapers>> result) {
|
|
|
|
auto promises = std::move(pending_get_backgrounds_queries_);
|
|
|
|
CHECK(!promises.empty());
|
|
|
|
reset_to_empty(pending_get_backgrounds_queries_);
|
|
|
|
|
|
|
|
if (result.is_error()) {
|
2021-08-24 16:13:51 +02:00
|
|
|
// do not clear installed_backgrounds_
|
2019-05-07 04:51:56 +02:00
|
|
|
|
|
|
|
auto error = result.move_as_error();
|
|
|
|
for (auto &promise : promises) {
|
2021-09-03 08:07:18 +02:00
|
|
|
promise.second.set_error(error.clone());
|
2019-05-07 04:51:56 +02:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto wallpapers_ptr = result.move_as_ok();
|
|
|
|
LOG(INFO) << "Receive " << to_string(wallpapers_ptr);
|
|
|
|
if (wallpapers_ptr->get_id() == telegram_api::account_wallPapersNotModified::ID) {
|
|
|
|
for (auto &promise : promises) {
|
2021-09-03 08:07:18 +02:00
|
|
|
promise.second.set_value(get_backgrounds_object(promise.first));
|
2019-05-07 04:51:56 +02:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-08-24 16:13:51 +02:00
|
|
|
installed_backgrounds_.clear();
|
2019-05-07 04:51:56 +02:00
|
|
|
auto wallpapers = telegram_api::move_object_as<telegram_api::account_wallPapers>(wallpapers_ptr);
|
|
|
|
for (auto &wallpaper : wallpapers->wallpapers_) {
|
2021-08-27 11:10:51 +02:00
|
|
|
auto background = on_get_background(BackgroundId(), string(), std::move(wallpaper), false);
|
2021-08-24 16:13:51 +02:00
|
|
|
if (background.first.is_valid()) {
|
|
|
|
installed_backgrounds_.push_back(std::move(background));
|
2019-05-07 04:51:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto &promise : promises) {
|
2021-09-03 08:07:18 +02:00
|
|
|
promise.second.set_value(get_backgrounds_object(promise.first));
|
2019-05-07 04:51:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-18 00:39:57 +02:00
|
|
|
td_api::object_ptr<td_api::background> BackgroundManager::get_background_object(BackgroundId background_id,
|
2021-05-26 19:17:05 +02:00
|
|
|
bool for_dark_theme,
|
|
|
|
const BackgroundType *type) const {
|
|
|
|
const auto *background = get_background(background_id);
|
2019-05-07 04:51:56 +02:00
|
|
|
if (background == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2021-05-26 19:17:05 +02:00
|
|
|
if (type == nullptr) {
|
|
|
|
type = &background->type;
|
|
|
|
// first check another set_background_id to get correct type if both backgrounds are the same
|
|
|
|
if (background_id == set_background_id_[1 - static_cast<int>(for_dark_theme)]) {
|
|
|
|
type = &set_background_type_[1 - static_cast<int>(for_dark_theme)];
|
|
|
|
}
|
|
|
|
if (background_id == set_background_id_[for_dark_theme]) {
|
|
|
|
type = &set_background_type_[for_dark_theme];
|
|
|
|
}
|
2019-05-10 14:36:37 +02:00
|
|
|
}
|
2019-05-07 04:51:56 +02:00
|
|
|
return td_api::make_object<td_api::background>(
|
|
|
|
background->id.get(), background->is_default, background->is_dark, background->name,
|
2020-05-31 21:22:15 +02:00
|
|
|
td_->documents_manager_->get_document_object(background->file_id, PhotoFormat::Png),
|
2021-06-09 20:19:08 +02:00
|
|
|
type->get_background_type_object());
|
2019-05-07 04:51:56 +02:00
|
|
|
}
|
|
|
|
|
2019-06-18 00:39:57 +02:00
|
|
|
td_api::object_ptr<td_api::backgrounds> BackgroundManager::get_backgrounds_object(bool for_dark_theme) const {
|
2021-08-24 16:13:51 +02:00
|
|
|
auto backgrounds = transform(installed_backgrounds_,
|
|
|
|
[this, for_dark_theme](const std::pair<BackgroundId, BackgroundType> &background) {
|
|
|
|
return get_background_object(background.first, for_dark_theme, &background.second);
|
|
|
|
});
|
2019-06-18 00:39:57 +02:00
|
|
|
auto background_id = set_background_id_[for_dark_theme];
|
2021-08-24 16:13:51 +02:00
|
|
|
bool have_background = false;
|
|
|
|
for (const auto &background : installed_backgrounds_) {
|
|
|
|
if (background_id == background.first) {
|
|
|
|
have_background = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (background_id.is_valid() && !have_background) {
|
|
|
|
backgrounds.push_back(get_background_object(background_id, for_dark_theme, nullptr));
|
2019-06-18 00:39:57 +02:00
|
|
|
}
|
2021-06-10 17:58:19 +02:00
|
|
|
for (auto local_background_id : local_background_ids_[for_dark_theme]) {
|
|
|
|
if (local_background_id != background_id) {
|
2021-08-24 16:13:51 +02:00
|
|
|
backgrounds.push_back(get_background_object(local_background_id, for_dark_theme, nullptr));
|
2021-06-10 17:58:19 +02:00
|
|
|
}
|
|
|
|
}
|
2019-06-18 00:39:57 +02:00
|
|
|
std::stable_sort(backgrounds.begin(), backgrounds.end(),
|
|
|
|
[background_id, for_dark_theme](const td_api::object_ptr<td_api::background> &lhs,
|
|
|
|
const td_api::object_ptr<td_api::background> &rhs) {
|
|
|
|
auto get_order = [background_id,
|
|
|
|
for_dark_theme](const td_api::object_ptr<td_api::background> &background) {
|
|
|
|
if (background->id_ == background_id.get()) {
|
|
|
|
return 0;
|
|
|
|
}
|
2021-06-10 17:58:19 +02:00
|
|
|
int theme_score = background->is_dark_ == for_dark_theme ? 0 : 1;
|
|
|
|
int local_score = BackgroundId(background->id_).is_local() ? 0 : 2;
|
|
|
|
return 1 + local_score + theme_score;
|
2019-06-18 00:39:57 +02:00
|
|
|
};
|
|
|
|
return get_order(lhs) < get_order(rhs);
|
|
|
|
});
|
|
|
|
return td_api::make_object<td_api::backgrounds>(std::move(backgrounds));
|
2019-05-07 04:51:56 +02:00
|
|
|
}
|
|
|
|
|
2019-05-09 03:07:21 +02:00
|
|
|
FileSourceId BackgroundManager::get_background_file_source_id(BackgroundId background_id, int64 access_hash) {
|
2022-02-09 22:59:52 +01:00
|
|
|
if (!background_id.is_valid()) {
|
|
|
|
return FileSourceId();
|
|
|
|
}
|
|
|
|
|
2019-05-09 03:07:21 +02:00
|
|
|
Background *background = get_background_ref(background_id);
|
|
|
|
if (background != nullptr) {
|
|
|
|
if (!background->file_source_id.is_valid()) {
|
|
|
|
background->file_source_id =
|
|
|
|
td_->file_reference_manager_->create_background_file_source(background_id, background->access_hash);
|
|
|
|
}
|
|
|
|
return background->file_source_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto &result = background_id_to_file_source_id_[background_id];
|
|
|
|
if (result.first == 0) {
|
|
|
|
result.first = access_hash;
|
|
|
|
}
|
|
|
|
if (!result.second.is_valid()) {
|
|
|
|
result.second = td_->file_reference_manager_->create_background_file_source(background_id, result.first);
|
|
|
|
}
|
|
|
|
return result.second;
|
|
|
|
}
|
|
|
|
|
2019-05-10 18:04:14 +02:00
|
|
|
void BackgroundManager::get_current_state(vector<td_api::object_ptr<td_api::Update>> &updates) const {
|
|
|
|
if (td_->auth_manager_->is_bot()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-18 00:39:57 +02:00
|
|
|
updates.push_back(get_update_selected_background_object(false));
|
|
|
|
updates.push_back(get_update_selected_background_object(true));
|
2019-05-10 18:04:14 +02:00
|
|
|
}
|
|
|
|
|
2019-05-07 04:51:56 +02:00
|
|
|
} // namespace td
|