Allow to specify message_ttl during creation of basic group and channel chats.

This commit is contained in:
levlam 2022-11-29 15:44:12 +03:00
parent 62e721fa6f
commit 2844156dfc
7 changed files with 72 additions and 38 deletions

View File

@ -5586,16 +5586,20 @@ 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 @user_ids Identifiers of users to be added to the basic group @title Title of the new basic group; 1-128 characters
createNewBasicGroupChat user_ids:vector<int53> title:string = Chat;
//@description Creates a new basic group and sends a corresponding messageBasicGroupChatCreate. Returns the newly created chat
//@user_ids Identifiers of users to be added to the basic group
//@title Title of the new basic group; 1-128 characters
//@message_ttl Message TTL value, in seconds; must be from 0 up to 365 * 86400 and be divisible by 86400
createNewBasicGroupChat user_ids:vector<int53> title:string message_ttl:int32 = Chat;
//@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
//@is_channel Pass true to create a channel chat
//@param_description Chat description; 0-255 characters
//@location Chat location if a location-based supergroup is being created; pass null to create an ordinary supergroup chat
//@message_ttl Message TTL value, in seconds; must be from 0 up to 365 * 86400 and be divisible by 86400
//@for_import Pass true to create a supergroup for importing messages using importMessage
createNewSupergroupChat title:string is_channel:Bool description:string location:chatLocation for_import:Bool = Chat;
createNewSupergroupChat title:string is_channel:Bool description:string location:chatLocation message_ttl:int32 for_import:Bool = Chat;
//@description Creates a new secret chat. Returns the newly created chat @user_id Identifier of the target user
createNewSecretChat user_id:int53 = Chat;

View File

@ -16,6 +16,10 @@ int32 MessageTtl::get_message_ttl_object() const {
return period_;
}
int32 MessageTtl::get_input_ttl_period() const {
return period_;
}
bool operator==(const MessageTtl &lhs, const MessageTtl &rhs) {
return lhs.period_ == rhs.period_;
}

View File

