Maintain last message of Saved Messages topics.

This commit is contained in:
levlam 2024-02-05 00:56:20 +03:00
parent 7267e1e89c
commit 3d8c749536
3 changed files with 98 additions and 0 deletions

View File

@ -70,6 +70,7 @@
#include "td/telegram/RepliedMessageInfo.hpp"
#include "td/telegram/ReplyMarkup.h"
#include "td/telegram/ReplyMarkup.hpp"
#include "td/telegram/SavedMessagesManager.h"
#include "td/telegram/SecretChatsManager.h"
#include "td/telegram/SponsoredMessageManager.h"
#include "td/telegram/StickerType.h"
@ -15178,6 +15179,10 @@ void MessagesManager::on_message_deleted(Dialog *d, Message *m, bool is_permanen
if (m->is_topic_message) {
td_->forum_topic_manager_->on_topic_message_count_changed(d->dialog_id, m->top_thread_message_id, -1);
}
if (is_permanently_deleted && !td_->auth_manager_->is_bot() && m->saved_messages_topic_id.is_valid()) {
CHECK(d->dialog_id == td_->dialog_manager_->get_my_dialog_id());
td_->saved_messages_manager_->on_topic_message_deleted(m->saved_messages_topic_id, m->message_id);
}
added_message_count_--;
}
@ -32723,6 +32728,11 @@ MessagesManager::Message *MessagesManager::add_message_to_dialog(Dialog *d, uniq
update_message_max_reply_media_timestamp(d, result_message, false);
update_message_max_own_media_timestamp(d, result_message);
if (!td_->auth_manager_->is_bot() && from_update && m->saved_messages_topic_id.is_valid()) {
CHECK(dialog_id == td_->dialog_manager_->get_my_dialog_id());
td_->saved_messages_manager_->set_topic_last_message_id(m->saved_messages_topic_id, m->message_id);
}
result_message->debug_source = source;
d->being_added_message_id = MessageId();

View File

@ -326,6 +326,66 @@ void SavedMessagesManager::tear_down() {
parent_.reset();
}
SavedMessagesManager::SavedMessagesTopic *SavedMessagesManager::get_topic(
SavedMessagesTopicId saved_messages_topic_id) {
CHECK(saved_messages_topic_id.is_valid());
auto it = saved_messages_topics_.find(saved_messages_topic_id);
if (it == saved_messages_topics_.end()) {
return nullptr;
}
return it->second.get();
}
SavedMessagesManager::SavedMessagesTopic *SavedMessagesManager::add_topic(
SavedMessagesTopicId saved_messages_topic_id) {
CHECK(saved_messages_topic_id.is_valid());
auto &result = saved_messages_topics_[saved_messages_topic_id];
if (result == nullptr) {
result = make_unique<SavedMessagesTopic>();
}
return result.get();
}
void SavedMessagesManager::set_topic_last_message_id(SavedMessagesTopicId saved_messages_topic_id,
MessageId last_message_id) {
auto *topic = add_topic(saved_messages_topic_id);
do_set_topic_last_message_id(topic, last_message_id);
on_topic_changed(saved_messages_topic_id, topic);
}
void SavedMessagesManager::do_set_topic_last_message_id(SavedMessagesTopic *topic, MessageId last_message_id) {
CHECK(last_message_id.is_valid());
CHECK(last_message_id.is_server());
if (topic->last_message_id_ == last_message_id) {
return;
}
topic->last_message_id_ = last_message_id;
topic->is_changed_ = true;
}
void SavedMessagesManager::on_topic_message_deleted(SavedMessagesTopicId saved_messages_topic_id,
MessageId message_id) {
auto *topic = get_topic(saved_messages_topic_id);
if (topic == nullptr || topic->last_message_id_ != message_id) {
return;
}
topic->last_message_id_ = MessageId();
topic->is_changed_ = true;
on_topic_changed(saved_messages_topic_id, topic);
}
void SavedMessagesManager::on_topic_changed(SavedMessagesTopicId saved_messages_topic_id, SavedMessagesTopic *topic) {
if (!topic->is_changed_) {
return;
}
// TODO send updateSavedMessagesTopic
}
void SavedMessagesManager::get_pinned_saved_messages_topics(
Promise<td_api::object_ptr<td_api::foundSavedMessagesTopics>> &&promise) {
td_->create_handler<GetPinnedSavedDialogsQuery>(std::move(promise))->send();
@ -476,7 +536,16 @@ void SavedMessagesManager::on_get_saved_messages_topics(
LOG(ERROR) << "Can't add last " << last_message_id << " to " << saved_messages_topic_id;
total_count--;
continue;
} else {
CHECK(full_message_id.get_message_id() == last_topic_message_id);
}
auto *topic = add_topic(saved_messages_topic_id);
if (last_topic_message_id.is_valid() && topic->last_message_id_ == MessageId()) {
do_set_topic_last_message_id(topic, last_topic_message_id);
}
on_topic_changed(saved_messages_topic_id, topic);
found_saved_messages_topics.push_back(td_api::make_object<td_api::foundSavedMessagesTopic>(
saved_messages_topic_id.get_saved_messages_topic_object(td_),
td_->messages_manager_->get_message_object(full_message_id, "on_get_saved_messages_topics")));

View File

@ -23,6 +23,10 @@ class SavedMessagesManager final : public Actor {
public:
SavedMessagesManager(Td *td, ActorShared<> parent);
void set_topic_last_message_id(SavedMessagesTopicId saved_messages_topic_id, MessageId last_message_id);
void on_topic_message_deleted(SavedMessagesTopicId saved_messages_topic_id, MessageId message_id);
void get_pinned_saved_messages_topics(Promise<td_api::object_ptr<td_api::foundSavedMessagesTopics>> &&promise);
void get_saved_messages_topics(const string &offset, int32 limit,
@ -53,10 +57,25 @@ class SavedMessagesManager final : public Actor {
private:
static constexpr int32 MAX_GET_HISTORY = 100; // server side limit
struct SavedMessagesTopic {
MessageId last_message_id_;
bool is_changed_ = true;
};
void tear_down() final;
SavedMessagesTopic *get_topic(SavedMessagesTopicId saved_messages_topic_id);
SavedMessagesTopic *add_topic(SavedMessagesTopicId saved_messages_topic_id);
void do_set_topic_last_message_id(SavedMessagesTopic *topic, MessageId last_message_id);
void on_topic_changed(SavedMessagesTopicId saved_messages_topic_id, SavedMessagesTopic *topic);
Td *td_;
ActorShared<> parent_;
FlatHashMap<SavedMessagesTopicId, unique_ptr<SavedMessagesTopic>, SavedMessagesTopicIdHash> saved_messages_topics_;
};
} // namespace td