Add updateSavedMessagesTopic and more fields about the topic.

This commit is contained in:
levlam 2024-02-06 01:21:55 +03:00
parent b45cd6e298
commit 0baa1424f9
4 changed files with 89 additions and 4 deletions

View File

@ -1902,8 +1902,12 @@ savedMessagesTopicAuthorHidden = SavedMessagesTopic;
savedMessagesTopicSavedFromChat chat_id:int53 = SavedMessagesTopic;
//@description Contains information about a found Saved Messages topic @topic The topic @last_message Last message in the topic; may be null if none or unknown
foundSavedMessagesTopic topic:SavedMessagesTopic last_message:message = FoundSavedMessagesTopic;
//@description Contains information about a found Saved Messages topic
//@topic The topic
//@is_pinned True, if the topic is pinned
//@order A parameter used to determine order of the topic in the topic list. Topics must be sorted by the order in descending order
//@last_message Last message in the topic; may be null if none or unknown
foundSavedMessagesTopic topic:SavedMessagesTopic is_pinned:Bool order:int64 last_message:message = FoundSavedMessagesTopic;
//@description Contains a list of Saved Messages topics
//@total_count Total number of Saved Messages topics found
@ -6506,6 +6510,9 @@ updateChatFolders chat_folders:vector<chatFolderInfo> main_chat_list_position:in
//@online_member_count New number of online members in the chat, or 0 if unknown
updateChatOnlineMemberCount chat_id:int53 online_member_count:int32 = Update;
//@description Basic information about a Saved Messages topic has changed @topic New information about the topic
updateSavedMessagesTopic topic:foundSavedMessagesTopic = Update;
//@description The list of pinned Saved Messages topics has changed. The app can call getPinnedSavedMessagesTopics to get the new list
updatePinnedSavedMessagesTopics = Update;

View File

