From cb1d429eb23d28aef68b3312adb7a645c9f6ee23 Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 20 Nov 2018 17:08:44 +0300 Subject: [PATCH] Support removing notifications by max_message_id. GitOrigin-RevId: 7841a68dcc05cb70910da0b28b93996c87782f4b --- td/telegram/NotificationManager.cpp | 21 +++++++++++++++------ td/telegram/NotificationManager.h | 3 ++- td/telegram/NotificationType.cpp | 12 ++++++++++++ td/telegram/NotificationType.h | 2 ++ td/telegram/Td.cpp | 2 +- 5 files changed, 32 insertions(+), 8 deletions(-) diff --git a/td/telegram/NotificationManager.cpp b/td/telegram/NotificationManager.cpp index e823ff815..ecae69920 100644 --- a/td/telegram/NotificationManager.cpp +++ b/td/telegram/NotificationManager.cpp @@ -532,6 +532,9 @@ void NotificationManager::remove_notification(NotificationGroupId group_id, Noti added_notifications.push_back( get_notification_object(group_it->first.dialog_id, group_it->second.notifications[old_group_size - max_notification_group_size_ - 1])); + if (added_notifications.back()->type_ == nullptr) { + added_notifications.pop_back(); + } } else { // TODO preload more notifications in the group } @@ -552,11 +555,12 @@ void NotificationManager::remove_notification(NotificationGroupId group_id, Noti } void NotificationManager::remove_notification_group(NotificationGroupId group_id, NotificationId max_notification_id, - int32 new_total_count, Promise &&promise) { + MessageId max_message_id, int32 new_total_count, + Promise &&promise) { if (!group_id.is_valid()) { return promise.set_error(Status::Error(400, "Group identifier is invalid")); } - if (!max_notification_id.is_valid()) { + if (!max_notification_id.is_valid() && !max_message_id.is_valid()) { return promise.set_error(Status::Error(400, "Notification identifier is invalid")); } @@ -564,9 +568,11 @@ void NotificationManager::remove_notification_group(NotificationGroupId group_id return promise.set_value(Unit()); } - VLOG(notifications) << "Remove " << group_id << " up to " << max_notification_id; + VLOG(notifications) << "Remove " << group_id << " up to " << max_notification_id << " or " << max_message_id; - // TODO remove notifications from database by max_notification_id, save that they are removed + if (max_notification_id.is_valid()) { + // TODO remove notifications from database by max_notification_id, save that they are removed + } auto group_it = get_group(group_id); if (group_it == groups_.end()) { @@ -577,7 +583,8 @@ void NotificationManager::remove_notification_group(NotificationGroupId group_id auto pending_delete_end = group_it->second.pending_notifications.begin(); for (auto it = group_it->second.pending_notifications.begin(); it != group_it->second.pending_notifications.end(); ++it) { - if (it->notification_id.get() <= max_notification_id.get()) { + if (it->notification_id.get() <= max_notification_id.get() || + (max_message_id.is_valid() && it->type->get_message_id().get() <= max_message_id.get())) { pending_delete_end = it + 1; } } @@ -595,7 +602,9 @@ void NotificationManager::remove_notification_group(NotificationGroupId group_id auto old_group_size = group_it->second.notifications.size(); auto notification_delete_end = old_group_size; for (size_t pos = 0; pos < notification_delete_end; pos++) { - if (group_it->second.notifications[pos].notification_id.get() > max_notification_id.get()) { + auto ¬ification = group_it->second.notifications[pos]; + if (notification.notification_id.get() > max_notification_id.get() && + (!max_message_id.is_valid() || notification.type->get_message_id().get() > max_message_id.get())) { notification_delete_end = pos; } } diff --git a/td/telegram/NotificationManager.h b/td/telegram/NotificationManager.h index 08c5acfe3..b096783c3 100644 --- a/td/telegram/NotificationManager.h +++ b/td/telegram/NotificationManager.h @@ -7,6 +7,7 @@ #pragma once #include "td/telegram/DialogId.h" +#include "td/telegram/MessageId.h" #include "td/telegram/NotificationGroupId.h" #include "td/telegram/NotificationId.h" #include "td/telegram/NotificationType.h" @@ -53,7 +54,7 @@ class NotificationManager : public Actor { Promise &&promise); void remove_notification_group(NotificationGroupId group_id, NotificationId max_notification_id, - int32 new_total_count, Promise &&promise); + MessageId max_message_id, int32 new_total_count, Promise &&promise); void on_notification_group_count_max_changed(); diff --git a/td/telegram/NotificationType.cpp b/td/telegram/NotificationType.cpp index 1db55aef7..64f97599b 100644 --- a/td/telegram/NotificationType.cpp +++ b/td/telegram/NotificationType.cpp @@ -16,6 +16,10 @@ class NotificationTypeMessage : public NotificationType { return true; } + MessageId get_message_id() const override { + return message_id_; + } + td_api::object_ptr get_notification_type_object(DialogId dialog_id) const override { auto message_object = G()->td().get_actor_unsafe()->messages_manager_->get_message_object({dialog_id, message_id_}); if (message_object == nullptr) { @@ -44,6 +48,10 @@ class NotificationTypeSecretChat : public NotificationType { return false; } + MessageId get_message_id() const override { + return MessageId(); + } + td_api::object_ptr get_notification_type_object(DialogId dialog_id) const override { return td_api::make_object(); } @@ -66,6 +74,10 @@ class NotificationTypeCall : public NotificationType { return false; } + MessageId get_message_id() const override { + return MessageId(); + } + td_api::object_ptr get_notification_type_object(DialogId dialog_id) const override { return td_api::make_object(call_id_.get()); } diff --git a/td/telegram/NotificationType.h b/td/telegram/NotificationType.h index 781004678..694759908 100644 --- a/td/telegram/NotificationType.h +++ b/td/telegram/NotificationType.h @@ -28,6 +28,8 @@ class NotificationType { virtual bool can_be_delayed() const = 0; + virtual MessageId get_message_id() const = 0; + virtual td_api::object_ptr get_notification_type_object(DialogId dialog_id) const = 0; virtual StringBuilder &to_string_builder(StringBuilder &string_builder) const = 0; diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 43fc0416e..7308dc917 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -5110,7 +5110,7 @@ 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_), -1, + NotificationId(request.max_notification_id_), MessageId(), -1, std::move(promise)); }