From 956e31810acb4a82bff78ea66e5dab7760584bd3 Mon Sep 17 00:00:00 2001 From: levlam Date: Sun, 16 Jul 2023 15:39:55 +0300 Subject: [PATCH] Add promise to StoryManager::on_get_user_stories. --- td/telegram/ContactsManager.cpp | 2 +- td/telegram/StoryManager.cpp | 56 ++++++++++++++++++++------------- td/telegram/StoryManager.h | 9 ++++-- 3 files changed, 41 insertions(+), 26 deletions(-) diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index 97cda79b8..c3000987d 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -12540,7 +12540,7 @@ void ContactsManager::on_get_user_full(tl_object_ptr &&u td_->messages_manager_->on_update_dialog_is_translatable(DialogId(user_id), !user->translations_disabled_); - td_->story_manager_->on_get_user_stories(DialogId(user_id), std::move(user->stories_)); + td_->story_manager_->on_get_user_stories(DialogId(user_id), std::move(user->stories_), Promise()); UserFull *user_full = add_user_full(user_id); user_full->expires_at = Time::now() + USER_FULL_EXPIRE_TIME; diff --git a/td/telegram/StoryManager.cpp b/td/telegram/StoryManager.cpp index 9fe33b8da..9458ad1c7 100644 --- a/td/telegram/StoryManager.cpp +++ b/td/telegram/StoryManager.cpp @@ -992,7 +992,8 @@ void StoryManager::on_story_expire_timeout(int64 story_global_id) { auto active_stories = get_active_stories(owner_dialog_id); if (active_stories != nullptr && contains(active_stories->story_ids_, story_full_id.get_story_id())) { auto story_ids = active_stories->story_ids_; - on_update_active_stories(owner_dialog_id, active_stories->max_read_story_id_, std::move(story_ids)); + on_update_active_stories(owner_dialog_id, active_stories->max_read_story_id_, std::move(story_ids), + Promise()); } } @@ -1240,7 +1241,7 @@ StoryManager::ActiveStories *StoryManager::on_get_active_stories_from_database(D if (status.is_error()) { LOG(ERROR) << "Receive invalid active stories in " << owner_dialog_id << " from database: " << status << ' ' << format::as_hex_dump<4>(value.as_slice()); - save_active_stories(owner_dialog_id, nullptr); + save_active_stories(owner_dialog_id, nullptr, Promise()); failed_to_load_active_stories_.insert(owner_dialog_id); return nullptr; } @@ -1250,7 +1251,8 @@ StoryManager::ActiveStories *StoryManager::on_get_active_stories_from_database(D story_ids.push_back(on_get_story_info(owner_dialog_id, std::move(story_info))); } - on_update_active_stories(owner_dialog_id, saved_active_stories.max_read_story_id_, std::move(story_ids)); + on_update_active_stories(owner_dialog_id, saved_active_stories.max_read_story_id_, std::move(story_ids), + Promise()); return get_active_stories_editable(owner_dialog_id); } @@ -1347,7 +1349,7 @@ void StoryManager::on_load_active_stories( auto max_story_date = MIN_DIALOG_DATE; vector owner_dialog_ids; for (auto &user_stories : stories->user_stories_) { - auto owner_dialog_id = on_get_user_stories(DialogId(), std::move(user_stories)); + auto owner_dialog_id = on_get_user_stories(DialogId(), std::move(user_stories), Promise()); auto active_stories = get_active_stories(owner_dialog_id); if (active_stories == nullptr) { LOG(ERROR) << "Receive invalid stories"; @@ -1385,7 +1387,7 @@ void StoryManager::on_load_active_stories( LOG(INFO) << "Delete active stories in " << delete_dialog_ids; } for (auto dialog_id : delete_dialog_ids) { - on_update_active_stories(dialog_id, StoryId(), vector()); + on_update_active_stories(dialog_id, StoryId(), vector(), Promise()); load_dialog_expiring_stories(dialog_id, 0, "on_load_active_stories 1"); } update_story_list_sent_total_count(story_list_id, story_list); @@ -1670,7 +1672,7 @@ void StoryManager::on_get_dialog_expiring_stories(DialogId owner_dialog_id, Promise> &&promise) { TRY_STATUS_PROMISE(promise, G()->close_status()); td_->contacts_manager_->on_get_users(std::move(stories->users_), "on_get_dialog_expiring_stories"); - owner_dialog_id = on_get_user_stories(owner_dialog_id, std::move(stories->stories_)); + owner_dialog_id = on_get_user_stories(owner_dialog_id, std::move(stories->stories_), Promise()); if (promise) { promise.set_value(get_chat_active_stories_object(owner_dialog_id)); } else { @@ -2391,7 +2393,8 @@ StoryId StoryManager::on_get_new_story(DialogId owner_dialog_id, i--; } story_ids[i] = story_id; - on_update_active_stories(owner_dialog_id, active_stories->max_read_story_id_, std::move(story_ids)); + on_update_active_stories(owner_dialog_id, active_stories->max_read_story_id_, std::move(story_ids), + Promise()); } } @@ -2498,7 +2501,8 @@ void StoryManager::on_delete_story(StoryFullId story_full_id) { if (active_stories != nullptr && contains(active_stories->story_ids_, story_id)) { auto story_ids = active_stories->story_ids_; td::remove(story_ids, story_id); - on_update_active_stories(owner_dialog_id, active_stories->max_read_story_id_, std::move(story_ids)); + on_update_active_stories(owner_dialog_id, active_stories->max_read_story_id_, std::move(story_ids), + Promise()); } delete_story_from_database(story_full_id); @@ -2623,11 +2627,14 @@ std::pair> StoryManager::on_get_stories( } DialogId StoryManager::on_get_user_stories(DialogId owner_dialog_id, - telegram_api::object_ptr &&user_stories) { + telegram_api::object_ptr &&user_stories, + Promise &&promise) { if (user_stories == nullptr) { if (owner_dialog_id.is_valid()) { LOG(INFO) << "Receive no stories in " << owner_dialog_id; - on_update_active_stories(owner_dialog_id, StoryId(), {}); + on_update_active_stories(owner_dialog_id, StoryId(), {}, std::move(promise)); + } else { + promise.set_value(Unit()); } return owner_dialog_id; } @@ -2635,11 +2642,12 @@ DialogId StoryManager::on_get_user_stories(DialogId owner_dialog_id, DialogId story_dialog_id(UserId(user_stories->user_id_)); if (owner_dialog_id.is_valid() && owner_dialog_id != story_dialog_id) { LOG(ERROR) << "Receive stories from " << story_dialog_id << " instead of " << owner_dialog_id; - on_update_active_stories(owner_dialog_id, StoryId(), {}); + on_update_active_stories(owner_dialog_id, StoryId(), {}, std::move(promise)); return owner_dialog_id; } if (!story_dialog_id.is_valid()) { LOG(ERROR) << "Receive stories in " << story_dialog_id; + promise.set_value(Unit()); return owner_dialog_id; } owner_dialog_id = story_dialog_id; @@ -2669,12 +2677,12 @@ DialogId StoryManager::on_get_user_stories(DialogId owner_dialog_id, } } - on_update_active_stories(story_dialog_id, max_read_story_id, std::move(story_ids)); + on_update_active_stories(story_dialog_id, max_read_story_id, std::move(story_ids), std::move(promise)); return story_dialog_id; } void StoryManager::on_update_active_stories(DialogId owner_dialog_id, StoryId max_read_story_id, - vector &&story_ids) { + vector &&story_ids, Promise &&promise) { td::remove_if(story_ids, [&](StoryId story_id) { if (!story_id.is_server()) { return true; @@ -2704,10 +2712,11 @@ void StoryManager::on_update_active_stories(DialogId owner_dialog_id, StoryId ma update_story_list_sent_total_count(active_stories->story_list_id_); } active_stories_.erase(owner_dialog_id); - save_active_stories(owner_dialog_id, nullptr); + save_active_stories(owner_dialog_id, nullptr, std::move(promise)); send_update_chat_active_stories(owner_dialog_id, nullptr); } else { max_read_story_ids_.erase(owner_dialog_id); + promise.set_value(Unit()); } failed_to_load_active_stories_.insert(owner_dialog_id); return; @@ -2745,7 +2754,9 @@ void StoryManager::on_update_active_stories(DialogId owner_dialog_id, StoryId ma send_update_chat_active_stories(owner_dialog_id, active_stories.get()); } if (need_save_to_database) { - save_active_stories(owner_dialog_id, active_stories.get()); + save_active_stories(owner_dialog_id, active_stories.get(), std::move(promise)); + } else { + promise.set_value(Unit()); } } @@ -2847,13 +2858,14 @@ void StoryManager::send_update_chat_active_stories(DialogId owner_dialog_id, send_closure(G()->td(), &Td::send_update, get_update_chat_active_stories(owner_dialog_id, active_stories)); } -void StoryManager::save_active_stories(DialogId owner_dialog_id, const ActiveStories *active_stories) const { +void StoryManager::save_active_stories(DialogId owner_dialog_id, const ActiveStories *active_stories, + Promise &&promise) const { if (!G()->use_message_database()) { - return; + return promise.set_value(Unit()); } if (active_stories == nullptr) { LOG(INFO) << "Delete active stories of " << owner_dialog_id << " from database"; - G()->td_db()->get_story_db_async()->delete_active_stories(owner_dialog_id, Promise()); + G()->td_db()->get_story_db_async()->delete_active_stories(owner_dialog_id, std::move(promise)); } else { LOG(INFO) << "Add active stories of " << owner_dialog_id << " to database"; auto order = active_stories->story_list_id_.is_valid() ? active_stories->private_order_ : 0; @@ -2866,7 +2878,7 @@ void StoryManager::save_active_stories(DialogId owner_dialog_id, const ActiveSto } } G()->td_db()->get_story_db_async()->add_active_stories(owner_dialog_id, active_stories->story_list_id_, order, - log_event_store(saved_active_stories), Promise()); + log_event_store(saved_active_stories), std::move(promise)); } } @@ -2897,7 +2909,7 @@ bool StoryManager::on_update_read_stories(DialogId owner_dialog_id, StoryId max_ << active_stories->story_ids_ << " from " << active_stories->max_read_story_id_ << " to " << max_read_story_id; auto story_ids = active_stories->story_ids_; - on_update_active_stories(owner_dialog_id, max_read_story_id, std::move(story_ids)); + on_update_active_stories(owner_dialog_id, max_read_story_id, std::move(story_ids), Promise()); return true; } return false; @@ -2957,7 +2969,7 @@ void StoryManager::on_dialog_active_stories_order_updated(DialogId owner_dialog_ send_update_chat_active_stories(owner_dialog_id, active_stories); } if (need_save_to_database) { - save_active_stories(owner_dialog_id, active_stories); + save_active_stories(owner_dialog_id, active_stories, Promise()); } } @@ -3654,7 +3666,7 @@ void StoryManager::on_binlog_events(vector &&events) { } } else { auto story_ids = active_stories->story_ids_; - on_update_active_stories(dialog_id, max_read_story_id, std::move(story_ids)); + on_update_active_stories(dialog_id, max_read_story_id, std::move(story_ids), Promise()); } read_stories_on_server(dialog_id, max_read_story_id, event.id_); break; diff --git a/td/telegram/StoryManager.h b/td/telegram/StoryManager.h index 917f1367c..2a386c747 100644 --- a/td/telegram/StoryManager.h +++ b/td/telegram/StoryManager.h @@ -229,7 +229,8 @@ class StoryManager final : public Actor { telegram_api::object_ptr &&stories); DialogId on_get_user_stories(DialogId owner_dialog_id, - telegram_api::object_ptr &&user_stories); + telegram_api::object_ptr &&user_stories, + Promise &&promise); bool on_update_read_stories(DialogId owner_dialog_id, StoryId max_read_story_id); @@ -438,7 +439,8 @@ class StoryManager final : public Actor { void on_toggle_story_is_pinned(StoryId story_id, bool is_pinned, Promise &&promise); - void on_update_active_stories(DialogId owner_dialog_id, StoryId max_read_story_id, vector &&story_ids); + void on_update_active_stories(DialogId owner_dialog_id, StoryId max_read_story_id, vector &&story_ids, + Promise &&promise); bool update_active_stories_order(DialogId owner_dialog_id, ActiveStories *active_stories, bool *need_save_to_database); @@ -452,7 +454,8 @@ class StoryManager final : public Actor { void send_update_chat_active_stories(DialogId owner_dialog_id, const ActiveStories *active_stories) const; - void save_active_stories(DialogId owner_dialog_id, const ActiveStories *active_stories) const; + void save_active_stories(DialogId owner_dialog_id, const ActiveStories *active_stories, + Promise &&promise) const; void increment_story_views(DialogId owner_dialog_id, PendingStoryViews &story_views);