Add class StoryListId.

This commit is contained in:
levlam 2023-07-08 15:42:23 +03:00
parent 186f496a54
commit a1038bb20c
5 changed files with 128 additions and 34 deletions

View File

@ -768,6 +768,7 @@ set(TDLIB_SOURCE
td/telegram/StoryFullId.h
td/telegram/StoryId.h
td/telegram/StoryInteractionInfo.h
td/telegram/StoryListId.h
td/telegram/StoryManager.h
td/telegram/SuggestedAction.h
td/telegram/Support.h

95
td/telegram/StoryListId.h Normal file
View File

@ -0,0 +1,95 @@
//
// 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/td_api.h"
#include "td/utils/common.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/StringBuilder.h"
namespace td {
class StoryListId {
enum class Type : int32 { None = -1, Main, Archive };
Type type_ = Type::None;
friend struct StoryListIdHash;
explicit StoryListId(Type type) : type_(type) {
}
public:
StoryListId() = default;
explicit StoryListId(const td_api::object_ptr<td_api::StoryList> &story_list) {
if (story_list == nullptr) {
return;
}
switch (story_list->get_id()) {
case td_api::storyListMain::ID:
type_ = Type::Main;
break;
case td_api::storyListArchive::ID:
type_ = Type::Archive;
break;
default:
UNREACHABLE();
}
}
static StoryListId main() {
return StoryListId(Type::Main);
}
static StoryListId archive() {
return StoryListId(Type::Archive);
}
td_api::object_ptr<td_api::StoryList> get_story_list_object() const {
switch (type_) {
case Type::None:
return nullptr;
case Type::Main:
return td_api::make_object<td_api::storyListMain>();
case Type::Archive:
return td_api::make_object<td_api::storyListArchive>();
default:
UNREACHABLE();
}
}
bool is_valid() const {
return type_ == Type::Main || type_ == Type::Archive;
}
bool operator==(const StoryListId &other) const {
return type_ == other.type_;
}
bool operator!=(const StoryListId &other) const {
return type_ != other.type_;
}
};
struct StoryListIdHash {
uint32 operator()(StoryListId story_list_id) const {
return Hash<int32>()(static_cast<int32>(story_list_id.type_));
}
};
inline StringBuilder &operator<<(StringBuilder &string_builder, StoryListId story_list_id) {
if (story_list_id == StoryListId::main()) {
return string_builder << "MainStoryList";
}
if (story_list_id == StoryListId::archive()) {
return string_builder << "ArchiveStoryList";
}
return string_builder << "InvalidStoryList";
}
} // namespace td

View File

@ -1002,12 +1002,11 @@ void StoryManager::add_pending_story_dependencies(Dependencies &dependencies, co
add_story_dependencies(dependencies, pending_story->story_.get());
}
void StoryManager::load_active_stories(const td_api::object_ptr<td_api::StoryList> &story_list_ptr,
Promise<Unit> &&promise) {
if (story_list_ptr == nullptr) {
void StoryManager::load_active_stories(StoryListId story_list_id, Promise<Unit> &&promise) {
if (!story_list_id.is_valid()) {
return promise.set_error(Status::Error(400, "Story list must be non-empty"));
}
bool is_hidden = story_list_ptr->get_id() == td_api::storyListArchive::ID;
bool is_hidden = story_list_id == StoryListId::archive();
auto &story_list = story_lists_[is_hidden];
if (story_list.list_last_story_date_ == MAX_DIALOG_DATE) {
return promise.set_error(Status::Error(404, "Not found"));
@ -1852,16 +1851,12 @@ td_api::object_ptr<td_api::chatActiveStories> StoryManager::get_chat_active_stor
DialogId owner_dialog_id) const {
const auto *active_stories = get_active_stories(owner_dialog_id);
td_api::object_ptr<td_api::StoryList> list;
StoryListId story_list_id;
StoryId max_read_story_id;
vector<td_api::object_ptr<td_api::storyInfo>> stories;
int64 order = 0;
if (active_stories != nullptr) {
if (active_stories->story_list_id_ == 1) {
list = td_api::make_object<td_api::storyListArchive>();
} else if (active_stories->story_list_id_ == 0) {
list = td_api::make_object<td_api::storyListMain>();
}
story_list_id = active_stories->story_list_id_;
max_read_story_id = active_stories->max_read_story_id_;
for (auto story_id : active_stories->story_ids_) {
auto story_info = get_story_info_object({owner_dialog_id, story_id});
@ -1869,19 +1864,15 @@ td_api::object_ptr<td_api::chatActiveStories> StoryManager::get_chat_active_stor
stories.push_back(std::move(story_info));
}
}
if (list != nullptr) {
if (story_list_id.is_valid()) {
order = active_stories->public_order_;
}
} else if (is_subscribed_to_dialog_stories(owner_dialog_id)) {
if (are_dialog_stories_hidden(owner_dialog_id)) {
list = td_api::make_object<td_api::storyListArchive>();
} else {
list = td_api::make_object<td_api::storyListMain>();
}
} else {
story_list_id = get_dialog_story_list_id(owner_dialog_id);
}
return td_api::make_object<td_api::chatActiveStories>(
td_->messages_manager_->get_chat_id_object(owner_dialog_id, "updateChatActiveStories"), std::move(list), order,
max_read_story_id.get(), std::move(stories));
td_->messages_manager_->get_chat_id_object(owner_dialog_id, "updateChatActiveStories"),
story_list_id.get_story_list_object(), order, max_read_story_id.get(), std::move(stories));
}
vector<FileId> StoryManager::get_story_file_ids(const Story *story) const {
@ -2416,11 +2407,11 @@ bool StoryManager::update_active_stories_order(DialogId owner_dialog_id, ActiveS
LOG(INFO) << "Update order of active stories of " << owner_dialog_id << " from " << active_stories->private_order_
<< '/' << active_stories->public_order_ << " to " << new_private_order;
int32 story_list_id = -1;
StoryListId story_list_id = get_dialog_story_list_id(owner_dialog_id);
int64 new_public_order = 0;
if (is_subscribed_to_dialog_stories(owner_dialog_id)) {
story_list_id = static_cast<int32>(are_dialog_stories_hidden(owner_dialog_id));
auto &story_list = story_lists_[story_list_id];
bool is_hidden = story_list_id == StoryListId::archive();
auto &story_list = story_lists_[is_hidden];
if (DialogDate(new_private_order, owner_dialog_id) <= story_list.list_last_story_date_) {
new_public_order = new_private_order;
}
@ -2451,11 +2442,12 @@ bool StoryManager::update_active_stories_order(DialogId owner_dialog_id, ActiveS
void StoryManager::delete_active_stories_from_story_list(DialogId owner_dialog_id,
const ActiveStories *active_stories) {
if (active_stories->story_list_id_ < 0) {
if (!active_stories->story_list_id_.is_valid()) {
return;
}
bool is_deleted = story_lists_[active_stories->story_list_id_].ordered_stories_.erase(
{active_stories->private_order_, owner_dialog_id}) > 0;
bool is_hidden = active_stories->story_list_id_ == StoryListId::archive();
bool is_deleted =
story_lists_[is_hidden].ordered_stories_.erase({active_stories->private_order_, owner_dialog_id}) > 0;
CHECK(is_deleted);
}
@ -2514,19 +2506,23 @@ bool StoryManager::is_subscribed_to_dialog_stories(DialogId owner_dialog_id) con
}
}
bool StoryManager::are_dialog_stories_hidden(DialogId owner_dialog_id) const {
StoryListId StoryManager::get_dialog_story_list_id(DialogId owner_dialog_id) const {
if (!is_subscribed_to_dialog_stories(owner_dialog_id)) {
return StoryListId();
}
switch (owner_dialog_id.get_type()) {
case DialogType::User:
if (owner_dialog_id == DialogId(td_->contacts_manager_->get_my_id())) {
return false;
if (owner_dialog_id != DialogId(td_->contacts_manager_->get_my_id()) &&
td_->contacts_manager_->get_user_stories_hidden(owner_dialog_id.get_user_id())) {
return StoryListId::archive();
}
return td_->contacts_manager_->get_user_stories_hidden(owner_dialog_id.get_user_id());
return StoryListId::main();
case DialogType::Chat:
case DialogType::Channel:
case DialogType::SecretChat:
case DialogType::None:
default:
return false;
return StoryListId::archive();
}
}

View File

@ -16,6 +16,7 @@
#include "td/telegram/StoryFullId.h"
#include "td/telegram/StoryId.h"
#include "td/telegram/StoryInteractionInfo.h"
#include "td/telegram/StoryListId.h"
#include "td/telegram/td_api.h"
#include "td/telegram/telegram_api.h"
#include "td/telegram/UserPrivacySettingRule.h"
@ -106,7 +107,7 @@ class StoryManager final : public Actor {
struct ActiveStories {
StoryId max_read_story_id_;
vector<StoryId> story_ids_;
int32 story_list_id_ = -1;
StoryListId story_list_id_;
int64 private_order_ = 0;
int64 public_order_ = 0;
};
@ -154,7 +155,7 @@ class StoryManager final : public Actor {
void delete_story(StoryId story_id, Promise<Unit> &&promise);
void load_active_stories(const td_api::object_ptr<td_api::StoryList> &story_list_ptr, Promise<Unit> &&promise);
void load_active_stories(StoryListId story_list_id, Promise<Unit> &&promise);
void reload_active_stories();
@ -276,7 +277,7 @@ class StoryManager final : public Actor {
bool is_subscribed_to_dialog_stories(DialogId owner_dialog_id) const;
bool are_dialog_stories_hidden(DialogId owner_dialog_id) const;
StoryListId get_dialog_story_list_id(DialogId owner_dialog_id) const;
void add_story_dependencies(Dependencies &dependencies, const Story *story);

View File

@ -122,6 +122,7 @@
#include "td/telegram/StickerType.h"
#include "td/telegram/StorageManager.h"
#include "td/telegram/StoryId.h"
#include "td/telegram/StoryListId.h"
#include "td/telegram/StoryManager.h"
#include "td/telegram/SuggestedAction.h"
#include "td/telegram/Support.h"
@ -5665,7 +5666,7 @@ void Td::on_request(uint64 id, const td_api::deleteStory &request) {
void Td::on_request(uint64 id, const td_api::loadActiveStories &request) {
CHECK_IS_USER();
CREATE_OK_REQUEST_PROMISE();
story_manager_->load_active_stories(request.story_list_, std::move(promise));
story_manager_->load_active_stories(StoryListId(request.story_list_), std::move(promise));
}
void Td::on_request(uint64 id, const td_api::toggleChatStoriesAreHidden &request) {