From c5c9707837b63e4a0f3852a69ae473f60cc084cb Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 3 Feb 2023 12:39:23 +0300 Subject: [PATCH] Add td_api::getAutosaveSettings. --- td/generate/scheme/td_api.tl | 22 +++++++ td/telegram/AutosaveManager.cpp | 110 ++++++++++++++++++++++++++++++++ td/telegram/AutosaveManager.h | 38 +++++++++++ td/telegram/Td.cpp | 6 ++ td/telegram/Td.h | 2 + td/telegram/cli.cpp | 2 + 6 files changed, 180 insertions(+) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 43e11e1cd..a01dbfcd7 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -4816,6 +4816,25 @@ autoDownloadSettings is_auto_download_enabled:Bool max_photo_file_size:int32 max autoDownloadSettingsPresets low:autoDownloadSettings medium:autoDownloadSettings high:autoDownloadSettings = AutoDownloadSettingsPresets; +//@description Contains autosave settings for a chat or a chat scope +//@autosave_photos True, if photo autosave is enabled +//@autosave_videos True, if video autosave is enabled +//@max_video_file_size The maximum size of a video file to be autosaved, in bytes; 0 if not limited +chatAutosaveSettings autosave_photos:Bool autosave_videos:Bool max_video_file_size:int53 = ChatAutosaveSettings; + +//@description Contains autosave settings for a chat, which overrides settings for the corresponding scope +//@chat_id Chat identifier +//@settings Autosave settings for the chat +chatAutosaveException chat_id:int53 settings:chatAutosaveSettings = ChatAutosaveException; + +//@description Describes autosave settings +//@private_chat_settings Default autosave settings for private chats +//@group_settings Default autosave settings for basic group and supergroup chats +//@channel_settings Default autosave settings for channel chats +//@exceptions Autosave exceptions for specific chats +autosaveSettings private_chat_settings:chatAutosaveSettings group_settings:chatAutosaveSettings channel_settings:chatAutosaveSettings exceptions:vector = AutosaveSettings; + + //@class ConnectionState @description Describes the current state of the connection to Telegram servers //@description Currently waiting for the network to become available. Use setNetworkType to change the available network type @@ -7809,6 +7828,9 @@ getAutoDownloadSettingsPresets = AutoDownloadSettingsPresets; //@description Sets auto-download settings @settings New user auto-download settings @type Type of the network for which the new settings are relevant setAutoDownloadSettings settings:autoDownloadSettings type:NetworkType = Ok; +//@description Returns autosave settings for the current user +getAutosaveSettings = AutosaveSettings; + //@description Returns information about a bank card @bank_card_number The bank card number getBankCardInfo bank_card_number:string = BankCardInfo; diff --git a/td/telegram/AutosaveManager.cpp b/td/telegram/AutosaveManager.cpp index fdae9165b..cabd3c5ab 100644 --- a/td/telegram/AutosaveManager.cpp +++ b/td/telegram/AutosaveManager.cpp @@ -6,10 +6,43 @@ // #include "td/telegram/AutosaveManager.h" +#include "td/telegram/ContactsManager.h" +#include "td/telegram/MessagesManager.h" #include "td/telegram/Td.h" +#include "td/utils/algorithm.h" +#include "td/utils/buffer.h" + namespace td { +class GetAutosaveSettingsQuery final : public Td::ResultHandler { + Promise> promise_; + + public: + explicit GetAutosaveSettingsQuery(Promise> &&promise) + : promise_(std::move(promise)) { + } + + void send() { + send_query(G()->net_query_creator().create(telegram_api::account_getAutoSaveSettings())); + } + + void on_result(BufferSlice packet) final { + auto result_ptr = fetch_result(packet); + if (result_ptr.is_error()) { + return on_error(result_ptr.move_as_error()); + } + + auto ptr = result_ptr.move_as_ok(); + LOG(INFO) << "Receive result for GetAutoSaveSettingsQuery: " << to_string(ptr); + promise_.set_value(std::move(ptr)); + } + + void on_error(Status status) final { + promise_.set_error(std::move(status)); + } +}; + AutosaveManager::AutosaveManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) { } @@ -17,4 +50,81 @@ void AutosaveManager::tear_down() { parent_.reset(); } +AutosaveManager::DialogAutosaveSettings::DialogAutosaveSettings(const telegram_api::autoSaveSettings *settings) { + CHECK(settings != nullptr); + autosave_photos_ = settings->photos_; + autosave_videos_ = settings->videos_; + max_video_file_size_ = settings->video_max_size_; +} + +td_api::object_ptr +AutosaveManager::DialogAutosaveSettings::get_chat_autosave_settings_object() const { + return td_api::make_object(autosave_photos_, autosave_videos_, max_video_file_size_); +} + +td_api::object_ptr +AutosaveManager::DialogAutosaveSettings::get_chat_autosave_exception_object(DialogId dialog_id) const { + return td_api::make_object(dialog_id.get(), get_chat_autosave_settings_object()); +} + +td_api::object_ptr AutosaveManager::AutosaveSettings::get_autosave_settings_object() const { + CHECK(are_inited_); + auto exceptions = transform(exceptions_, [](const auto &exception) { + return exception.second.get_chat_autosave_exception_object(exception.first); + }); + return td_api::make_object( + user_settings_.get_chat_autosave_settings_object(), user_settings_.get_chat_autosave_settings_object(), + user_settings_.get_chat_autosave_settings_object(), std::move(exceptions)); +} + +void AutosaveManager::get_autosave_settings(Promise> &&promise) { + if (settings_.are_inited_) { + return promise.set_value(settings_.get_autosave_settings_object()); + } + + load_settings_queries_.push_back(std::move(promise)); + if (load_settings_queries_.size() != 1) { + return; + } + + auto query_promise = PromiseCreator::lambda( + [actor_id = actor_id(this)](Result> r_settings) { + send_closure(actor_id, &AutosaveManager::on_get_autosave_settings, std::move(r_settings)); + }); + td_->create_handler(std::move(query_promise))->send(); +} + +void AutosaveManager::on_get_autosave_settings( + Result> r_settings) { + CHECK(!settings_.are_inited_); + if (G()->close_flag() && r_settings.is_ok()) { + r_settings = Global::request_aborted_error(); + } + if (r_settings.is_error()) { + return fail_promises(load_settings_queries_, r_settings.move_as_error()); + } + + auto settings = r_settings.move_as_ok(); + settings_.are_inited_ = true; + 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"); + 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(); + 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()); + } + + auto promises = std::move(load_settings_queries_); + for (auto &promise : promises) { + promise.set_value(settings_.get_autosave_settings_object()); + } +} + } // namespace td diff --git a/td/telegram/AutosaveManager.h b/td/telegram/AutosaveManager.h index 57a92bcc8..9da888ea1 100644 --- a/td/telegram/AutosaveManager.h +++ b/td/telegram/AutosaveManager.h @@ -6,9 +6,16 @@ // #pragma once +#include "td/telegram/DialogId.h" +#include "td/telegram/td_api.h" +#include "td/telegram/telegram_api.h" + #include "td/actor/actor.h" #include "td/utils/common.h" +#include "td/utils/FlatHashMap.h" +#include "td/utils/Promise.h" +#include "td/utils/Status.h" namespace td { @@ -18,11 +25,42 @@ class AutosaveManager final : public Actor { public: AutosaveManager(Td *td, ActorShared<> parent); + void get_autosave_settings(Promise> &&promise); + private: + struct DialogAutosaveSettings { + bool autosave_photos_ = false; + bool autosave_videos_ = false; + int64 max_video_file_size_ = 0; + + DialogAutosaveSettings() = default; + + explicit DialogAutosaveSettings(const telegram_api::autoSaveSettings *settings); + + td_api::object_ptr get_chat_autosave_settings_object() const; + + td_api::object_ptr get_chat_autosave_exception_object(DialogId dialog_id) const; + }; + + struct AutosaveSettings { + bool are_inited_ = false; + DialogAutosaveSettings user_settings_; + DialogAutosaveSettings chat_settings_; + DialogAutosaveSettings broadcast_settings_; + FlatHashMap exceptions_; + + td_api::object_ptr get_autosave_settings_object() const; + }; + void tear_down() final; + void on_get_autosave_settings(Result> r_settings); + Td *td_; ActorShared<> parent_; + + AutosaveSettings settings_; + vector>> load_settings_queries_; }; } // namespace td diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index ebc7d55ea..46419e82d 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -4946,6 +4946,12 @@ void Td::on_request(uint64 id, const td_api::setAutoDownloadSettings &request) { std::move(promise)); } +void Td::on_request(uint64 id, const td_api::getAutosaveSettings &request) { + CHECK_IS_USER(); + CREATE_REQUEST_PROMISE(); + autosave_manager_->get_autosave_settings(std::move(promise)); +} + void Td::on_request(uint64 id, const td_api::getTopChats &request) { CHECK_IS_USER(); CREATE_REQUEST_PROMISE(); diff --git a/td/telegram/Td.h b/td/telegram/Td.h index 2aa647336..f6e5190eb 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -590,6 +590,8 @@ class Td final : public Actor { void on_request(uint64 id, const td_api::setAutoDownloadSettings &request); + void on_request(uint64 id, const td_api::getAutosaveSettings &request); + void on_request(uint64 id, const td_api::getTopChats &request); void on_request(uint64 id, const td_api::removeTopChat &request); diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index 83397d792..be72415c3 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -2831,6 +2831,8 @@ class CliClient final : public Actor { } else if (op == "sads") { send_request(td_api::make_object( td_api::make_object(), as_network_type(args))); + } else if (op == "gaus") { + send_request(td_api::make_object()); } else if (op == "ansc") { int32 sent_bytes; int32 received_bytes;