From 37e5847cc3cda03fc1c78ffb220ffb404471bad1 Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 15 Jun 2023 18:34:05 +0300 Subject: [PATCH] Add td_api::closeStory and register opened owned stories. --- td/generate/scheme/td_api.tl | 5 +++++ td/telegram/StoryManager.cpp | 42 +++++++++++++++++++++++++++++++++++- td/telegram/StoryManager.h | 6 ++++++ td/telegram/Td.cpp | 7 ++++++ td/telegram/Td.h | 2 ++ td/telegram/cli.cpp | 5 +++++ 6 files changed, 66 insertions(+), 1 deletion(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 1335c5d98..0afa460cb 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -7274,6 +7274,11 @@ getArchivedStories from_story_id:int32 limit:int32 = Stories; //@story_id The identifier of the story openStory story_sender_user_id:int53 story_id:int32 = Ok; +//@description Informs TDLib that a story is closed by the user +//@story_sender_user_id The identifier of the sender of the closed story +//@story_id The identifier of the story +closeStory story_sender_user_id:int53 story_id:int32 = Ok; + //@description Returns information about a bot that can be added to attachment menu @bot_user_id Bot's user identifier getAttachmentMenuBot bot_user_id:int53 = AttachmentMenuBot; diff --git a/td/telegram/StoryManager.cpp b/td/telegram/StoryManager.cpp index a7e4e8862..c36145766 100644 --- a/td/telegram/StoryManager.cpp +++ b/td/telegram/StoryManager.cpp @@ -694,7 +694,18 @@ void StoryManager::open_story(DialogId owner_dialog_id, StoryId story_id, Promis StoryFullId story_full_id{owner_dialog_id, story_id}; const Story *story = get_story(story_full_id); - if (story == nullptr || story->content_ == nullptr) { + if (story == nullptr) { + return promise.set_value(Unit()); + } + + if (is_story_owned(owner_dialog_id)) { + auto &open_count = opened_owned_stories_[story_full_id]; + if (++open_count == 1) { + on_owned_story_opened(story_full_id); + } + } + + if (story->content_ == nullptr) { return promise.set_value(Unit()); } @@ -721,6 +732,35 @@ void StoryManager::open_story(DialogId owner_dialog_id, StoryId story_id, Promis promise.set_value(Unit()); } +void StoryManager::close_story(DialogId owner_dialog_id, StoryId story_id, Promise &&promise) { + if (!td_->messages_manager_->have_dialog_info_force(owner_dialog_id)) { + return promise.set_error(Status::Error(400, "Story sender not found")); + } + if (!td_->messages_manager_->have_input_peer(owner_dialog_id, AccessRights::Read)) { + return promise.set_error(Status::Error(400, "Can't access the story sender")); + } + if (!story_id.is_valid()) { + return promise.set_error(Status::Error(400, "Invalid story identifier specified")); + } + + StoryFullId story_full_id{owner_dialog_id, story_id}; + if (is_story_owned(owner_dialog_id)) { + auto &open_count = opened_owned_stories_[story_full_id]; + if (open_count == 0) { + return promise.set_error(Status::Error(400, "The story wasn't opened")); + } + if (--open_count == 0) { + opened_owned_stories_.erase(story_full_id); + } + } + + promise.set_value(Unit()); +} + +void StoryManager::on_owned_story_opened(StoryFullId story_full_id) { + // TODO reget story view counter +} + void StoryManager::increment_story_views(DialogId owner_dialog_id, PendingStoryViews &story_views) { CHECK(!story_views.has_query_); vector viewed_story_ids; diff --git a/td/telegram/StoryManager.h b/td/telegram/StoryManager.h index b81a9f872..b0452bdc8 100644 --- a/td/telegram/StoryManager.h +++ b/td/telegram/StoryManager.h @@ -117,6 +117,8 @@ class StoryManager final : public Actor { void open_story(DialogId owner_dialog_id, StoryId story_id, Promise &&promise); + void close_story(DialogId owner_dialog_id, StoryId story_id, Promise &&promise); + StoryId on_get_story(DialogId owner_dialog_id, telegram_api::object_ptr &&story_item_ptr); std::pair> on_get_stories(DialogId owner_dialog_id, vector &&expected_story_ids, @@ -236,6 +238,8 @@ class StoryManager final : public Actor { void send_update_active_stories(DialogId owner_dialog_id); + void on_owned_story_opened(StoryFullId story_full_id); + void increment_story_views(DialogId owner_dialog_id, PendingStoryViews &story_views); void on_increment_story_views(DialogId owner_dialog_id); @@ -264,6 +268,8 @@ class StoryManager final : public Actor { FlatHashMap pending_story_views_; + FlatHashMap opened_owned_stories_; + uint32 send_story_count_ = 0; FlatHashMap, FileIdHash> being_uploaded_files_; diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index d46d00c47..762d254d8 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -6450,6 +6450,13 @@ void Td::on_request(uint64 id, const td_api::openStory &request) { std::move(promise)); } +void Td::on_request(uint64 id, const td_api::closeStory &request) { + CHECK_IS_USER(); + CREATE_OK_REQUEST_PROMISE(); + story_manager_->close_story(DialogId(UserId(request.story_sender_user_id_)), StoryId(request.story_id_), + std::move(promise)); +} + void Td::on_request(uint64 id, const td_api::getAttachmentMenuBot &request) { CHECK_IS_USER(); CREATE_REQUEST_PROMISE(); diff --git a/td/telegram/Td.h b/td/telegram/Td.h index aca1c97bb..cd887f833 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -1008,6 +1008,8 @@ class Td final : public Actor { void on_request(uint64 id, const td_api::openStory &request); + void on_request(uint64 id, const td_api::closeStory &request); + void on_request(uint64 id, const td_api::getAttachmentMenuBot &request); void on_request(uint64 id, const td_api::toggleBotIsAddedToAttachmentMenu &request); diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index 36525bc48..ee682699f 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -4063,6 +4063,11 @@ class CliClient final : public Actor { StoryId story_id; get_args(args, story_sender_user_id, story_id); send_request(td_api::make_object(story_sender_user_id, story_id)); + } else if (op == "cs") { + UserId story_sender_user_id; + StoryId story_id; + get_args(args, story_sender_user_id, story_id); + send_request(td_api::make_object(story_sender_user_id, story_id)); } else if (op == "gamb") { UserId user_id; get_args(args, user_id);