2020-07-11 22:50:21 +02:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2020-07-11 22:50:21 +02:00
|
|
|
//
|
|
|
|
// 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)
|
|
|
|
//
|
|
|
|
#include "td/telegram/SuggestedAction.h"
|
|
|
|
|
2021-02-22 16:06:18 +01:00
|
|
|
#include "td/telegram/ChannelId.h"
|
2021-11-26 12:48:43 +01:00
|
|
|
#include "td/telegram/ConfigManager.h"
|
|
|
|
#include "td/telegram/ContactsManager.h"
|
2021-02-22 16:25:49 +01:00
|
|
|
#include "td/telegram/Global.h"
|
|
|
|
#include "td/telegram/Td.h"
|
2021-02-22 16:06:18 +01:00
|
|
|
|
2021-02-25 13:56:26 +01:00
|
|
|
#include "td/actor/actor.h"
|
|
|
|
|
2021-02-20 22:21:14 +01:00
|
|
|
#include "td/utils/algorithm.h"
|
2023-03-06 18:36:56 +01:00
|
|
|
#include "td/utils/logging.h"
|
2021-12-09 22:07:59 +01:00
|
|
|
#include "td/utils/misc.h"
|
|
|
|
#include "td/utils/Status.h"
|
2021-02-20 22:21:14 +01:00
|
|
|
|
2021-02-22 16:25:49 +01:00
|
|
|
#include <algorithm>
|
|
|
|
|
2020-07-11 22:50:21 +02:00
|
|
|
namespace td {
|
|
|
|
|
2021-02-20 23:06:45 +01:00
|
|
|
void SuggestedAction::init(Type type) {
|
|
|
|
type_ = type;
|
2020-07-11 22:50:21 +02:00
|
|
|
}
|
|
|
|
|
2021-02-20 23:06:45 +01:00
|
|
|
SuggestedAction::SuggestedAction(Slice action_str) {
|
|
|
|
if (action_str == Slice("AUTOARCHIVE_POPULAR")) {
|
|
|
|
init(Type::EnableArchiveAndMuteNewChats);
|
2021-06-11 01:48:10 +02:00
|
|
|
} else if (action_str == Slice("VALIDATE_PASSWORD")) {
|
|
|
|
init(Type::CheckPassword);
|
2021-06-11 01:13:46 +02:00
|
|
|
} else if (action_str == Slice("VALIDATE_PHONE_NUMBER")) {
|
|
|
|
init(Type::CheckPhoneNumber);
|
2021-02-20 23:06:45 +01:00
|
|
|
} else if (action_str == Slice("NEWCOMER_TICKS")) {
|
2021-12-23 18:00:41 +01:00
|
|
|
init(Type::ViewChecksHint);
|
2023-01-17 11:22:21 +01:00
|
|
|
} else if (action_str == Slice("SETUP_PASSWORD")) {
|
|
|
|
init(Type::SetPassword);
|
2023-01-23 14:03:32 +01:00
|
|
|
} else if (action_str == Slice("PREMIUM_UPGRADE")) {
|
|
|
|
init(Type::UpgradePremium);
|
2023-01-23 14:11:26 +01:00
|
|
|
} else if (action_str == Slice("PREMIUM_ANNUAL")) {
|
|
|
|
init(Type::SubscribeToAnnualPremium);
|
2020-07-11 22:50:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-22 16:06:18 +01:00
|
|
|
SuggestedAction::SuggestedAction(Slice action_str, DialogId dialog_id) {
|
|
|
|
CHECK(dialog_id.is_valid());
|
|
|
|
if (action_str == Slice("CONVERT_GIGAGROUP")) {
|
|
|
|
type_ = Type::ConvertToGigagroup;
|
|
|
|
dialog_id_ = dialog_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SuggestedAction::SuggestedAction(const td_api::object_ptr<td_api::SuggestedAction> &suggested_action) {
|
|
|
|
if (suggested_action == nullptr) {
|
2021-02-20 23:06:45 +01:00
|
|
|
return;
|
2020-07-11 22:50:21 +02:00
|
|
|
}
|
2021-02-22 16:06:18 +01:00
|
|
|
switch (suggested_action->get_id()) {
|
2020-07-11 22:50:21 +02:00
|
|
|
case td_api::suggestedActionEnableArchiveAndMuteNewChats::ID:
|
2021-02-20 23:06:45 +01:00
|
|
|
init(Type::EnableArchiveAndMuteNewChats);
|
|
|
|
break;
|
2021-06-11 01:48:10 +02:00
|
|
|
case td_api::suggestedActionCheckPassword::ID:
|
|
|
|
init(Type::CheckPassword);
|
|
|
|
break;
|
2020-07-11 22:50:21 +02:00
|
|
|
case td_api::suggestedActionCheckPhoneNumber::ID:
|
2021-02-20 23:06:45 +01:00
|
|
|
init(Type::CheckPhoneNumber);
|
|
|
|
break;
|
2021-12-23 18:00:41 +01:00
|
|
|
case td_api::suggestedActionViewChecksHint::ID:
|
|
|
|
init(Type::ViewChecksHint);
|
2021-02-20 23:06:45 +01:00
|
|
|
break;
|
2021-02-22 16:06:18 +01:00
|
|
|
case td_api::suggestedActionConvertToBroadcastGroup::ID: {
|
|
|
|
auto action = static_cast<const td_api::suggestedActionConvertToBroadcastGroup *>(suggested_action.get());
|
2021-02-22 21:34:10 +01:00
|
|
|
ChannelId channel_id(action->supergroup_id_);
|
|
|
|
if (channel_id.is_valid()) {
|
|
|
|
type_ = Type::ConvertToGigagroup;
|
|
|
|
dialog_id_ = DialogId(channel_id);
|
|
|
|
}
|
2021-02-22 16:06:18 +01:00
|
|
|
break;
|
|
|
|
}
|
2021-11-26 14:22:39 +01:00
|
|
|
case td_api::suggestedActionSetPassword::ID: {
|
|
|
|
auto action = static_cast<const td_api::suggestedActionSetPassword *>(suggested_action.get());
|
|
|
|
type_ = Type::SetPassword;
|
|
|
|
otherwise_relogin_days_ = action->authorization_delay_;
|
|
|
|
break;
|
|
|
|
}
|
2023-01-23 14:03:32 +01:00
|
|
|
case td_api::suggestedActionUpgradePremium::ID:
|
|
|
|
init(Type::UpgradePremium);
|
|
|
|
break;
|
2023-01-23 14:11:26 +01:00
|
|
|
case td_api::suggestedActionSubscribeToAnnualPremium::ID:
|
|
|
|
init(Type::SubscribeToAnnualPremium);
|
|
|
|
break;
|
2020-07-11 22:50:21 +02:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-20 23:06:45 +01:00
|
|
|
string SuggestedAction::get_suggested_action_str() const {
|
|
|
|
switch (type_) {
|
|
|
|
case Type::EnableArchiveAndMuteNewChats:
|
|
|
|
return "AUTOARCHIVE_POPULAR";
|
2021-06-11 01:48:10 +02:00
|
|
|
case Type::CheckPassword:
|
|
|
|
return "VALIDATE_PASSWORD";
|
2021-06-11 01:13:46 +02:00
|
|
|
case Type::CheckPhoneNumber:
|
|
|
|
return "VALIDATE_PHONE_NUMBER";
|
2021-12-23 18:00:41 +01:00
|
|
|
case Type::ViewChecksHint:
|
2021-02-20 23:06:45 +01:00
|
|
|
return "NEWCOMER_TICKS";
|
2021-02-22 16:06:18 +01:00
|
|
|
case Type::ConvertToGigagroup:
|
|
|
|
return "CONVERT_GIGAGROUP";
|
2023-01-17 11:22:21 +01:00
|
|
|
case Type::SetPassword:
|
|
|
|
return "SETUP_PASSWORD";
|
2023-01-23 14:03:32 +01:00
|
|
|
case Type::UpgradePremium:
|
|
|
|
return "PREMIUM_UPGRADE";
|
2023-01-23 14:11:26 +01:00
|
|
|
case Type::SubscribeToAnnualPremium:
|
|
|
|
return "PREMIUM_ANNUAL";
|
2021-02-20 23:06:45 +01:00
|
|
|
default:
|
|
|
|
return string();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
td_api::object_ptr<td_api::SuggestedAction> SuggestedAction::get_suggested_action_object() const {
|
|
|
|
switch (type_) {
|
|
|
|
case Type::Empty:
|
2020-07-11 22:50:21 +02:00
|
|
|
return nullptr;
|
2021-02-20 23:06:45 +01:00
|
|
|
case Type::EnableArchiveAndMuteNewChats:
|
2020-07-11 22:50:21 +02:00
|
|
|
return td_api::make_object<td_api::suggestedActionEnableArchiveAndMuteNewChats>();
|
2021-06-11 01:48:10 +02:00
|
|
|
case Type::CheckPassword:
|
|
|
|
return td_api::make_object<td_api::suggestedActionCheckPassword>();
|
2021-02-20 23:06:45 +01:00
|
|
|
case Type::CheckPhoneNumber:
|
2020-07-11 22:50:21 +02:00
|
|
|
return td_api::make_object<td_api::suggestedActionCheckPhoneNumber>();
|
2021-12-23 18:00:41 +01:00
|
|
|
case Type::ViewChecksHint:
|
|
|
|
return td_api::make_object<td_api::suggestedActionViewChecksHint>();
|
2021-02-22 16:06:18 +01:00
|
|
|
case Type::ConvertToGigagroup:
|
|
|
|
return td_api::make_object<td_api::suggestedActionConvertToBroadcastGroup>(dialog_id_.get_channel_id().get());
|
2021-11-26 14:22:39 +01:00
|
|
|
case Type::SetPassword:
|
|
|
|
return td_api::make_object<td_api::suggestedActionSetPassword>(otherwise_relogin_days_);
|
2023-01-23 14:03:32 +01:00
|
|
|
case Type::UpgradePremium:
|
|
|
|
return td_api::make_object<td_api::suggestedActionUpgradePremium>();
|
2023-01-23 14:11:26 +01:00
|
|
|
case Type::SubscribeToAnnualPremium:
|
|
|
|
return td_api::make_object<td_api::suggestedActionSubscribeToAnnualPremium>();
|
2020-07-11 22:50:21 +02:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-20 22:21:14 +01:00
|
|
|
td_api::object_ptr<td_api::updateSuggestedActions> get_update_suggested_actions_object(
|
2023-01-17 11:22:21 +01:00
|
|
|
const vector<SuggestedAction> &added_actions, const vector<SuggestedAction> &removed_actions, const char *source) {
|
|
|
|
LOG(INFO) << "Get updateSuggestedActions from " << source;
|
2021-02-20 23:06:45 +01:00
|
|
|
auto get_object = [](const SuggestedAction &action) {
|
|
|
|
return action.get_suggested_action_object();
|
|
|
|
};
|
|
|
|
return td_api::make_object<td_api::updateSuggestedActions>(transform(added_actions, get_object),
|
|
|
|
transform(removed_actions, get_object));
|
2021-02-20 22:21:14 +01:00
|
|
|
}
|
|
|
|
|
2021-02-22 16:25:49 +01:00
|
|
|
void update_suggested_actions(vector<SuggestedAction> &suggested_actions,
|
|
|
|
vector<SuggestedAction> &&new_suggested_actions) {
|
|
|
|
td::unique(new_suggested_actions);
|
|
|
|
if (new_suggested_actions == suggested_actions) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<SuggestedAction> added_actions;
|
|
|
|
vector<SuggestedAction> removed_actions;
|
|
|
|
auto old_it = suggested_actions.begin();
|
|
|
|
auto new_it = new_suggested_actions.begin();
|
|
|
|
while (old_it != suggested_actions.end() || new_it != new_suggested_actions.end()) {
|
|
|
|
if (old_it != suggested_actions.end() && (new_it == new_suggested_actions.end() || *old_it < *new_it)) {
|
|
|
|
removed_actions.push_back(*old_it++);
|
|
|
|
} else if (old_it == suggested_actions.end() || *new_it < *old_it) {
|
|
|
|
added_actions.push_back(*new_it++);
|
|
|
|
} else {
|
2021-08-04 02:43:48 +02:00
|
|
|
++old_it;
|
|
|
|
++new_it;
|
2021-02-22 16:25:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
CHECK(!added_actions.empty() || !removed_actions.empty());
|
|
|
|
suggested_actions = std::move(new_suggested_actions);
|
2023-01-17 11:22:21 +01:00
|
|
|
send_closure(G()->td(), &Td::send_update,
|
|
|
|
get_update_suggested_actions_object(added_actions, removed_actions, "update_suggested_actions"));
|
2021-02-22 16:25:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void remove_suggested_action(vector<SuggestedAction> &suggested_actions, SuggestedAction suggested_action) {
|
|
|
|
if (td::remove(suggested_actions, suggested_action)) {
|
2023-01-17 11:22:21 +01:00
|
|
|
send_closure(G()->td(), &Td::send_update,
|
|
|
|
get_update_suggested_actions_object({}, {suggested_action}, "remove_suggested_action"));
|
2021-02-22 16:25:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-26 12:48:43 +01:00
|
|
|
void dismiss_suggested_action(SuggestedAction action, Promise<Unit> &&promise) {
|
|
|
|
switch (action.type_) {
|
|
|
|
case SuggestedAction::Type::Empty:
|
|
|
|
return promise.set_error(Status::Error(400, "Action must be non-empty"));
|
|
|
|
case SuggestedAction::Type::EnableArchiveAndMuteNewChats:
|
|
|
|
case SuggestedAction::Type::CheckPassword:
|
|
|
|
case SuggestedAction::Type::CheckPhoneNumber:
|
2021-12-23 18:00:41 +01:00
|
|
|
case SuggestedAction::Type::ViewChecksHint:
|
2023-01-23 14:03:32 +01:00
|
|
|
case SuggestedAction::Type::UpgradePremium:
|
2023-01-23 14:11:26 +01:00
|
|
|
case SuggestedAction::Type::SubscribeToAnnualPremium:
|
2021-11-26 12:48:43 +01:00
|
|
|
return send_closure_later(G()->config_manager(), &ConfigManager::dismiss_suggested_action, std::move(action),
|
|
|
|
std::move(promise));
|
|
|
|
case SuggestedAction::Type::ConvertToGigagroup:
|
|
|
|
return send_closure_later(G()->contacts_manager(), &ContactsManager::dismiss_dialog_suggested_action,
|
|
|
|
std::move(action), std::move(promise));
|
2021-11-26 14:22:39 +01:00
|
|
|
case SuggestedAction::Type::SetPassword: {
|
2023-01-17 11:22:21 +01:00
|
|
|
if (action.otherwise_relogin_days_ < 0) {
|
2021-11-26 14:22:39 +01:00
|
|
|
return promise.set_error(Status::Error(400, "Invalid authorization_delay specified"));
|
|
|
|
}
|
2023-01-17 11:22:21 +01:00
|
|
|
if (action.otherwise_relogin_days_ == 0) {
|
|
|
|
return send_closure_later(G()->config_manager(), &ConfigManager::dismiss_suggested_action, std::move(action),
|
|
|
|
std::move(promise));
|
|
|
|
}
|
2022-08-17 15:11:13 +02:00
|
|
|
auto days = narrow_cast<int32>(G()->get_option_integer("otherwise_relogin_days"));
|
2021-11-26 14:22:39 +01:00
|
|
|
if (days == action.otherwise_relogin_days_) {
|
|
|
|
vector<SuggestedAction> removed_actions{SuggestedAction{SuggestedAction::Type::SetPassword, DialogId(), days}};
|
2023-01-17 11:22:21 +01:00
|
|
|
send_closure(G()->td(), &Td::send_update,
|
|
|
|
get_update_suggested_actions_object({}, removed_actions, "dismiss_suggested_action"));
|
2022-08-17 15:11:13 +02:00
|
|
|
G()->set_option_empty("otherwise_relogin_days");
|
2021-11-26 14:22:39 +01:00
|
|
|
}
|
|
|
|
return promise.set_value(Unit());
|
|
|
|
}
|
2021-11-26 12:48:43 +01:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-11 22:50:21 +02:00
|
|
|
} // namespace td
|