Return failed to add members in createNewBasicGroupChat.

This commit is contained in:
levlam 2024-04-09 16:19:33 +03:00
parent 69257ffa6a
commit 27721159bd
6 changed files with 59 additions and 27 deletions

View File

@ -1855,6 +1855,10 @@ failedToAddMember user_id:int53 premium_would_allow_invite:Bool premium_required
failedToAddMembers failed_to_add_members:vector<failedToAddMember> = FailedToAddMembers;
//@description Contains information about a newly created basic group chat @chat_id Chat identifier @failed_to_add_members Information about failed to add members
createdBasicGroupChat chat_id:int53 failed_to_add_members:failedToAddMembers = CreatedBasicGroupChat;
//@description Describes a chat located nearby @chat_id Chat identifier @distance Distance to the chat location, in meters
chatNearby chat_id:int53 distance:int32 = ChatNearby;
@ -8538,11 +8542,11 @@ createSupergroupChat supergroup_id:int53 force:Bool = Chat;
//@description Returns an existing chat corresponding to a known secret chat @secret_chat_id Secret chat identifier
createSecretChat secret_chat_id:int32 = Chat;
//@description Creates a new basic group and sends a corresponding messageBasicGroupChatCreate. Returns the newly created chat
//@description Creates a new basic group and sends a corresponding messageBasicGroupChatCreate. Returns information about the newly created chat
//@user_ids Identifiers of users to be added to the basic group; may be empty to create a basic group without other members
//@title Title of the new basic group; 1-128 characters
//@message_auto_delete_time Message auto-delete time value, in seconds; must be from 0 up to 365 * 86400 and be divisible by 86400. If 0, then messages aren't deleted automatically
createNewBasicGroupChat user_ids:vector<int53> title:string message_auto_delete_time:int32 = Chat;
createNewBasicGroupChat user_ids:vector<int53> title:string message_auto_delete_time:int32 = CreatedBasicGroupChat;
//@description Creates a new supergroup or channel and sends a corresponding messageSupergroupChatCreate. Returns the newly created chat
//@title Title of the new chat; 1-128 characters

View File

