Delay updateChatReadInbox updates in opened chats.

This commit is contained in:
levlam 2023-05-01 15:33:51 +03:00
parent 03e4013c9d
commit 3c2e08633b
2 changed files with 37 additions and 1 deletions

View File

@ -5992,6 +5992,9 @@ MessagesManager::MessagesManager(Td *td, ActorShared<> parent)
update_viewed_messages_timeout_.set_callback(on_update_viewed_messages_timeout_callback); update_viewed_messages_timeout_.set_callback(on_update_viewed_messages_timeout_callback);
update_viewed_messages_timeout_.set_callback_data(static_cast<void *>(this)); update_viewed_messages_timeout_.set_callback_data(static_cast<void *>(this));
send_update_chat_read_inbox_timeout_.set_callback(on_send_update_chat_read_inbox_timeout_callback);
send_update_chat_read_inbox_timeout_.set_callback_data(static_cast<void *>(this));
} }
MessagesManager::~MessagesManager() { MessagesManager::~MessagesManager() {
@ -6139,6 +6142,16 @@ void MessagesManager::on_update_viewed_messages_timeout_callback(void *messages_
DialogId(dialog_id_int)); DialogId(dialog_id_int));
} }
void MessagesManager::on_send_update_chat_read_inbox_timeout_callback(void *messages_manager_ptr, int64 dialog_id_int) {
if (G()->close_flag()) {
return;
}
auto messages_manager = static_cast<MessagesManager *>(messages_manager_ptr);
send_closure_later(messages_manager->actor_id(messages_manager),
&MessagesManager::on_send_update_chat_read_inbox_timeout, DialogId(dialog_id_int));
}
BufferSlice MessagesManager::get_dialog_database_value(const Dialog *d) { BufferSlice MessagesManager::get_dialog_database_value(const Dialog *d) {
// can't use log_event_store, because it tries to parse stored Dialog // can't use log_event_store, because it tries to parse stored Dialog
LogEventStorerCalcLength storer_calc_length; LogEventStorerCalcLength storer_calc_length;
@ -13214,6 +13227,16 @@ void MessagesManager::on_update_viewed_messages_timeout(DialogId dialog_id) {
update_viewed_messages_timeout_.add_timeout_in(dialog_id.get(), UPDATE_VIEWED_MESSAGES_PERIOD); update_viewed_messages_timeout_.add_timeout_in(dialog_id.get(), UPDATE_VIEWED_MESSAGES_PERIOD);
} }
void MessagesManager::on_send_update_chat_read_inbox_timeout(DialogId dialog_id) {
if (G()->close_flag()) {
return;
}
if (postponed_chat_read_inbox_updates_.erase(dialog_id) > 0) {
send_update_chat_read_inbox(get_dialog(dialog_id), true, "on_send_update_chat_read_inbox_timeout");
}
}
int32 MessagesManager::get_message_date(const tl_object_ptr<telegram_api::Message> &message_ptr) { int32 MessagesManager::get_message_date(const tl_object_ptr<telegram_api::Message> &message_ptr) {
switch (message_ptr->get_id()) { switch (message_ptr->get_id()) {
case telegram_api::messageEmpty::ID: case telegram_api::messageEmpty::ID:
@ -20651,6 +20674,10 @@ void MessagesManager::close_dialog(Dialog *d) {
} }
if (!td_->auth_manager_->is_bot()) { if (!td_->auth_manager_->is_bot()) {
if (postponed_chat_read_inbox_updates_.erase(dialog_id) > 0) {
send_update_chat_read_inbox(d, false, "close_dialog");
}
auto online_count_it = dialog_online_member_counts_.find(dialog_id); auto online_count_it = dialog_online_member_counts_.find(dialog_id);
if (online_count_it != dialog_online_member_counts_.end()) { if (online_count_it != dialog_online_member_counts_.end()) {
auto &info = online_count_it->second; auto &info = online_count_it->second;
@ -30573,10 +30600,14 @@ void MessagesManager::send_update_chat_read_inbox(const Dialog *d, bool force, c
LOG_CHECK(d->is_update_new_chat_sent) << "Wrong " << d->dialog_id << " in send_update_chat_read_inbox from " LOG_CHECK(d->is_update_new_chat_sent) << "Wrong " << d->dialog_id << " in send_update_chat_read_inbox from "
<< source; << source;
if (!force && (running_get_difference_ || running_get_channel_difference(d->dialog_id) || if (!force && (running_get_difference_ || running_get_channel_difference(d->dialog_id) ||
get_channel_difference_to_log_event_id_.count(d->dialog_id) != 0)) { get_channel_difference_to_log_event_id_.count(d->dialog_id) != 0 ||
(d->open_count > 0 && d->server_unread_count + d->local_unread_count > 0))) {
LOG(INFO) << "Postpone updateChatReadInbox in " << d->dialog_id << "(" << get_dialog_title(d->dialog_id) << ") to " LOG(INFO) << "Postpone updateChatReadInbox in " << d->dialog_id << "(" << get_dialog_title(d->dialog_id) << ") to "
<< d->server_unread_count << " + " << d->local_unread_count << " from " << source; << d->server_unread_count << " + " << d->local_unread_count << " from " << source;
postponed_chat_read_inbox_updates_.insert(d->dialog_id); postponed_chat_read_inbox_updates_.insert(d->dialog_id);
if (d->open_count > 0) {
send_update_chat_read_inbox_timeout_.add_timeout_in(d->dialog_id.get(), 0.1);
}
} else { } else {
postponed_chat_read_inbox_updates_.erase(d->dialog_id); postponed_chat_read_inbox_updates_.erase(d->dialog_id);
LOG(INFO) << "Send updateChatReadInbox in " << d->dialog_id << "(" << get_dialog_title(d->dialog_id) << ") to " LOG(INFO) << "Send updateChatReadInbox in " << d->dialog_id << "(" << get_dialog_title(d->dialog_id) << ") to "

View File

@ -2303,6 +2303,8 @@ class MessagesManager final : public Actor {
void on_update_viewed_messages_timeout(DialogId dialog_id); void on_update_viewed_messages_timeout(DialogId dialog_id);
void on_send_update_chat_read_inbox_timeout(DialogId dialog_id);
bool delete_newer_server_messages_at_the_end(Dialog *d, MessageId max_message_id); bool delete_newer_server_messages_at_the_end(Dialog *d, MessageId max_message_id);
template <class T, class It> template <class T, class It>
@ -3175,6 +3177,8 @@ class MessagesManager final : public Actor {
static void on_update_viewed_messages_timeout_callback(void *messages_manager_ptr, int64 dialog_id_int); static void on_update_viewed_messages_timeout_callback(void *messages_manager_ptr, int64 dialog_id_int);
static void on_send_update_chat_read_inbox_timeout_callback(void *messages_manager_ptr, int64 dialog_id_int);
void load_secret_thumbnail(FileId thumbnail_file_id); void load_secret_thumbnail(FileId thumbnail_file_id);
void on_upload_media(FileId file_id, tl_object_ptr<telegram_api::InputFile> input_file, void on_upload_media(FileId file_id, tl_object_ptr<telegram_api::InputFile> input_file,
@ -3583,6 +3587,7 @@ class MessagesManager final : public Actor {
MultiTimeout update_dialog_online_member_count_timeout_{"UpdateDialogOnlineMemberCountTimeout"}; MultiTimeout update_dialog_online_member_count_timeout_{"UpdateDialogOnlineMemberCountTimeout"};
MultiTimeout preload_folder_dialog_list_timeout_{"PreloadFolderDialogListTimeout"}; MultiTimeout preload_folder_dialog_list_timeout_{"PreloadFolderDialogListTimeout"};
MultiTimeout update_viewed_messages_timeout_{"UpdateViewedMessagesTimeout"}; MultiTimeout update_viewed_messages_timeout_{"UpdateViewedMessagesTimeout"};
MultiTimeout send_update_chat_read_inbox_timeout_{"SendUpdateChatReadInboxTimeout"};
Hints dialogs_hints_; // search dialogs by title and usernames Hints dialogs_hints_; // search dialogs by title and usernames