Move chat suggested actions to DialogManager.

This commit is contained in:
levlam 2024-03-23 23:15:04 +03:00
parent 4227491ef4
commit 07a29ef56b
5 changed files with 155 additions and 140 deletions

View File

@ -59,6 +59,7 @@
#include "td/telegram/StickerPhotoSize.h" #include "td/telegram/StickerPhotoSize.h"
#include "td/telegram/StickersManager.h" #include "td/telegram/StickersManager.h"
#include "td/telegram/StoryManager.h" #include "td/telegram/StoryManager.h"
#include "td/telegram/SuggestedAction.h"
#include "td/telegram/Td.h" #include "td/telegram/Td.h"
#include "td/telegram/TdDb.h" #include "td/telegram/TdDb.h"
#include "td/telegram/telegram_api.h" #include "td/telegram/telegram_api.h"
@ -96,38 +97,6 @@
namespace td { namespace td {
class DismissSuggestionQuery final : public Td::ResultHandler {
Promise<Unit> promise_;
DialogId dialog_id_;
public:
explicit DismissSuggestionQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
}
void send(SuggestedAction action) {
dialog_id_ = action.dialog_id_;
auto input_peer = td_->dialog_manager_->get_input_peer(dialog_id_, AccessRights::Read);
CHECK(input_peer != nullptr);
send_query(G()->net_query_creator().create(
telegram_api::help_dismissSuggestion(std::move(input_peer), action.get_suggested_action_str())));
}
void on_result(BufferSlice packet) final {
auto result_ptr = fetch_result<telegram_api::help_dismissSuggestion>(packet);
if (result_ptr.is_error()) {
return on_error(result_ptr.move_as_error());
}
promise_.set_value(Unit());
}
void on_error(Status status) final {
td_->dialog_manager_->on_get_dialog_error(dialog_id_, status, "DismissSuggestionQuery");
promise_.set_error(std::move(status));
}
};
class GetContactsQuery final : public Td::ResultHandler { class GetContactsQuery final : public Td::ResultHandler {
public: public:
void send(int64 hash) { void send(int64 hash) {
@ -6787,18 +6756,12 @@ void ContactsManager::toggle_channel_is_forum(ChannelId channel_id, bool is_foru
} }
void ContactsManager::convert_channel_to_gigagroup(ChannelId channel_id, Promise<Unit> &&promise) { void ContactsManager::convert_channel_to_gigagroup(ChannelId channel_id, Promise<Unit> &&promise) {
auto c = get_channel(channel_id); if (!can_convert_channel_to_gigagroup(channel_id)) {
if (c == nullptr) { return promise.set_error(Status::Error(400, "Can't convert the chat to a broadcast group"));
return promise.set_error(Status::Error(400, "Supergroup not found"));
}
if (!get_channel_status(c).is_creator()) {
return promise.set_error(Status::Error(400, "Not enough rights to convert group to broadcast group"));
}
if (get_channel_type(c) != ChannelType::Megagroup) {
return promise.set_error(Status::Error(400, "Chat must be a supergroup"));
} }
remove_dialog_suggested_action(SuggestedAction{SuggestedAction::Type::ConvertToGigagroup, DialogId(channel_id)}); td_->dialog_manager_->remove_dialog_suggested_action(
SuggestedAction{SuggestedAction::Type::ConvertToGigagroup, DialogId(channel_id)});
td_->create_handler<ConvertToGigagroupQuery>(std::move(promise))->send(channel_id); td_->create_handler<ConvertToGigagroupQuery>(std::move(promise))->send(channel_id);
} }
@ -7019,6 +6982,15 @@ bool ContactsManager::can_get_channel_story_statistics(ChannelId channel_id) con
return c->status.can_post_messages(); return c->status.can_post_messages();
} }
bool ContactsManager::can_convert_channel_to_gigagroup(ChannelId channel_id) const {
const Channel *c = get_channel(channel_id);
return c == nullptr || get_channel_type(c) != ChannelType::Megagroup || !get_channel_status(c).is_creator() ||
c->is_gigagroup ||
c->default_permissions != RestrictedRights(false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false,
ChannelType::Unknown);
}
void ContactsManager::report_channel_spam(ChannelId channel_id, const vector<MessageId> &message_ids, void ContactsManager::report_channel_spam(ChannelId channel_id, const vector<MessageId> &message_ids,
Promise<Unit> &&promise) { Promise<Unit> &&promise) {
auto c = get_channel(channel_id); auto c = get_channel(channel_id);
@ -7494,62 +7466,6 @@ void ContactsManager::unregister_message_channels(MessageFullId message_full_id,
} }
} }
void ContactsManager::remove_dialog_suggested_action(SuggestedAction action) {
auto it = dialog_suggested_actions_.find(action.dialog_id_);
if (it == dialog_suggested_actions_.end()) {
return;
}
remove_suggested_action(it->second, action);
if (it->second.empty()) {
dialog_suggested_actions_.erase(it);
}
}
void ContactsManager::dismiss_dialog_suggested_action(SuggestedAction action, Promise<Unit> &&promise) {
auto dialog_id = action.dialog_id_;
if (!td_->messages_manager_->have_dialog(dialog_id)) {
return promise.set_error(Status::Error(400, "Chat not found"));
}
if (!td_->dialog_manager_->have_input_peer(dialog_id, AccessRights::Read)) {
return promise.set_error(Status::Error(400, "Can't access the chat"));
}
auto it = dialog_suggested_actions_.find(dialog_id);
if (it == dialog_suggested_actions_.end() || !td::contains(it->second, action)) {
return promise.set_value(Unit());
}
auto action_str = action.get_suggested_action_str();
if (action_str.empty()) {
return promise.set_value(Unit());
}
auto &queries = dismiss_suggested_action_queries_[dialog_id];
queries.push_back(std::move(promise));
if (queries.size() == 1) {
auto query_promise = PromiseCreator::lambda([actor_id = actor_id(this), action](Result<Unit> &&result) {
send_closure(actor_id, &ContactsManager::on_dismiss_suggested_action, action, std::move(result));
});
td_->create_handler<DismissSuggestionQuery>(std::move(query_promise))->send(std::move(action));
}
}
void ContactsManager::on_dismiss_suggested_action(SuggestedAction action, Result<Unit> &&result) {
auto it = dismiss_suggested_action_queries_.find(action.dialog_id_);
CHECK(it != dismiss_suggested_action_queries_.end());
auto promises = std::move(it->second);
dismiss_suggested_action_queries_.erase(it);
if (result.is_error()) {
fail_promises(promises, result.move_as_error());
return;
}
remove_dialog_suggested_action(action);
set_promises(promises);
}
void ContactsManager::on_import_contacts_finished(int64 random_id, vector<UserId> imported_contact_user_ids, void ContactsManager::on_import_contacts_finished(int64 random_id, vector<UserId> imported_contact_user_ids,
vector<int32> unimported_contact_invites) { vector<int32> unimported_contact_invites) {
LOG(INFO) << "Contacts import with random_id " << random_id LOG(INFO) << "Contacts import with random_id " << random_id
@ -9967,7 +9883,8 @@ void ContactsManager::update_channel(Channel *c, ChannelId channel_id, bool from
if (c->default_permissions != RestrictedRights(false, false, false, false, false, false, false, false, false, false, if (c->default_permissions != RestrictedRights(false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false, false, false, false, false,
ChannelType::Unknown)) { ChannelType::Unknown)) {
remove_dialog_suggested_action(SuggestedAction{SuggestedAction::Type::ConvertToGigagroup, DialogId(channel_id)}); td_->dialog_manager_->remove_dialog_suggested_action(
SuggestedAction{SuggestedAction::Type::ConvertToGigagroup, DialogId(channel_id)});
} }
c->is_default_permissions_changed = false; c->is_default_permissions_changed = false;
} }
@ -11067,33 +10984,8 @@ void ContactsManager::on_get_chat_full(tl_object_ptr<telegram_api::ChatFull> &&c
} }
} }
if (dismiss_suggested_action_queries_.count(DialogId(channel_id)) == 0) { td_->dialog_manager_->set_dialog_pending_suggestions(DialogId(channel_id),
auto it = dialog_suggested_actions_.find(DialogId(channel_id)); std::move(channel->pending_suggestions_));
if (it != dialog_suggested_actions_.end() || !channel->pending_suggestions_.empty()) {
vector<SuggestedAction> 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, false, false, false, false, false,
false, ChannelType::Unknown))) {
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<SuggestedAction>()).first;
}
update_suggested_actions(it->second, std::move(suggested_actions));
if (it->second.empty()) {
dialog_suggested_actions_.erase(it);
}
}
}
} }
promise.set_value(Unit()); promise.set_value(Unit());
} }
@ -13619,7 +13511,8 @@ void ContactsManager::on_channel_status_changed(Channel *c, ChannelId channel_id
send_get_channel_full_query(nullptr, channel_id, Auto(), "update channel owner"); send_get_channel_full_query(nullptr, channel_id, Auto(), "update channel owner");
td_->dialog_participant_manager_->reload_dialog_administrators(DialogId(channel_id), {}, Auto()); td_->dialog_participant_manager_->reload_dialog_administrators(DialogId(channel_id), {}, Auto());
remove_dialog_suggested_action(SuggestedAction{SuggestedAction::Type::ConvertToGigagroup, DialogId(channel_id)}); td_->dialog_manager_->remove_dialog_suggested_action(
SuggestedAction{SuggestedAction::Type::ConvertToGigagroup, DialogId(channel_id)});
} }
if (old_status.is_member() != new_status.is_member() || new_status.is_banned()) { if (old_status.is_member() != new_status.is_member() || new_status.is_banned()) {
@ -15647,7 +15540,8 @@ void ContactsManager::on_get_channel(telegram_api::channel &channel, const char
is_forum = false; is_forum = false;
} }
if (is_gigagroup) { if (is_gigagroup) {
remove_dialog_suggested_action(SuggestedAction{SuggestedAction::Type::ConvertToGigagroup, DialogId(channel_id)}); td_->dialog_manager_->remove_dialog_suggested_action(
SuggestedAction{SuggestedAction::Type::ConvertToGigagroup, DialogId(channel_id)});
} }
DialogParticipantStatus status = [&] { DialogParticipantStatus status = [&] {

View File

@ -36,7 +36,6 @@
#include "td/telegram/SecretChatId.h" #include "td/telegram/SecretChatId.h"
#include "td/telegram/StickerSetId.h" #include "td/telegram/StickerSetId.h"
#include "td/telegram/StoryId.h" #include "td/telegram/StoryId.h"
#include "td/telegram/SuggestedAction.h"
#include "td/telegram/td_api.h" #include "td/telegram/td_api.h"
#include "td/telegram/telegram_api.h" #include "td/telegram/telegram_api.h"
#include "td/telegram/UserId.h" #include "td/telegram/UserId.h"
@ -526,6 +525,8 @@ class ContactsManager final : public Actor {
bool can_get_channel_story_statistics(ChannelId channel_id) const; bool can_get_channel_story_statistics(ChannelId channel_id) const;
bool can_convert_channel_to_gigagroup(ChannelId channel_id) const;
void get_created_public_dialogs(PublicDialogType type, Promise<td_api::object_ptr<td_api::chats>> &&promise, void get_created_public_dialogs(PublicDialogType type, Promise<td_api::object_ptr<td_api::chats>> &&promise,
bool from_binlog); bool from_binlog);
@ -537,8 +538,6 @@ class ContactsManager final : public Actor {
vector<DialogId> get_inactive_channels(Promise<Unit> &&promise); vector<DialogId> get_inactive_channels(Promise<Unit> &&promise);
void dismiss_dialog_suggested_action(SuggestedAction action, Promise<Unit> &&promise);
bool is_user_contact(UserId user_id, bool is_mutual = false) const; bool is_user_contact(UserId user_id, bool is_mutual = false) const;
bool is_user_premium(UserId user_id) const; bool is_user_premium(UserId user_id) const;
@ -1624,10 +1623,6 @@ class ContactsManager final : public Actor {
void finish_get_chat_participant(ChatId chat_id, UserId user_id, Promise<DialogParticipant> &&promise); void finish_get_chat_participant(ChatId chat_id, UserId user_id, Promise<DialogParticipant> &&promise);
void remove_dialog_suggested_action(SuggestedAction action);
void on_dismiss_suggested_action(SuggestedAction action, Result<Unit> &&result);
bool need_poll_user_active_stories(const User *u, UserId user_id) const; bool need_poll_user_active_stories(const User *u, UserId user_id) const;
static bool get_user_has_unread_stories(const User *u); static bool get_user_has_unread_stories(const User *u);
@ -1791,9 +1786,6 @@ class ContactsManager final : public Actor {
QueryCombiner get_user_full_queries_{"GetUserFullCombiner", 2.0}; QueryCombiner get_user_full_queries_{"GetUserFullCombiner", 2.0};
QueryCombiner get_chat_full_queries_{"GetChatFullCombiner", 2.0}; QueryCombiner get_chat_full_queries_{"GetChatFullCombiner", 2.0};
FlatHashMap<DialogId, vector<SuggestedAction>, DialogIdHash> dialog_suggested_actions_;
FlatHashMap<DialogId, vector<Promise<Unit>>, DialogIdHash> dismiss_suggested_action_queries_;
class UploadProfilePhotoCallback; class UploadProfilePhotoCallback;
std::shared_ptr<UploadProfilePhotoCallback> upload_profile_photo_callback_; std::shared_ptr<UploadProfilePhotoCallback> upload_profile_photo_callback_;

View File

@ -134,6 +134,38 @@ class ResolveUsernameQuery final : public Td::ResultHandler {
} }
}; };
class DismissSuggestionQuery final : public Td::ResultHandler {
Promise<Unit> promise_;
DialogId dialog_id_;
public:
explicit DismissSuggestionQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
}
void send(SuggestedAction action) {
dialog_id_ = action.dialog_id_;
auto input_peer = td_->dialog_manager_->get_input_peer(dialog_id_, AccessRights::Read);
CHECK(input_peer != nullptr);
send_query(G()->net_query_creator().create(
telegram_api::help_dismissSuggestion(std::move(input_peer), action.get_suggested_action_str())));
}
void on_result(BufferSlice packet) final {
auto result_ptr = fetch_result<telegram_api::help_dismissSuggestion>(packet);
if (result_ptr.is_error()) {
return on_error(result_ptr.move_as_error());
}
promise_.set_value(Unit());
}
void on_error(Status status) final {
td_->dialog_manager_->on_get_dialog_error(dialog_id_, status, "DismissSuggestionQuery");
promise_.set_error(std::move(status));
}
};
class MigrateChatQuery final : public Td::ResultHandler { class MigrateChatQuery final : public Td::ResultHandler {
Promise<Unit> promise_; Promise<Unit> promise_;
@ -2232,4 +2264,89 @@ void DialogManager::drop_username(const string &username) {
} }
} }
void DialogManager::set_dialog_pending_suggestions(DialogId dialog_id, vector<string> &&pending_suggestions) {
if (dismiss_suggested_action_queries_.count(dialog_id) != 0) {
return;
}
auto it = dialog_suggested_actions_.find(dialog_id);
if (it == dialog_suggested_actions_.end() && !pending_suggestions.empty()) {
return;
}
vector<SuggestedAction> suggested_actions;
for (auto &action_str : pending_suggestions) {
SuggestedAction suggested_action(action_str, dialog_id);
if (!suggested_action.is_empty()) {
if (suggested_action == SuggestedAction{SuggestedAction::Type::ConvertToGigagroup, dialog_id} &&
(dialog_id.get_type() != DialogType::Channel ||
!td_->contacts_manager_->can_convert_channel_to_gigagroup(dialog_id.get_channel_id()))) {
LOG(INFO) << "Skip ConvertToGigagroup suggested action";
} else {
suggested_actions.push_back(suggested_action);
}
}
}
if (it == dialog_suggested_actions_.end()) {
it = dialog_suggested_actions_.emplace(dialog_id, vector<SuggestedAction>()).first;
}
update_suggested_actions(it->second, std::move(suggested_actions));
if (it->second.empty()) {
dialog_suggested_actions_.erase(it);
}
}
void DialogManager::remove_dialog_suggested_action(SuggestedAction action) {
auto it = dialog_suggested_actions_.find(action.dialog_id_);
if (it == dialog_suggested_actions_.end()) {
return;
}
remove_suggested_action(it->second, action);
if (it->second.empty()) {
dialog_suggested_actions_.erase(it);
}
}
void DialogManager::dismiss_dialog_suggested_action(SuggestedAction action, Promise<Unit> &&promise) {
auto dialog_id = action.dialog_id_;
if (!td_->messages_manager_->have_dialog(dialog_id)) {
return promise.set_error(Status::Error(400, "Chat not found"));
}
if (!have_input_peer(dialog_id, AccessRights::Read)) {
return promise.set_error(Status::Error(400, "Can't access the chat"));
}
auto it = dialog_suggested_actions_.find(dialog_id);
if (it == dialog_suggested_actions_.end() || !td::contains(it->second, action)) {
return promise.set_value(Unit());
}
auto action_str = action.get_suggested_action_str();
if (action_str.empty()) {
return promise.set_value(Unit());
}
auto &queries = dismiss_suggested_action_queries_[dialog_id];
queries.push_back(std::move(promise));
if (queries.size() == 1) {
auto query_promise = PromiseCreator::lambda([actor_id = actor_id(this), action](Result<Unit> &&result) {
send_closure(actor_id, &DialogManager::on_dismiss_suggested_action, action, std::move(result));
});
td_->create_handler<DismissSuggestionQuery>(std::move(query_promise))->send(std::move(action));
}
}
void DialogManager::on_dismiss_suggested_action(SuggestedAction action, Result<Unit> &&result) {
auto it = dismiss_suggested_action_queries_.find(action.dialog_id_);
CHECK(it != dismiss_suggested_action_queries_.end());
auto promises = std::move(it->second);
dismiss_suggested_action_queries_.erase(it);
if (result.is_error()) {
return fail_promises(promises, result.move_as_error());
}
remove_dialog_suggested_action(action);
set_promises(promises);
}
} // namespace td } // namespace td

View File

@ -20,6 +20,7 @@
#include "td/telegram/MessageId.h" #include "td/telegram/MessageId.h"
#include "td/telegram/NotificationSettingsScope.h" #include "td/telegram/NotificationSettingsScope.h"
#include "td/telegram/Photo.h" #include "td/telegram/Photo.h"
#include "td/telegram/SuggestedAction.h"
#include "td/telegram/td_api.h" #include "td/telegram/td_api.h"
#include "td/telegram/telegram_api.h" #include "td/telegram/telegram_api.h"
#include "td/telegram/UserId.h" #include "td/telegram/UserId.h"
@ -215,6 +216,12 @@ class DialogManager final : public Actor {
void reload_voice_chat_on_search(const string &username); void reload_voice_chat_on_search(const string &username);
void set_dialog_pending_suggestions(DialogId dialog_id, vector<string> &&pending_suggestions);
void dismiss_dialog_suggested_action(SuggestedAction action, Promise<Unit> &&promise);
void remove_dialog_suggested_action(SuggestedAction action);
private: private:
static constexpr size_t MAX_TITLE_LENGTH = 128; // server side limit for chat title static constexpr size_t MAX_TITLE_LENGTH = 128; // server side limit for chat title
@ -240,6 +247,8 @@ class DialogManager final : public Actor {
void on_resolve_dialog(const string &username, ChannelId channel_id, Promise<DialogId> &&promise); void on_resolve_dialog(const string &username, ChannelId channel_id, Promise<DialogId> &&promise);
void on_dismiss_suggested_action(SuggestedAction action, Result<Unit> &&result);
class UploadDialogPhotoCallback; class UploadDialogPhotoCallback;
std::shared_ptr<UploadDialogPhotoCallback> upload_dialog_photo_callback_; std::shared_ptr<UploadDialogPhotoCallback> upload_dialog_photo_callback_;
@ -275,6 +284,9 @@ class DialogManager final : public Actor {
FlatHashMap<string, vector<Promise<Unit>>> resolve_dialog_username_queries_; FlatHashMap<string, vector<Promise<Unit>>> resolve_dialog_username_queries_;
FlatHashMap<DialogId, vector<SuggestedAction>, DialogIdHash> dialog_suggested_actions_;
FlatHashMap<DialogId, vector<Promise<Unit>>, DialogIdHash> dismiss_suggested_action_queries_;
Td *td_; Td *td_;
ActorShared<> parent_; ActorShared<> parent_;
}; };

View File

@ -8,7 +8,7 @@
#include "td/telegram/ChannelId.h" #include "td/telegram/ChannelId.h"
#include "td/telegram/ConfigManager.h" #include "td/telegram/ConfigManager.h"
#include "td/telegram/ContactsManager.h" #include "td/telegram/DialogManager.h"
#include "td/telegram/Global.h" #include "td/telegram/Global.h"
#include "td/telegram/Td.h" #include "td/telegram/Td.h"
@ -222,7 +222,7 @@ void dismiss_suggested_action(SuggestedAction action, Promise<Unit> &&promise) {
return send_closure_later(G()->config_manager(), &ConfigManager::dismiss_suggested_action, std::move(action), return send_closure_later(G()->config_manager(), &ConfigManager::dismiss_suggested_action, std::move(action),
std::move(promise)); std::move(promise));
case SuggestedAction::Type::ConvertToGigagroup: case SuggestedAction::Type::ConvertToGigagroup:
return send_closure_later(G()->contacts_manager(), &ContactsManager::dismiss_dialog_suggested_action, return send_closure_later(G()->dialog_manager(), &DialogManager::dismiss_dialog_suggested_action,
std::move(action), std::move(promise)); std::move(action), std::move(promise));
case SuggestedAction::Type::SetPassword: { case SuggestedAction::Type::SetPassword: {
if (action.otherwise_relogin_days_ < 0) { if (action.otherwise_relogin_days_ < 0) {