From 113e264a5feb24c1b6692db08677d6217a1e3efe Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 29 Nov 2018 00:51:25 +0300 Subject: [PATCH] Add MessagesManager::get_message_notification_group_keys_from_database. GitOrigin-RevId: 8ad93b1dc1782b5f0bf94d70ca8deba55a6ea58f --- CMakeLists.txt | 1 + td/telegram/DialogDb.cpp | 12 ++------ td/telegram/MessagesManager.cpp | 34 +++++++++++++++++++++ td/telegram/MessagesManager.h | 4 +++ td/telegram/NotificationGroupKey.h | 43 +++++++++++++++++++++++++++ td/telegram/NotificationManager.cpp | 46 ++++++++++++++++++++++------- td/telegram/NotificationManager.h | 29 +++++------------- 7 files changed, 127 insertions(+), 42 deletions(-) create mode 100644 td/telegram/NotificationGroupKey.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 5bd972ad0..eb45ac646 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -537,6 +537,7 @@ set(TDLIB_SOURCE td/telegram/net/TempAuthKeyWatchdog.h td/telegram/Notification.h td/telegram/NotificationGroupId.h + td/telegram/NotificationGroupKey.h td/telegram/NotificationId.h td/telegram/NotificationManager.h td/telegram/NotificationSettings.h diff --git a/td/telegram/DialogDb.cpp b/td/telegram/DialogDb.cpp index 55ddbcaa4..967ce7bc7 100644 --- a/td/telegram/DialogDb.cpp +++ b/td/telegram/DialogDb.cpp @@ -97,9 +97,8 @@ class DialogDbImpl : public DialogDbSyncInterface { "BY dialog_order DESC, dialog_id DESC LIMIT ?3")); TRY_RESULT( get_dialogs_by_last_notification_date_stmt, - db_.get_statement("SELECT data, dialog_id, last_notification_date FROM dialogs WHERE " - "last_notification_date < ?1 OR (last_notification_date = ?1 AND dialog_id < ?2) ORDER " - "BY last_notification_date DESC, dialog_id DESC LIMIT ?3")); + db_.get_statement("SELECT data FROM dialogs WHERE last_notification_date < ?1 OR (last_notification_date = ?1 " + "AND dialog_id < ?2) ORDER BY last_notification_date DESC, dialog_id DESC LIMIT ?3")); TRY_RESULT(get_dialog_by_notification_group_id_stmt, db_.get_statement("SELECT data FROM dialogs WHERE notification_group_id = ?1")); @@ -207,12 +206,7 @@ class DialogDbImpl : public DialogDbSyncInterface { std::vector dialogs; TRY_STATUS(get_dialogs_by_last_notification_date_stmt_.step()); while (get_dialogs_by_last_notification_date_stmt_.has_row()) { - BufferSlice data(get_dialogs_by_last_notification_date_stmt_.view_blob(0)); - auto loaded_dialog_id = get_dialogs_by_last_notification_date_stmt_.view_int64(1); - auto loaded_dialog_last_notification_date = get_dialogs_by_last_notification_date_stmt_.view_int32(2); - LOG(INFO) << "Load chat " << loaded_dialog_id << " with last notification date " - << loaded_dialog_last_notification_date; - dialogs.emplace_back(std::move(data)); + dialogs.emplace_back(get_dialogs_by_last_notification_date_stmt_.view_blob(0)); TRY_STATUS(get_dialogs_by_last_notification_date_stmt_.step()); } diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index f0f02e1fb..594dcd7fb 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -17467,6 +17467,10 @@ vector MessagesManager::get_message_notifications_from_database(Di NotificationId from_notification_id, int32 limit) { CHECK(d != nullptr); + if (!G()->parameters().use_message_db) { + return {}; + } + auto result = G()->td_db()->get_messages_db_sync()->get_messages_from_notification_id(d->dialog_id, from_notification_id, limit); if (result.is_error()) { @@ -17485,6 +17489,36 @@ vector MessagesManager::get_message_notifications_from_database(Di return res; } +vector MessagesManager::get_message_notification_group_keys_from_database( + int32 from_last_notification_date, DialogId from_dialog_id, int32 limit) { + if (!G()->parameters().use_message_db) { + return {}; + } + + Result> r_dialogs = + G()->td_db()->get_dialog_db_sync()->get_dialogs_by_last_notification_date(from_last_notification_date, + from_dialog_id, limit); + r_dialogs.ensure(); + auto dialogs = r_dialogs.move_as_ok(); + + vector group_keys; + group_keys.reserve(dialogs.size()); + for (auto &dialog : dialogs) { + Dialog *d = on_load_dialog_from_database(DialogId(), std::move(dialog)); + if (d == nullptr) { + continue; + } + + CHECK(d->dialog_id.is_valid()); + CHECK(d->last_notification_date != 0); + CHECK(d->message_notification_group_id.is_valid()); + group_keys.emplace_back(d->message_notification_group_id, d->dialog_id, d->last_notification_date); + + VLOG(notifications) << "Load " << group_keys.back() << " from database"; + } + return group_keys; +} + int32 MessagesManager::get_dialog_pending_notification_count(Dialog *d) { CHECK(d != nullptr); if (is_dialog_muted(d)) { diff --git a/td/telegram/MessagesManager.h b/td/telegram/MessagesManager.h index 0093602c2..9c981644c 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -25,6 +25,7 @@ #include "td/telegram/net/NetQuery.h" #include "td/telegram/Notification.h" #include "td/telegram/NotificationGroupId.h" +#include "td/telegram/NotificationGroupKey.h" #include "td/telegram/NotificationId.h" #include "td/telegram/NotificationSettings.h" #include "td/telegram/ReplyMarkup.h" @@ -664,6 +665,9 @@ class MessagesManager : public Actor { }; MessageNotificationGroup get_message_notification_group_force(NotificationGroupId group_id); + vector get_message_notification_group_keys_from_database(int32 from_last_notification_date, + DialogId from_dialog_id, int32 limit); + void on_binlog_events(vector &&events); void get_payment_form(FullMessageId full_message_id, Promise> &&promise); diff --git a/td/telegram/NotificationGroupKey.h b/td/telegram/NotificationGroupKey.h new file mode 100644 index 000000000..67f334cc0 --- /dev/null +++ b/td/telegram/NotificationGroupKey.h @@ -0,0 +1,43 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2018 +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +#pragma once + +#include "td/telegram/DialogId.h" +#include "td/telegram/NotificationGroupId.h" + +#include "td/utils/common.h" +#include "td/utils/StringBuilder.h" + +namespace td { + +struct NotificationGroupKey { + NotificationGroupId group_id; + DialogId dialog_id; + int32 last_notification_date = 0; + + NotificationGroupKey() = default; + NotificationGroupKey(NotificationGroupId group_id, DialogId dialog_id, int32 last_notification_date) + : group_id(group_id), dialog_id(dialog_id), last_notification_date(last_notification_date) { + } + + bool operator<(const NotificationGroupKey &other) const { + if (last_notification_date != other.last_notification_date) { + return last_notification_date > other.last_notification_date; + } + if (dialog_id != other.dialog_id) { + return dialog_id.get() > other.dialog_id.get(); + } + return group_id.get() > other.group_id.get(); + } +}; + +inline StringBuilder &operator<<(StringBuilder &string_builder, const NotificationGroupKey &group_key) { + return string_builder << '[' << group_key.group_id << ',' << group_key.dialog_id << ',' + << group_key.last_notification_date << ']'; +} + +} // namespace td diff --git a/td/telegram/NotificationManager.cpp b/td/telegram/NotificationManager.cpp index b048aefe0..3f90de5ef 100644 --- a/td/telegram/NotificationManager.cpp +++ b/td/telegram/NotificationManager.cpp @@ -17,6 +17,7 @@ #include "td/utils/misc.h" #include +#include #include #include #include @@ -86,6 +87,10 @@ void NotificationManager::start_up() { on_notification_cloud_delay_changed(); on_notification_default_delay_changed(); + last_loaded_notification_date_ = std::numeric_limits::max(); + + load_message_notification_groups_from_database(false); + // TODO send updateActiveNotifications } @@ -103,7 +108,8 @@ NotificationManager::NotificationGroups::iterator NotificationManager::get_group return groups_.end(); } -NotificationManager::NotificationGroups::iterator NotificationManager::get_group_force(NotificationGroupId group_id) { +NotificationManager::NotificationGroups::iterator NotificationManager::get_group_force(NotificationGroupId group_id, + bool send_update) { auto group_it = get_group(group_id); if (group_it != groups_.end()) { return group_it; @@ -114,23 +120,44 @@ NotificationManager::NotificationGroups::iterator NotificationManager::get_group return groups_.end(); } - NotificationGroupKey group_key; - group_key.group_id = group_id; - group_key.dialog_id = message_group.dialog_id; - group_key.last_notification_date = 0; + NotificationGroupKey group_key(group_id, message_group.dialog_id, 0); for (auto ¬ification : message_group.notifications) { - if (notification.date >= group_key.last_notification_date) { + if (notification.date > group_key.last_notification_date) { group_key.last_notification_date = notification.date; } } + std::reverse(message_group.notifications.begin(), message_group.notifications.end()); + NotificationGroup group; group.total_count = message_group.total_count; group.notifications = std::move(message_group.notifications); + // TODO send update about the new group, if needed + return groups_.emplace(std::move(group_key), std::move(group)).first; } +void NotificationManager::load_message_notification_groups_from_database(bool send_update) { + if (last_loaded_notification_date_ == 0) { + // everything was already loaded + return; + } + vector group_keys = td_->messages_manager_->get_message_notification_group_keys_from_database( + last_loaded_notification_date_, last_loaded_notification_dialog_id_, max_notification_group_count_); + if (group_keys.empty()) { + last_loaded_notification_date_ = 0; + last_loaded_notification_dialog_id_ = DialogId(); + return; + } + + last_loaded_notification_date_ = group_keys.back().last_notification_date; + last_loaded_notification_dialog_id_ = group_keys.back().dialog_id; + for (auto &group_key : group_keys) { + get_group_force(group_key.group_id, send_update); + } +} + int32 NotificationManager::get_max_notification_group_size() const { return max_notification_group_size_; } @@ -159,7 +186,7 @@ NotificationGroupId NotificationManager::get_next_notification_group_id() { return current_notification_group_id_; } -NotificationManager::NotificationGroupKey NotificationManager::get_last_updated_group_key() const { +NotificationGroupKey NotificationManager::get_last_updated_group_key() const { int32 left = max_notification_group_count_; auto it = groups_.begin(); while (it != groups_.end() && left > 1) { @@ -228,10 +255,7 @@ void NotificationManager::add_notification(NotificationGroupId group_id, DialogI auto group_it = get_group_force(group_id); if (group_it == groups_.end()) { - NotificationGroupKey group_key; - group_key.group_id = group_id; - group_key.dialog_id = dialog_id; - group_key.last_notification_date = 0; + NotificationGroupKey group_key(group_id, dialog_id, 0); group_it = std::move(groups_.emplace(group_key, NotificationGroup()).first); } diff --git a/td/telegram/NotificationManager.h b/td/telegram/NotificationManager.h index 801cec080..3fff9975a 100644 --- a/td/telegram/NotificationManager.h +++ b/td/telegram/NotificationManager.h @@ -5,11 +5,11 @@ // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // #pragma once - #include "td/telegram/DialogId.h" #include "td/telegram/MessageId.h" #include "td/telegram/Notification.h" #include "td/telegram/NotificationGroupId.h" +#include "td/telegram/NotificationGroupKey.h" #include "td/telegram/NotificationId.h" #include "td/telegram/NotificationType.h" #include "td/telegram/td_api.h" @@ -101,26 +101,6 @@ class NotificationManager : public Actor { unique_ptr type; }; - struct NotificationGroupKey { - NotificationGroupId group_id; - DialogId dialog_id; - int32 last_notification_date = 0; - - bool operator<(const NotificationGroupKey &other) const { - if (last_notification_date != other.last_notification_date) { - return last_notification_date > other.last_notification_date; - } - if (dialog_id != other.dialog_id) { - return dialog_id.get() > other.dialog_id.get(); - } - return group_id.get() > other.group_id.get(); - } - - friend StringBuilder &operator<<(StringBuilder &string_builder, const NotificationGroupKey &group_key) { - return string_builder << '[' << group_key.group_id << ',' << group_key.dialog_id << ',' - << group_key.last_notification_date << ']'; - } - }; struct NotificationGroup { int32 total_count = 0; @@ -150,7 +130,9 @@ class NotificationManager : public Actor { NotificationGroups::iterator get_group(NotificationGroupId group_id); - NotificationGroups::iterator get_group_force(NotificationGroupId group_id); + NotificationGroups::iterator get_group_force(NotificationGroupId group_id, bool send_update = true); + + void load_message_notification_groups_from_database(bool send_update); NotificationGroupKey get_last_updated_group_key() const; @@ -191,6 +173,9 @@ class NotificationManager : public Actor { int32 notification_cloud_delay_ms_ = DEFAULT_ONLINE_CLOUD_DELAY_MS; int32 notification_default_delay_ms_ = DEFAULT_DEFAULT_DELAY_MS; + int32 last_loaded_notification_date_ = 0; + DialogId last_loaded_notification_dialog_id_; + bool running_get_difference_ = false; std::unordered_set running_get_chat_difference_;