@ -34,6 +34,8 @@ class MessageTtl {
int32 get_message_ttl_object() const;
int32 get_input_ttl_period() const;
template <class StorerT>
void store(StorerT &storer) const {
td::store(period_, storer);

View File

@ -1013,11 +1013,12 @@ class CreateChatQuery final : public Td::ResultHandler {
explicit CreateChatQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
}
void send(vector<tl_object_ptr<telegram_api::InputUser>> &&input_users, const string &title, int64 random_id) {
void send(vector<tl_object_ptr<telegram_api::InputUser>> &&input_users, const string &title, MessageTtl message_ttl,
int64 random_id) {
random_id_ = random_id;
int32 flags = 0;
send_query(
G()->net_query_creator().create(telegram_api::messages_createChat(flags, std::move(input_users), title, 0)));
int32 flags = telegram_api::messages_createChat::TTL_PERIOD_MASK;
send_query(G()->net_query_creator().create(
telegram_api::messages_createChat(flags, std::move(input_users), title, message_ttl.get_input_ttl_period())));
}
void on_result(BufferSlice packet) final {
@ -1046,8 +1047,8 @@ class CreateChannelQuery final : public Td::ResultHandler {
}
void send(const string &title, bool is_megagroup, const string &about, const DialogLocation &location,
bool for_import, int64 random_id) {
int32 flags = 0;
bool for_import, MessageTtl message_ttl, int64 random_id) {
int32 flags = telegram_api::channels_createChannel::TTL_PERIOD_MASK;
if (is_megagroup) {
flags |= telegram_api::channels_createChannel::MEGAGROUP_MASK;
} else {
@ -1061,9 +1062,9 @@ class CreateChannelQuery final : public Td::ResultHandler {
}
random_id_ = random_id;
send_query(G()->net_query_creator().create(
telegram_api::channels_createChannel(flags, false /*ignored*/, false /*ignored*/, false /*ignored*/, title,
about, location.get_input_geo_point(), location.get_address(), 0)));
send_query(G()->net_query_creator().create(telegram_api::channels_createChannel(
flags, false /*ignored*/, false /*ignored*/, false /*ignored*/, title, about, location.get_input_geo_point(),
location.get_address(), message_ttl.get_input_ttl_period())));
}
void on_result(BufferSlice packet) final {
@ -21145,8 +21146,8 @@ void MessagesManager::create_dialog(DialogId dialog_id, bool force, Promise<Unit
promise.set_value(Unit());
}
DialogId MessagesManager::create_new_group_chat(const vector<UserId> &user_ids, const string &title, int64 &random_id,
Promise<Unit> &&promise) {
DialogId MessagesManager::create_new_group_chat(const vector<UserId> &user_ids, const string &title,
MessageTtl message_ttl, int64 &random_id, Promise<Unit> &&promise) {
LOG(INFO) << "Trying to create group chat \"" << title << "\" with members " << format::as_array(user_ids);
if (random_id != 0) {
@ -21192,13 +21193,14 @@ DialogId MessagesManager::create_new_group_chat(const vector<UserId> &user_ids,
} while (random_id == 0 || created_dialogs_.count(random_id) > 0);
created_dialogs_[random_id]; // reserve place for result
td_->create_handler<CreateChatQuery>(std::move(promise))->send(std::move(input_users), new_title, random_id);
td_->create_handler<CreateChatQuery>(std::move(promise))
->send(std::move(input_users), new_title, message_ttl, random_id);
return DialogId();
}
DialogId MessagesManager::create_new_channel_chat(const string &title, bool is_megagroup, const string &description,
const DialogLocation &location, bool for_import, int64 &random_id,
Promise<Unit> &&promise) {
const DialogLocation &location, bool for_import,
MessageTtl message_ttl, int64 &random_id, Promise<Unit> &&promise) {
LOG(INFO) << "Trying to create " << (is_megagroup ? "supergroup" : "broadcast") << " with title \"" << title
<< "\", description \"" << description << "\" and " << location;
@ -21232,7 +21234,7 @@ DialogId MessagesManager::create_new_channel_chat(const string &title, bool is_m
td_->create_handler<CreateChannelQuery>(std::move(promise))
->send(new_title, is_megagroup, strip_empty_characters(description, MAX_DESCRIPTION_LENGTH), location, for_import,
random_id);
message_ttl, random_id);
return DialogId();
}
@ -33383,7 +33385,7 @@ void MessagesManager::on_create_new_dialog_success(int64 random_id, tl_object_pt
DialogType expected_type, Promise<Unit> &&promise) {
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) {
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 on_create_new_dialog_fail(random_id, Status::Error(500, "Unsupported server response"), std::move(promise));
}
@ -33397,6 +33399,15 @@ void MessagesManager::on_create_new_dialog_success(int64 random_id, tl_object_pt
return on_create_new_dialog_fail(random_id, Status::Error(500, "Chat of wrong type has been created"),
std::move(promise));
}
if ((*message)->get_id() != telegram_api::messageService::ID) {
return on_create_new_dialog_fail(random_id, Status::Error(500, "Invalid message received"), std::move(promise));
}
auto action_id = static_cast<const telegram_api::messageService *>((*message).get())->action_->get_id();
if (action_id != telegram_api::messageActionChatCreate::ID &&
action_id != telegram_api::messageActionChannelCreate::ID) {
return on_create_new_dialog_fail(random_id, Status::Error(500, "Invalid service message received"),
std::move(promise));
}
auto it = created_dialogs_.find(random_id);
CHECK(it != created_dialogs_.end());

View File

@ -706,12 +706,12 @@ class MessagesManager final : public Actor {
void create_dialog(DialogId dialog_id, bool force, Promise<Unit> &&promise);
DialogId create_new_group_chat(const vector<UserId> &user_ids, const string &title, int64 &random_id,
Promise<Unit> &&promise);
DialogId create_new_group_chat(const vector<UserId> &user_ids, const string &title, MessageTtl message_ttl,
int64 &random_id, Promise<Unit> &&promise);
DialogId create_new_channel_chat(const string &title, bool is_megagroup, const string &description,
const DialogLocation &location, bool for_import, int64 &random_id,
Promise<Unit> &&promise);
const DialogLocation &location, bool for_import, MessageTtl message_ttl,
int64 &random_id, Promise<Unit> &&promise);
void create_new_secret_chat(UserId user_id, Promise<SecretChatId> &&promise);

View File

@ -1628,12 +1628,14 @@ class CreateChatRequest final : public RequestActor<> {
class CreateNewGroupChatRequest final : public RequestActor<> {
vector<UserId> user_ids_;
string title_;
MessageTtl message_ttl_;
int64 random_id_;
DialogId dialog_id_;
void do_run(Promise<Unit> &&promise) final {
dialog_id_ = td_->messages_manager_->create_new_group_chat(user_ids_, title_, random_id_, std::move(promise));
dialog_id_ =
td_->messages_manager_->create_new_group_chat(user_ids_, title_, message_ttl_, random_id_, std::move(promise));
}
void do_send_result() final {
@ -1642,10 +1644,12 @@ class CreateNewGroupChatRequest final : public RequestActor<> {
}
public:
CreateNewGroupChatRequest(ActorShared<Td> td, uint64 request_id, vector<UserId> user_ids, string title)
CreateNewGroupChatRequest(ActorShared<Td> td, uint64 request_id, vector<UserId> user_ids, string title,
int32 message_ttl)
: RequestActor(std::move(td), request_id)
, user_ids_(std::move(user_ids))
, title_(std::move(title))
, message_ttl_(message_ttl)
, random_id_(0) {
}
};
@ -1691,13 +1695,14 @@ class CreateNewSupergroupChatRequest final : public RequestActor<> {
string description_;
DialogLocation location_;
bool for_import_;
MessageTtl message_ttl_;
int64 random_id_;
DialogId dialog_id_;
void do_run(Promise<Unit> &&promise) final {
dialog_id_ = td_->messages_manager_->create_new_channel_chat(title_, is_megagroup_, description_, location_,
for_import_, random_id_, std::move(promise));
dialog_id_ = td_->messages_manager_->create_new_channel_chat(
title_, is_megagroup_, description_, location_, for_import_, message_ttl_, random_id_, std::move(promise));
}
void do_send_result() final {
@ -1708,13 +1713,14 @@ class CreateNewSupergroupChatRequest final : public RequestActor<> {
public:
CreateNewSupergroupChatRequest(ActorShared<Td> td, uint64 request_id, string title, bool is_megagroup,
string description, td_api::object_ptr<td_api::chatLocation> &&location,
bool for_import)
bool for_import, int32 message_ttl)
: RequestActor(std::move(td), request_id)
, title_(std::move(title))
, is_megagroup_(is_megagroup)
, description_(std::move(description))
, location_(std::move(location))
, for_import_(for_import)
, message_ttl_(message_ttl)
, random_id_(0) {
}
};
@ -5637,7 +5643,8 @@ void Td::on_request(uint64 id, td_api::createSecretChat &request) {
void Td::on_request(uint64 id, td_api::createNewBasicGroupChat &request) {
CHECK_IS_USER();
CLEAN_INPUT_STRING(request.title_);
CREATE_REQUEST(CreateNewGroupChatRequest, UserId::get_user_ids(request.user_ids_), std::move(request.title_));
CREATE_REQUEST(CreateNewGroupChatRequest, UserId::get_user_ids(request.user_ids_), std::move(request.title_),
request.message_ttl_);
}
void Td::on_request(uint64 id, td_api::createNewSupergroupChat &request) {
@ -5645,8 +5652,10 @@ void Td::on_request(uint64 id, td_api::createNewSupergroupChat &request) {
CLEAN_INPUT_STRING(request.title_);
CLEAN_INPUT_STRING(request.description_);
CREATE_REQUEST(CreateNewSupergroupChatRequest, std::move(request.title_), !request.is_channel_,
std::move(request.description_), std::move(request.location_), request.for_import_);
std::move(request.description_), std::move(request.location_), request.for_import_,
request.message_ttl_);
}
void Td::on_request(uint64 id, td_api::createNewSecretChat &request) {
CREATE_REQUEST(CreateNewSecretChatRequest, request.user_id_);
}

View File

@ -4258,18 +4258,22 @@ class CliClient final : public Actor {
} else if (op == "cnbgc") {
string user_ids_string;
string title;
get_args(args, user_ids_string, title);
send_request(td_api::make_object<td_api::createNewBasicGroupChat>(as_user_ids(user_ids_string), title));
} else if (op == "cnchc") {
send_request(td_api::make_object<td_api::createNewSupergroupChat>(args, true, "Description", nullptr, false));
} else if (op == "cnsgc") {
send_request(td_api::make_object<td_api::createNewSupergroupChat>(args, false, "Description", nullptr, false));
int32 message_ttl;
get_args(args, user_ids_string, title, message_ttl);
send_request(
td_api::make_object<td_api::createNewBasicGroupChat>(as_user_ids(user_ids_string), title, message_ttl));
} else if (op == "cnchc" || op == "cnchcttl") {
send_request(td_api::make_object<td_api::createNewSupergroupChat>(args, true, "Description", nullptr,
op == "cnchcttl" ? 86400 : 0, false));
} else if (op == "cnsgc" || op == "cnsgcttl") {
send_request(td_api::make_object<td_api::createNewSupergroupChat>(args, false, "Description", nullptr,
op == "cnsgcttl" ? 86400 : 0, false));
} else if (op == "cnsgcloc") {
send_request(td_api::make_object<td_api::createNewSupergroupChat>(
args, false, "Description",
td_api::make_object<td_api::chatLocation>(as_location("40.0", "60.0", ""), "address"), false));
td_api::make_object<td_api::chatLocation>(as_location("40.0", "60.0", ""), "address"), 0, false));
} else if (op == "cnsgcimport") {
send_request(td_api::make_object<td_api::createNewSupergroupChat>(args, false, "Description", nullptr, true));
send_request(td_api::make_object<td_api::createNewSupergroupChat>(args, false, "Description", nullptr, 0, true));
} else if (op == "UpgradeBasicGroupChatToSupergroupChat") {
ChatId chat_id;
get_args(args, chat_id);