2018-12-31 20:04:05 +01:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
2018-12-31 20:04:05 +01: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/AnimationsManager.h"
|
|
|
|
|
|
|
|
#include "td/telegram/AuthManager.h"
|
|
|
|
#include "td/telegram/DialogId.h"
|
2019-04-09 17:38:57 +02:00
|
|
|
#include "td/telegram/Document.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/DocumentsManager.h"
|
2019-01-21 21:30:28 +01:00
|
|
|
#include "td/telegram/FileReferenceManager.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/files/FileManager.h"
|
2019-01-19 22:54:29 +01:00
|
|
|
#include "td/telegram/files/FileType.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/Global.h"
|
|
|
|
#include "td/telegram/logevent/LogEvent.h"
|
|
|
|
#include "td/telegram/misc.h"
|
2022-08-18 16:00:18 +02:00
|
|
|
#include "td/telegram/OptionManager.h"
|
2022-04-09 22:21:07 +02:00
|
|
|
#include "td/telegram/PhotoFormat.h"
|
2021-05-27 20:15:30 +02:00
|
|
|
#include "td/telegram/secret_api.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/Td.h"
|
|
|
|
#include "td/telegram/td_api.h"
|
2021-06-02 14:43:56 +02:00
|
|
|
#include "td/telegram/TdDb.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/telegram_api.h"
|
|
|
|
|
2018-07-18 03:11:48 +02:00
|
|
|
#include "td/db/SqliteKeyValueAsync.h"
|
|
|
|
|
2021-01-01 13:59:53 +01:00
|
|
|
#include "td/utils/algorithm.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/misc.h"
|
|
|
|
#include "td/utils/Random.h"
|
|
|
|
#include "td/utils/Time.h"
|
|
|
|
#include "td/utils/tl_helpers.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class GetSavedGifsQuery final : public Td::ResultHandler {
|
2019-02-02 11:54:40 +01:00
|
|
|
bool is_repair_ = false;
|
2019-01-15 01:04:10 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2021-09-03 11:27:59 +02:00
|
|
|
void send(bool is_repair, int64 hash) {
|
2019-02-02 11:54:40 +01:00
|
|
|
is_repair_ = is_repair;
|
2020-03-15 22:17:11 +01:00
|
|
|
send_query(G()->net_query_creator().create(telegram_api::messages_getSavedGifs(hash)));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_getSavedGifs>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto ptr = result_ptr.move_as_ok();
|
2021-11-08 13:20:38 +01:00
|
|
|
td_->animations_manager_->on_get_saved_animations(is_repair_, std::move(ptr));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
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-02-14 16:36:16 +01:00
|
|
|
LOG(ERROR) << "Receive error for get saved animations: " << status;
|
|
|
|
}
|
2021-11-08 13:20:38 +01:00
|
|
|
td_->animations_manager_->on_get_saved_animations_failed(is_repair_, std::move(status));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class SaveGifQuery final : public Td::ResultHandler {
|
2019-01-23 20:20:48 +01:00
|
|
|
FileId file_id_;
|
|
|
|
string file_reference_;
|
|
|
|
bool unsave_ = false;
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
Promise<Unit> promise_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit SaveGifQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
2019-01-23 20:20:48 +01:00
|
|
|
void send(FileId file_id, tl_object_ptr<telegram_api::inputDocument> &&input_document, bool unsave) {
|
|
|
|
CHECK(input_document != nullptr);
|
|
|
|
CHECK(file_id.is_valid());
|
|
|
|
file_id_ = file_id;
|
|
|
|
file_reference_ = input_document->file_reference_.as_slice().str();
|
|
|
|
unsave_ = unsave;
|
2020-03-15 22:17:11 +01:00
|
|
|
send_query(G()->net_query_creator().create(telegram_api::messages_saveGif(std::move(input_document), unsave)));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_saveGif>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool result = result_ptr.move_as_ok();
|
2019-01-23 20:20:48 +01:00
|
|
|
LOG(INFO) << "Receive result for save GIF: " << result;
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!result) {
|
2021-11-08 13:20:38 +01:00
|
|
|
td_->animations_manager_->reload_saved_animations(true);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
promise_.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2021-11-08 13:20:38 +01:00
|
|
|
if (!td_->auth_manager_->is_bot() && FileReferenceManager::is_file_reference_error(status)) {
|
2019-02-08 15:54:23 +01:00
|
|
|
VLOG(file_references) << "Receive " << status << " for " << file_id_;
|
2021-11-08 13:20:38 +01:00
|
|
|
td_->file_manager_->delete_file_reference(file_id_, file_reference_);
|
|
|
|
td_->file_reference_manager_->repair_file_reference(
|
2019-01-23 20:20:48 +01:00
|
|
|
file_id_, PromiseCreator::lambda([animation_id = file_id_, unsave = unsave_,
|
|
|
|
promise = std::move(promise_)](Result<Unit> result) mutable {
|
|
|
|
if (result.is_error()) {
|
|
|
|
return promise.set_error(Status::Error(400, "Failed to find the animation"));
|
|
|
|
}
|
|
|
|
|
|
|
|
send_closure(G()->animations_manager(), &AnimationsManager::send_save_gif_query, animation_id, unsave,
|
|
|
|
std::move(promise));
|
|
|
|
}));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-30 23:35:58 +02:00
|
|
|
if (!G()->is_expected_error(status)) {
|
2019-02-14 16:36:16 +01:00
|
|
|
LOG(ERROR) << "Receive error for save GIF: " << status;
|
|
|
|
}
|
2021-11-08 13:20:38 +01:00
|
|
|
td_->animations_manager_->reload_saved_animations(true);
|
2018-12-31 20:04:05 +01:00
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
AnimationsManager::AnimationsManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) {
|
2022-08-17 21:16:16 +02:00
|
|
|
on_update_animation_search_emojis();
|
|
|
|
on_update_animation_search_provider();
|
2022-08-17 21:00:55 +02:00
|
|
|
on_update_saved_animations_limit();
|
|
|
|
|
2020-11-21 14:34:39 +01:00
|
|
|
next_saved_animations_load_time_ = Time::now();
|
2022-08-17 21:00:55 +02:00
|
|
|
|
|
|
|
G()->td_db()->get_binlog_pmc()->erase("saved_animations_limit"); // legacy
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2022-07-20 12:40:14 +02:00
|
|
|
AnimationsManager::~AnimationsManager() {
|
|
|
|
Scheduler::instance()->destroy_on_scheduler(G()->get_gc_scheduler_id(), animations_);
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void AnimationsManager::tear_down() {
|
|
|
|
parent_.reset();
|
|
|
|
}
|
|
|
|
|
2018-09-27 20:14:32 +02:00
|
|
|
int32 AnimationsManager::get_animation_duration(FileId file_id) const {
|
2022-08-03 22:23:32 +02:00
|
|
|
const auto *animation = get_animation(file_id);
|
|
|
|
CHECK(animation != nullptr);
|
|
|
|
return animation->duration;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-08-28 13:16:29 +02:00
|
|
|
tl_object_ptr<td_api::animation> AnimationsManager::get_animation_object(FileId file_id) const {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!file_id.is_valid()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2022-08-03 22:23:32 +02:00
|
|
|
auto animation = get_animation(file_id);
|
2021-08-28 13:16:29 +02:00
|
|
|
CHECK(animation != nullptr);
|
2020-05-31 21:22:15 +02:00
|
|
|
auto thumbnail =
|
|
|
|
animation->animated_thumbnail.file_id.is_valid()
|
|
|
|
? get_thumbnail_object(td_->file_manager_.get(), animation->animated_thumbnail, PhotoFormat::Mpeg4)
|
|
|
|
: get_thumbnail_object(td_->file_manager_.get(), animation->thumbnail, PhotoFormat::Jpeg);
|
|
|
|
return make_tl_object<td_api::animation>(animation->duration, animation->dimensions.width,
|
|
|
|
animation->dimensions.height, animation->file_name, animation->mime_type,
|
|
|
|
animation->has_stickers, get_minithumbnail_object(animation->minithumbnail),
|
|
|
|
std::move(thumbnail), td_->file_manager_->get_file_object(file_id));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-09-27 03:19:03 +02:00
|
|
|
FileId AnimationsManager::on_get_animation(unique_ptr<Animation> new_animation, bool replace) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto file_id = new_animation->file_id;
|
2019-08-01 02:58:49 +02:00
|
|
|
CHECK(file_id.is_valid());
|
2018-12-31 20:04:05 +01:00
|
|
|
auto &a = animations_[file_id];
|
2019-02-15 19:49:02 +01:00
|
|
|
LOG(INFO) << (a == nullptr ? "Add" : (replace ? "Replace" : "Ignore")) << " animation " << file_id << " of size "
|
|
|
|
<< new_animation->dimensions;
|
2018-12-31 20:04:05 +01:00
|
|
|
if (a == nullptr) {
|
|
|
|
a = std::move(new_animation);
|
|
|
|
} else if (replace) {
|
|
|
|
CHECK(a->file_id == file_id);
|
|
|
|
if (a->mime_type != new_animation->mime_type) {
|
|
|
|
LOG(DEBUG) << "Animation " << file_id << " info has changed";
|
2022-10-20 00:16:24 +02:00
|
|
|
a->mime_type = std::move(new_animation->mime_type);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (a->file_name != new_animation->file_name) {
|
|
|
|
LOG(DEBUG) << "Animation " << file_id << " file name has changed";
|
|
|
|
a->file_name = std::move(new_animation->file_name);
|
|
|
|
}
|
|
|
|
if (a->dimensions != new_animation->dimensions) {
|
2021-12-02 12:15:37 +01:00
|
|
|
LOG(DEBUG) << "Animation " << file_id << " dimensions have changed";
|
2018-12-31 20:04:05 +01:00
|
|
|
a->dimensions = new_animation->dimensions;
|
|
|
|
}
|
|
|
|
if (a->duration != new_animation->duration) {
|
|
|
|
LOG(DEBUG) << "Animation " << file_id << " duration has changed";
|
|
|
|
a->duration = new_animation->duration;
|
|
|
|
}
|
2019-03-01 20:51:33 +01:00
|
|
|
if (a->minithumbnail != new_animation->minithumbnail) {
|
|
|
|
a->minithumbnail = std::move(new_animation->minithumbnail);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
if (a->thumbnail != new_animation->thumbnail) {
|
|
|
|
if (!a->thumbnail.file_id.is_valid()) {
|
|
|
|
LOG(DEBUG) << "Animation " << file_id << " thumbnail has changed";
|
|
|
|
} else {
|
|
|
|
LOG(INFO) << "Animation " << file_id << " thumbnail has changed from " << a->thumbnail << " to "
|
|
|
|
<< new_animation->thumbnail;
|
|
|
|
}
|
2022-10-20 00:16:24 +02:00
|
|
|
a->thumbnail = std::move(new_animation->thumbnail);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2020-05-30 00:48:56 +02:00
|
|
|
if (a->animated_thumbnail != new_animation->animated_thumbnail) {
|
|
|
|
if (!a->animated_thumbnail.file_id.is_valid()) {
|
|
|
|
LOG(DEBUG) << "Animation " << file_id << " animated thumbnail has changed";
|
|
|
|
} else {
|
|
|
|
LOG(INFO) << "Animation " << file_id << " animated thumbnail has changed from " << a->animated_thumbnail
|
|
|
|
<< " to " << new_animation->animated_thumbnail;
|
|
|
|
}
|
2022-10-20 00:16:24 +02:00
|
|
|
a->animated_thumbnail = std::move(new_animation->animated_thumbnail);
|
2020-05-30 00:48:56 +02:00
|
|
|
}
|
2020-05-29 10:51:51 +02:00
|
|
|
if (a->has_stickers != new_animation->has_stickers && new_animation->has_stickers) {
|
|
|
|
a->has_stickers = new_animation->has_stickers;
|
|
|
|
}
|
|
|
|
if (a->sticker_file_ids != new_animation->sticker_file_ids && !new_animation->sticker_file_ids.empty()) {
|
|
|
|
a->sticker_file_ids = std::move(new_animation->sticker_file_ids);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return file_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
const AnimationsManager::Animation *AnimationsManager::get_animation(FileId file_id) const {
|
2022-08-04 09:50:34 +02:00
|
|
|
return animations_.get_pointer(file_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
FileId AnimationsManager::get_animation_thumbnail_file_id(FileId file_id) const {
|
|
|
|
auto animation = get_animation(file_id);
|
|
|
|
CHECK(animation != nullptr);
|
|
|
|
return animation->thumbnail.file_id;
|
|
|
|
}
|
|
|
|
|
2020-05-30 00:48:56 +02:00
|
|
|
FileId AnimationsManager::get_animation_animated_thumbnail_file_id(FileId file_id) const {
|
|
|
|
auto animation = get_animation(file_id);
|
|
|
|
CHECK(animation != nullptr);
|
|
|
|
return animation->animated_thumbnail.file_id;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void AnimationsManager::delete_animation_thumbnail(FileId file_id) {
|
|
|
|
auto &animation = animations_[file_id];
|
|
|
|
CHECK(animation != nullptr);
|
|
|
|
animation->thumbnail = PhotoSize();
|
2020-07-07 10:41:01 +02:00
|
|
|
animation->animated_thumbnail = AnimationSize();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
FileId AnimationsManager::dup_animation(FileId new_id, FileId old_id) {
|
2018-01-25 23:09:07 +01:00
|
|
|
LOG(INFO) << "Dup animation " << old_id << " to " << new_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
const Animation *old_animation = get_animation(old_id);
|
|
|
|
CHECK(old_animation != nullptr);
|
|
|
|
auto &new_animation = animations_[new_id];
|
2022-02-09 22:59:52 +01:00
|
|
|
CHECK(new_animation == nullptr);
|
2018-09-27 03:19:03 +02:00
|
|
|
new_animation = make_unique<Animation>(*old_animation);
|
2018-12-31 20:04:05 +01:00
|
|
|
new_animation->file_id = new_id;
|
2022-11-21 09:40:43 +01:00
|
|
|
new_animation->thumbnail.file_id = td_->file_manager_->dup_file_id(new_animation->thumbnail.file_id, "dup_animation");
|
2020-05-30 00:48:56 +02:00
|
|
|
new_animation->animated_thumbnail.file_id =
|
2022-11-21 09:40:43 +01:00
|
|
|
td_->file_manager_->dup_file_id(new_animation->animated_thumbnail.file_id, "dup_animation");
|
2018-12-31 20:04:05 +01:00
|
|
|
return new_id;
|
|
|
|
}
|
|
|
|
|
2022-08-03 20:38:03 +02:00
|
|
|
void AnimationsManager::merge_animations(FileId new_id, FileId old_id) {
|
2021-08-26 17:50:28 +02:00
|
|
|
CHECK(old_id.is_valid() && new_id.is_valid());
|
|
|
|
CHECK(new_id != old_id);
|
2018-01-05 16:50:35 +01:00
|
|
|
|
|
|
|
LOG(INFO) << "Merge animations " << new_id << " and " << old_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
const Animation *old_ = get_animation(old_id);
|
|
|
|
CHECK(old_ != nullptr);
|
|
|
|
|
2021-01-28 13:11:14 +01:00
|
|
|
bool need_merge = true;
|
2022-08-03 20:58:07 +02:00
|
|
|
const auto *new_ = get_animation(new_id);
|
|
|
|
if (new_ == nullptr) {
|
2022-08-03 20:38:03 +02:00
|
|
|
dup_animation(new_id, old_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
|
|
|
if (old_->thumbnail != new_->thumbnail) {
|
|
|
|
// LOG_STATUS(td_->file_manager_->merge(new_->thumbnail.file_id, old_->thumbnail.file_id));
|
|
|
|
}
|
2021-08-03 02:13:56 +02:00
|
|
|
if (new_->file_name.size() == old_->file_name.size() + 4 && new_->file_name == old_->file_name + ".mp4") {
|
2021-01-28 13:11:14 +01:00
|
|
|
need_merge = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (need_merge) {
|
|
|
|
LOG_STATUS(td_->file_manager_->merge(new_id, old_id));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-30 00:48:56 +02:00
|
|
|
void AnimationsManager::create_animation(FileId file_id, string minithumbnail, PhotoSize thumbnail,
|
2020-07-07 10:41:01 +02:00
|
|
|
AnimationSize animated_thumbnail, bool has_stickers,
|
2020-05-29 10:51:51 +02:00
|
|
|
vector<FileId> &&sticker_file_ids, string file_name, string mime_type,
|
|
|
|
int32 duration, Dimensions dimensions, bool replace) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto a = make_unique<Animation>();
|
|
|
|
a->file_id = file_id;
|
|
|
|
a->file_name = std::move(file_name);
|
|
|
|
a->mime_type = std::move(mime_type);
|
2018-02-12 11:37:54 +01:00
|
|
|
a->duration = max(duration, 0);
|
2018-12-31 20:04:05 +01:00
|
|
|
a->dimensions = dimensions;
|
2021-05-27 20:15:30 +02:00
|
|
|
if (!td_->auth_manager_->is_bot()) {
|
|
|
|
a->minithumbnail = std::move(minithumbnail);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
a->thumbnail = std::move(thumbnail);
|
2020-05-30 00:48:56 +02:00
|
|
|
a->animated_thumbnail = std::move(animated_thumbnail);
|
2020-05-29 10:51:51 +02:00
|
|
|
a->has_stickers = has_stickers;
|
|
|
|
a->sticker_file_ids = std::move(sticker_file_ids);
|
2018-12-31 20:04:05 +01:00
|
|
|
on_get_animation(std::move(a), replace);
|
|
|
|
}
|
|
|
|
|
|
|
|
tl_object_ptr<telegram_api::InputMedia> AnimationsManager::get_input_media(
|
|
|
|
FileId file_id, tl_object_ptr<telegram_api::InputFile> input_file,
|
2022-12-26 12:20:48 +01:00
|
|
|
tl_object_ptr<telegram_api::InputFile> input_thumbnail, bool has_spoiler) const {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto file_view = td_->file_manager_->get_file_view(file_id);
|
|
|
|
if (file_view.is_encrypted()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-11-17 20:41:28 +01:00
|
|
|
if (file_view.has_remote_location() && !file_view.main_remote_location().is_web() && input_file == nullptr) {
|
2022-12-26 12:20:48 +01:00
|
|
|
int32 flags = 0;
|
|
|
|
if (has_spoiler) {
|
|
|
|
flags |= telegram_api::inputMediaDocument::SPOILER_MASK;
|
|
|
|
}
|
2022-12-08 18:58:37 +01:00
|
|
|
return make_tl_object<telegram_api::inputMediaDocument>(
|
2022-12-26 12:20:48 +01:00
|
|
|
flags, false /*ignored*/, file_view.main_remote_location().as_input_document(), 0, string());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (file_view.has_url()) {
|
2022-12-26 12:20:48 +01:00
|
|
|
int32 flags = 0;
|
|
|
|
if (has_spoiler) {
|
|
|
|
flags |= telegram_api::inputMediaDocumentExternal::SPOILER_MASK;
|
|
|
|
}
|
|
|
|
return make_tl_object<telegram_api::inputMediaDocumentExternal>(flags, false /*ignored*/, file_view.url(), 0);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-02-12 02:50:30 +01:00
|
|
|
if (input_file != nullptr) {
|
2018-12-31 20:04:05 +01:00
|
|
|
const Animation *animation = get_animation(file_id);
|
|
|
|
CHECK(animation != nullptr);
|
|
|
|
|
|
|
|
vector<tl_object_ptr<telegram_api::DocumentAttribute>> attributes;
|
|
|
|
if (!animation->file_name.empty()) {
|
|
|
|
attributes.push_back(make_tl_object<telegram_api::documentAttributeFilename>(animation->file_name));
|
|
|
|
}
|
|
|
|
string mime_type = animation->mime_type;
|
2019-04-11 16:13:42 +02:00
|
|
|
if (mime_type == "video/mp4") {
|
2018-12-31 20:04:05 +01:00
|
|
|
attributes.push_back(make_tl_object<telegram_api::documentAttributeVideo>(
|
2023-05-01 20:07:01 +02:00
|
|
|
0, false /*ignored*/, false /*ignored*/, false /*ignored*/, animation->duration, animation->dimensions.width,
|
2024-07-08 12:34:56 +02:00
|
|
|
animation->dimensions.height, 0, 0.0));
|
2018-12-31 20:04:05 +01:00
|
|
|
} else if (animation->dimensions.width != 0 && animation->dimensions.height != 0) {
|
|
|
|
if (!begins_with(mime_type, "image/")) {
|
|
|
|
mime_type = "image/gif";
|
|
|
|
}
|
|
|
|
attributes.push_back(make_tl_object<telegram_api::documentAttributeImageSize>(animation->dimensions.width,
|
|
|
|
animation->dimensions.height));
|
|
|
|
}
|
|
|
|
int32 flags = 0;
|
2020-05-29 10:51:51 +02:00
|
|
|
vector<tl_object_ptr<telegram_api::InputDocument>> added_stickers;
|
|
|
|
if (animation->has_stickers) {
|
|
|
|
flags |= telegram_api::inputMediaUploadedDocument::STICKERS_MASK;
|
|
|
|
added_stickers = td_->file_manager_->get_input_documents(animation->sticker_file_ids);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
if (input_thumbnail != nullptr) {
|
|
|
|
flags |= telegram_api::inputMediaUploadedDocument::THUMB_MASK;
|
|
|
|
}
|
2022-12-26 12:20:48 +01:00
|
|
|
if (has_spoiler) {
|
|
|
|
flags |= telegram_api::inputMediaUploadedDocument::SPOILER_MASK;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
return make_tl_object<telegram_api::inputMediaUploadedDocument>(
|
2022-12-08 18:58:37 +01:00
|
|
|
flags, false /*ignored*/, false /*ignored*/, false /*ignored*/, std::move(input_file),
|
|
|
|
std::move(input_thumbnail), mime_type, std::move(attributes), std::move(added_stickers), 0);
|
2019-02-12 02:50:30 +01:00
|
|
|
} else {
|
|
|
|
CHECK(!file_view.has_remote_location());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-01-23 20:20:48 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
SecretInputMedia AnimationsManager::get_secret_input_media(FileId animation_file_id,
|
|
|
|
tl_object_ptr<telegram_api::InputEncryptedFile> input_file,
|
2022-05-11 06:46:06 +02:00
|
|
|
const string &caption, BufferSlice thumbnail,
|
|
|
|
int32 layer) const {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto *animation = get_animation(animation_file_id);
|
|
|
|
CHECK(animation != nullptr);
|
|
|
|
auto file_view = td_->file_manager_->get_file_view(animation_file_id);
|
2022-05-11 00:53:18 +02:00
|
|
|
if (!file_view.is_encrypted_secret() || file_view.encryption_key().empty()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return SecretInputMedia{};
|
|
|
|
}
|
|
|
|
if (file_view.has_remote_location()) {
|
2019-11-17 20:41:28 +01:00
|
|
|
input_file = file_view.main_remote_location().as_input_encrypted_file();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (!input_file) {
|
|
|
|
return SecretInputMedia{};
|
|
|
|
}
|
|
|
|
if (animation->thumbnail.file_id.is_valid() && thumbnail.empty()) {
|
|
|
|
return SecretInputMedia{};
|
|
|
|
}
|
|
|
|
vector<tl_object_ptr<secret_api::DocumentAttribute>> attributes;
|
|
|
|
if (!animation->file_name.empty()) {
|
|
|
|
attributes.push_back(make_tl_object<secret_api::documentAttributeFilename>(animation->file_name));
|
|
|
|
}
|
|
|
|
if (animation->duration != 0 && animation->mime_type == "video/mp4") {
|
2022-09-28 11:17:04 +02:00
|
|
|
attributes.push_back(make_tl_object<secret_api::documentAttributeVideo>(
|
2021-06-08 17:31:29 +02:00
|
|
|
0, false, animation->duration, animation->dimensions.width, animation->dimensions.height));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (animation->dimensions.width != 0 && animation->dimensions.height != 0) {
|
|
|
|
attributes.push_back(make_tl_object<secret_api::documentAttributeImageSize>(animation->dimensions.width,
|
|
|
|
animation->dimensions.height));
|
|
|
|
}
|
|
|
|
attributes.push_back(make_tl_object<secret_api::documentAttributeAnimated>());
|
|
|
|
|
2022-05-11 00:53:18 +02:00
|
|
|
return {std::move(input_file),
|
|
|
|
std::move(thumbnail),
|
|
|
|
animation->thumbnail.dimensions,
|
|
|
|
animation->mime_type,
|
|
|
|
file_view,
|
|
|
|
std::move(attributes),
|
2022-05-11 06:46:06 +02:00
|
|
|
caption,
|
|
|
|
layer};
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2022-08-17 21:16:16 +02:00
|
|
|
void AnimationsManager::on_update_animation_search_emojis() {
|
2020-05-30 01:42:45 +02:00
|
|
|
if (G()->close_flag()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (td_->auth_manager_->is_bot()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-18 16:00:18 +02:00
|
|
|
auto animation_search_emojis = td_->option_manager_->get_option_string("animation_search_emojis");
|
2020-05-30 01:42:45 +02:00
|
|
|
is_animation_search_emojis_inited_ = true;
|
|
|
|
if (animation_search_emojis_ == animation_search_emojis) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
animation_search_emojis_ = std::move(animation_search_emojis);
|
|
|
|
|
|
|
|
try_send_update_animation_search_parameters();
|
|
|
|
}
|
|
|
|
|
2022-08-17 21:16:16 +02:00
|
|
|
void AnimationsManager::on_update_animation_search_provider() {
|
2020-05-30 01:42:45 +02:00
|
|
|
if (G()->close_flag()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (td_->auth_manager_->is_bot()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-18 16:00:18 +02:00
|
|
|
string animation_search_provider = td_->option_manager_->get_option_string("animation_search_provider");
|
2020-05-30 01:42:45 +02:00
|
|
|
is_animation_search_provider_inited_ = true;
|
|
|
|
if (animation_search_provider_ == animation_search_provider) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
animation_search_provider_ = std::move(animation_search_provider);
|
|
|
|
|
|
|
|
try_send_update_animation_search_parameters();
|
|
|
|
}
|
|
|
|
|
2022-08-17 20:40:03 +02:00
|
|
|
void AnimationsManager::on_update_saved_animations_limit() {
|
2022-08-18 16:00:18 +02:00
|
|
|
if (G()->close_flag()) {
|
|
|
|
return;
|
|
|
|
}
|
2024-01-24 13:24:16 +01:00
|
|
|
if (td_->auth_manager_->is_bot()) {
|
|
|
|
return;
|
|
|
|
}
|
2022-08-18 22:31:14 +02:00
|
|
|
auto saved_animations_limit =
|
2022-08-18 16:00:18 +02:00
|
|
|
narrow_cast<int32>(td_->option_manager_->get_option_integer("saved_animations_limit", 200));
|
2018-12-31 20:04:05 +01:00
|
|
|
if (saved_animations_limit != saved_animations_limit_) {
|
|
|
|
if (saved_animations_limit > 0) {
|
|
|
|
LOG(INFO) << "Update saved animations limit to " << saved_animations_limit;
|
|
|
|
saved_animations_limit_ = saved_animations_limit;
|
|
|
|
if (static_cast<int32>(saved_animation_ids_.size()) > saved_animations_limit_) {
|
|
|
|
saved_animation_ids_.resize(saved_animations_limit_);
|
|
|
|
send_update_saved_animations();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "Receive wrong saved animations limit = " << saved_animations_limit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class AnimationsManager::AnimationListLogEvent {
|
|
|
|
public:
|
|
|
|
vector<FileId> animation_ids;
|
|
|
|
|
|
|
|
AnimationListLogEvent() = default;
|
|
|
|
|
|
|
|
explicit AnimationListLogEvent(vector<FileId> animation_ids) : animation_ids(std::move(animation_ids)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class StorerT>
|
|
|
|
void store(StorerT &storer) const {
|
|
|
|
AnimationsManager *animations_manager = storer.context()->td().get_actor_unsafe()->animations_manager_.get();
|
|
|
|
td::store(narrow_cast<int32>(animation_ids.size()), storer);
|
|
|
|
for (auto animation_id : animation_ids) {
|
|
|
|
animations_manager->store_animation(animation_id, storer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ParserT>
|
|
|
|
void parse(ParserT &parser) {
|
|
|
|
AnimationsManager *animations_manager = parser.context()->td().get_actor_unsafe()->animations_manager_.get();
|
|
|
|
int32 size = parser.fetch_int();
|
|
|
|
animation_ids.resize(size);
|
|
|
|
for (auto &animation_id : animation_ids) {
|
|
|
|
animation_id = animations_manager->parse_animation(parser);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void AnimationsManager::reload_saved_animations(bool force) {
|
2019-04-29 23:37:55 +02:00
|
|
|
if (G()->close_flag()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-21 14:34:39 +01:00
|
|
|
if (!td_->auth_manager_->is_bot() && !are_saved_animations_being_loaded_ &&
|
2018-12-31 20:04:05 +01:00
|
|
|
(next_saved_animations_load_time_ < Time::now() || force)) {
|
|
|
|
LOG_IF(INFO, force) << "Reload saved animations";
|
2020-11-21 14:34:39 +01:00
|
|
|
are_saved_animations_being_loaded_ = true;
|
2019-01-15 01:04:10 +01:00
|
|
|
td_->create_handler<GetSavedGifsQuery>()->send(false, get_saved_animations_hash("reload_saved_animations"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-02 11:54:40 +01:00
|
|
|
void AnimationsManager::repair_saved_animations(Promise<Unit> &&promise) {
|
2019-01-15 01:04:10 +01:00
|
|
|
if (td_->auth_manager_->is_bot()) {
|
2021-12-02 12:15:37 +01:00
|
|
|
return promise.set_error(Status::Error(400, "Bots have no saved animations"));
|
2019-01-15 01:04:10 +01:00
|
|
|
}
|
|
|
|
|
2019-02-02 11:54:40 +01:00
|
|
|
repair_saved_animations_queries_.push_back(std::move(promise));
|
|
|
|
if (repair_saved_animations_queries_.size() == 1u) {
|
2019-01-15 01:04:10 +01:00
|
|
|
td_->create_handler<GetSavedGifsQuery>()->send(true, 0);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<FileId> AnimationsManager::get_saved_animations(Promise<Unit> &&promise) {
|
|
|
|
if (!are_saved_animations_loaded_) {
|
|
|
|
load_saved_animations(std::move(promise));
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
reload_saved_animations(false);
|
|
|
|
|
|
|
|
promise.set_value(Unit());
|
|
|
|
return saved_animation_ids_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnimationsManager::load_saved_animations(Promise<Unit> &&promise) {
|
|
|
|
if (td_->auth_manager_->is_bot()) {
|
|
|
|
are_saved_animations_loaded_ = true;
|
|
|
|
}
|
|
|
|
if (are_saved_animations_loaded_) {
|
|
|
|
promise.set_value(Unit());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
load_saved_animations_queries_.push_back(std::move(promise));
|
|
|
|
if (load_saved_animations_queries_.size() == 1u) {
|
2023-03-13 23:41:04 +01:00
|
|
|
if (G()->use_sqlite_pmc()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
LOG(INFO) << "Trying to load saved animations from database";
|
|
|
|
G()->td_db()->get_sqlite_pmc()->get("ans", PromiseCreator::lambda([](string value) {
|
|
|
|
send_closure(G()->animations_manager(),
|
|
|
|
&AnimationsManager::on_load_saved_animations_from_database,
|
|
|
|
std::move(value));
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
LOG(INFO) << "Trying to load saved animations from server";
|
|
|
|
reload_saved_animations(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnimationsManager::on_load_saved_animations_from_database(const string &value) {
|
2020-05-16 01:25:03 +02:00
|
|
|
if (G()->close_flag()) {
|
|
|
|
return;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
if (value.empty()) {
|
|
|
|
LOG(INFO) << "Saved animations aren't found in database";
|
|
|
|
reload_saved_animations(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(INFO) << "Successfully loaded saved animations list of size " << value.size() << " from database";
|
|
|
|
|
|
|
|
AnimationListLogEvent log_event;
|
|
|
|
log_event_parse(log_event, value).ensure();
|
|
|
|
|
|
|
|
on_load_saved_animations_finished(std::move(log_event.animation_ids), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnimationsManager::on_load_saved_animations_finished(vector<FileId> &&saved_animation_ids, bool from_database) {
|
2018-03-08 15:17:53 +01:00
|
|
|
if (static_cast<int32>(saved_animation_ids.size()) > saved_animations_limit_) {
|
|
|
|
saved_animation_ids.resize(saved_animations_limit_);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
saved_animation_ids_ = std::move(saved_animation_ids);
|
|
|
|
are_saved_animations_loaded_ = true;
|
|
|
|
send_update_saved_animations(from_database);
|
2022-04-13 16:40:12 +02:00
|
|
|
set_promises(load_saved_animations_queries_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AnimationsManager::on_get_saved_animations(
|
2019-02-02 11:54:40 +01:00
|
|
|
bool is_repair, tl_object_ptr<telegram_api::messages_SavedGifs> &&saved_animations_ptr) {
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(!td_->auth_manager_->is_bot());
|
2019-02-02 11:54:40 +01:00
|
|
|
if (!is_repair) {
|
2020-11-21 14:34:39 +01:00
|
|
|
are_saved_animations_being_loaded_ = false;
|
2019-01-15 01:04:10 +01:00
|
|
|
next_saved_animations_load_time_ = Time::now_cached() + Random::fast(30 * 60, 50 * 60);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
CHECK(saved_animations_ptr != nullptr);
|
|
|
|
int32 constructor_id = saved_animations_ptr->get_id();
|
|
|
|
if (constructor_id == telegram_api::messages_savedGifsNotModified::ID) {
|
2019-02-02 11:54:40 +01:00
|
|
|
if (is_repair) {
|
2019-01-15 01:04:10 +01:00
|
|
|
return on_get_saved_animations_failed(true, Status::Error(500, "Failed to reload saved animations"));
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
LOG(INFO) << "Saved animations are not modified";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
CHECK(constructor_id == telegram_api::messages_savedGifs::ID);
|
|
|
|
auto saved_animations = move_tl_object_as<telegram_api::messages_savedGifs>(saved_animations_ptr);
|
|
|
|
LOG(INFO) << "Receive " << saved_animations->gifs_.size() << " saved animations from server";
|
|
|
|
|
|
|
|
vector<FileId> saved_animation_ids;
|
|
|
|
saved_animation_ids.reserve(saved_animations->gifs_.size());
|
|
|
|
for (auto &document_ptr : saved_animations->gifs_) {
|
|
|
|
int32 document_constructor_id = document_ptr->get_id();
|
|
|
|
if (document_constructor_id == telegram_api::documentEmpty::ID) {
|
2019-01-23 20:20:48 +01:00
|
|
|
LOG(ERROR) << "Empty saved animation document received";
|
2018-12-31 20:04:05 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
CHECK(document_constructor_id == telegram_api::document::ID);
|
|
|
|
auto document =
|
|
|
|
td_->documents_manager_->on_get_document(move_tl_object_as<telegram_api::document>(document_ptr), DialogId());
|
2019-04-09 17:38:57 +02:00
|
|
|
if (document.type != Document::Type::Animation) {
|
|
|
|
LOG(ERROR) << "Receive " << document << " instead of animation as saved animation";
|
2018-12-31 20:04:05 +01:00
|
|
|
continue;
|
|
|
|
}
|
2019-02-02 11:54:40 +01:00
|
|
|
if (!is_repair) {
|
2019-04-09 17:38:57 +02:00
|
|
|
saved_animation_ids.push_back(document.file_id);
|
2019-01-15 01:04:10 +01:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-02-02 11:54:40 +01:00
|
|
|
if (is_repair) {
|
2022-04-13 16:40:12 +02:00
|
|
|
set_promises(repair_saved_animations_queries_);
|
2019-01-15 01:04:10 +01:00
|
|
|
} else {
|
|
|
|
on_load_saved_animations_finished(std::move(saved_animation_ids));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-01-15 01:04:10 +01:00
|
|
|
LOG_IF(ERROR, get_saved_animations_hash("on_get_saved_animations") != saved_animations->hash_)
|
|
|
|
<< "Saved animations hash mismatch: " << saved_animations->hash_ << " vs "
|
|
|
|
<< get_saved_animations_hash("on_get_saved_animations 2");
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-02-02 11:54:40 +01:00
|
|
|
void AnimationsManager::on_get_saved_animations_failed(bool is_repair, Status error) {
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(error.is_error());
|
2019-02-02 11:54:40 +01:00
|
|
|
if (!is_repair) {
|
2020-11-21 14:34:39 +01:00
|
|
|
are_saved_animations_being_loaded_ = false;
|
2019-01-15 01:04:10 +01:00
|
|
|
next_saved_animations_load_time_ = Time::now_cached() + Random::fast(5, 10);
|
|
|
|
}
|
2022-04-13 16:40:12 +02:00
|
|
|
fail_promises(is_repair ? repair_saved_animations_queries_ : load_saved_animations_queries_, std::move(error));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-09-03 11:27:59 +02:00
|
|
|
int64 AnimationsManager::get_saved_animations_hash(const char *source) const {
|
|
|
|
vector<uint64> numbers;
|
|
|
|
numbers.reserve(saved_animation_ids_.size());
|
2018-12-31 20:04:05 +01:00
|
|
|
for (auto animation_id : saved_animation_ids_) {
|
|
|
|
auto animation = get_animation(animation_id);
|
|
|
|
CHECK(animation != nullptr);
|
2018-03-07 00:02:49 +01:00
|
|
|
auto file_view = td_->file_manager_->get_file_view(animation_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(file_view.has_remote_location());
|
2020-03-23 23:12:23 +01:00
|
|
|
if (!file_view.remote_location().is_document()) {
|
|
|
|
LOG(ERROR) << "Saved animation remote location is not document: " << source << " " << file_view.remote_location();
|
|
|
|
continue;
|
|
|
|
}
|
2021-09-03 11:27:59 +02:00
|
|
|
numbers.push_back(file_view.remote_location().get_id());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
return get_vector_hash(numbers);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnimationsManager::add_saved_animation(const tl_object_ptr<td_api::InputFile> &input_file,
|
|
|
|
Promise<Unit> &&promise) {
|
|
|
|
if (!are_saved_animations_loaded_) {
|
|
|
|
load_saved_animations(std::move(promise));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-02-09 16:31:02 +01:00
|
|
|
TRY_RESULT_PROMISE(promise, file_id,
|
|
|
|
td_->file_manager_->get_input_file_id(FileType::Animation, input_file, DialogId(), false, false));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2023-02-09 16:31:02 +01:00
|
|
|
add_saved_animation_impl(file_id, true, std::move(promise));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-01-23 20:20:48 +01:00
|
|
|
void AnimationsManager::send_save_gif_query(FileId animation_id, bool unsave, Promise<Unit> &&promise) {
|
2021-10-07 15:36:21 +02:00
|
|
|
TRY_STATUS_PROMISE(promise, G()->close_status());
|
2019-04-08 16:57:41 +02:00
|
|
|
|
2019-01-23 20:20:48 +01:00
|
|
|
// TODO invokeAfter and log event
|
|
|
|
auto file_view = td_->file_manager_->get_file_view(animation_id);
|
|
|
|
CHECK(file_view.has_remote_location());
|
2019-02-12 17:17:20 +01:00
|
|
|
LOG_CHECK(file_view.remote_location().is_document()) << file_view.remote_location();
|
2019-01-23 20:20:48 +01:00
|
|
|
CHECK(!file_view.remote_location().is_web());
|
|
|
|
td_->create_handler<SaveGifQuery>(std::move(promise))
|
|
|
|
->send(animation_id, file_view.remote_location().as_input_document(), unsave);
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void AnimationsManager::add_saved_animation_by_id(FileId animation_id) {
|
2020-06-05 06:26:26 +02:00
|
|
|
auto animation = get_animation(animation_id);
|
|
|
|
CHECK(animation != nullptr);
|
|
|
|
if (animation->has_stickers) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
// TODO log event
|
2019-11-30 19:22:40 +01:00
|
|
|
add_saved_animation_impl(animation_id, false, Auto());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-11-30 19:22:40 +01:00
|
|
|
void AnimationsManager::add_saved_animation_impl(FileId animation_id, bool add_on_server, Promise<Unit> &&promise) {
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(!td_->auth_manager_->is_bot());
|
|
|
|
|
2019-02-15 19:49:02 +01:00
|
|
|
auto file_view = td_->file_manager_->get_file_view(animation_id);
|
|
|
|
if (file_view.empty()) {
|
2021-09-24 09:59:51 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Animation file not found"));
|
2019-02-15 19:49:02 +01:00
|
|
|
}
|
|
|
|
|
2022-07-03 11:01:08 +02:00
|
|
|
LOG(INFO) << "Add saved animation " << animation_id << " with main file " << file_view.get_main_file_id();
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!are_saved_animations_loaded_) {
|
2019-11-30 19:22:40 +01:00
|
|
|
load_saved_animations(
|
|
|
|
PromiseCreator::lambda([animation_id, add_on_server, promise = std::move(promise)](Result<> result) mutable {
|
|
|
|
if (result.is_ok()) {
|
|
|
|
send_closure(G()->animations_manager(), &AnimationsManager::add_saved_animation_impl, animation_id,
|
|
|
|
add_on_server, std::move(promise));
|
|
|
|
} else {
|
|
|
|
promise.set_error(result.move_as_error());
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
return;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-02-16 03:29:28 +01:00
|
|
|
auto is_equal = [animation_id](FileId file_id) {
|
|
|
|
return file_id == animation_id ||
|
|
|
|
(file_id.get_remote() == animation_id.get_remote() && animation_id.get_remote() != 0);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!saved_animation_ids_.empty() && is_equal(saved_animation_ids_[0])) {
|
|
|
|
// fast path
|
2018-03-08 19:04:28 +01:00
|
|
|
if (saved_animation_ids_[0].get_remote() == 0 && animation_id.get_remote() != 0) {
|
|
|
|
saved_animation_ids_[0] = animation_id;
|
|
|
|
save_saved_animations_to_database();
|
|
|
|
}
|
|
|
|
|
2019-11-30 19:22:40 +01:00
|
|
|
return promise.set_value(Unit());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto animation = get_animation(animation_id);
|
|
|
|
if (animation == nullptr) {
|
2021-09-24 09:59:51 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Animation not found"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (animation->mime_type != "video/mp4") {
|
2021-09-24 09:59:51 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Only MPEG4 animations can be saved"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!file_view.has_remote_location()) {
|
2021-09-24 09:59:51 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Can save only sent animations"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (file_view.remote_location().is_web()) {
|
2021-09-24 09:59:51 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Can't save web animations"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-03-27 15:11:15 +02:00
|
|
|
if (!file_view.remote_location().is_document()) {
|
2021-09-24 09:59:51 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Can't save encrypted animations"));
|
2018-03-27 15:11:15 +02:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2023-10-01 20:17:30 +02:00
|
|
|
add_to_top_if(saved_animation_ids_, static_cast<size_t>(saved_animations_limit_), animation_id, is_equal);
|
|
|
|
|
2019-02-16 03:29:28 +01:00
|
|
|
CHECK(is_equal(saved_animation_ids_[0]));
|
2018-03-08 19:04:28 +01:00
|
|
|
if (saved_animation_ids_[0].get_remote() == 0 && animation_id.get_remote() != 0) {
|
|
|
|
saved_animation_ids_[0] = animation_id;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
send_update_saved_animations();
|
2019-11-30 19:22:40 +01:00
|
|
|
if (add_on_server) {
|
|
|
|
send_save_gif_query(animation_id, false, std::move(promise));
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AnimationsManager::remove_saved_animation(const tl_object_ptr<td_api::InputFile> &input_file,
|
|
|
|
Promise<Unit> &&promise) {
|
|
|
|
if (!are_saved_animations_loaded_) {
|
|
|
|
load_saved_animations(std::move(promise));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-02-09 16:31:02 +01:00
|
|
|
TRY_RESULT_PROMISE(promise, file_id,
|
|
|
|
td_->file_manager_->get_input_file_id(FileType::Animation, input_file, DialogId(), false, false));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-07-03 11:17:04 +02:00
|
|
|
auto is_equal = [animation_id = file_id](FileId file_id) {
|
|
|
|
return file_id == animation_id ||
|
|
|
|
(file_id.get_remote() == animation_id.get_remote() && animation_id.get_remote() != 0);
|
|
|
|
};
|
|
|
|
if (!td::remove_if(saved_animation_ids_, is_equal)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
|
|
|
auto animation = get_animation(file_id);
|
|
|
|
if (animation == nullptr) {
|
2021-09-24 09:59:51 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Animation not found"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-01-23 20:20:48 +01:00
|
|
|
send_save_gif_query(file_id, true, std::move(promise));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
send_update_saved_animations();
|
|
|
|
}
|
|
|
|
|
2020-05-30 01:42:45 +02:00
|
|
|
void AnimationsManager::try_send_update_animation_search_parameters() const {
|
|
|
|
auto update_animation_search_parameters = get_update_animation_search_parameters_object();
|
|
|
|
if (update_animation_search_parameters != nullptr) {
|
|
|
|
send_closure(G()->td(), &Td::send_update, std::move(update_animation_search_parameters));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
td_api::object_ptr<td_api::updateAnimationSearchParameters>
|
|
|
|
AnimationsManager::get_update_animation_search_parameters_object() const {
|
|
|
|
if (!is_animation_search_emojis_inited_ || !is_animation_search_provider_inited_) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return td_api::make_object<td_api::updateAnimationSearchParameters>(animation_search_provider_,
|
|
|
|
full_split(animation_search_emojis_, ','));
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:48:14 +02:00
|
|
|
td_api::object_ptr<td_api::updateSavedAnimations> AnimationsManager::get_update_saved_animations_object() const {
|
2018-09-20 21:38:19 +02:00
|
|
|
return td_api::make_object<td_api::updateSavedAnimations>(
|
2019-01-21 18:19:02 +01:00
|
|
|
td_->file_manager_->get_file_ids_object(saved_animation_ids_));
|
2018-09-20 21:38:19 +02:00
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void AnimationsManager::send_update_saved_animations(bool from_database) {
|
|
|
|
if (are_saved_animations_loaded_) {
|
2019-01-23 20:20:48 +01:00
|
|
|
vector<FileId> new_saved_animation_file_ids = saved_animation_ids_;
|
2019-01-21 21:30:28 +01:00
|
|
|
for (auto &animation_id : saved_animation_ids_) {
|
2020-05-30 00:48:56 +02:00
|
|
|
auto animation = get_animation(animation_id);
|
|
|
|
CHECK(animation != nullptr);
|
|
|
|
if (animation->thumbnail.file_id.is_valid()) {
|
|
|
|
new_saved_animation_file_ids.push_back(animation->thumbnail.file_id);
|
|
|
|
}
|
|
|
|
if (animation->animated_thumbnail.file_id.is_valid()) {
|
|
|
|
new_saved_animation_file_ids.push_back(animation->animated_thumbnail.file_id);
|
2019-01-23 20:20:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
std::sort(new_saved_animation_file_ids.begin(), new_saved_animation_file_ids.end());
|
|
|
|
if (new_saved_animation_file_ids != saved_animation_file_ids_) {
|
2019-01-30 22:37:38 +01:00
|
|
|
td_->file_manager_->change_files_source(get_saved_animations_file_source_id(), saved_animation_file_ids_,
|
2019-01-23 20:20:48 +01:00
|
|
|
new_saved_animation_file_ids);
|
|
|
|
saved_animation_file_ids_ = std::move(new_saved_animation_file_ids);
|
2019-01-21 21:30:28 +01:00
|
|
|
}
|
|
|
|
|
2018-09-26 17:48:14 +02:00
|
|
|
send_closure(G()->td(), &Td::send_update, get_update_saved_animations_object());
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-03-08 19:04:28 +01:00
|
|
|
if (!from_database) {
|
|
|
|
save_saved_animations_to_database();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-08 19:04:28 +01:00
|
|
|
void AnimationsManager::save_saved_animations_to_database() {
|
2023-03-13 23:41:04 +01:00
|
|
|
if (G()->use_sqlite_pmc()) {
|
2018-03-08 19:04:28 +01:00
|
|
|
LOG(INFO) << "Save saved animations to database";
|
|
|
|
AnimationListLogEvent log_event(saved_animation_ids_);
|
|
|
|
G()->td_db()->get_sqlite_pmc()->set("ans", log_event_store(log_event).as_slice().str(), Auto());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-30 22:37:38 +01:00
|
|
|
FileSourceId AnimationsManager::get_saved_animations_file_source_id() {
|
|
|
|
if (!saved_animations_file_source_id_.is_valid()) {
|
|
|
|
saved_animations_file_source_id_ = td_->file_reference_manager_->create_saved_animations_file_source();
|
|
|
|
}
|
|
|
|
return saved_animations_file_source_id_;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
string AnimationsManager::get_animation_search_text(FileId file_id) const {
|
|
|
|
auto animation = get_animation(file_id);
|
|
|
|
CHECK(animation != nullptr);
|
|
|
|
return animation->file_name;
|
|
|
|
}
|
|
|
|
|
2018-09-20 21:38:19 +02:00
|
|
|
void AnimationsManager::get_current_state(vector<td_api::object_ptr<td_api::Update>> &updates) const {
|
2018-09-21 09:15:07 +02:00
|
|
|
if (td_->auth_manager_->is_bot()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-20 21:38:19 +02:00
|
|
|
if (are_saved_animations_loaded_) {
|
2018-09-26 17:48:14 +02:00
|
|
|
updates.push_back(get_update_saved_animations_object());
|
2018-09-20 21:38:19 +02:00
|
|
|
}
|
2020-05-30 01:42:45 +02:00
|
|
|
auto update_animation_search_parameters = get_update_animation_search_parameters_object();
|
|
|
|
if (update_animation_search_parameters != nullptr) {
|
|
|
|
updates.push_back(std::move(update_animation_search_parameters));
|
|
|
|
}
|
2018-09-20 21:38:19 +02:00
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|