Move migrate_dialog_to_megagroup to ContactsManager.

This commit is contained in:
levlam 2024-01-10 15:57:19 +03:00
parent 1bc8e90c10
commit a409af0973
5 changed files with 47 additions and 68 deletions

View File

@ -7865,24 +7865,56 @@ void ContactsManager::delete_chat_participant(ChatId chat_id, UserId user_id, bo
td_->create_handler<DeleteChatUserQuery>(std::move(promise))->send(chat_id, std::move(input_user), revoke_messages);
}
ChannelId ContactsManager::migrate_chat_to_megagroup(ChatId chat_id, Promise<Unit> &promise) {
void ContactsManager::migrate_dialog_to_megagroup(DialogId dialog_id,
Promise<td_api::object_ptr<td_api::chat>> &&promise) {
LOG(INFO) << "Trying to upgrade " << dialog_id << " to a supergroup";
if (!td_->dialog_manager_->have_dialog_force(dialog_id, "migrate_dialog_to_megagroup")) {
return promise.set_error(Status::Error(400, "Chat not found"));
}
if (dialog_id.get_type() != DialogType::Chat) {
return promise.set_error(Status::Error(400, "Only basic group chats can be converted to supergroup"));
}
auto chat_id = dialog_id.get_chat_id();
auto c = get_chat(chat_id);
if (c == nullptr) {
promise.set_error(Status::Error(400, "Chat info not found"));
return ChannelId();
return promise.set_error(Status::Error(400, "Chat info not found"));
}
if (!c->status.is_creator()) {
promise.set_error(Status::Error(400, "Need creator rights in the chat"));
return ChannelId();
return promise.set_error(Status::Error(400, "Need creator rights in the chat"));
}
if (c->migrated_to_channel_id.is_valid()) {
return c->migrated_to_channel_id;
return on_migrate_chat_to_megagroup(chat_id, std::move(promise));
}
td_->create_handler<MigrateChatQuery>(std::move(promise))->send(chat_id);
return ChannelId();
auto query_promise = PromiseCreator::lambda(
[actor_id = actor_id(this), chat_id, promise = std::move(promise)](Result<Unit> &&result) mutable {
if (result.is_error()) {
return promise.set_error(result.move_as_error());
}
send_closure(actor_id, &ContactsManager::on_migrate_chat_to_megagroup, chat_id, std::move(promise));
});
td_->create_handler<MigrateChatQuery>(std::move(query_promise))->send(chat_id);
}
void ContactsManager::on_migrate_chat_to_megagroup(ChatId chat_id,
Promise<td_api::object_ptr<td_api::chat>> &&promise) {
auto c = get_chat(chat_id);
CHECK(c != nullptr);
if (!c->migrated_to_channel_id.is_valid()) {
LOG(ERROR) << "Can't find the supergroup to which the basic group has migrated";
return promise.set_error(Status::Error(500, "Supergroup not found"));
}
auto channel_id = c->migrated_to_channel_id;
if (!have_channel(channel_id)) {
LOG(ERROR) << "Can't find info about the supergroup to which the basic group has migrated";
return promise.set_error(Status::Error(500, "Supergroup info is not found"));
}
auto dialog_id = DialogId(channel_id);
td_->dialog_manager_->force_create_dialog(dialog_id, "on_migrate_chat_to_megagroup");
promise.set_value(td_->messages_manager_->get_chat_object(dialog_id));
}
vector<ChannelId> ContactsManager::get_channel_ids(vector<tl_object_ptr<telegram_api::Chat>> &&chats,

View File

@ -509,7 +509,7 @@ class ContactsManager final : public Actor {
void transfer_dialog_ownership(DialogId dialog_id, UserId user_id, const string &password, Promise<Unit> &&promise);
ChannelId migrate_chat_to_megagroup(ChatId chat_id, Promise<Unit> &promise);
void migrate_dialog_to_megagroup(DialogId dialog_id, Promise<td_api::object_ptr<td_api::chat>> &&promise);
void get_channel_recommendations(DialogId dialog_id, bool return_local,
Promise<td_api::object_ptr<td_api::chats>> &&chats_promise,
@ -1332,6 +1332,8 @@ class ContactsManager final : public Actor {
void send_get_chat_full_query(ChatId chat_id, Promise<Unit> &&promise, const char *source);
void on_migrate_chat_to_megagroup(ChatId chat_id, Promise<td_api::object_ptr<td_api::chat>> &&promise);
const Channel *get_channel(ChannelId channel_id) const;
Channel *get_channel(ChannelId channel_id);
Channel *get_channel_force(ChannelId channel_id, const char *source);

View File

@ -18202,39 +18202,6 @@ DialogId MessagesManager::create_new_channel_chat(const string &title, bool is_f
return DialogId();
}
DialogId MessagesManager::migrate_dialog_to_megagroup(DialogId dialog_id, Promise<Unit> &&promise) {
LOG(INFO) << "Trying to convert " << dialog_id << " to supergroup";
if (dialog_id.get_type() != DialogType::Chat) {
promise.set_error(Status::Error(400, "Only basic group chats can be converted to supergroup"));
return DialogId();
}
auto channel_id = td_->contacts_manager_->migrate_chat_to_megagroup(dialog_id.get_chat_id(), promise);
if (!channel_id.is_valid()) {
return DialogId();
}
if (!td_->contacts_manager_->have_channel(channel_id)) {
LOG(ERROR) << "Can't find info about supergroup to which the group has migrated";
promise.set_error(Status::Error(400, "Supergroup is not found"));
return DialogId();
}
auto new_dialog_id = DialogId(channel_id);
Dialog *d = get_dialog_force(new_dialog_id, "migrate_dialog_to_megagroup");
if (d == nullptr) {
d = add_dialog(new_dialog_id, "migrate_dialog_to_megagroup");
if (d->pts == 0) {
d->pts = 1;
}
update_dialog_pos(d, "migrate_dialog_to_megagroup");
}
promise.set_value(Unit());
return new_dialog_id;
}
bool MessagesManager::is_dialog_opened(DialogId dialog_id) const {
const Dialog *d = get_dialog(dialog_id);
return d != nullptr && d->open_count > 0;

View File

@ -641,8 +641,6 @@ class MessagesManager final : public Actor {
const DialogLocation &location, bool for_import, MessageTtl message_ttl,
int64 &random_id, Promise<Unit> &&promise);
DialogId migrate_dialog_to_megagroup(DialogId dialog_id, Promise<Unit> &&promise);
bool is_dialog_opened(DialogId dialog_id) const;
Status open_dialog(DialogId dialog_id) TD_WARN_UNUSED_RESULT;

View File

@ -1719,27 +1719,6 @@ class CreateNewSupergroupChatRequest final : public RequestActor<> {
}
};
class UpgradeGroupChatToSupergroupChatRequest final : public RequestActor<> {
string title_;
DialogId dialog_id_;
DialogId result_dialog_id_;
void do_run(Promise<Unit> &&promise) final {
result_dialog_id_ = td_->messages_manager_->migrate_dialog_to_megagroup(dialog_id_, std::move(promise));
}
void do_send_result() final {
CHECK(result_dialog_id_.is_valid());
send_result(td_->messages_manager_->get_chat_object(result_dialog_id_));
}
public:
UpgradeGroupChatToSupergroupChatRequest(ActorShared<Td> td, uint64 request_id, int64 dialog_id)
: RequestActor(std::move(td), request_id), dialog_id_(dialog_id) {
}
};
class CheckChatInviteLinkRequest final : public RequestActor<> {
string invite_link_;
@ -6366,7 +6345,8 @@ void Td::on_request(uint64 id, td_api::getGroupCallStreamSegment &request) {
void Td::on_request(uint64 id, const td_api::upgradeBasicGroupChatToSupergroupChat &request) {
CHECK_IS_USER();
CREATE_REQUEST(UpgradeGroupChatToSupergroupChatRequest, request.chat_id_);
CREATE_REQUEST_PROMISE();
contacts_manager_->migrate_dialog_to_megagroup(DialogId(request.chat_id_), std::move(promise));
}
void Td::on_request(uint64 id, const td_api::getChatListsToAddChat &request) {