From 254007eab84d377540b0cd6f8fabfd946bb8c79b Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 4 Dec 2018 02:25:29 +0300 Subject: [PATCH] Notifications fixes and improvements. GitOrigin-RevId: be1ebd6df5f003207ed450d867772286ebcbea27 --- td/telegram/MessagesManager.cpp | 5 +++- td/telegram/NotificationManager.cpp | 42 ++++++++++++++++++----------- td/telegram/cli.cpp | 12 +++++---- 3 files changed, 37 insertions(+), 22 deletions(-) diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index ea530a62..d1ff3af7 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -9867,7 +9867,7 @@ void MessagesManager::try_restore_dialog_reply_markup(Dialog *d, const Message * bool MessagesManager::set_dialog_last_notification(Dialog *d, int32 last_notification_date, NotificationId last_notification_id, const char *source) { if (last_notification_date != d->last_notification_date || d->last_notification_id != last_notification_id) { - VLOG(notifications) << "Set dialog last notification to " << last_notification_id << " sent at " + VLOG(notifications) << "Set " << d->dialog_id << " last notification to " << last_notification_id << " sent at " << last_notification_date << " from " << source; d->last_notification_date = last_notification_date; d->last_notification_id = last_notification_id; @@ -10405,6 +10405,7 @@ void MessagesManager::fix_dialog_last_notification_id(Dialog *d, MessageId messa } } if (G()->parameters().use_message_db) { + VLOG(notifications) << "Get message notification in " << d->dialog_id << " from " << d->last_notification_id; G()->td_db()->get_messages_db_async()->get_messages_from_notification_id( d->dialog_id, d->last_notification_id, 1, PromiseCreator::lambda( @@ -10422,6 +10423,7 @@ void MessagesManager::do_fix_dialog_last_notification_id(DialogId dialog_id, Not return; } + VLOG(notifications) << "Receive " << result.ok().size() << " messages with notifications in " << dialog_id; Dialog *d = get_dialog(dialog_id); CHECK(d != nullptr); if (d->last_notification_id != prev_last_notification_id) { @@ -10436,6 +10438,7 @@ void MessagesManager::do_fix_dialog_last_notification_id(DialogId dialog_id, Not NotificationId last_notification_id; for (auto &message : messages) { auto m = on_get_message_from_database(dialog_id, d, std::move(message)); + VLOG(notifications) << "Receive " << m->message_id << " with " << m->notification_id << " in " << dialog_id; if (is_message_has_active_notification(d, m)) { last_notification_date = m->date; last_notification_id = m->notification_id; diff --git a/td/telegram/NotificationManager.cpp b/td/telegram/NotificationManager.cpp index 0fe9c394..5b3b9500 100644 --- a/td/telegram/NotificationManager.cpp +++ b/td/telegram/NotificationManager.cpp @@ -670,6 +670,7 @@ void NotificationManager::flush_pending_updates(int32 group_id, const char *sour // all other additions and edits can be merged to the first addition/edit // i.e. in edit+delete+add chain we want to remove deletion and merge addition to the edit + bool is_hidden = get_last_updated_group_key() < group_keys_[NotificationGroupId(group_id)]; bool is_changed = true; while (is_changed) { is_changed = false; @@ -796,7 +797,7 @@ void NotificationManager::flush_pending_updates(int32 group_id, const char *sour break; } } - if (update != nullptr && cur_pos == 1 && (updates.size() > 1 || update_ptr->total_count_ == 0)) { + if (update != nullptr && cur_pos == 1 && (updates.size() > 1 || update_ptr->total_count_ == 0 || is_hidden)) { VLOG(notifications) << "Remove empty update " << cur_pos; CHECK(moved_deleted_notification_ids.empty()); is_changed = true; @@ -876,6 +877,16 @@ void NotificationManager::flush_pending_updates(int32 group_id, const char *sour return; } + auto has_common_notifications = [](const vector> ¬ifications, + const vector ¬ification_ids) { + for (auto ¬ification : notifications) { + if (std::find(notification_ids.begin(), notification_ids.end(), notification->id_) != notification_ids.end()) { + return true; + } + } + return false; + }; + size_t last_update_pos = 0; for (size_t i = 1; i < updates.size(); i++) { if (updates[last_update_pos]->get_id() == td_api::updateNotificationGroup::ID && @@ -883,21 +894,20 @@ void NotificationManager::flush_pending_updates(int32 group_id, const char *sour auto last_update_ptr = static_cast(updates[last_update_pos].get()); auto update_ptr = static_cast(updates[i].get()); if (last_update_ptr->notification_settings_chat_id_ == update_ptr->notification_settings_chat_id_ && - last_update_ptr->is_silent_ == update_ptr->is_silent_) { - if ((last_update_ptr->added_notifications_.empty() && update_ptr->added_notifications_.empty()) || - (last_update_ptr->removed_notification_ids_.empty() && update_ptr->removed_notification_ids_.empty())) { - // combine updates - VLOG(notifications) << "Combine " << as_notification_update(last_update_ptr) << " and " - << as_notification_update(update_ptr); - CHECK(last_update_ptr->notification_group_id_ == update_ptr->notification_group_id_); - CHECK(last_update_ptr->chat_id_ == update_ptr->chat_id_); - last_update_ptr->total_count_ = update_ptr->total_count_; - append(last_update_ptr->added_notifications_, std::move(update_ptr->added_notifications_)); - append(last_update_ptr->removed_notification_ids_, std::move(update_ptr->removed_notification_ids_)); - updates[i] = nullptr; - is_changed = true; - continue; - } + (!last_update_ptr->is_silent_ || update_ptr->is_silent_) && + !has_common_notifications(last_update_ptr->added_notifications_, update_ptr->removed_notification_ids_) && + !has_common_notifications(update_ptr->added_notifications_, last_update_ptr->removed_notification_ids_)) { + // combine updates + VLOG(notifications) << "Combine " << as_notification_update(last_update_ptr) << " and " + << as_notification_update(update_ptr); + CHECK(last_update_ptr->notification_group_id_ == update_ptr->notification_group_id_); + CHECK(last_update_ptr->chat_id_ == update_ptr->chat_id_); + last_update_ptr->total_count_ = update_ptr->total_count_; + append(last_update_ptr->added_notifications_, std::move(update_ptr->added_notifications_)); + append(last_update_ptr->removed_notification_ids_, std::move(update_ptr->removed_notification_ids_)); + updates[i] = nullptr; + is_changed = true; + continue; } } last_update_pos++; diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index dbfc4185..1cb3cd10 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -545,7 +545,7 @@ class CliClient final : public Actor { template static vector to_integers(Slice ids_string, char delimiter = ' ') { - return transform(full_split(ids_string, delimiter), to_integer); + return transform(transform(full_split(ids_string, delimiter), trim), to_integer); } void on_result(uint64 generation, uint64 id, tl_object_ptr result) { @@ -3294,10 +3294,12 @@ class CliClient final : public Actor { send_request(make_tl_object()); } else if (op == "rn") { 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))); + string notification_ids; + std::tie(group_id, notification_ids) = split(args); + char delimiter = notification_ids.find(',') != string::npos ? ',' : ' '; + for (auto notification_id : to_integers(notification_ids, delimiter)) { + send_request(make_tl_object(to_integer(group_id), notification_id)); + } } else if (op == "rng") { string group_id; string max_notification_id;