From bd531081adc3bf0535a39e5f565cba31819f0de2 Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 26 Nov 2018 20:05:06 +0300 Subject: [PATCH] Add Notification.h. GitOrigin-RevId: e102973d5585fdf346ac01b09bd2262f9c11fd2e --- CMakeLists.txt | 1 + td/telegram/AuthManager.cpp | 2 +- td/telegram/MessagesManager.cpp | 10 ++++++ td/telegram/MessagesManager.h | 8 +++++ td/telegram/Notification.h | 41 ++++++++++++++++++++++ td/telegram/NotificationManager.cpp | 54 ++++++++++++++++++++--------- td/telegram/NotificationManager.h | 16 ++------- 7 files changed, 101 insertions(+), 31 deletions(-) create mode 100644 td/telegram/Notification.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 7ffe57d9c..5bd972ad0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -535,6 +535,7 @@ set(TDLIB_SOURCE td/telegram/net/SessionProxy.h td/telegram/net/SessionMultiProxy.h td/telegram/net/TempAuthKeyWatchdog.h + td/telegram/Notification.h td/telegram/NotificationGroupId.h td/telegram/NotificationId.h td/telegram/NotificationManager.h diff --git a/td/telegram/AuthManager.cpp b/td/telegram/AuthManager.cpp index 37d7b120d..36fbfb1db 100644 --- a/td/telegram/AuthManager.cpp +++ b/td/telegram/AuthManager.cpp @@ -5,11 +5,11 @@ // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // #include "td/telegram/AuthManager.h" -#include "td/telegram/AuthManager.hpp" #include "td/telegram/td_api.h" #include "td/telegram/telegram_api.h" +#include "td/telegram/AuthManager.hpp" #include "td/telegram/ConfigManager.h" #include "td/telegram/ConfigShared.h" #include "td/telegram/ContactsManager.h" diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 410504161..0c5c9fcbe 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -5,6 +5,7 @@ // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // #include "td/telegram/MessagesManager.h" + #include "td/telegram/AnimationsManager.h" #include "td/telegram/AuthManager.h" #include "td/telegram/ChatId.h" @@ -17419,6 +17420,15 @@ NotificationGroupId MessagesManager::get_dialog_message_notification_group_id(Di return d->message_notification_group_id; } +MessagesManager::MessageNotificationGroup MessagesManager::get_message_notification_group_force( + NotificationGroupId group_id) { + MessageNotificationGroup result; + if (G()->parameters().use_message_db) { + // TODO load message notification group + } + return result; +} + 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 988b2d063..d63d47f58 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -23,6 +23,7 @@ #include "td/telegram/MessageId.h" #include "td/telegram/MessagesDb.h" #include "td/telegram/net/NetQuery.h" +#include "td/telegram/Notification.h" #include "td/telegram/NotificationGroupId.h" #include "td/telegram/NotificationId.h" #include "td/telegram/NotificationSettings.h" @@ -656,6 +657,13 @@ class MessagesManager : public Actor { vector> users, vector> chats); + struct MessageNotificationGroup { + DialogId dialog_id; + int32 total_count = 0; + vector notifications; + }; + MessageNotificationGroup get_message_notification_group_force(NotificationGroupId group_id); + void on_binlog_events(vector &&events); void get_payment_form(FullMessageId full_message_id, Promise> &&promise); diff --git a/td/telegram/Notification.h b/td/telegram/Notification.h new file mode 100644 index 000000000..ae9679b06 --- /dev/null +++ b/td/telegram/Notification.h @@ -0,0 +1,41 @@ +// +// 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/NotificationId.h" +#include "td/telegram/NotificationType.h" +#include "td/telegram/td_api.h" + +#include "td/utils/common.h" +#include "td/utils/StringBuilder.h" + +namespace td { + +class Notification { + public: + NotificationId notification_id; + int32 date = 0; + unique_ptr type; + + Notification(NotificationId notification_id, int32 date, unique_ptr type) + : notification_id(notification_id), date(date), type(std::move(type)) { + } +}; + +inline td_api::object_ptr get_notification_object(DialogId dialog_id, + const Notification ¬ification) { + return td_api::make_object(notification.notification_id.get(), + notification.type->get_notification_type_object(dialog_id)); +} + +inline StringBuilder &operator<<(StringBuilder &sb, const Notification notification) { + return sb << "notification[" << notification.notification_id << ", " << notification.date << ", " + << *notification.type << ']'; +} + +} // namespace td diff --git a/td/telegram/NotificationManager.cpp b/td/telegram/NotificationManager.cpp index f85d4fadb..7a0592cb3 100644 --- a/td/telegram/NotificationManager.cpp +++ b/td/telegram/NotificationManager.cpp @@ -10,6 +10,7 @@ #include "td/telegram/ConfigShared.h" #include "td/telegram/ContactsManager.h" #include "td/telegram/Global.h" +#include "td/telegram/MessagesManager.h" #include "td/telegram/Td.h" #include "td/telegram/TdDb.h" @@ -85,7 +86,7 @@ void NotificationManager::start_up() { on_notification_cloud_delay_changed(); on_notification_default_delay_changed(); - // TODO load groups + // TODO send updateActiveNotifications } void NotificationManager::tear_down() { @@ -102,6 +103,34 @@ NotificationManager::NotificationGroups::iterator NotificationManager::get_group return groups_.end(); } +NotificationManager::NotificationGroups::iterator NotificationManager::get_group_force(NotificationGroupId group_id) { + auto group_it = get_group(group_id); + if (group_it != groups_.end()) { + return group_it; + } + + auto message_group = td_->messages_manager_->get_message_notification_group_force(group_id); + if (!message_group.dialog_id.is_valid()) { + 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; + for (auto ¬ification : message_group.notifications) { + if (notification.date >= group_key.last_notification_date) { + group_key.last_notification_date = notification.date; + } + } + + NotificationGroup group; + group.total_count = message_group.total_count; + group.notifications = std::move(message_group.notifications); + + return groups_.emplace(std::move(group_key), std::move(group)).first; +} + NotificationId NotificationManager::get_max_notification_id() const { return current_notification_id_; } @@ -193,15 +222,13 @@ void NotificationManager::add_notification(NotificationGroupId group_id, DialogI << " with settings from " << notification_settings_dialog_id << (is_silent ? " silently" : " with sound") << ": " << *type; - auto group_it = get_group(group_id); + 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; group_it = std::move(groups_.emplace(group_key, NotificationGroup()).first); - - // TODO synchronously load old group notifications from the database } PendingNotification notification; @@ -223,12 +250,6 @@ void NotificationManager::add_notification(NotificationGroupId group_id, DialogI group.pending_notifications.push_back(std::move(notification)); } -td_api::object_ptr NotificationManager::get_notification_object( - DialogId dialog_id, const Notification ¬ification) { - return td_api::make_object(notification.notification_id.get(), - notification.type->get_notification_type_object(dialog_id)); -} - struct NotificationUpdate { const td_api::Update *update; }; @@ -619,8 +640,8 @@ void NotificationManager::do_flush_pending_notifications(NotificationGroupKey &g vector> added_notifications; added_notifications.reserve(pending_notifications.size()); for (auto &pending_notification : pending_notifications) { - Notification notification{pending_notification.notification_id, pending_notification.date, - std::move(pending_notification.type)}; + Notification notification(pending_notification.notification_id, pending_notification.date, + std::move(pending_notification.type)); added_notifications.push_back(get_notification_object(group_key.dialog_id, notification)); if (added_notifications.back()->type_ == nullptr) { added_notifications.pop_back(); @@ -864,7 +885,8 @@ void NotificationManager::on_notifications_removed( /* if (last_loaded_group_key_ < last_group_key) { - // TODO load new groups from database + TODO + load_notification_groups_from_database(); } */ } @@ -929,9 +951,8 @@ void NotificationManager::remove_notification(NotificationGroupId group_id, Noti // TODO remove notification from database by notification_id - auto group_it = get_group(group_id); + auto group_it = get_group_force(group_id); if (group_it == groups_.end()) { - // TODO synchronously load the group return promise.set_value(Unit()); } @@ -1014,10 +1035,9 @@ void NotificationManager::remove_notification_group(NotificationGroupId group_id // TODO remove notifications from database by max_notification_id, save that they are removed } - auto group_it = get_group(group_id); + auto group_it = get_group_force(group_id); if (group_it == groups_.end()) { VLOG(notifications) << "Can't find " << group_id; - // TODO synchronously load the group return promise.set_value(Unit()); } diff --git a/td/telegram/NotificationManager.h b/td/telegram/NotificationManager.h index 14489d6ee..9296d6fa3 100644 --- a/td/telegram/NotificationManager.h +++ b/td/telegram/NotificationManager.h @@ -8,6 +8,7 @@ #include "td/telegram/DialogId.h" #include "td/telegram/MessageId.h" +#include "td/telegram/Notification.h" #include "td/telegram/NotificationGroupId.h" #include "td/telegram/NotificationId.h" #include "td/telegram/NotificationType.h" @@ -90,16 +91,6 @@ class NotificationManager : public Actor { static constexpr int32 MIN_UPDATE_DELAY_MS = 50; static constexpr int32 MAX_UPDATE_DELAY_MS = 60000; - struct Notification { - NotificationId notification_id; - int32 date = 0; - unique_ptr type; - - Notification(NotificationId notification_id, int32 date, unique_ptr type) - : notification_id(notification_id), date(date), type(std::move(type)) { - } - }; - struct PendingNotification { int32 date = 0; DialogId settings_dialog_id; @@ -148,9 +139,6 @@ class NotificationManager : public Actor { void start_up() override; void tear_down() override; - static td_api::object_ptr get_notification_object(DialogId dialog_id, - const Notification ¬ification); - void add_update(int32 group_id, td_api::object_ptr update); void add_update_notification_group(td_api::object_ptr update); @@ -160,6 +148,8 @@ class NotificationManager : public Actor { NotificationGroups::iterator get_group(NotificationGroupId group_id); + NotificationGroups::iterator get_group_force(NotificationGroupId group_id); + NotificationGroupKey get_last_updated_group_key() const; void send_remove_group_update(const NotificationGroupKey &group_key, const NotificationGroup &group,