From c5f84de9cd494e89525f8774d614cf700ab78944 Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 22 Feb 2021 23:34:10 +0300 Subject: [PATCH] Support channel suggested actions. --- td/telegram/ConfigManager.cpp | 2 +- td/telegram/ContactsManager.cpp | 38 +++++++++++++++++++++++++++++++-- td/telegram/SuggestedAction.cpp | 7 ++++-- td/telegram/SuggestedAction.h | 4 ++++ 4 files changed, 46 insertions(+), 5 deletions(-) diff --git a/td/telegram/ConfigManager.cpp b/td/telegram/ConfigManager.cpp index 05a3ab1f0..cdfee4540 100644 --- a/td/telegram/ConfigManager.cpp +++ b/td/telegram/ConfigManager.cpp @@ -1660,7 +1660,7 @@ void ConfigManager::process_app_config(tl_object_ptr &c if (action->get_id() == telegram_api::jsonString::ID) { Slice action_str = static_cast(action.get())->value_; SuggestedAction suggested_action(action_str); - if (suggested_action != SuggestedAction()) { + if (!suggested_action.is_empty()) { if (archive_and_mute && suggested_action == SuggestedAction{SuggestedAction::Type::EnableArchiveAndMuteNewChats}) { LOG(INFO) << "Skip EnableArchiveAndMuteNewChats suggested action"; diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index 909317793..c977d08b1 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -7485,11 +7485,11 @@ void ContactsManager::remove_dialog_suggested_action(SuggestedAction action) { } void ContactsManager::dismiss_suggested_action(SuggestedAction action, Promise &&promise) { - if (action == SuggestedAction()) { + if (action.is_empty()) { return promise.set_error(Status::Error(400, "Action must be non-empty")); } auto dialog_id = action.dialog_id_; - if (!dialog_id.is_valid()) { + if (dialog_id == DialogId()) { send_closure_later(G()->config_manager(), &ConfigManager::dismiss_suggested_action, std::move(action), std::move(promise)); return; @@ -9685,6 +9685,10 @@ void ContactsManager::update_channel(Channel *c, ChannelId channel_id, bool from } if (c->is_default_permissions_changed) { td_->messages_manager_->on_dialog_permissions_updated(DialogId(channel_id)); + if (c->default_permissions != + RestrictedRights(false, false, false, false, false, false, false, false, false, false, false)) { + remove_dialog_suggested_action(SuggestedAction{SuggestedAction::Type::ConvertToGigagroup, DialogId(channel_id)}); + } } if (!td_->auth_manager_->is_bot()) { if (c->restriction_reasons.empty()) { @@ -10466,6 +10470,32 @@ void ContactsManager::on_get_chat_full(tl_object_ptr &&c update_channel_full(linked_channel_full, linked_channel_id); } } + + if (dismiss_suggested_action_queries_.count(DialogId(channel_id)) == 0) { + auto it = dialog_suggested_actions_.find(DialogId(channel_id)); + if (it != dialog_suggested_actions_.end() || !channel->pending_suggestions_.empty()) { + vector suggested_actions; + for (auto &action_str : channel->pending_suggestions_) { + SuggestedAction suggested_action(action_str, DialogId(channel_id)); + if (!suggested_action.is_empty()) { + if (suggested_action == SuggestedAction{SuggestedAction::Type::ConvertToGigagroup, DialogId(channel_id)} && + (c->is_gigagroup || c->default_permissions != RestrictedRights(false, false, false, false, false, false, + false, false, false, false, false))) { + LOG(INFO) << "Skip ConvertToGigagroup suggested action"; + } else { + suggested_actions.push_back(suggested_action); + } + } + } + if (it == dialog_suggested_actions_.end()) { + it = dialog_suggested_actions_.emplace(DialogId(channel_id), vector()).first; + } + update_suggested_actions(it->second, std::move(suggested_actions)); + if (it->second.empty()) { + dialog_suggested_actions_.erase(it); + } + } + } } promise.set_value(Unit()); } @@ -12752,6 +12782,7 @@ void ContactsManager::on_channel_status_changed(Channel *c, ChannelId channel_id send_get_channel_full_query(nullptr, channel_id, Auto(), "update channel owner"); reload_dialog_administrators(DialogId(channel_id), 0, Auto()); + remove_dialog_suggested_action(SuggestedAction{SuggestedAction::Type::ConvertToGigagroup, DialogId(channel_id)}); } if (need_reload_group_call) { send_closure_later(G()->messages_manager(), &MessagesManager::on_update_dialog_group_call_rights, @@ -14997,6 +15028,9 @@ void ContactsManager::on_chat_update(telegram_api::channel &channel, const char is_slow_mode_enabled = false; is_gigagroup = false; } + if (is_gigagroup) { + remove_dialog_suggested_action(SuggestedAction{SuggestedAction::Type::ConvertToGigagroup, DialogId(channel_id)}); + } DialogParticipantStatus status = [&] { bool has_left = (channel.flags_ & CHANNEL_FLAG_USER_HAS_LEFT) != 0; diff --git a/td/telegram/SuggestedAction.cpp b/td/telegram/SuggestedAction.cpp index e7d980c8e..f04df3fa9 100644 --- a/td/telegram/SuggestedAction.cpp +++ b/td/telegram/SuggestedAction.cpp @@ -52,8 +52,11 @@ SuggestedAction::SuggestedAction(const td_api::object_ptr(suggested_action.get()); - type_ = Type::ConvertToGigagroup; - dialog_id_ = DialogId(ChannelId(action->supergroup_id_)); + ChannelId channel_id(action->supergroup_id_); + if (channel_id.is_valid()) { + type_ = Type::ConvertToGigagroup; + dialog_id_ = DialogId(channel_id); + } break; } default: diff --git a/td/telegram/SuggestedAction.h b/td/telegram/SuggestedAction.h index 774e024b1..bb8477923 100644 --- a/td/telegram/SuggestedAction.h +++ b/td/telegram/SuggestedAction.h @@ -32,6 +32,10 @@ struct SuggestedAction { explicit SuggestedAction(const td_api::object_ptr &suggested_action); + bool is_empty() const { + return type_ == Type::Empty; + } + string get_suggested_action_str() const; td_api::object_ptr get_suggested_action_object() const;