Add reload_saved_animations.

GitOrigin-RevId: e69e2d60bc5464b880ed478dedc084656a1e878d
This commit is contained in:
levlam 2019-01-15 03:04:10 +03:00
parent 6f813b25ac
commit 626d1e07d0
2 changed files with 52 additions and 17 deletions

View File

@ -36,8 +36,11 @@
namespace td { namespace td {
class GetSavedGifsQuery : public Td::ResultHandler { class GetSavedGifsQuery : public Td::ResultHandler {
bool is_reload_ = false;
public: public:
void send(int32 hash) { void send(bool is_reload, int32 hash) {
is_reload_ = is_reload;
LOG(INFO) << "Send get saved animations request with hash = " << hash; LOG(INFO) << "Send get saved animations request with hash = " << hash;
send_query(G()->net_query_creator().create(create_storer(telegram_api::messages_getSavedGifs(hash)))); send_query(G()->net_query_creator().create(create_storer(telegram_api::messages_getSavedGifs(hash))));
} }
@ -49,12 +52,12 @@ class GetSavedGifsQuery : public Td::ResultHandler {
} }
auto ptr = result_ptr.move_as_ok(); auto ptr = result_ptr.move_as_ok();
td->animations_manager_->on_get_saved_animations(std::move(ptr)); td->animations_manager_->on_get_saved_animations(is_reload_, std::move(ptr));
} }
void on_error(uint64 id, Status status) override { void on_error(uint64 id, Status status) override {
LOG(ERROR) << "Receive error for get saved animations: " << status; LOG(ERROR) << "Receive error for get saved animations: " << status;
td->animations_manager_->on_get_saved_animations_failed(std::move(status)); td->animations_manager_->on_get_saved_animations_failed(is_reload_, std::move(status));
} }
}; };
@ -402,7 +405,18 @@ void AnimationsManager::reload_saved_animations(bool force) {
(next_saved_animations_load_time_ < Time::now() || force)) { (next_saved_animations_load_time_ < Time::now() || force)) {
LOG_IF(INFO, force) << "Reload saved animations"; LOG_IF(INFO, force) << "Reload saved animations";
next_saved_animations_load_time_ = -1; next_saved_animations_load_time_ = -1;
td_->create_handler<GetSavedGifsQuery>()->send(get_saved_animations_hash("reload_saved_animations")); td_->create_handler<GetSavedGifsQuery>()->send(false, get_saved_animations_hash("reload_saved_animations"));
}
}
void AnimationsManager::reload_saved_animations(Promise<Unit> &&promise) {
if (td_->auth_manager_->is_bot()) {
return promise.set_error(Status::Error(400, "Bots has no saved animations"));
}
reload_saved_animations_queries_.push_back(std::move(promise));
if (reload_saved_animations_queries_.size() == 1u) {
td_->create_handler<GetSavedGifsQuery>()->send(true, 0);
} }
} }
@ -471,13 +485,18 @@ void AnimationsManager::on_load_saved_animations_finished(vector<FileId> &&saved
} }
void AnimationsManager::on_get_saved_animations( void AnimationsManager::on_get_saved_animations(
tl_object_ptr<telegram_api::messages_SavedGifs> &&saved_animations_ptr) { bool is_reload, tl_object_ptr<telegram_api::messages_SavedGifs> &&saved_animations_ptr) {
CHECK(!td_->auth_manager_->is_bot()); CHECK(!td_->auth_manager_->is_bot());
if (!is_reload) {
next_saved_animations_load_time_ = Time::now_cached() + Random::fast(30 * 60, 50 * 60); next_saved_animations_load_time_ = Time::now_cached() + Random::fast(30 * 60, 50 * 60);
}
CHECK(saved_animations_ptr != nullptr); CHECK(saved_animations_ptr != nullptr);
int32 constructor_id = saved_animations_ptr->get_id(); int32 constructor_id = saved_animations_ptr->get_id();
if (constructor_id == telegram_api::messages_savedGifsNotModified::ID) { if (constructor_id == telegram_api::messages_savedGifsNotModified::ID) {
if (is_reload) {
return on_get_saved_animations_failed(true, Status::Error(500, "Failed to reload saved animations"));
}
LOG(INFO) << "Saved animations are not modified"; LOG(INFO) << "Saved animations are not modified";
return; return;
} }
@ -500,21 +519,34 @@ void AnimationsManager::on_get_saved_animations(
LOG(ERROR) << "Receive " << static_cast<int>(document.first) << " instead of animation as saved animation"; LOG(ERROR) << "Receive " << static_cast<int>(document.first) << " instead of animation as saved animation";
continue; continue;
} }
if (!is_reload) {
saved_animation_ids.push_back(document.second); saved_animation_ids.push_back(document.second);
} }
}
if (is_reload) {
auto promises = std::move(reload_saved_animations_queries_);
reload_saved_animations_queries_.clear();
for (auto &promise : promises) {
promise.set_value(Unit());
}
} else {
on_load_saved_animations_finished(std::move(saved_animation_ids)); on_load_saved_animations_finished(std::move(saved_animation_ids));
LOG_IF(ERROR, get_saved_animations_hash("on_get_saved_animations") != saved_animations->hash_) LOG_IF(ERROR, get_saved_animations_hash("on_get_saved_animations") != saved_animations->hash_)
<< "Saved animations hash mismatch: " << saved_animations->hash_ << " vs " << "Saved animations hash mismatch: " << saved_animations->hash_ << " vs "
<< get_saved_animations_hash("on_get_saved_animations 2"); << get_saved_animations_hash("on_get_saved_animations 2");
}
} }
void AnimationsManager::on_get_saved_animations_failed(Status error) { void AnimationsManager::on_get_saved_animations_failed(bool is_reload, Status error) {
CHECK(error.is_error()); CHECK(error.is_error());
if (!is_reload) {
next_saved_animations_load_time_ = Time::now_cached() + Random::fast(5, 10); next_saved_animations_load_time_ = Time::now_cached() + Random::fast(5, 10);
auto promises = std::move(load_saved_animations_queries_); }
load_saved_animations_queries_.clear(); auto &queries = is_reload ? reload_saved_animations_queries_ : load_saved_animations_queries_;
auto promises = std::move(queries);
queries.clear();
for (auto &promise : promises) { for (auto &promise : promises) {
promise.set_error(error.clone()); promise.set_error(error.clone());
} }

View File

@ -59,9 +59,11 @@ class AnimationsManager : public Actor {
void reload_saved_animations(bool force); void reload_saved_animations(bool force);
void on_get_saved_animations(tl_object_ptr<telegram_api::messages_SavedGifs> &&saved_animations_ptr); void reload_saved_animations(Promise<Unit> &&promise);
void on_get_saved_animations_failed(Status error); void on_get_saved_animations(bool is_reload, tl_object_ptr<telegram_api::messages_SavedGifs> &&saved_animations_ptr);
void on_get_saved_animations_failed(bool is_reload, Status error);
vector<FileId> get_saved_animations(Promise<Unit> &&promise); vector<FileId> get_saved_animations(Promise<Unit> &&promise);
@ -133,6 +135,7 @@ class AnimationsManager : public Actor {
double next_saved_animations_load_time_ = 0; double next_saved_animations_load_time_ = 0;
bool are_saved_animations_loaded_ = false; bool are_saved_animations_loaded_ = false;
vector<Promise<Unit>> load_saved_animations_queries_; vector<Promise<Unit>> load_saved_animations_queries_;
vector<Promise<Unit>> reload_saved_animations_queries_;
}; };
} // namespace td } // namespace td