diff --git a/CMakeLists.txt b/CMakeLists.txt index f0b6142d9..7ed1095d1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -763,6 +763,7 @@ set(TDLIB_SOURCE td/telegram/StorageManager.h td/telegram/StoryContent.h td/telegram/StoryContentType.h + td/telegram/StoryFullId.h td/telegram/StoryId.h td/telegram/StoryInteractionInfo.h td/telegram/StoryManager.h diff --git a/td/telegram/StoryFullId.h b/td/telegram/StoryFullId.h new file mode 100644 index 000000000..2fbfa2877 --- /dev/null +++ b/td/telegram/StoryFullId.h @@ -0,0 +1,69 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023 +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +#pragma once + +#include "td/telegram/DialogId.h" +#include "td/telegram/StoryId.h" + +#include "td/utils/common.h" +#include "td/utils/StringBuilder.h" + +namespace td { + +struct StoryFullId { + private: + DialogId dialog_id; + StoryId story_id; + + public: + StoryFullId() : dialog_id(), story_id() { + } + + StoryFullId(DialogId dialog_id, StoryId story_id) : dialog_id(dialog_id), story_id(story_id) { + } + + bool operator==(const StoryFullId &other) const { + return dialog_id == other.dialog_id && story_id == other.story_id; + } + + bool operator!=(const StoryFullId &other) const { + return !(*this == other); + } + + DialogId get_dialog_id() const { + return dialog_id; + } + + StoryId get_story_id() const { + return story_id; + } + + template + void store(StorerT &storer) const { + dialog_id.store(storer); + story_id.store(storer); + } + + template + void parse(ParserT &parser) { + dialog_id.parse(parser); + story_id.parse(parser); + } +}; + +struct StoryFullIdHash { + uint32 operator()(StoryFullId story_full_id) const { + return DialogIdHash()(story_full_id.get_dialog_id()) * 2023654985u + + StoryIdHash()(story_full_id.get_story_id()); + } +}; + +inline StringBuilder &operator<<(StringBuilder &string_builder, StoryFullId story_full_id) { + return string_builder << story_full_id.get_story_id() << " in " << story_full_id.get_dialog_id(); +} + +} // namespace td diff --git a/td/telegram/StoryManager.cpp b/td/telegram/StoryManager.cpp index 56748889f..b18473487 100644 --- a/td/telegram/StoryManager.cpp +++ b/td/telegram/StoryManager.cpp @@ -28,12 +28,12 @@ bool StoryManager::is_local_story_id(StoryId story_id) { return story_id.get() < 0; } -const StoryManager::Story *StoryManager::get_story(StoryId story_id) const { - return stories_.get_pointer(story_id); +const StoryManager::Story *StoryManager::get_story(StoryFullId story_full_id) const { + return stories_.get_pointer(story_full_id); } -StoryManager::Story *StoryManager::get_story_editable(StoryId story_id) { - return stories_.get_pointer(story_id); +StoryManager::Story *StoryManager::get_story_editable(StoryFullId story_full_id) { + return stories_.get_pointer(story_full_id); } StoryId StoryManager::on_get_story(DialogId owner_dialog_id, @@ -44,13 +44,14 @@ StoryId StoryManager::on_get_story(DialogId owner_dialog_id, return StoryId(); } - auto story = get_story_editable(story_id); + StoryFullId story_full_id{owner_dialog_id, story_id}; + auto story = get_story_editable(story_full_id); bool is_changed = false; bool need_save_to_database = false; if (story == nullptr) { auto s = make_unique(); story = s.get(); - stories_.set(story_id, std::move(s)); + stories_.set(story_full_id, std::move(s)); } CHECK(story != nullptr); diff --git a/td/telegram/StoryManager.h b/td/telegram/StoryManager.h index cd3c38b70..fbe53b372 100644 --- a/td/telegram/StoryManager.h +++ b/td/telegram/StoryManager.h @@ -8,6 +8,7 @@ #include "td/telegram/DialogId.h" #include "td/telegram/MessageEntity.h" +#include "td/telegram/StoryFullId.h" #include "td/telegram/StoryId.h" #include "td/telegram/StoryInteractionInfo.h" #include "td/telegram/UserId.h" @@ -49,13 +50,13 @@ class StoryManager final : public Actor { void tear_down() final; - const Story *get_story(StoryId story_id) const; + const Story *get_story(StoryFullId story_full_id) const; - Story *get_story_editable(StoryId story_id); + Story *get_story_editable(StoryFullId story_full_id); static bool is_local_story_id(StoryId story_id); - WaitFreeHashMap, StoryIdHash> stories_; + WaitFreeHashMap, StoryFullIdHash> stories_; Td *td_; ActorShared<> parent_;