From ab04885dc4ca309b37edac03ef0d0f395a33778b Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 30 Jun 2023 14:10:42 +0300 Subject: [PATCH] Update active stories order. --- td/generate/scheme/td_api.tl | 4 +-- td/telegram/StoryManager.cpp | 61 ++++++++++++++++++++++++++++++++++-- td/telegram/StoryManager.h | 4 +++ 3 files changed, 64 insertions(+), 5 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index deb635345..493b15036 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -4959,7 +4959,7 @@ storyInfo story_id:int32 date:int32 = StoryInfo; //@order A parameter used to determine order of the stories in the story list. Stories must be sorted by the pair (order, story_sender_chat_id) in descending order //@story_sender_chat_id Identifier of the sender of the stories //@max_read_story_id Identifier of the last read active story -//@stories Basic information about the stories; use getStory to get full information about the stories +//@stories Basic information about the stories; use getStory to get full information about the stories. The stories are in a chronological order (i.e., in order of increasing story identifiers) activeStories list:StoryList order:int53 story_sender_chat_id:int53 max_read_story_id:int32 stories:vector = ActiveStories; @@ -7288,7 +7288,7 @@ loadActiveStories story_list:StoryList = Ok; //@description Toggles whether stories posted by the chat are available above chat lists @chat_id Identifier of the chat that posted stories @are_hidden Pass true to make the stories unavailable above the chat lists; pass false to make them available toggleChatStoriesAreHidden chat_id:int53 are_hidden:Bool = Ok; -//@description Returns the list of active stories posted by the given chat. The stories are returned in a chronological order (i.e., in order of increasing story_id) @chat_id Chat identifier +//@description Returns the list of active stories posted by the given chat @chat_id Chat identifier getChatActiveStories chat_id:int53 = ActiveStories; //@description Returns the list of pinned stories posted by the given chat. The stories are returned in a reverse chronological order (i.e., in order of decreasing story_id). diff --git a/td/telegram/StoryManager.cpp b/td/telegram/StoryManager.cpp index dde944d5f..22c39453b 100644 --- a/td/telegram/StoryManager.cpp +++ b/td/telegram/StoryManager.cpp @@ -847,6 +847,10 @@ const StoryManager::ActiveStories *StoryManager::get_active_stories(DialogId own return active_stories_.get_pointer(owner_dialog_id); } +StoryManager::ActiveStories *StoryManager::get_active_stories_editable(DialogId owner_dialog_id) { + return active_stories_.get_pointer(owner_dialog_id); +} + void StoryManager::load_active_stories(const td_api::object_ptr &story_list_ptr, Promise &&promise) { if (story_list_ptr == nullptr) { @@ -2096,10 +2100,55 @@ void StoryManager::on_update_active_stories(DialogId owner_dialog_id, StoryId ma if (active_stories->max_read_story_id_ != max_read_story_id || active_stories->story_ids_ != story_ids) { active_stories->max_read_story_id_ = max_read_story_id; active_stories->story_ids_ = std::move(story_ids); + update_active_stories_order(owner_dialog_id, active_stories.get()); + send_update_active_stories(owner_dialog_id); + } else if (update_active_stories_order(owner_dialog_id, active_stories.get())) { send_update_active_stories(owner_dialog_id); } } +bool StoryManager::update_active_stories_order(DialogId owner_dialog_id, ActiveStories *active_stories) { + CHECK(active_stories != nullptr); + CHECK(!active_stories->story_ids_.empty()); + CHECK(owner_dialog_id.is_valid()); + + auto last_story_id = active_stories->story_ids_.back(); + const Story *last_story = get_story({owner_dialog_id, last_story_id}); + CHECK(last_story != nullptr); + + int64 new_private_order = 0; + new_private_order += last_story->date_; + if (owner_dialog_id.get_type() == DialogType::User && + td_->contacts_manager_->is_user_premium(owner_dialog_id.get_user_id())) { + new_private_order += static_cast(1) << 33; + } + if (owner_dialog_id == get_changelog_story_dialog_id()) { + new_private_order += static_cast(1) << 34; + } + if (active_stories->max_read_story_id_.get() < last_story_id.get()) { + new_private_order += static_cast(1) << 35; + } + if (owner_dialog_id == DialogId(td_->contacts_manager_->get_my_id())) { + new_private_order += static_cast(1) << 36; + } + + if (active_stories->private_order_ != new_private_order) { + active_stories->private_order_ = new_private_order; + } + + int64 new_public_order = 0; + if (is_subscribed_to_dialog_stories(owner_dialog_id)) { + // TODO check last_story_date_ + new_public_order = active_stories->private_order_; + } + + if (active_stories->public_order_ != new_public_order) { + active_stories->public_order_ = new_public_order; + return true; + } + return false; +} + void StoryManager::send_update_active_stories(DialogId owner_dialog_id) { send_closure(G()->td(), &Td::send_update, td_api::make_object(get_active_stories_object(owner_dialog_id))); @@ -2149,14 +2198,20 @@ bool StoryManager::is_subscribed_to_dialog_stories(DialogId owner_dialog_id) con } void StoryManager::on_dialog_user_is_contact_updated(DialogId owner_dialog_id) { - if (active_stories_.count(owner_dialog_id)) { + auto active_stories = get_active_stories_editable(owner_dialog_id); + if (active_stories != nullptr) { + update_active_stories_order(owner_dialog_id, active_stories); send_update_active_stories(owner_dialog_id); } } void StoryManager::on_dialog_stories_hidden_updated(DialogId owner_dialog_id) { - if (active_stories_.count(owner_dialog_id) && is_subscribed_to_dialog_stories(owner_dialog_id)) { - send_update_active_stories(owner_dialog_id); + auto active_stories = get_active_stories_editable(owner_dialog_id); + if (active_stories != nullptr) { + update_active_stories_order(owner_dialog_id, active_stories); + if (is_subscribed_to_dialog_stories(owner_dialog_id)) { + send_update_active_stories(owner_dialog_id); + } } } diff --git a/td/telegram/StoryManager.h b/td/telegram/StoryManager.h index aa7c6cf36..2aa57cda6 100644 --- a/td/telegram/StoryManager.h +++ b/td/telegram/StoryManager.h @@ -249,6 +249,8 @@ class StoryManager final : public Actor { const ActiveStories *get_active_stories(DialogId owner_dialog_id) const; + ActiveStories *get_active_stories_editable(DialogId owner_dialog_id); + void on_story_changed(StoryFullId story_full_id, const Story *story, bool is_changed, bool need_save_to_database); void register_story_global_id(StoryFullId story_full_id, Story *story); @@ -323,6 +325,8 @@ class StoryManager final : public Actor { void on_update_active_stories(DialogId owner_dialog_id, StoryId max_read_story_id, vector &&story_ids); + bool update_active_stories_order(DialogId owner_dialog_id, ActiveStories *active_stories); + void send_update_active_stories(DialogId owner_dialog_id); void increment_story_views(DialogId owner_dialog_id, PendingStoryViews &story_views);