diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 58255dd59..29d0d4323 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -5487,7 +5487,7 @@ updateAnimationSearchParameters provider:string emojis:vector = Update; //@description The list of suggested to the user actions has changed @added_actions Added suggested actions @removed_actions Removed suggested actions updateSuggestedActions added_actions:vector removed_actions:vector = Update; -//@description Autosave settings for some type of chats were updated @scope Type of chats for which autosave settings were updated @settings The new autosave settings +//@description Autosave settings for some type of chats were updated @scope Type of chats for which autosave settings were updated @settings The new autosave settings; may be null if the settings are reset to default updateAutosaveSettings scope:AutosaveSettingsScope settings:scopeAutosaveSettings = Update; //@description A new incoming inline query; for bots only diff --git a/td/telegram/AutosaveManager.cpp b/td/telegram/AutosaveManager.cpp index c6b27d89a..f126d2d22 100644 --- a/td/telegram/AutosaveManager.cpp +++ b/td/telegram/AutosaveManager.cpp @@ -62,6 +62,7 @@ class SaveAutoSaveSettingsQuery final : public Td::ResultHandler { } else if (broadcasts) { flags |= telegram_api::account_saveAutoSaveSettings::BROADCASTS_MASK; } else { + flags |= telegram_api::account_saveAutoSaveSettings::PEER_MASK; input_peer = td_->messages_manager_->get_input_peer(dialog_id, AccessRights::Read); if (input_peer == nullptr) { if (dialog_id.get_type() == DialogType::SecretChat) { @@ -160,6 +161,9 @@ AutosaveManager::DialogAutosaveSettings::get_input_auto_save_settings() const { td_api::object_ptr AutosaveManager::DialogAutosaveSettings::get_scope_autosave_settings_object() const { + if (!are_inited_) { + return nullptr; + } return td_api::make_object(autosave_photos_, autosave_videos_, max_video_file_size_); } @@ -173,6 +177,10 @@ bool AutosaveManager::DialogAutosaveSettings::operator==(const DialogAutosaveSet autosave_videos_ == other.autosave_videos_ && max_video_file_size_ == other.max_video_file_size_; } +bool AutosaveManager::DialogAutosaveSettings::operator!=(const DialogAutosaveSettings &other) const { + return !operator==(other); +} + td_api::object_ptr AutosaveManager::AutosaveSettings::get_autosave_settings_object() const { CHECK(are_inited_); auto exceptions = transform(exceptions_, [](const auto &exception) { @@ -217,18 +225,49 @@ void AutosaveManager::on_get_autosave_settings( td_->contacts_manager_->on_get_users(std::move(settings->users_), "on_get_autosave_settings"); td_->contacts_manager_->on_get_chats(std::move(settings->chats_), "on_get_autosave_settings"); + DialogAutosaveSettings new_user_settings(settings->users_settings_.get()); + DialogAutosaveSettings new_chat_settings(settings->chats_settings_.get()); + DialogAutosaveSettings new_broadcast_settings(settings->broadcasts_settings_.get()); + settings_.are_inited_ = true; - settings_.user_settings_ = DialogAutosaveSettings(settings->users_settings_.get()); - settings_.chat_settings_ = DialogAutosaveSettings(settings->chats_settings_.get()); - settings_.broadcast_settings_ = DialogAutosaveSettings(settings->broadcasts_settings_.get()); - settings_.exceptions_.clear(); + if (settings_.user_settings_ != new_user_settings) { + settings_.user_settings_ = std::move(new_user_settings); + send_update_autosave_settings(td_api::make_object(), + settings_.user_settings_); + } + if (settings_.chat_settings_ != new_chat_settings) { + settings_.chat_settings_ = std::move(new_chat_settings); + send_update_autosave_settings(td_api::make_object(), + settings_.chat_settings_); + } + if (settings_.broadcast_settings_ != new_broadcast_settings) { + settings_.broadcast_settings_ = std::move(new_broadcast_settings); + send_update_autosave_settings(td_api::make_object(), + settings_.broadcast_settings_); + } + FlatHashSet exception_dialog_ids; + for (auto &exception : settings_.exceptions_) { + exception_dialog_ids.insert(exception.first); + } for (auto &exception : settings->exceptions_) { DialogId dialog_id(exception->peer_); if (!dialog_id.is_valid()) { continue; } td_->messages_manager_->force_create_dialog(dialog_id, "on_get_autosave_settings"); - settings_.exceptions_[dialog_id] = DialogAutosaveSettings(exception->settings_.get()); + DialogAutosaveSettings new_settings(exception->settings_.get()); + auto ¤t_settings = settings_.exceptions_[dialog_id]; + if (current_settings != new_settings) { + current_settings = std::move(new_settings); + send_update_autosave_settings(td_api::make_object(dialog_id.get()), + current_settings); + } + exception_dialog_ids.erase(dialog_id); + } + for (auto dialog_id : exception_dialog_ids) { + settings_.exceptions_.erase(dialog_id); + send_update_autosave_settings(td_api::make_object(dialog_id.get()), + DialogAutosaveSettings()); } auto promises = std::move(load_settings_queries_); @@ -274,7 +313,7 @@ void AutosaveManager::set_autosave_settings(td_api::object_ptrcreate_handler(std::move(promise)) @@ -294,8 +335,23 @@ void AutosaveManager::set_autosave_settings(td_api::object_ptr &&promise) { + for (const auto &exception : settings_.exceptions_) { + send_update_autosave_settings(td_api::make_object(exception.first.get()), + DialogAutosaveSettings()); + } settings_.exceptions_.clear(); td_->create_handler(std::move(promise))->send(); } +td_api::object_ptr AutosaveManager::get_update_autosave_settings( + td_api::object_ptr &&scope, const DialogAutosaveSettings &settings) { + return td_api::make_object(std::move(scope), + settings.get_scope_autosave_settings_object()); +} + +void AutosaveManager::send_update_autosave_settings(td_api::object_ptr &&scope, + const DialogAutosaveSettings &settings) { + send_closure(G()->td(), &Td::send_update, get_update_autosave_settings(std::move(scope), settings)); +} + } // namespace td diff --git a/td/telegram/AutosaveManager.h b/td/telegram/AutosaveManager.h index 28c57ceb9..1f2175869 100644 --- a/td/telegram/AutosaveManager.h +++ b/td/telegram/AutosaveManager.h @@ -59,6 +59,8 @@ class AutosaveManager final : public Actor { DialogId dialog_id) const; bool operator==(const DialogAutosaveSettings &other) const; + + bool operator!=(const DialogAutosaveSettings &other) const; }; struct AutosaveSettings { @@ -75,6 +77,12 @@ class AutosaveManager final : public Actor { void on_get_autosave_settings(Result> r_settings); + static td_api::object_ptr get_update_autosave_settings( + td_api::object_ptr &&scope, const DialogAutosaveSettings &settings); + + void send_update_autosave_settings(td_api::object_ptr &&scope, + const DialogAutosaveSettings &settings); + Td *td_; ActorShared<> parent_;