From 063e8872dfff7cf2f3fd2c01754de7e74e5b9d19 Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 5 Mar 2019 06:01:49 +0300 Subject: [PATCH] Fix notification usage for bots. GitOrigin-RevId: 4645b1c91e5bb1e6a7ef5429efe690c52a8ee257 --- td/telegram/AuthManager.cpp | 2 ++ td/telegram/MessagesManager.cpp | 11 +++++++-- td/telegram/NotificationManager.cpp | 35 ++++++++++++++++++++++------- td/telegram/NotificationManager.h | 2 ++ 4 files changed, 40 insertions(+), 10 deletions(-) diff --git a/td/telegram/AuthManager.cpp b/td/telegram/AuthManager.cpp index 49863c73..2eb77155 100644 --- a/td/telegram/AuthManager.cpp +++ b/td/telegram/AuthManager.cpp @@ -18,6 +18,7 @@ #include "td/telegram/misc.h" #include "td/telegram/net/DcId.h" #include "td/telegram/net/NetQueryDispatcher.h" +#include "td/telegram/NotificationManager.h" #include "td/telegram/PasswordManager.h" #include "td/telegram/Td.h" #include "td/telegram/TdDb.h" @@ -560,6 +561,7 @@ void AuthManager::on_authorization(tl_object_ptrflags_ & telegram_api::auth_authorization::TMP_SESSIONS_MASK) != 0) { G()->shared_config().set_option_integer("session_count", auth->tmp_sessions_); } + td->notification_manager_->init(); td->updates_manager_->get_difference("on_authorization"); td->on_online_updated(false, true); td->schedule_get_terms_of_service(0); diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 671e3212..bf255e39 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -8994,7 +8994,9 @@ void MessagesManager::init() { channels_notification_settings_.disable_pinned_message_notifications = false; channels_notification_settings_.disable_mention_notifications = false; channels_notification_settings_.is_synchronized = false; - send_get_scope_notification_settings_query(NotificationSettingsScope::Channel, Promise<>()); + if (!td_->auth_manager_->is_bot()) { + send_get_scope_notification_settings_query(NotificationSettingsScope::Channel, Promise<>()); + } } G()->td_db()->get_binlog_pmc()->erase("nsfac"); @@ -20030,6 +20032,11 @@ void MessagesManager::send_get_dialog_notification_settings_query(DialogId dialo void MessagesManager::send_get_scope_notification_settings_query(NotificationSettingsScope scope, Promise &&promise) { + if (td_->auth_manager_->is_bot()) { + LOG(ERROR) << "Can't get notification settings for " << scope; + return promise.set_error(Status::Error(500, "Wrong getScopeNotificationSettings query")); + } + td_->create_handler(std::move(promise))->send(scope); } @@ -23064,7 +23071,7 @@ void MessagesManager::fix_new_dialog(Dialog *d, unique_ptr &&last_datab } if (d->notification_settings.is_synchronized && !d->notification_settings.is_use_default_fixed && - have_input_peer(dialog_id, AccessRights::Read)) { + have_input_peer(dialog_id, AccessRights::Read) && !td_->auth_manager_->is_bot()) { LOG(INFO) << "Reget notification settings of " << dialog_id; if (d->dialog_id.get_type() == DialogType::SecretChat) { if (d->notification_settings.mute_until == 0 && users_notification_settings_.mute_until == 0) { diff --git a/td/telegram/NotificationManager.cpp b/td/telegram/NotificationManager.cpp index 23c4c189..476a45d4 100644 --- a/td/telegram/NotificationManager.cpp +++ b/td/telegram/NotificationManager.cpp @@ -141,7 +141,7 @@ void NotificationManager::on_flush_pending_updates_timeout_callback(void *notifi } bool NotificationManager::is_disabled() const { - return td_->auth_manager_->is_bot() || G()->close_flag(); + return !td_->auth_manager_->is_authorized() || td_->auth_manager_->is_bot() || G()->close_flag(); } namespace { @@ -179,6 +179,14 @@ string NotificationManager::get_is_contact_registered_notifications_synchronized } void NotificationManager::start_up() { + init(); +} + +void NotificationManager::init() { + if (is_disabled()) { + return; + } + disable_contact_registered_notifications_ = G()->shared_config().get_option_boolean("disable_contact_registered_notifications"); auto sync_state = G()->td_db()->get_binlog_pmc()->get(get_is_contact_registered_notifications_synchronized_key()); @@ -193,10 +201,6 @@ void NotificationManager::start_up() { run_contact_registered_notifications_sync(); } - if (is_disabled()) { - return; - } - current_notification_id_ = NotificationId(to_integer(G()->td_db()->get_binlog_pmc()->get("notification_id_current"))); current_notification_group_id_ = @@ -2136,6 +2140,10 @@ void NotificationManager::on_notification_default_delay_changed() { } void NotificationManager::on_disable_contact_registered_notifications_changed() { + if (is_disabled()) { + return; + } + auto is_disabled = G()->shared_config().get_option_boolean("disable_contact_registered_notifications"); if (is_disabled == disable_contact_registered_notifications_) { @@ -2162,6 +2170,10 @@ void NotificationManager::on_get_disable_contact_registered_notifications(bool i } void NotificationManager::set_contact_registered_notifications_sync_state(SyncState new_state) { + if (is_disabled()) { + return; + } + contact_registered_notifications_sync_state_ = new_state; string value; value += static_cast(static_cast(new_state) + '0'); @@ -2170,6 +2182,10 @@ void NotificationManager::set_contact_registered_notifications_sync_state(SyncSt } void NotificationManager::run_contact_registered_notifications_sync() { + if (is_disabled()) { + return; + } + auto is_disabled = disable_contact_registered_notifications_; if (contact_registered_notifications_sync_state_ == SyncState::NotSynced && !is_disabled) { set_contact_registered_notifications_sync_state(SyncState::Completed); @@ -2197,13 +2213,16 @@ void NotificationManager::on_contact_registered_notifications_sync(bool is_disab set_contact_registered_notifications_sync_state(SyncState::Completed); } else { // let's resend the query forever - if (!G()->close_flag()) { - run_contact_registered_notifications_sync(); - } + run_contact_registered_notifications_sync(); } } void NotificationManager::get_disable_contact_registered_notifications(Promise &&promise) { + if (is_disabled()) { + promise.set_value(Unit()); + return; + } + td_->create_handler(std::move(promise))->send(); } diff --git a/td/telegram/NotificationManager.h b/td/telegram/NotificationManager.h index 24a4a54f..3ceba649 100644 --- a/td/telegram/NotificationManager.h +++ b/td/telegram/NotificationManager.h @@ -46,6 +46,8 @@ class NotificationManager : public Actor { NotificationManager(Td *td, ActorShared<> parent); + void init(); + size_t get_max_notification_group_size() const; NotificationId get_max_notification_id() const;