From 2d7608459eebd064edec0c9bd7c7955ec4db65c7 Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 17 Jan 2023 13:22:21 +0300 Subject: [PATCH] Support standalone SetPassword suggested action. --- td/generate/scheme/td_api.tl | 3 ++- td/telegram/ConfigManager.cpp | 9 ++++++--- td/telegram/OptionManager.cpp | 2 +- td/telegram/PasswordManager.cpp | 1 + td/telegram/SuggestedAction.cpp | 22 +++++++++++++++++----- td/telegram/SuggestedAction.h | 2 +- 6 files changed, 28 insertions(+), 11 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 998546ef0..ae389e840 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -4816,7 +4816,8 @@ suggestedActionViewChecksHint = SuggestedAction; //@description Suggests the user to convert specified supergroup to a broadcast group @supergroup_id Supergroup identifier suggestedActionConvertToBroadcastGroup supergroup_id:int53 = SuggestedAction; -//@description Suggests the user to set a 2-step verification password to be able to log in again @authorization_delay The number of days to pass between consecutive authorizations if the user declines to set password +//@description Suggests the user to set a 2-step verification password to be able to log in again +//@authorization_delay The number of days to pass between consecutive authorizations if the user declines to set password; if 0, then the user is advised to set the password for security reasons suggestedActionSetPassword authorization_delay:int32 = SuggestedAction; diff --git a/td/telegram/ConfigManager.cpp b/td/telegram/ConfigManager.cpp index fc25db0e9..5002de8c2 100644 --- a/td/telegram/ConfigManager.cpp +++ b/td/telegram/ConfigManager.cpp @@ -1463,8 +1463,6 @@ void ConfigManager::process_app_config(tl_object_ptr &c CHECK(config != nullptr); LOG(INFO) << "Receive app config " << to_string(config); - const bool archive_and_mute = G()->get_option_boolean("archive_and_mute_new_chats_from_unknown_users"); - string autologin_token; vector autologin_domains; vector url_auth_domains; @@ -1664,6 +1662,8 @@ void ConfigManager::process_app_config(tl_object_ptr &c if (key == "pending_suggestions") { if (value->get_id() == telegram_api::jsonArray::ID) { auto actions = std::move(static_cast(value)->value_); + const bool archive_and_mute = G()->get_option_boolean("archive_and_mute_new_chats_from_unknown_users"); + auto otherwise_relogin_days = G()->get_option_integer("otherwise_relogin_days"); for (auto &action : actions) { auto action_str = get_json_value_string(std::move(action), key); SuggestedAction suggested_action(action_str); @@ -1671,6 +1671,9 @@ void ConfigManager::process_app_config(tl_object_ptr &c if (archive_and_mute && suggested_action == SuggestedAction{SuggestedAction::Type::EnableArchiveAndMuteNewChats}) { LOG(INFO) << "Skip EnableArchiveAndMuteNewChats suggested action"; + } else if (otherwise_relogin_days > 0 && + suggested_action == SuggestedAction{SuggestedAction::Type::SetPassword}) { + LOG(INFO) << "Skip SetPassword suggested action"; } else { suggested_actions.push_back(suggested_action); } @@ -2025,7 +2028,7 @@ void ConfigManager::process_app_config(tl_object_ptr &c void ConfigManager::get_current_state(vector> &updates) const { if (!suggested_actions_.empty()) { - updates.push_back(get_update_suggested_actions_object(suggested_actions_, {})); + updates.push_back(get_update_suggested_actions_object(suggested_actions_, {}, "get_current_state")); } } diff --git a/td/telegram/OptionManager.cpp b/td/telegram/OptionManager.cpp index 3d6efb643..57a551fe4 100644 --- a/td/telegram/OptionManager.cpp +++ b/td/telegram/OptionManager.cpp @@ -303,7 +303,7 @@ td_api::object_ptr OptionManager::get_internal_option_update(Sli auto days = narrow_cast(get_option_integer(name)); if (days > 0) { vector added_actions{SuggestedAction{SuggestedAction::Type::SetPassword, DialogId(), days}}; - return get_update_suggested_actions_object(added_actions, {}); + return get_update_suggested_actions_object(added_actions, {}, "get_internal_option_update"); } } return nullptr; diff --git a/td/telegram/PasswordManager.cpp b/td/telegram/PasswordManager.cpp index e72f15fb5..3ccbdc54b 100644 --- a/td/telegram/PasswordManager.cpp +++ b/td/telegram/PasswordManager.cpp @@ -821,6 +821,7 @@ void PasswordManager::do_get_state(Promise promise) { dismiss_suggested_action(SuggestedAction{SuggestedAction::Type::SetPassword, DialogId(), days}, Promise()); } + dismiss_suggested_action(SuggestedAction{SuggestedAction::Type::SetPassword}, Promise()); } else { state.has_password = false; send_closure(actor_id, &PasswordManager::drop_cached_secret); diff --git a/td/telegram/SuggestedAction.cpp b/td/telegram/SuggestedAction.cpp index 630d59377..fe8de1b61 100644 --- a/td/telegram/SuggestedAction.cpp +++ b/td/telegram/SuggestedAction.cpp @@ -35,6 +35,8 @@ SuggestedAction::SuggestedAction(Slice action_str) { init(Type::CheckPhoneNumber); } else if (action_str == Slice("NEWCOMER_TICKS")) { init(Type::ViewChecksHint); + } else if (action_str == Slice("SETUP_PASSWORD")) { + init(Type::SetPassword); } } @@ -95,6 +97,8 @@ string SuggestedAction::get_suggested_action_str() const { return "NEWCOMER_TICKS"; case Type::ConvertToGigagroup: return "CONVERT_GIGAGROUP"; + case Type::SetPassword: + return "SETUP_PASSWORD"; default: return string(); } @@ -123,7 +127,8 @@ td_api::object_ptr SuggestedAction::get_suggested_actio } td_api::object_ptr get_update_suggested_actions_object( - const vector &added_actions, const vector &removed_actions) { + const vector &added_actions, const vector &removed_actions, const char *source) { + LOG(INFO) << "Get updateSuggestedActions from " << source; auto get_object = [](const SuggestedAction &action) { return action.get_suggested_action_object(); }; @@ -154,12 +159,14 @@ void update_suggested_actions(vector &suggested_actions, } CHECK(!added_actions.empty() || !removed_actions.empty()); suggested_actions = std::move(new_suggested_actions); - send_closure(G()->td(), &Td::send_update, get_update_suggested_actions_object(added_actions, removed_actions)); + send_closure(G()->td(), &Td::send_update, + get_update_suggested_actions_object(added_actions, removed_actions, "update_suggested_actions")); } void remove_suggested_action(vector &suggested_actions, SuggestedAction suggested_action) { if (td::remove(suggested_actions, suggested_action)) { - send_closure(G()->td(), &Td::send_update, get_update_suggested_actions_object({}, {suggested_action})); + send_closure(G()->td(), &Td::send_update, + get_update_suggested_actions_object({}, {suggested_action}, "remove_suggested_action")); } } @@ -177,13 +184,18 @@ void dismiss_suggested_action(SuggestedAction action, Promise &&promise) { return send_closure_later(G()->contacts_manager(), &ContactsManager::dismiss_dialog_suggested_action, std::move(action), std::move(promise)); case SuggestedAction::Type::SetPassword: { - if (action.otherwise_relogin_days_ <= 0) { + if (action.otherwise_relogin_days_ < 0) { return promise.set_error(Status::Error(400, "Invalid authorization_delay specified")); } + if (action.otherwise_relogin_days_ == 0) { + return send_closure_later(G()->config_manager(), &ConfigManager::dismiss_suggested_action, std::move(action), + std::move(promise)); + } auto days = narrow_cast(G()->get_option_integer("otherwise_relogin_days")); if (days == action.otherwise_relogin_days_) { vector removed_actions{SuggestedAction{SuggestedAction::Type::SetPassword, DialogId(), days}}; - send_closure(G()->td(), &Td::send_update, get_update_suggested_actions_object({}, removed_actions)); + send_closure(G()->td(), &Td::send_update, + get_update_suggested_actions_object({}, removed_actions, "dismiss_suggested_action")); G()->set_option_empty("otherwise_relogin_days"); } return promise.set_value(Unit()); diff --git a/td/telegram/SuggestedAction.h b/td/telegram/SuggestedAction.h index 957ed75e6..175351040 100644 --- a/td/telegram/SuggestedAction.h +++ b/td/telegram/SuggestedAction.h @@ -67,7 +67,7 @@ inline bool operator<(const SuggestedAction &lhs, const SuggestedAction &rhs) { } td_api::object_ptr get_update_suggested_actions_object( - const vector &added_actions, const vector &removed_actions); + const vector &added_actions, const vector &removed_actions, const char *source); void update_suggested_actions(vector &suggested_actions, vector &&new_suggested_actions);