Move pinned topics to TopicList.

This commit is contained in:
levlam 2024-02-07 12:49:00 +03:00
parent bb5d9d8777
commit aae57bf18a
2 changed files with 22 additions and 18 deletions

View File

@ -421,7 +421,7 @@ void SavedMessagesManager::load_saved_messages_topics(int32 limit, Promise<Unit>
if (topic_list_.last_topic_date_ == MAX_TOPIC_DATE) { if (topic_list_.last_topic_date_ == MAX_TOPIC_DATE) {
return promise.set_error(Status::Error(404, "Not Found")); return promise.set_error(Status::Error(404, "Not Found"));
} }
if (!are_pinned_saved_messages_topics_inited_) { if (!topic_list_.are_pinned_saved_messages_topics_inited_) {
return get_pinned_saved_dialogs(limit, std::move(promise)); return get_pinned_saved_dialogs(limit, std::move(promise));
} }
get_saved_dialogs(limit, std::move(promise)); get_saved_dialogs(limit, std::move(promise));
@ -586,11 +586,11 @@ void SavedMessagesManager::on_get_saved_messages_topics(
} }
if (is_pinned) { if (is_pinned) {
if (!are_pinned_saved_messages_topics_inited_ && total_count < limit) { if (!topic_list_.are_pinned_saved_messages_topics_inited_ && total_count < limit) {
get_saved_dialogs(limit - total_count, std::move(promise)); get_saved_dialogs(limit - total_count, std::move(promise));
promise = Promise<Unit>(); promise = Promise<Unit>();
} }
are_pinned_saved_messages_topics_inited_ = true; topic_list_.are_pinned_saved_messages_topics_inited_ = true;
set_pinned_saved_messages_topics(std::move(added_saved_messages_topic_ids)); set_pinned_saved_messages_topics(std::move(added_saved_messages_topic_ids));
set_last_topic_date({MIN_PINNED_TOPIC_ORDER - 1, SavedMessagesTopicId()}); set_last_topic_date({MIN_PINNED_TOPIC_ORDER - 1, SavedMessagesTopicId()});
} else if (is_last) { } else if (is_last) {
@ -646,17 +646,17 @@ int64 SavedMessagesManager::get_next_pinned_saved_messages_topic_order() {
} }
bool SavedMessagesManager::set_pinned_saved_messages_topics(vector<SavedMessagesTopicId> saved_messages_topic_ids) { bool SavedMessagesManager::set_pinned_saved_messages_topics(vector<SavedMessagesTopicId> saved_messages_topic_ids) {
if (pinned_saved_messages_topic_ids_ == saved_messages_topic_ids) { if (topic_list_.pinned_saved_messages_topic_ids_ == saved_messages_topic_ids) {
return false; return false;
} }
LOG(INFO) << "Update pinned Saved Messages topics from " << pinned_saved_messages_topic_ids_ << " to " LOG(INFO) << "Update pinned Saved Messages topics from " << topic_list_.pinned_saved_messages_topic_ids_ << " to "
<< saved_messages_topic_ids; << saved_messages_topic_ids;
FlatHashSet<SavedMessagesTopicId, SavedMessagesTopicIdHash> old_pinned_saved_messages_topic_ids; FlatHashSet<SavedMessagesTopicId, SavedMessagesTopicIdHash> old_pinned_saved_messages_topic_ids;
for (auto pinned_saved_messages_topic_id : pinned_saved_messages_topic_ids_) { for (auto pinned_saved_messages_topic_id : topic_list_.pinned_saved_messages_topic_ids_) {
old_pinned_saved_messages_topic_ids.insert(pinned_saved_messages_topic_id); old_pinned_saved_messages_topic_ids.insert(pinned_saved_messages_topic_id);
} }
auto pinned_saved_messages_topic_ids = pinned_saved_messages_topic_ids_; auto pinned_saved_messages_topic_ids = topic_list_.pinned_saved_messages_topic_ids_;
std::reverse(pinned_saved_messages_topic_ids.begin(), pinned_saved_messages_topic_ids.end()); std::reverse(pinned_saved_messages_topic_ids.begin(), pinned_saved_messages_topic_ids.end());
std::reverse(saved_messages_topic_ids.begin(), saved_messages_topic_ids.end()); std::reverse(saved_messages_topic_ids.begin(), saved_messages_topic_ids.end());
auto old_it = pinned_saved_messages_topic_ids.begin(); auto old_it = pinned_saved_messages_topic_ids.begin();
@ -689,18 +689,21 @@ bool SavedMessagesManager::set_saved_messages_topic_is_pinned(SavedMessagesTopic
bool SavedMessagesManager::set_saved_messages_topic_is_pinned(SavedMessagesTopic *topic, bool is_pinned) { bool SavedMessagesManager::set_saved_messages_topic_is_pinned(SavedMessagesTopic *topic, bool is_pinned) {
CHECK(!td_->auth_manager_->is_bot()); CHECK(!td_->auth_manager_->is_bot());
CHECK(topic != nullptr); CHECK(topic != nullptr);
if (!are_pinned_saved_messages_topics_inited_) { if (!topic_list_.are_pinned_saved_messages_topics_inited_) {
return false; return false;
} }
auto saved_messages_topic_id = topic->saved_messages_topic_id_; auto saved_messages_topic_id = topic->saved_messages_topic_id_;
if (is_pinned) { if (is_pinned) {
if (!pinned_saved_messages_topic_ids_.empty() && pinned_saved_messages_topic_ids_[0] == saved_messages_topic_id) { if (!topic_list_.pinned_saved_messages_topic_ids_.empty() &&
topic_list_.pinned_saved_messages_topic_ids_[0] == saved_messages_topic_id) {
return false; return false;
} }
topic->pinned_order_ = get_next_pinned_saved_messages_topic_order(); topic->pinned_order_ = get_next_pinned_saved_messages_topic_order();
add_to_top(pinned_saved_messages_topic_ids_, pinned_saved_messages_topic_ids_.size() + 1, saved_messages_topic_id); add_to_top(topic_list_.pinned_saved_messages_topic_ids_, topic_list_.pinned_saved_messages_topic_ids_.size() + 1,
saved_messages_topic_id);
} else { } else {
if (topic->pinned_order_ == 0 || !td::remove(pinned_saved_messages_topic_ids_, saved_messages_topic_id)) { if (topic->pinned_order_ == 0 ||
!td::remove(topic_list_.pinned_saved_messages_topic_ids_, saved_messages_topic_id)) {
return false; return false;
} }
topic->pinned_order_ = 0; topic->pinned_order_ = 0;
@ -863,14 +866,15 @@ int32 SavedMessagesManager::get_pinned_saved_messages_topic_limit() const {
void SavedMessagesManager::toggle_saved_messages_topic_is_pinned(SavedMessagesTopicId saved_messages_topic_id, void SavedMessagesManager::toggle_saved_messages_topic_is_pinned(SavedMessagesTopicId saved_messages_topic_id,
bool is_pinned, Promise<Unit> &&promise) { bool is_pinned, Promise<Unit> &&promise) {
TRY_STATUS_PROMISE(promise, saved_messages_topic_id.is_valid_status(td_)); TRY_STATUS_PROMISE(promise, saved_messages_topic_id.is_valid_status(td_));
if (!are_pinned_saved_messages_topics_inited_) { if (!topic_list_.are_pinned_saved_messages_topics_inited_) {
return promise.set_error(Status::Error(400, "Pinned Saved Messages topics must be loaded first")); return promise.set_error(Status::Error(400, "Pinned Saved Messages topics must be loaded first"));
} }
if (get_topic(saved_messages_topic_id) == nullptr) { if (get_topic(saved_messages_topic_id) == nullptr) {
return promise.set_error(Status::Error(400, "Can't find Saved Messages topic")); return promise.set_error(Status::Error(400, "Can't find Saved Messages topic"));
} }
if (is_pinned && !td::contains(pinned_saved_messages_topic_ids_, saved_messages_topic_id) && if (is_pinned && !td::contains(topic_list_.pinned_saved_messages_topic_ids_, saved_messages_topic_id) &&
static_cast<size_t>(get_pinned_saved_messages_topic_limit()) <= pinned_saved_messages_topic_ids_.size()) { static_cast<size_t>(get_pinned_saved_messages_topic_limit()) <=
topic_list_.pinned_saved_messages_topic_ids_.size()) {
return promise.set_error(Status::Error(400, "The maximum number of pinned chats exceeded")); return promise.set_error(Status::Error(400, "The maximum number of pinned chats exceeded"));
} }
if (!set_saved_messages_topic_is_pinned(saved_messages_topic_id, is_pinned)) { if (!set_saved_messages_topic_is_pinned(saved_messages_topic_id, is_pinned)) {
@ -887,7 +891,7 @@ void SavedMessagesManager::set_pinned_saved_messages_topics(vector<SavedMessages
return promise.set_error(Status::Error(400, "Can't find Saved Messages topic")); return promise.set_error(Status::Error(400, "Can't find Saved Messages topic"));
} }
} }
if (!are_pinned_saved_messages_topics_inited_) { if (!topic_list_.are_pinned_saved_messages_topics_inited_) {
return promise.set_error(Status::Error(400, "Pinned Saved Messages topics must be loaded first")); return promise.set_error(Status::Error(400, "Pinned Saved Messages topics must be loaded first"));
} }
if (static_cast<size_t>(get_pinned_saved_messages_topic_limit()) < saved_messages_topic_ids.size()) { if (static_cast<size_t>(get_pinned_saved_messages_topic_limit()) < saved_messages_topic_ids.size()) {

View File

@ -105,6 +105,9 @@ class SavedMessagesManager final : public Actor {
static const TopicDate MAX_TOPIC_DATE; static const TopicDate MAX_TOPIC_DATE;
struct TopicList { struct TopicList {
vector<SavedMessagesTopicId> pinned_saved_messages_topic_ids_;
bool are_pinned_saved_messages_topics_inited_ = false;
std::set<TopicDate> ordered_topics_; std::set<TopicDate> ordered_topics_;
TopicDate last_topic_date_ = MIN_TOPIC_DATE; // in memory TopicDate last_topic_date_ = MIN_TOPIC_DATE; // in memory
@ -166,9 +169,6 @@ class SavedMessagesManager final : public Actor {
FlatHashMap<SavedMessagesTopicId, unique_ptr<SavedMessagesTopic>, SavedMessagesTopicIdHash> saved_messages_topics_; FlatHashMap<SavedMessagesTopicId, unique_ptr<SavedMessagesTopic>, SavedMessagesTopicIdHash> saved_messages_topics_;
vector<SavedMessagesTopicId> pinned_saved_messages_topic_ids_;
bool are_pinned_saved_messages_topics_inited_ = false;
int64 current_pinned_saved_messages_topic_order_ = MIN_PINNED_TOPIC_ORDER; int64 current_pinned_saved_messages_topic_order_ = MIN_PINNED_TOPIC_ORDER;
TopicList topic_list_; TopicList topic_list_;