diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index ba1008a94..fdae15185 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -2271,6 +2271,9 @@ updateNotificationGroup notification_group_id:int32 type:NotificationGroupType c //@description Contains active notifications that was shown on previous application launches. This update is sent only if a message database is used. In that case it comes once before any updateNotification and updateNotificationGroup update @groups Lists of active notification groups updateActiveNotifications groups:vector = Update; +//@description Describes, whether there are some pending notification updates. Can be used to prevent application from killing, while there are some pending notifiations @have_pending_notifications True, if there are some pending notification updates, which will be sent soon +updateHavePendingNotifications have_pending_notifications:Bool = Update; + //@description Some messages were deleted @chat_id Chat identifier @message_ids Identifiers of the deleted messages //@is_permanent True, if the messages are permanently deleted by a user (as opposed to just becoming unaccessible) //@from_cache True, if the messages are deleted only from the cache and can possibly be retrieved again in the future diff --git a/td/generate/scheme/td_api.tlo b/td/generate/scheme/td_api.tlo index 23c0fa866..3f98fc39d 100644 Binary files a/td/generate/scheme/td_api.tlo and b/td/generate/scheme/td_api.tlo differ diff --git a/td/telegram/NotificationManager.cpp b/td/telegram/NotificationManager.cpp index dcb8c2291..dbd8468c5 100644 --- a/td/telegram/NotificationManager.cpp +++ b/td/telegram/NotificationManager.cpp @@ -525,7 +525,9 @@ void NotificationManager::try_reuse_notification_group_id(NotificationGroupId gr flush_pending_notifications_timeout_.cancel_timeout(group_id.get()); flush_pending_updates_timeout_.cancel_timeout(group_id.get()); - pending_updates_.erase(group_id.get()); + if (pending_updates_.erase(group_id.get()) == 1) { + on_pending_notification_update_count_changed(-1); + } } current_notification_group_id_ = NotificationGroupId(current_notification_group_id_.get() - 1); @@ -625,6 +627,9 @@ void NotificationManager::add_notification(NotificationGroupId group_id, Notific group.pending_notifications_flush_time = flush_time; flush_pending_notifications_timeout_.set_timeout_at(group_id.get(), group.pending_notifications_flush_time); } + if (group.pending_notifications.empty()) { + on_pending_notification_update_count_changed(1); + } group.pending_notifications.push_back(std::move(notification)); } @@ -671,7 +676,11 @@ NotificationUpdate as_notification_update(const td_api::Update *update) { void NotificationManager::add_update(int32 group_id, td_api::object_ptr update) { VLOG(notifications) << "Add " << as_notification_update(update.get()); - pending_updates_[group_id].push_back(std::move(update)); + auto &updates = pending_updates_[group_id]; + if (updates.empty()) { + on_pending_notification_update_count_changed(1); + } + updates.push_back(std::move(update)); if (!running_get_difference_ && running_get_chat_difference_.count(group_id) == 0) { flush_pending_updates_timeout_.add_timeout_in(group_id, MIN_UPDATE_DELAY_MS * 1e-3); } else { @@ -1023,13 +1032,14 @@ void NotificationManager::flush_pending_updates(int32 group_id, const char *sour VLOG(notifications) << "Send " << as_notification_update(update.get()); send_closure(G()->td(), &Td::send_update, std::move(update)); } + on_pending_notification_update_count_changed(-1); } void NotificationManager::flush_all_pending_updates(bool include_delayed_chats, const char *source) { VLOG(notifications) << "Flush all pending notification updates " << (include_delayed_chats ? "with delayed chats " : "") << "from " << source; vector ready_group_keys; - for (auto &it : pending_updates_) { + for (const auto &it : pending_updates_) { if (include_delayed_chats || running_get_chat_difference_.count(it.first) == 0) { auto group_it = get_group(NotificationGroupId(it.first)); CHECK(group_it != groups_.end()); @@ -1218,6 +1228,7 @@ void NotificationManager::flush_pending_notifications(NotificationGroupId group_ group.pending_notifications_flush_time = 0; group.pending_notifications.clear(); + on_pending_notification_update_count_changed(-1); // if we can delete a lot of notifications simultaneously if (group.notifications.size() > keep_notification_group_size_ + EXTRA_GROUP_SIZE && group.type != NotificationGroupType::Calls) { @@ -1423,6 +1434,7 @@ void NotificationManager::remove_notification(NotificationGroupId group_id, Noti if (group_it->second.pending_notifications.empty()) { group_it->second.pending_notifications_flush_time = 0; flush_pending_notifications_timeout_.cancel_timeout(group_id.get()); + on_pending_notification_update_count_changed(-1); } return promise.set_value(Unit()); } @@ -1524,6 +1536,7 @@ void NotificationManager::remove_notification_group(NotificationGroupId group_id if (group_it->second.pending_notifications.empty()) { group_it->second.pending_notifications_flush_time = 0; flush_pending_notifications_timeout_.cancel_timeout(group_id.get()); + on_pending_notification_update_count_changed(-1); } if (new_total_count != -1) { new_total_count -= static_cast(group_it->second.pending_notifications.size()); @@ -1893,6 +1906,7 @@ void NotificationManager::before_get_difference() { } running_get_difference_ = true; + on_pending_notification_update_count_changed(1); } void NotificationManager::after_get_difference() { @@ -1902,6 +1916,7 @@ void NotificationManager::after_get_difference() { CHECK(running_get_difference_); running_get_difference_ = false; + on_pending_notification_update_count_changed(-1); flush_pending_notifications_timeout_.set_timeout_in(0, MIN_NOTIFICATION_DELAY_MS * 1e-3); } @@ -1922,6 +1937,7 @@ void NotificationManager::before_get_chat_difference(NotificationGroupId group_i VLOG(notifications) << "Before get chat difference in " << group_id; CHECK(group_id.is_valid()); running_get_chat_difference_.insert(group_id.get()); + on_pending_notification_update_count_changed(1); } void NotificationManager::after_get_chat_difference(NotificationGroupId group_id) { @@ -1934,6 +1950,7 @@ void NotificationManager::after_get_chat_difference(NotificationGroupId group_id auto erased_count = running_get_chat_difference_.erase(group_id.get()); if (erased_count == 1) { flush_pending_notifications_timeout_.set_timeout_in(-group_id.get(), MIN_NOTIFICATION_DELAY_MS * 1e-3); + on_pending_notification_update_count_changed(-1); } } @@ -1964,6 +1981,7 @@ void NotificationManager::get_current_state(vector= 0); + bool have_pending = pending_notification_update_count_ != 0; + if (had_pending != have_pending && !is_destroyed_) { + auto update = td_api::make_object(have_pending); + VLOG(notifications) << "Send " << oneline(to_string(update)); + send_closure(G()->td(), &Td::send_update, std::move(update)); + } +} + } // namespace td diff --git a/td/telegram/NotificationManager.h b/td/telegram/NotificationManager.h index 932013ae3..65371bf05 100644 --- a/td/telegram/NotificationManager.h +++ b/td/telegram/NotificationManager.h @@ -218,6 +218,8 @@ class NotificationManager : public Actor { void after_get_chat_difference_impl(NotificationGroupId group_id); + void on_pending_notification_update_count_changed(int32 diff); + NotificationId current_notification_id_; NotificationGroupId current_notification_group_id_; @@ -229,6 +231,8 @@ class NotificationManager : public Actor { int32 notification_cloud_delay_ms_ = DEFAULT_ONLINE_CLOUD_DELAY_MS; int32 notification_default_delay_ms_ = DEFAULT_DEFAULT_DELAY_MS; + int32 pending_notification_update_count_ = 0; + NotificationGroupKey last_loaded_notification_group_key_; bool is_destroyed_ = false;