@ -27,6 +27,7 @@
#include "td/telegram/MessageTtl.h"
#include "td/telegram/MinChannel.h"
#include "td/telegram/misc.h"
#include "td/telegram/MissingInvitee.h"
#include "td/telegram/net/NetQuery.h"
#include "td/telegram/OptionManager.h"
#include "td/telegram/PeerColor.h"
@ -71,10 +72,11 @@
namespace td {
class CreateChatQuery final : public Td::ResultHandler {
Promise<td_api::object_ptr<td_api::chat>> promise_;
Promise<td_api::object_ptr<td_api::createdBasicGroupChat>> promise_;
public:
explicit CreateChatQuery(Promise<td_api::object_ptr<td_api::chat>> &&promise) : promise_(std::move(promise)) {
explicit CreateChatQuery(Promise<td_api::object_ptr<td_api::createdBasicGroupChat>> &&promise)
: promise_(std::move(promise)) {
}
void send(vector<tl_object_ptr<telegram_api::InputUser>> &&input_users, const string &title, MessageTtl message_ttl) {
@ -91,7 +93,8 @@ class CreateChatQuery final : public Td::ResultHandler {
auto ptr = result_ptr.move_as_ok();
LOG(INFO) << "Receive result for CreateChatQuery: " << to_string(ptr);
td_->messages_manager_->on_create_new_dialog(std::move(ptr->updates_), DialogType::Chat, std::move(promise_));
td_->messages_manager_->on_create_new_dialog(
std::move(ptr->updates_), MissingInvitees(std::move(ptr->missing_invitees_)), std::move(promise_), Auto());
}
void on_error(Status status) final {
@ -134,7 +137,7 @@ class CreateChannelQuery final : public Td::ResultHandler {
return on_error(result_ptr.move_as_error());
}
td_->messages_manager_->on_create_new_dialog(result_ptr.move_as_ok(), DialogType::Channel, std::move(promise_));
td_->messages_manager_->on_create_new_dialog(result_ptr.move_as_ok(), Auto(), Auto(), std::move(promise_));
}
void on_error(Status status) final {
@ -7459,7 +7462,7 @@ FileSourceId ChatManager::get_channel_full_file_source_id(ChannelId channel_id)
}
void ChatManager::create_new_chat(const vector<UserId> &user_ids, const string &title, MessageTtl message_ttl,
Promise<td_api::object_ptr<td_api::chat>> &&promise) {
Promise<td_api::object_ptr<td_api::createdBasicGroupChat>> &&promise) {
auto new_title = clean_name(title, MAX_TITLE_LENGTH);
if (new_title.empty()) {
return promise.set_error(Status::Error(400, "Title must be non-empty"));
@ -7467,11 +7470,8 @@ void ChatManager::create_new_chat(const vector<UserId> &user_ids, const string &
vector<telegram_api::object_ptr<telegram_api::InputUser>> input_users;
for (auto user_id : user_ids) {
auto r_input_user = td_->user_manager_->get_input_user(user_id);
if (r_input_user.is_error()) {
return promise.set_error(r_input_user.move_as_error());
}
input_users.push_back(r_input_user.move_as_ok());
TRY_RESULT_PROMISE(promise, input_user, td_->user_manager_->get_input_user(user_id));
input_users.push_back(std::move(input_user));
}
td_->create_handler<CreateChatQuery>(std::move(promise))->send(std::move(input_users), new_title, message_ttl);

View File

@ -297,7 +297,7 @@ class ChatManager final : public Actor {
vector<DialogId> get_inactive_channels(Promise<Unit> &&promise);
void create_new_chat(const vector<UserId> &user_ids, const string &title, MessageTtl message_ttl,
Promise<td_api::object_ptr<td_api::chat>> &&promise);
Promise<td_api::object_ptr<td_api::createdBasicGroupChat>> &&promise);
bool have_chat(ChatId chat_id) const;
bool have_chat_force(ChatId chat_id, const char *source);

View File

@ -55,6 +55,7 @@
#include "td/telegram/MessageReplyInfo.hpp"
#include "td/telegram/MessageSender.h"
#include "td/telegram/misc.h"
#include "td/telegram/MissingInvitee.h"
#include "td/telegram/net/DcId.h"
#include "td/telegram/NotificationGroupInfo.hpp"
#include "td/telegram/NotificationGroupType.h"
@ -13720,7 +13721,12 @@ MessageFullId MessagesManager::on_get_message(MessageInfo &&message_info, const
auto pending_created_dialog = std::move(it->second);
pending_created_dialogs_.erase(it);
pending_created_dialog.promise_.set_value(get_chat_object(d, "on_get_message"));
if (pending_created_dialog.chat_promise_) {
pending_created_dialog.chat_promise_.set_value(td_api::make_object<td_api::createdBasicGroupChat>(
get_chat_id_object(dialog_id, "on_get_message"), std::move(pending_created_dialog.failed_to_add_members_)));
} else {
pending_created_dialog.channel_promise_.set_value(get_chat_object(d, "on_get_message"));
}
}
}
@ -30891,15 +30897,20 @@ void MessagesManager::set_dialog_message_ttl(Dialog *d, MessageTtl message_ttl)
}
void MessagesManager::on_create_new_dialog(telegram_api::object_ptr<telegram_api::Updates> &&updates,
DialogType expected_type,
Promise<td_api::object_ptr<td_api::chat>> &&promise) {
MissingInvitees &&missing_invitees,
Promise<td_api::object_ptr<td_api::createdBasicGroupChat>> &&chat_promise,
Promise<td_api::object_ptr<td_api::chat>> &&channel_promise) {
LOG(INFO) << "Receive result for creation of a chat: " << to_string(updates);
auto fail = [&](Slice message) {
chat_promise.set_error(Status::Error(500, message));
channel_promise.set_error(Status::Error(500, message));
};
auto sent_messages = UpdatesManager::get_new_messages(updates.get());
auto sent_messages_random_ids = UpdatesManager::get_sent_messages_random_ids(updates.get());
if (sent_messages.size() != 1u || sent_messages_random_ids.size() != 1u) {
LOG(ERROR) << "Receive wrong result for create group or channel chat " << oneline(to_string(updates));
return promise.set_error(Status::Error(500, "Unsupported server response"));
return fail("Unsupported server response");
}
auto *message = sent_messages.begin()->first;
@ -30907,20 +30918,21 @@ void MessagesManager::on_create_new_dialog(telegram_api::object_ptr<telegram_api
// TODO check that message_random_id equals random_id after messages_createChat will be updated
if (sent_messages.begin()->second) {
return promise.set_error(Status::Error(500, "Scheduled message received"));
return fail("Scheduled message received");
}
auto expected_type = chat_promise ? DialogType::Chat : DialogType::Channel;
auto dialog_id = DialogId::get_message_dialog_id(message);
if (dialog_id.get_type() != expected_type) {
return promise.set_error(Status::Error(500, "Chat of wrong type has been created"));
return fail("Chat of wrong type has been created");
}
if (message->get_id() != telegram_api::messageService::ID) {
return promise.set_error(Status::Error(500, "Invalid message received"));
return fail("Invalid message received");
}
auto action_id = static_cast<const telegram_api::messageService *>(message)->action_->get_id();
if (action_id != telegram_api::messageActionChatCreate::ID &&
action_id != telegram_api::messageActionChannelCreate::ID) {
return promise.set_error(Status::Error(500, "Invalid service message received"));
return fail("Invalid service message received");
}
const Dialog *d = get_dialog(dialog_id);
@ -30928,16 +30940,25 @@ void MessagesManager::on_create_new_dialog(telegram_api::object_ptr<telegram_api
// dialog have been already created and at least one non-temporary message was added,
// i.e. we are not interested in the creation of dialog by searchMessages
// then messages have already been added, so just set promise
return promise.set_value(get_chat_object(d, "on_create_new_dialog"));
if (chat_promise) {
return chat_promise.set_value(td_api::make_object<td_api::createdBasicGroupChat>(
get_chat_id_object(dialog_id, "on_create_new_dialog"),
missing_invitees.get_failed_to_add_members_object(td_->user_manager_.get())));
} else {
return channel_promise.set_value(get_chat_object(d, "on_create_new_dialog"));
}
}
if (pending_created_dialogs_.count(dialog_id) == 0) {
PendingCreatedDialog pending_created_dialog;
pending_created_dialog.promise_ = std::move(promise);
pending_created_dialog.failed_to_add_members_ =
missing_invitees.get_failed_to_add_members_object(td_->user_manager_.get());
pending_created_dialog.chat_promise_ = std::move(chat_promise);
pending_created_dialog.channel_promise_ = std::move(channel_promise);
pending_created_dialogs_.emplace(dialog_id, std::move(pending_created_dialog));
} else {
LOG(ERROR) << "Receive twice " << dialog_id << " as result of chat creation";
return promise.set_error(Status::Error(500, "Chat was created earlier"));
return fail("Chat was created earlier");
}
td_->updates_manager_->on_get_updates(std::move(updates), Promise<Unit>());

View File

@ -114,6 +114,7 @@ struct InputMessageContent;
class MessageContent;
class MessageForwardInfo;
struct MessageReactions;
class MissingInvitees;
class Td;
class Usernames;
@ -888,8 +889,10 @@ class MessagesManager final : public Actor {
void on_upload_message_media_fail(DialogId dialog_id, MessageId message_id, Status error);
void on_create_new_dialog(telegram_api::object_ptr<telegram_api::Updates> &&updates, DialogType expected_type,
Promise<td_api::object_ptr<td_api::chat>> &&promise);
void on_create_new_dialog(telegram_api::object_ptr<telegram_api::Updates> &&updates,
MissingInvitees &&missing_invitees,
Promise<td_api::object_ptr<td_api::createdBasicGroupChat>> &&chat_promise,
Promise<td_api::object_ptr<td_api::chat>> &&channel_promise);
void on_get_channel_difference(DialogId dialog_id, int32 request_pts, int32 request_limit,
tl_object_ptr<telegram_api::updates_ChannelDifference> &&difference_ptr,
@ -3210,7 +3213,9 @@ class MessagesManager final : public Actor {
vector<ChannelId> created_public_broadcasts_;
struct PendingCreatedDialog {
Promise<td_api::object_ptr<td_api::chat>> promise_;
td_api::object_ptr<td_api::failedToAddMembers> failed_to_add_members_;
Promise<td_api::object_ptr<td_api::createdBasicGroupChat>> chat_promise_;
Promise<td_api::object_ptr<td_api::chat>> channel_promise_;
};
FlatHashMap<DialogId, PendingCreatedDialog, DialogIdHash> pending_created_dialogs_;

View File

@ -42,6 +42,8 @@ class MissingInvitees {
friend StringBuilder &operator<<(StringBuilder &string_builder, const MissingInvitees &invitees);
public:
MissingInvitees() = default;
explicit MissingInvitees(vector<telegram_api::object_ptr<telegram_api::missingInvitee>> &&invitees);
td_api::object_ptr<td_api::failedToAddMembers> get_failed_to_add_members_object(UserManager *user_manager) const;