2018-12-31 20:04:05 +01:00
|
|
|
//
|
2018-12-31 23:02:34 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2019
|
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"
|
2018-12-04 19:18:07 +01:00
|
|
|
#include "td/telegram/SecretChatActor.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/Td.h"
|
2019-01-06 20:59:17 +01:00
|
|
|
#include "td/telegram/TdDb.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#include "td/telegram/secret_api.h"
|
|
|
|
#include "td/telegram/td_api.h"
|
|
|
|
#include "td/telegram/telegram_api.h"
|
|
|
|
|
2018-07-18 03:11:48 +02:00
|
|
|
#include "td/actor/PromiseFuture.h"
|
|
|
|
|
|
|
|
#include "td/db/SqliteKeyValueAsync.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 {
|
|
|
|
|
|
|
|
class GetSavedGifsQuery : 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:
|
2019-02-02 11:54:40 +01:00
|
|
|
void send(bool is_repair, int32 hash) {
|
|
|
|
is_repair_ = is_repair;
|
2018-12-31 20:04:05 +01:00
|
|
|
LOG(INFO) << "Send get saved animations request with hash = " << hash;
|
|
|
|
send_query(G()->net_query_creator().create(create_storer(telegram_api::messages_getSavedGifs(hash))));
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_result(uint64 id, BufferSlice packet) override {
|
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_getSavedGifs>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
|
|
|
return on_error(id, result_ptr.move_as_error());
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ptr = result_ptr.move_as_ok();
|
2019-02-02 11:54:40 +01:00
|
|
|
td->animations_manager_->on_get_saved_animations(is_repair_, std::move(ptr));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void on_error(uint64 id, Status status) override {
|
2019-02-14 16:36:16 +01:00
|
|
|
if (!G()->close_flag()) {
|
|
|
|
LOG(ERROR) << "Receive error for get saved animations: " << status;
|
|
|
|
}
|
2019-02-02 11:54:40 +01:00
|
|
|
td->animations_manager_->on_get_saved_animations_failed(is_repair_, std::move(status));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class SaveGifQuery : 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;
|
2018-12-31 20:04:05 +01:00
|
|
|
send_query(G()->net_query_creator().create(
|
|
|
|
create_storer(telegram_api::messages_saveGif(std::move(input_document), unsave))));
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_result(uint64 id, BufferSlice packet) override {
|
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_saveGif>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
|
|
|
return on_error(id, result_ptr.move_as_error());
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
td->animations_manager_->reload_saved_animations(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
promise_.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_error(uint64 id, Status status) override {
|
2019-01-23 20:20:48 +01:00
|
|
|
if (FileReferenceManager::is_file_reference_error(status)) {
|
2019-02-08 15:54:23 +01:00
|
|
|
VLOG(file_references) << "Receive " << status << " for " << file_id_;
|
2019-01-23 20:20:48 +01:00
|
|
|
td->file_manager_->delete_file_reference(file_id_, file_reference_);
|
|
|
|
td->file_reference_manager_->repair_file_reference(
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-02-14 16:36:16 +01:00
|
|
|
if (!G()->close_flag()) {
|
|
|
|
LOG(ERROR) << "Receive error for save GIF: " << status;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
td->animations_manager_->reload_saved_animations(true);
|
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
AnimationsManager::AnimationsManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) {
|
|
|
|
auto limit_string = G()->td_db()->get_binlog_pmc()->get("saved_animations_limit");
|
|
|
|
if (!limit_string.empty()) {
|
|
|
|
auto new_limit = to_integer<int32>(limit_string);
|
|
|
|
if (new_limit > 0) {
|
|
|
|
LOG(DEBUG) << "Load saved animations limit = " << new_limit;
|
|
|
|
saved_animations_limit_ = new_limit;
|
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "Wrong saved animations limit = \"" << limit_string << "\" stored in database";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnimationsManager::tear_down() {
|
|
|
|
parent_.reset();
|
|
|
|
}
|
|
|
|
|
2018-09-27 20:14:32 +02:00
|
|
|
int32 AnimationsManager::get_animation_duration(FileId file_id) const {
|
|
|
|
auto it = animations_.find(file_id);
|
|
|
|
CHECK(it != animations_.end());
|
|
|
|
return it->second->duration;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
tl_object_ptr<td_api::animation> AnimationsManager::get_animation_object(FileId file_id, const char *source) {
|
|
|
|
if (!file_id.is_valid()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto &animation = animations_[file_id];
|
2019-02-12 17:17:20 +01:00
|
|
|
LOG_CHECK(animation != nullptr) << source << " " << file_id << " "
|
2019-02-12 22:28:47 +01:00
|
|
|
<< static_cast<int32>(td_->file_manager_->get_file_view(file_id).get_type());
|
2018-12-31 20:04:05 +01:00
|
|
|
// TODO can we make that function const?
|
|
|
|
animation->is_changed = false;
|
|
|
|
return make_tl_object<td_api::animation>(animation->duration, animation->dimensions.width,
|
|
|
|
animation->dimensions.height, animation->file_name, animation->mime_type,
|
2019-03-01 20:51:33 +01:00
|
|
|
get_minithumbnail_object(animation->minithumbnail),
|
2018-12-31 20:04:05 +01:00
|
|
|
get_photo_size_object(td_->file_manager_.get(), &animation->thumbnail),
|
2018-03-07 00:02:49 +01:00
|
|
|
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";
|
|
|
|
a->mime_type = new_animation->mime_type;
|
|
|
|
a->is_changed = true;
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
a->is_changed = true;
|
|
|
|
}
|
|
|
|
if (a->dimensions != new_animation->dimensions) {
|
|
|
|
LOG(DEBUG) << "Animation " << file_id << " dimensions has changed";
|
|
|
|
a->dimensions = new_animation->dimensions;
|
|
|
|
a->is_changed = true;
|
|
|
|
}
|
|
|
|
if (a->duration != new_animation->duration) {
|
|
|
|
LOG(DEBUG) << "Animation " << file_id << " duration has changed";
|
|
|
|
a->duration = new_animation->duration;
|
|
|
|
a->is_changed = true;
|
|
|
|
}
|
2019-03-01 20:51:33 +01:00
|
|
|
if (a->minithumbnail != new_animation->minithumbnail) {
|
|
|
|
a->minithumbnail = std::move(new_animation->minithumbnail);
|
|
|
|
a->is_changed = true;
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
a->thumbnail = new_animation->thumbnail;
|
|
|
|
a->is_changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return file_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
const AnimationsManager::Animation *AnimationsManager::get_animation(FileId file_id) const {
|
|
|
|
auto animation = animations_.find(file_id);
|
|
|
|
if (animation == animations_.end()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK(animation->second->file_id == file_id);
|
|
|
|
return animation->second.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnimationsManager::delete_animation_thumbnail(FileId file_id) {
|
|
|
|
auto &animation = animations_[file_id];
|
|
|
|
CHECK(animation != nullptr);
|
|
|
|
animation->thumbnail = PhotoSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
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];
|
|
|
|
CHECK(!new_animation);
|
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;
|
|
|
|
new_animation->thumbnail.file_id = td_->file_manager_->dup_file_id(new_animation->thumbnail.file_id);
|
|
|
|
return new_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AnimationsManager::merge_animations(FileId new_id, FileId old_id, bool can_delete_old) {
|
|
|
|
if (!old_id.is_valid()) {
|
|
|
|
LOG(ERROR) << "Old file id is invalid";
|
|
|
|
return true;
|
|
|
|
}
|
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);
|
|
|
|
if (old_id == new_id) {
|
|
|
|
return old_->is_changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto new_it = animations_.find(new_id);
|
|
|
|
if (new_it == animations_.end()) {
|
|
|
|
auto &old = animations_[old_id];
|
|
|
|
old->is_changed = true;
|
|
|
|
if (!can_delete_old) {
|
|
|
|
dup_animation(new_id, old_id);
|
|
|
|
} else {
|
|
|
|
old->file_id = new_id;
|
|
|
|
animations_.emplace(new_id, std::move(old));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Animation *new_ = new_it->second.get();
|
|
|
|
CHECK(new_ != nullptr);
|
|
|
|
|
|
|
|
new_->is_changed = true;
|
|
|
|
if (old_->thumbnail != new_->thumbnail) {
|
|
|
|
// LOG_STATUS(td_->file_manager_->merge(new_->thumbnail.file_id, old_->thumbnail.file_id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LOG_STATUS(td_->file_manager_->merge(new_id, old_id));
|
|
|
|
if (can_delete_old) {
|
|
|
|
animations_.erase(old_id);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-03-01 20:51:33 +01:00
|
|
|
void AnimationsManager::create_animation(FileId file_id, string minithumbnail, PhotoSize thumbnail, 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;
|
2019-03-01 20:51:33 +01:00
|
|
|
a->minithumbnail = std::move(minithumbnail);
|
2018-12-31 20:04:05 +01:00
|
|
|
a->thumbnail = std::move(thumbnail);
|
|
|
|
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,
|
2018-01-30 18:06:54 +01:00
|
|
|
tl_object_ptr<telegram_api::InputFile> input_thumbnail) 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) {
|
|
|
|
return make_tl_object<telegram_api::inputMediaDocument>(0, file_view.main_remote_location().as_input_document(), 0);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (file_view.has_url()) {
|
2018-01-30 18:06:54 +01:00
|
|
|
return make_tl_object<telegram_api::inputMediaDocumentExternal>(0, 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>(
|
2018-02-07 00:31:38 +01:00
|
|
|
0, false /*ignored*/, false /*ignored*/, animation->duration, animation->dimensions.width,
|
|
|
|
animation->dimensions.height));
|
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;
|
|
|
|
if (input_thumbnail != nullptr) {
|
|
|
|
flags |= telegram_api::inputMediaUploadedDocument::THUMB_MASK;
|
|
|
|
}
|
|
|
|
return make_tl_object<telegram_api::inputMediaUploadedDocument>(
|
|
|
|
flags, false /*ignored*/, std::move(input_file), std::move(input_thumbnail), mime_type, std::move(attributes),
|
2018-01-30 18:06:54 +01:00
|
|
|
vector<tl_object_ptr<telegram_api::InputDocument>>(), 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,
|
2018-12-04 19:18:07 +01: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);
|
|
|
|
auto &encryption_key = file_view.encryption_key();
|
2018-06-08 21:23:49 +02:00
|
|
|
if (!file_view.is_encrypted_secret() || 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") {
|
2018-12-04 19:18:07 +01:00
|
|
|
if (layer >= SecretChatActor::VIDEO_NOTES_LAYER) {
|
|
|
|
attributes.push_back(make_tl_object<secret_api::documentAttributeVideo66>(
|
|
|
|
0, false, animation->duration, animation->dimensions.width, animation->dimensions.height));
|
|
|
|
} else {
|
|
|
|
attributes.push_back(make_tl_object<secret_api::documentAttributeVideo>(
|
|
|
|
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>());
|
|
|
|
|
|
|
|
return SecretInputMedia{
|
|
|
|
std::move(input_file),
|
|
|
|
make_tl_object<secret_api::decryptedMessageMediaDocument>(
|
|
|
|
std::move(thumbnail), animation->thumbnail.dimensions.width, animation->thumbnail.dimensions.height,
|
|
|
|
animation->mime_type, narrow_cast<int32>(file_view.size()), BufferSlice(encryption_key.key_slice()),
|
|
|
|
BufferSlice(encryption_key.iv_slice()), std::move(attributes), caption)};
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnimationsManager::on_update_saved_animations_limit(int32 saved_animations_limit) {
|
|
|
|
if (saved_animations_limit != saved_animations_limit_) {
|
|
|
|
if (saved_animations_limit > 0) {
|
|
|
|
LOG(INFO) << "Update saved animations limit to " << saved_animations_limit;
|
|
|
|
G()->td_db()->get_binlog_pmc()->set("saved_animations_limit", to_string(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;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!td_->auth_manager_->is_bot() && next_saved_animations_load_time_ >= 0 &&
|
|
|
|
(next_saved_animations_load_time_ < Time::now() || force)) {
|
|
|
|
LOG_IF(INFO, force) << "Reload saved animations";
|
|
|
|
next_saved_animations_load_time_ = -1;
|
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()) {
|
|
|
|
return promise.set_error(Status::Error(400, "Bots has no saved animations"));
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
if (G()->parameters().use_file_db) { // otherwise there is no sqlite_pmc, TODO
|
|
|
|
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) {
|
|
|
|
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);
|
|
|
|
auto promises = std::move(load_saved_animations_queries_);
|
|
|
|
load_saved_animations_queries_.clear();
|
|
|
|
for (auto &promise : promises) {
|
|
|
|
promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
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) {
|
|
|
|
auto promises = std::move(repair_saved_animations_queries_);
|
|
|
|
repair_saved_animations_queries_.clear();
|
2019-01-15 01:04:10 +01:00
|
|
|
for (auto &promise : promises) {
|
|
|
|
promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
} 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) {
|
2019-01-15 01:04:10 +01:00
|
|
|
next_saved_animations_load_time_ = Time::now_cached() + Random::fast(5, 10);
|
|
|
|
}
|
2019-02-02 11:54:40 +01:00
|
|
|
auto &queries = is_repair ? repair_saved_animations_queries_ : load_saved_animations_queries_;
|
2019-01-15 01:04:10 +01:00
|
|
|
auto promises = std::move(queries);
|
|
|
|
queries.clear();
|
2018-12-31 20:04:05 +01:00
|
|
|
for (auto &promise : promises) {
|
|
|
|
promise.set_error(error.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-05 16:03:08 +02:00
|
|
|
int32 AnimationsManager::get_saved_animations_hash(const char *source) const {
|
2018-12-31 20:04:05 +01:00
|
|
|
vector<uint32> numbers;
|
|
|
|
numbers.reserve(saved_animation_ids_.size() * 2);
|
|
|
|
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());
|
2019-02-12 17:17:20 +01:00
|
|
|
LOG_CHECK(file_view.remote_location().is_document()) << source << " " << file_view.remote_location();
|
2018-12-31 20:04:05 +01:00
|
|
|
auto id = static_cast<uint64>(file_view.remote_location().get_id());
|
|
|
|
numbers.push_back(static_cast<uint32>(id >> 32));
|
|
|
|
numbers.push_back(static_cast<uint32>(id & 0xFFFFFFFF));
|
|
|
|
}
|
|
|
|
return get_vector_hash(numbers);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnimationsManager::add_saved_animation(const tl_object_ptr<td_api::InputFile> &input_file,
|
|
|
|
Promise<Unit> &&promise) {
|
|
|
|
if (td_->auth_manager_->is_bot()) {
|
|
|
|
return promise.set_error(Status::Error(7, "Method is not available for bots"));
|
|
|
|
}
|
|
|
|
if (!are_saved_animations_loaded_) {
|
|
|
|
load_saved_animations(std::move(promise));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto r_file_id = td_->file_manager_->get_input_file_id(FileType::Animation, input_file, DialogId(), false, false);
|
|
|
|
if (r_file_id.is_error()) {
|
|
|
|
return promise.set_error(Status::Error(7, r_file_id.error().message())); // TODO do not drop error code
|
|
|
|
}
|
|
|
|
|
|
|
|
add_saved_animation_inner(r_file_id.ok(), std::move(promise));
|
|
|
|
}
|
|
|
|
|
2019-01-23 20:20:48 +01:00
|
|
|
void AnimationsManager::send_save_gif_query(FileId animation_id, bool unsave, Promise<Unit> &&promise) {
|
2019-04-08 16:57:41 +02:00
|
|
|
if (G()->close_flag()) {
|
|
|
|
return promise.set_error(Status::Error(500, "Request aborted"));
|
|
|
|
}
|
|
|
|
|
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_inner(FileId animation_id, Promise<Unit> &&promise) {
|
|
|
|
if (add_saved_animation_impl(animation_id, promise)) {
|
2019-01-23 20:20:48 +01:00
|
|
|
send_save_gif_query(animation_id, false, std::move(promise));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnimationsManager::add_saved_animation_by_id(FileId animation_id) {
|
|
|
|
// TODO log event
|
|
|
|
Promise<Unit> promise;
|
|
|
|
add_saved_animation_impl(animation_id, promise);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AnimationsManager::add_saved_animation_impl(FileId animation_id, Promise<Unit> &promise) {
|
|
|
|
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()) {
|
|
|
|
promise.set_error(Status::Error(7, "Animation file not found"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(INFO) << "Add saved animation " << animation_id << " with main file " << file_view.file_id();
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!are_saved_animations_loaded_) {
|
|
|
|
load_saved_animations(PromiseCreator::lambda([animation_id, promise = std::move(promise)](Result<> result) mutable {
|
|
|
|
if (result.is_ok()) {
|
|
|
|
send_closure(G()->animations_manager(), &AnimationsManager::add_saved_animation_inner, animation_id,
|
|
|
|
std::move(promise));
|
|
|
|
} else {
|
|
|
|
promise.set_error(result.move_as_error());
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
promise.set_value(Unit());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto animation = get_animation(animation_id);
|
|
|
|
if (animation == nullptr) {
|
|
|
|
promise.set_error(Status::Error(7, "Animation not found"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (animation->mime_type != "video/mp4") {
|
|
|
|
promise.set_error(Status::Error(7, "Only MPEG4 animations can be saved"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!file_view.has_remote_location()) {
|
|
|
|
promise.set_error(Status::Error(7, "Can save only sent animations"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (file_view.remote_location().is_web()) {
|
|
|
|
promise.set_error(Status::Error(7, "Can't save web animations"));
|
|
|
|
return false;
|
|
|
|
}
|
2018-03-27 15:11:15 +02:00
|
|
|
if (!file_view.remote_location().is_document()) {
|
|
|
|
promise.set_error(Status::Error(7, "Can't save encrypted animations"));
|
|
|
|
return false;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-02-16 03:29:28 +01:00
|
|
|
auto it = std::find_if(saved_animation_ids_.begin(), saved_animation_ids_.end(), is_equal);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (it == saved_animation_ids_.end()) {
|
|
|
|
if (static_cast<int32>(saved_animation_ids_.size()) == saved_animations_limit_) {
|
|
|
|
saved_animation_ids_.back() = animation_id;
|
|
|
|
} else {
|
|
|
|
saved_animation_ids_.push_back(animation_id);
|
|
|
|
}
|
|
|
|
it = saved_animation_ids_.end() - 1;
|
|
|
|
}
|
|
|
|
std::rotate(saved_animation_ids_.begin(), it, it + 1);
|
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();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnimationsManager::remove_saved_animation(const tl_object_ptr<td_api::InputFile> &input_file,
|
|
|
|
Promise<Unit> &&promise) {
|
|
|
|
if (td_->auth_manager_->is_bot()) {
|
|
|
|
return promise.set_error(Status::Error(7, "Method is not available for bots"));
|
|
|
|
}
|
|
|
|
if (!are_saved_animations_loaded_) {
|
|
|
|
load_saved_animations(std::move(promise));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto r_file_id = td_->file_manager_->get_input_file_id(FileType::Animation, input_file, DialogId(), false, false);
|
|
|
|
if (r_file_id.is_error()) {
|
|
|
|
return promise.set_error(Status::Error(7, r_file_id.error().message())); // TODO do not drop error code
|
|
|
|
}
|
|
|
|
|
|
|
|
FileId file_id = r_file_id.ok();
|
2019-10-22 01:12:58 +02:00
|
|
|
if (!td::remove(saved_animation_ids_, file_id)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
|
|
|
auto animation = get_animation(file_id);
|
|
|
|
if (animation == nullptr) {
|
|
|
|
return promise.set_error(Status::Error(7, "Animation not found"));
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
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_) {
|
|
|
|
auto thumbnail_file_id = get_animation_thumbnail_file_id(animation_id);
|
|
|
|
if (thumbnail_file_id.is_valid()) {
|
2019-01-23 20:20:48 +01:00
|
|
|
new_saved_animation_file_ids.push_back(thumbnail_file_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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() {
|
|
|
|
if (G()->parameters().use_file_db) {
|
|
|
|
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-12-12 03:47:20 +01:00
|
|
|
void AnimationsManager::after_get_difference() {
|
|
|
|
if (td_->is_online()) {
|
|
|
|
get_saved_animations(Auto());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|