diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 138c3d594..2660a0e63 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -8507,7 +8507,7 @@ void MessagesManager::set_dialog_last_read_inbox_message_id(Dialog *d, MessageId } send_closure_later(G()->notification_manager(), &NotificationManager::remove_notification_group, d->message_notification_group.group_id, NotificationId(), d->last_read_inbox_message_id, - total_count, Promise()); + total_count, Slice(source) == Slice("view_messages"), Promise()); } if (d->mention_notification_group.group_id.is_valid() && d->pinned_message_notification_message_id.is_valid() && @@ -10259,7 +10259,7 @@ void MessagesManager::remove_dialog_mention_notifications(Dialog *d) { if (m->notification_id.is_valid() && is_message_notification_active(d, m) && is_from_mention_notification_group(d, m)) { send_closure_later(G()->notification_manager(), &NotificationManager::remove_notification, notification_group_id, - m->notification_id, false, Promise()); + m->notification_id, false, true, Promise()); } } @@ -10271,7 +10271,7 @@ void MessagesManager::remove_dialog_mention_notifications(Dialog *d) { if (m != nullptr && m->notification_id.is_valid() && is_message_notification_active(d, m)) { CHECK(is_from_mention_notification_group(d, m)); send_closure_later(G()->notification_manager(), &NotificationManager::remove_notification, - notification_group_id, m->notification_id, false, Promise()); + notification_group_id, m->notification_id, false, true, Promise()); } } } @@ -10829,7 +10829,7 @@ void MessagesManager::remove_message_notification_id(Dialog *d, Message *m, bool if (is_permanent) { if (had_active_notification) { send_closure_later(G()->notification_manager(), &NotificationManager::remove_notification, group_info.group_id, - notification_id, true, Promise()); + notification_id, true, true, Promise()); } // on_message_changed will be called by the caller @@ -10852,7 +10852,7 @@ void MessagesManager::remove_new_secret_chat_notification(Dialog *d, bool is_per if (is_permanent) { CHECK(d->message_notification_group.group_id.is_valid()); send_closure_later(G()->notification_manager(), &NotificationManager::remove_notification, - d->message_notification_group.group_id, notification_id, true, Promise()); + d->message_notification_group.group_id, notification_id, true, true, Promise()); } } @@ -19054,7 +19054,7 @@ void MessagesManager::remove_all_dialog_notifications(Dialog *d, NotificationGro << group_info.last_notification_id << " from " << source; group_info.max_removed_notification_id = group_info.last_notification_id; send_closure_later(G()->notification_manager(), &NotificationManager::remove_notification_group, - group_info.group_id, group_info.last_notification_id, MessageId(), 0, Promise()); + group_info.group_id, group_info.last_notification_id, MessageId(), 0, true, Promise()); if (d->new_secret_chat_notification_id.is_valid() && &group_info == &d->message_notification_group) { remove_new_secret_chat_notification(d, false); } else { @@ -19088,7 +19088,7 @@ void MessagesManager::remove_message_dialog_notifications(Dialog *d, MessageId m } send_closure_later(G()->notification_manager(), &NotificationManager::remove_notification_group, group_info.group_id, - NotificationId(), max_notification_message_id, 0, Promise()); + NotificationId(), max_notification_message_id, 0, true, Promise()); } void MessagesManager::send_update_message_send_succeeded(Dialog *d, MessageId old_message_id, const Message *m) const { @@ -22169,7 +22169,7 @@ MessagesManager::Message *MessagesManager::add_message_to_dialog(Dialog *d, uniq } send_closure_later(G()->notification_manager(), &NotificationManager::remove_notification, - d->mention_notification_group.group_id, notification_id, false, Promise()); + d->mention_notification_group.group_id, notification_id, false, false, Promise()); on_message_changed(d, message.get(), false, "remove_mention_notification"); } @@ -22581,7 +22581,7 @@ void MessagesManager::delete_message_from_database(Dialog *d, MessageId message_ } if (is_message_notification_active(d, m)) { send_closure_later(G()->notification_manager(), &NotificationManager::remove_notification, group_info.group_id, - m->notification_id, true, Promise()); + m->notification_id, true, false, Promise()); } } } diff --git a/td/telegram/NotificationManager.cpp b/td/telegram/NotificationManager.cpp index 1c2f5e11d..d97d11976 100644 --- a/td/telegram/NotificationManager.cpp +++ b/td/telegram/NotificationManager.cpp @@ -1502,7 +1502,7 @@ void NotificationManager::edit_notification(NotificationGroupId group_id, Notifi void NotificationManager::on_notifications_removed( NotificationGroups::iterator &&group_it, vector> &&added_notifications, - vector &&removed_notification_ids) { + vector &&removed_notification_ids, bool force_update) { VLOG(notifications) << "In on_notifications_removed for " << group_it->first.group_id << " with " << added_notifications.size() << " added notifications and " << removed_notification_ids.size() << " removed notifications, new total_count = " << group_it->second.total_count; @@ -1567,6 +1567,12 @@ void NotificationManager::on_notifications_removed( group_it->second = std::move(group); } + if (force_update) { + auto id = group_key.group_id.get(); + flush_pending_updates_timeout_.cancel_timeout(id); + flush_pending_updates(id, "on_notifications_removed"); + } + if (last_loaded_notification_group_key_ < last_group_key) { load_message_notification_groups_from_database(td::max(static_cast(max_notification_group_count_), 10) / 2, true); @@ -1620,7 +1626,7 @@ void NotificationManager::remove_added_notifications_from_pending_updates( } void NotificationManager::remove_notification(NotificationGroupId group_id, NotificationId notification_id, - bool is_permanent, Promise &&promise) { + bool is_permanent, bool force_update, Promise &&promise) { if (!group_id.is_valid()) { return promise.set_error(Status::Error(400, "Notification group identifier is invalid")); } @@ -1632,7 +1638,8 @@ void NotificationManager::remove_notification(NotificationGroupId group_id, Noti return promise.set_value(Unit()); } - VLOG(notifications) << "Remove " << notification_id << " from " << group_id; + VLOG(notifications) << "Remove " << notification_id << " from " << group_id + << " with force_update = " << force_update; auto group_it = get_group_force(group_id); if (group_it == groups_.end()) { @@ -1698,7 +1705,8 @@ void NotificationManager::remove_notification(NotificationGroupId group_id, Noti } if (is_total_count_changed || !removed_notification_ids.empty()) { - on_notifications_removed(std::move(group_it), std::move(added_notifications), std::move(removed_notification_ids)); + on_notifications_removed(std::move(group_it), std::move(added_notifications), std::move(removed_notification_ids), + force_update); } remove_added_notifications_from_pending_updates( @@ -1710,7 +1718,7 @@ void NotificationManager::remove_notification(NotificationGroupId group_id, Noti } void NotificationManager::remove_notification_group(NotificationGroupId group_id, NotificationId max_notification_id, - MessageId max_message_id, int32 new_total_count, + MessageId max_message_id, int32 new_total_count, bool force_update, Promise &&promise) { if (!group_id.is_valid()) { return promise.set_error(Status::Error(400, "Group identifier is invalid")); @@ -1724,7 +1732,7 @@ void NotificationManager::remove_notification_group(NotificationGroupId group_id } VLOG(notifications) << "Remove " << group_id << " up to " << max_notification_id << " or " << max_message_id - << " with new_total_count = " << new_total_count; + << " with new_total_count = " << new_total_count << " and force_update = " << force_update; auto group_it = get_group_force(group_id); if (group_it == groups_.end()) { @@ -1760,7 +1768,8 @@ void NotificationManager::remove_notification_group(NotificationGroupId group_id if (new_total_count != -1) { new_total_count -= static_cast(group_it->second.pending_notifications.size()); if (new_total_count < 0) { - LOG(ERROR) << "Have wrong new_total_count " << new_total_count; + LOG(ERROR) << "Have wrong new_total_count " << new_total_count << " + " + << group_it->second.pending_notifications.size(); } } @@ -1802,7 +1811,7 @@ void NotificationManager::remove_notification_group(NotificationGroupId group_id if (new_total_count != -1 || !removed_notification_ids.empty()) { on_notifications_removed(std::move(group_it), vector>(), - std::move(removed_notification_ids)); + std::move(removed_notification_ids), force_update); } else { VLOG(notifications) << "Have new_total_count = " << new_total_count << " and " << removed_notification_ids.size() << " removed notifications"; @@ -1859,7 +1868,8 @@ void NotificationManager::set_notification_total_count(NotificationGroupId group VLOG(notifications) << "Set total_count in " << group_id << " to " << new_total_count; group_it->second.total_count = new_total_count; - on_notifications_removed(std::move(group_it), vector>(), vector()); + on_notifications_removed(std::move(group_it), vector>(), vector(), + false); } vector NotificationManager::get_notification_group_message_ids(NotificationGroupId group_id) { @@ -1978,7 +1988,7 @@ void NotificationManager::remove_call_notification(DialogId dialog_id, CallId ca auto &active_notifications = active_call_notifications_[dialog_id]; for (auto it = active_notifications.begin(); it != active_notifications.end(); ++it) { if (it->call_id == call_id) { - remove_notification(group_id, it->notification_id, true, Promise()); + remove_notification(group_id, it->notification_id, true, true, Promise()); active_notifications.erase(it); if (active_notifications.empty()) { VLOG(notifications) << "Reuse call " << group_id; diff --git a/td/telegram/NotificationManager.h b/td/telegram/NotificationManager.h index e0f89e58c..c964b9508 100644 --- a/td/telegram/NotificationManager.h +++ b/td/telegram/NotificationManager.h @@ -69,10 +69,11 @@ class NotificationManager : public Actor { unique_ptr type); void remove_notification(NotificationGroupId group_id, NotificationId notification_id, bool is_permanent, - Promise &&promise); + bool force_update, Promise &&promise); void remove_notification_group(NotificationGroupId group_id, NotificationId max_notification_id, - MessageId max_message_id, int32 new_total_count, Promise &&promise); + MessageId max_message_id, int32 new_total_count, bool force_update, + Promise &&promise); void set_notification_total_count(NotificationGroupId group_id, int32 new_total_count); @@ -241,7 +242,7 @@ class NotificationManager : public Actor { void on_notifications_removed(NotificationGroups::iterator &&group_it, vector> &&added_notifications, - vector &&removed_notification_ids); + vector &&removed_notification_ids, bool force_update); void remove_added_notifications_from_pending_updates( NotificationGroupId group_id, diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 8a3c18f51..126e126a9 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -5252,14 +5252,14 @@ void Td::on_request(uint64 id, const td_api::removeNotification &request) { CHECK_IS_USER(); CREATE_OK_REQUEST_PROMISE(); notification_manager_->remove_notification(NotificationGroupId(request.notification_group_id_), - NotificationId(request.notification_id_), false, std::move(promise)); + NotificationId(request.notification_id_), false, true, std::move(promise)); } void Td::on_request(uint64 id, const td_api::removeNotificationGroup &request) { CHECK_IS_USER(); CREATE_OK_REQUEST_PROMISE(); notification_manager_->remove_notification_group(NotificationGroupId(request.notification_group_id_), - NotificationId(request.max_notification_id_), MessageId(), -1, + NotificationId(request.max_notification_id_), MessageId(), -1, true, std::move(promise)); }