@ -24,6 +24,8 @@
#include "td/utils/format.h"
#include "td/utils/logging.h"
#include <limits>
namespace td {
class GetPinnedSavedDialogsQuery final : public Td::ResultHandler {
@ -366,11 +368,29 @@ void SavedMessagesManager::on_topic_message_deleted(SavedMessagesTopicId saved_m
}
void SavedMessagesManager::on_topic_changed(SavedMessagesTopicId saved_messages_topic_id, SavedMessagesTopic *topic) {
CHECK(topic != nullptr);
if (!topic->is_changed_) {
return;
}
// TODO send updateSavedMessagesTopic
if (topic->private_order_ != 0) {
bool is_deleted = topic_list_.ordered_topics_.erase({topic->private_order_, saved_messages_topic_id}) > 0;
CHECK(is_deleted);
}
if (topic->pinned_order_ != 0) {
topic->private_order_ = topic->pinned_order_;
} else if (topic->last_message_id_ != MessageId()) {
topic->private_order_ = (static_cast<int64>(topic->last_message_date_) << 31) +
topic->last_message_id_.get_prev_server_message_id().get_server_message_id().get();
}
if (topic->private_order_ != 0) {
bool is_inserted = topic_list_.ordered_topics_.insert({topic->private_order_, saved_messages_topic_id}).second;
CHECK(is_inserted);
}
send_closure(G()->td(), &Td::send_update,
td_api::make_object<td_api::updateSavedMessagesTopic>(
get_found_saved_messages_topic_object(saved_messages_topic_id, topic)));
}
void SavedMessagesManager::get_pinned_saved_messages_topics(
@ -552,13 +572,18 @@ void SavedMessagesManager::on_get_saved_messages_topics(
td_api::object_ptr<td_api::foundSavedMessagesTopic> SavedMessagesManager::get_found_saved_messages_topic_object(
SavedMessagesTopicId saved_messages_topic_id, const SavedMessagesTopic *topic) const {
CHECK(topic != nullptr);
td_api::object_ptr<td_api::message> last_message_object;
if (topic->last_message_id_ != MessageId()) {
last_message_object = td_->messages_manager_->get_message_object(
{td_->dialog_manager_->get_my_dialog_id(), topic->last_message_id_}, "get_found_saved_messages_topic_object");
}
auto public_order = TopicDate(topic->private_order_, saved_messages_topic_id) <= topic_list_.last_topic_date_
? topic->private_order_
: static_cast<int64>(0);
return td_api::make_object<td_api::foundSavedMessagesTopic>(
saved_messages_topic_id.get_saved_messages_topic_object(td_), std::move(last_message_object));
saved_messages_topic_id.get_saved_messages_topic_object(td_), topic->pinned_order_ != 0, public_order,
std::move(last_message_object));
}
int64 SavedMessagesManager::get_next_pinned_saved_messages_topic_order() {
@ -816,4 +841,20 @@ void SavedMessagesManager::on_update_pinned_saved_messages_topics() {
send_closure(G()->td(), &Td::send_update, td_api::make_object<td_api::updatePinnedSavedMessagesTopics>());
}
void SavedMessagesManager::get_current_state(vector<td_api::object_ptr<td_api::Update>> &updates) const {
if (td_->auth_manager_->is_bot()) {
return;
}
for (const auto &it : saved_messages_topics_) {
const auto *topic = it.second.get();
updates.push_back(td_api::make_object<td_api::updateSavedMessagesTopic>(
get_found_saved_messages_topic_object(topic->saved_messages_topic_id_, topic)));
}
}
const SavedMessagesManager::TopicDate SavedMessagesManager::MIN_TOPIC_DATE{std::numeric_limits<int64>::max(),
SavedMessagesTopicId()};
const SavedMessagesManager::TopicDate SavedMessagesManager::MAX_TOPIC_DATE{0, SavedMessagesTopicId()};
} // namespace td

View File

@ -16,6 +16,8 @@
#include "td/utils/common.h"
#include "td/utils/Promise.h"
#include <set>
namespace td {
class Td;
@ -57,6 +59,8 @@ class SavedMessagesManager final : public Actor {
void on_update_pinned_saved_messages_topics();
void get_current_state(vector<td_api::object_ptr<td_api::Update>> &updates) const;
private:
static constexpr int32 MAX_GET_HISTORY = 100; // server side limit
@ -65,9 +69,38 @@ class SavedMessagesManager final : public Actor {
MessageId last_message_id_;
int32 last_message_date_ = 0;
int64 pinned_order_ = 0;
int64 private_order_ = 0;
bool is_changed_ = true;
};
class TopicDate {
int64 order_;
SavedMessagesTopicId topic_id_;
public:
TopicDate(int64 order, SavedMessagesTopicId topic_id) : order_(order), topic_id_(topic_id) {
}
bool operator<(const TopicDate &other) const {
return order_ > other.order_ ||
(order_ == other.order_ && topic_id_.get_unique_id() > other.topic_id_.get_unique_id());
}
bool operator<=(const TopicDate &other) const {
return order_ > other.order_ ||
(order_ == other.order_ && topic_id_.get_unique_id() >= other.topic_id_.get_unique_id());
}
};
static const TopicDate MIN_TOPIC_DATE;
static const TopicDate MAX_TOPIC_DATE;
struct TopicList {
std::set<TopicDate> ordered_topics_;
TopicDate last_topic_date_ = MIN_TOPIC_DATE; // in memory
};
void tear_down() final;
SavedMessagesTopic *get_topic(SavedMessagesTopicId saved_messages_topic_id);
@ -104,6 +137,8 @@ class SavedMessagesManager final : public Actor {
bool are_pinned_saved_messages_topics_inited_ = false;
int64 current_pinned_saved_messages_topic_order_ = static_cast<int64>(2147000000) << 32;
TopicList topic_list_;
};
} // namespace td

View File

@ -4305,6 +4305,8 @@ void Td::on_request(uint64 id, const td_api::getCurrentState &request) {
notification_manager_->get_current_state(updates);
saved_messages_manager_->get_current_state(updates);
story_manager_->get_current_state(updates);
config_manager_.get_actor_unsafe()->get_current_state(updates);