From c5c3b8c95ae2dc8eafe18f903601b14755f4d32a Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 16 Nov 2018 18:00:46 +0300 Subject: [PATCH] Implement updateNotification. GitOrigin-RevId: 804e527cc2819e8fa6420d8a7ceea4ad6688a641 --- td/generate/scheme/td_api.tl | 4 +-- td/generate/scheme/td_api.tlo | Bin 137492 -> 137540 bytes td/telegram/MessagesManager.cpp | 6 ++-- td/telegram/NotificationManager.cpp | 48 +++++++++++++++++++++++----- td/telegram/NotificationManager.h | 10 +++--- td/telegram/Td.cpp | 3 +- td/telegram/cli.cpp | 7 ++-- 7 files changed, 59 insertions(+), 19 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index a89498338..65ad73c49 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -2613,8 +2613,8 @@ getChatMessageByDate chat_id:int53 date:int32 = Message; getChatMessageCount chat_id:int53 filter:SearchMessagesFilter return_local:Bool = Count; -//@description Removes an active notification from notification list @notification_id Identifier of removed notification -removeNotification notification_id:int32 = Ok; +//@description Removes an active notification from notification list @notification_group_id Identifier of notification group to which the notification belongs @notification_id Identifier of removed notification +removeNotification notification_group_id:int32 notification_id:int32 = Ok; //@description Removes group of active notifications @notification_group_id Notification group identifier @max_notification_id Maximum identifier of removed notifications removeNotificationGroup notification_group_id:int32 max_notification_id:int32 = Ok; diff --git a/td/generate/scheme/td_api.tlo b/td/generate/scheme/td_api.tlo index cec55473ec3e2cc5c3705b3d7859e219f3ffedcf..0f5efd9954efd384c9c2baab1977c526d67959cb 100644 GIT binary patch delta 32 ocmbQTiQ~v7j)pCa6}MUZHfI@6uer@AGuauth_manager_->is_bot()) { send_update_message_edited(dialog_id, m); @@ -5587,7 +5588,8 @@ void MessagesManager::on_message_edited(FullMessageId full_message_id) { } if (m->notification_id.is_valid()) { - send_closure_later(G()->notification_manager(), &NotificationManager::edit_notification, m->notification_id, + send_closure_later(G()->notification_manager(), &NotificationManager::edit_notification, + get_dialog_message_notification_group_id(d), m->notification_id, create_new_message_notification(m->message_id)); } } diff --git a/td/telegram/NotificationManager.cpp b/td/telegram/NotificationManager.cpp index fe658cec9..a5cc9aff5 100644 --- a/td/telegram/NotificationManager.cpp +++ b/td/telegram/NotificationManager.cpp @@ -201,6 +201,20 @@ void NotificationManager::send_update_notification_group(td_api::object_ptrtd(), &Td::send_update, std::move(update)); } +void NotificationManager::send_update_notification(NotificationGroupId notification_group_id, DialogId dialog_id, + const Notification ¬ification) { + auto notification_object = get_notification_object(dialog_id, notification); + if (notification_object->type_ == nullptr) { + return; + } + + // TODO delay and combine updates while getDifference is running + auto update = + td_api::make_object(notification_group_id.get(), std::move(notification_object)); + VLOG(notifications) << "Send " << to_string(update); + send_closure(G()->td(), &Td::send_update, std::move(update)); +} + void NotificationManager::flush_pending_notifications(NotificationGroupKey &group_key, NotificationGroup &group, vector &pending_notifications) { if (pending_notifications.empty()) { @@ -363,7 +377,8 @@ void NotificationManager::flush_pending_notifications(NotificationGroupId group_ groups_.emplace(std::move(final_group_key), std::move(group)); } -void NotificationManager::edit_notification(NotificationId notification_id, unique_ptr type) { +void NotificationManager::edit_notification(NotificationGroupId group_id, NotificationId notification_id, + unique_ptr type) { if (is_disabled()) { return; } @@ -371,17 +386,28 @@ void NotificationManager::edit_notification(NotificationId notification_id, uniq CHECK(notification_id.is_valid()); CHECK(type != nullptr); VLOG(notifications) << "Edit " << notification_id << ": " << *type; -} -void NotificationManager::delete_notification(NotificationId notification_id) { - if (is_disabled()) { - return; + auto group_it = get_group(group_id); + auto &group = group_it->second; + for (size_t i = 0; i < group.notifications.size(); i++) { + auto ¬ification = group.notifications[i]; + if (notification.notification_id == notification_id) { + notification.type = std::move(type); + if (i + max_notification_group_size_ >= group.notifications.size()) { + send_update_notification(group_it->first.group_id, group_it->first.dialog_id, notification); + return; + } + } + } + for (auto ¬ification : group.pending_notifications) { + if (notification.notification_id == notification_id) { + notification.type = std::move(type); + } } - - CHECK(notification_id.is_valid()); } -void NotificationManager::remove_notification(NotificationId notification_id, Promise &&promise) { +void NotificationManager::remove_notification(NotificationGroupId group_id, NotificationId notification_id, + Promise &&promise) { if (!notification_id.is_valid()) { return promise.set_error(Status::Error(400, "Notification identifier is invalid")); } @@ -390,6 +416,10 @@ void NotificationManager::remove_notification(NotificationId notification_id, Pr return promise.set_value(Unit()); } + VLOG(notifications) << "Remove " << notification_id; + + // TODO remove notification from database by notification_id + // TODO update total_count promise.set_value(Unit()); } @@ -407,6 +437,8 @@ void NotificationManager::remove_notification_group(NotificationGroupId group_id return promise.set_value(Unit()); } + VLOG(notifications) << "Remove " << group_id << " up to " << max_notification_id; + // TODO update total_count promise.set_value(Unit()); } diff --git a/td/telegram/NotificationManager.h b/td/telegram/NotificationManager.h index 95d6d2091..8f44b0927 100644 --- a/td/telegram/NotificationManager.h +++ b/td/telegram/NotificationManager.h @@ -44,11 +44,10 @@ class NotificationManager : public Actor { DialogId notification_settings_dialog_id, bool is_silent, NotificationId notification_id, unique_ptr type); - void edit_notification(NotificationId notification_id, unique_ptr type); + void edit_notification(NotificationGroupId group_id, NotificationId notification_id, + unique_ptr type); - void delete_notification(NotificationId notification_id); - - void remove_notification(NotificationId notification_id, Promise &&promise); + void remove_notification(NotificationGroupId group_id, NotificationId notification_id, Promise &&promise); void remove_notification_group(NotificationGroupId group_id, NotificationId max_notification_id, Promise &&promise); @@ -134,6 +133,9 @@ class NotificationManager : public Actor { void send_update_notification_group(td_api::object_ptr update); + void send_update_notification(NotificationGroupId notification_group_id, DialogId dialog_id, + const Notification ¬ification); + NotificationGroups::iterator get_group(NotificationGroupId group_id); NotificationGroupKey get_last_updated_group_key() const; diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 15a40629d..90b075083 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -5085,7 +5085,8 @@ void Td::on_request(uint64 id, td_api::getChatMessageCount &request) { void Td::on_request(uint64 id, const td_api::removeNotification &request) { CHECK_IS_USER(); CREATE_OK_REQUEST_PROMISE(); - notification_manager_->remove_notification(NotificationId(request.notification_id_), std::move(promise)); + notification_manager_->remove_notification(NotificationGroupId(request.notification_group_id_), + NotificationId(request.notification_id_), std::move(promise)); } void Td::on_request(uint64 id, const td_api::removeNotificationGroup &request) { diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index 9bb16e291..dbfc41855 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -3293,8 +3293,11 @@ class CliClient final : public Actor { } else if (op == "rans") { send_request(make_tl_object()); } else if (op == "rn") { - string notification_id = args; - send_request(make_tl_object(to_integer(notification_id))); + string group_id; + string notification_id; + std::tie(group_id, notification_id) = split(args); + send_request( + make_tl_object(to_integer(group_id), to_integer(notification_id))); } else if (op == "rng") { string group_id; string max_notification_id;