From 29a1b7e3d6119ab7e59b26e296e78d2084b03c1f Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 13 Jun 2023 18:20:26 +0300 Subject: [PATCH] Support media timestamps in replies to stories. --- td/telegram/MessageContent.cpp | 17 +++++++++++++++++ td/telegram/StoryManager.cpp | 35 +++++++++++++++++++++++++++++++++- td/telegram/StoryManager.h | 7 +++++++ 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/td/telegram/MessageContent.cpp b/td/telegram/MessageContent.cpp index 668098ef4..f4eb68c72 100644 --- a/td/telegram/MessageContent.cpp +++ b/td/telegram/MessageContent.cpp @@ -3505,6 +3505,7 @@ bool can_message_content_have_media_timestamp(const MessageContent *content) { CHECK(content != nullptr); switch (content->get_type()) { case MessageContentType::Audio: + case MessageContentType::Story: case MessageContentType::Video: case MessageContentType::VideoNote: case MessageContentType::VoiceNote: @@ -4334,6 +4335,9 @@ void register_message_content(Td *td, const MessageContent *content, FullMessage case MessageContentType::SuggestProfilePhoto: return td->contacts_manager_->register_suggested_profile_photo( static_cast(content)->photo); + case MessageContentType::Story: + return td->story_manager_->register_story(static_cast(content)->story_full_id, + full_message_id, source); default: return; } @@ -4387,6 +4391,12 @@ void reregister_message_content(Td *td, const MessageContent *old_content, const return; } break; + case MessageContentType::Story: + if (static_cast(old_content)->story_full_id == + static_cast(new_content)->story_full_id) { + return; + } + break; default: return; } @@ -4424,6 +4434,9 @@ void unregister_message_content(Td *td, const MessageContent *content, FullMessa case MessageContentType::GiftPremium: return td->stickers_manager_->unregister_premium_gift(static_cast(content)->months, full_message_id, source); + case MessageContentType::Story: + return td->story_manager_->unregister_story(static_cast(content)->story_full_id, + full_message_id, source); default: return; } @@ -6112,6 +6125,10 @@ int32 get_message_content_media_duration(const MessageContent *content, const Td } case MessageContentType::Invoice: return static_cast(content)->input_invoice.get_duration(td); + case MessageContentType::Story: { + auto story_full_id = static_cast(content)->story_full_id; + return td->story_manager_->get_story_duration(story_full_id); + } case MessageContentType::Text: { auto web_page_id = static_cast(content)->web_page_id; return td->web_pages_manager_->get_web_page_media_duration(web_page_id); diff --git a/td/telegram/StoryManager.cpp b/td/telegram/StoryManager.cpp index 3b4a3b14f..e33791004 100644 --- a/td/telegram/StoryManager.cpp +++ b/td/telegram/StoryManager.cpp @@ -465,7 +465,8 @@ StoryManager::StoryManager(Td *td, ActorShared<> parent) : td_(td), parent_(std: } StoryManager::~StoryManager() { - Scheduler::instance()->destroy_on_scheduler(G()->get_gc_scheduler_id(), story_full_id_to_file_source_id_, stories_); + Scheduler::instance()->destroy_on_scheduler(G()->get_gc_scheduler_id(), story_full_id_to_file_source_id_, stories_, + inaccessible_story_full_ids_, deleted_story_full_ids_, story_messages_); } void StoryManager::tear_down() { @@ -637,6 +638,28 @@ int32 StoryManager::get_story_duration(StoryFullId story_full_id) const { return get_story_content_duration(td_, content); } +void StoryManager::register_story(StoryFullId story_full_id, FullMessageId full_message_id, const char *source) { + if (td_->auth_manager_->is_bot()) { + return; + } + + LOG(INFO) << "Register " << story_full_id << " from " << full_message_id << " from " << source; + story_messages_[story_full_id].insert(full_message_id); +} + +void StoryManager::unregister_story(StoryFullId story_full_id, FullMessageId full_message_id, const char *source) { + if (td_->auth_manager_->is_bot()) { + return; + } + LOG(INFO) << "Unregister " << story_full_id << " from " << full_message_id << " from " << source; + auto &message_ids = story_messages_[story_full_id]; + auto is_deleted = message_ids.erase(full_message_id) > 0; + LOG_CHECK(is_deleted) << source << ' ' << story_full_id << ' ' << full_message_id; + if (message_ids.empty()) { + story_messages_.erase(story_full_id); + } +} + td_api::object_ptr StoryManager::get_story_object(StoryFullId story_full_id) const { return get_story_object(story_full_id, get_story(story_full_id)); } @@ -881,6 +904,16 @@ void StoryManager::on_story_changed(StoryFullId story_full_id, const Story *stor send_closure_later(G()->messages_manager(), &MessagesManager::update_story_max_reply_media_timestamp_in_replied_messages, story_full_id); send_closure_later(G()->web_pages_manager(), &WebPagesManager::on_story_changed, story_full_id); + + if (story_messages_.count(story_full_id) != 0) { + vector full_message_ids; + story_messages_[story_full_id].foreach( + [&full_message_ids](const FullMessageId &full_message_id) { full_message_ids.push_back(full_message_id); }); + CHECK(!full_message_ids.empty()); + for (const auto &full_message_id : full_message_ids) { + td_->messages_manager_->on_external_update_message_content(full_message_id); + } + } } } diff --git a/td/telegram/StoryManager.h b/td/telegram/StoryManager.h index 509700981..1b9da4b11 100644 --- a/td/telegram/StoryManager.h +++ b/td/telegram/StoryManager.h @@ -9,6 +9,7 @@ #include "td/telegram/DialogId.h" #include "td/telegram/files/FileId.h" #include "td/telegram/files/FileSourceId.h" +#include "td/telegram/FullMessageId.h" #include "td/telegram/MessageEntity.h" #include "td/telegram/StoryFullId.h" #include "td/telegram/StoryId.h" @@ -116,6 +117,10 @@ class StoryManager final : public Actor { int32 get_story_duration(StoryFullId story_full_id) const; + void register_story(StoryFullId story_full_id, FullMessageId full_message_id, const char *source); + + void unregister_story(StoryFullId story_full_id, FullMessageId full_message_id, const char *source); + td_api::object_ptr get_story_object(StoryFullId story_full_id) const; td_api::object_ptr get_stories_object(int32 total_count, @@ -203,6 +208,8 @@ class StoryManager final : public Actor { WaitFreeHashSet deleted_story_full_ids_; + WaitFreeHashMap, StoryFullIdHash> story_messages_; + FlatHashMap, StoryFullIdHash> being_edited_stories_; uint32 send_story_count_ = 0;