Add td_api::setPersonalChat.

This commit is contained in:
levlam 2024-03-29 00:46:05 +03:00
parent 66522165ca
commit 01b7a0e14b
6 changed files with 94 additions and 1 deletions

View File

@ -9555,6 +9555,9 @@ reorderActiveUsernames usernames:vector<string> = Ok;
//@description Changes the birthdate of the current user @birthdate The new value of the current user's birthdate; pass null to remove the birthdate //@description Changes the birthdate of the current user @birthdate The new value of the current user's birthdate; pass null to remove the birthdate
setBirthdate birthdate:birthdate = Ok; setBirthdate birthdate:birthdate = Ok;
//@description Changes the personal chat of the current user @chat_id Identifier of the new personal chat; pass 0 to remove the chat. Use getSuitablePersonalChats to get suitable chats
setPersonalChat chat_id:int53 = Ok;
//@description Changes the emoji status of the current user; for Telegram Premium users only @emoji_status New emoji status; pass null to switch to the default badge //@description Changes the emoji status of the current user; for Telegram Premium users only @emoji_status New emoji status; pass null to switch to the default badge
setEmojiStatus emoji_status:emojiStatus = Ok; setEmojiStatus emoji_status:emojiStatus = Ok;

View File

@ -1035,6 +1035,44 @@ class UpdateBirthdayQuery final : public Td::ResultHandler {
} }
}; };
class UpdatePersonalChannelQuery final : public Td::ResultHandler {
Promise<Unit> promise_;
public:
explicit UpdatePersonalChannelQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
}
void send(ChannelId channel_id) {
telegram_api::object_ptr<telegram_api::InputChannel> input_channel;
if (channel_id == ChannelId()) {
input_channel = telegram_api::make_object<telegram_api::inputChannelEmpty>();
} else {
input_channel = td_->contacts_manager_->get_input_channel(channel_id);
CHECK(input_channel != nullptr);
}
send_query(G()->net_query_creator().create(telegram_api::account_updatePersonalChannel(std::move(input_channel)),
{{"me"}}));
}
void on_result(BufferSlice packet) final {
auto result_ptr = fetch_result<telegram_api::account_updatePersonalChannel>(packet);
if (result_ptr.is_error()) {
return on_error(result_ptr.move_as_error());
}
LOG(DEBUG) << "Receive result for UpdatePersonalChannelQuery: " << result_ptr.ok();
if (result_ptr.ok()) {
promise_.set_value(Unit());
} else {
promise_.set_error(Status::Error(400, "Failed to change personal chat"));
}
}
void on_error(Status status) final {
promise_.set_error(std::move(status));
}
};
class UpdateEmojiStatusQuery final : public Td::ResultHandler { class UpdateEmojiStatusQuery final : public Td::ResultHandler {
Promise<Unit> promise_; Promise<Unit> promise_;
@ -6393,7 +6431,7 @@ void ContactsManager::set_birthdate(Birthdate &&birthdate, Promise<Unit> &&promi
void ContactsManager::on_set_birthdate(Birthdate birthdate, Promise<Unit> &&promise) { void ContactsManager::on_set_birthdate(Birthdate birthdate, Promise<Unit> &&promise) {
auto my_user_id = get_my_id(); auto my_user_id = get_my_id();
UserFull *user_full = get_user_full_force(my_user_id, "on_set_birthdate"); UserFull *user_full = get_user_full_force(my_user_id, "on_set_birthdate");
if (user_full != nullptr) { if (user_full != nullptr && user_full->birthdate != birthdate) {
user_full->birthdate = std::move(birthdate); user_full->birthdate = std::move(birthdate);
user_full->is_changed = true; user_full->is_changed = true;
update_user_full(user_full, my_user_id, "on_set_birthdate"); update_user_full(user_full, my_user_id, "on_set_birthdate");
@ -6401,6 +6439,42 @@ void ContactsManager::on_set_birthdate(Birthdate birthdate, Promise<Unit> &&prom
promise.set_value(Unit()); promise.set_value(Unit());
} }
void ContactsManager::set_personal_channel(DialogId dialog_id, Promise<Unit> &&promise) {
ChannelId channel_id;
if (dialog_id != DialogId() && !td_->dialog_manager_->have_dialog_force(dialog_id, "set_personal_channel")) {
return promise.set_error(Status::Error(400, "Chat not found"));
}
if (dialog_id != DialogId()) {
if (dialog_id.get_type() != DialogType::Channel) {
return promise.set_error(Status::Error(400, "Chat can't be set as a personal chat"));
}
channel_id = dialog_id.get_channel_id();
if (!is_suitable_created_public_channel(PublicDialogType::ForPersonalDialog, get_channel(channel_id))) {
return promise.set_error(Status::Error(400, "Chat can't be set as a personal chat"));
}
}
auto query_promise = PromiseCreator::lambda(
[actor_id = actor_id(this), channel_id, promise = std::move(promise)](Result<Unit> result) mutable {
if (result.is_ok()) {
send_closure(actor_id, &ContactsManager::on_set_personal_channel, channel_id, std::move(promise));
} else {
promise.set_error(result.move_as_error());
}
});
td_->create_handler<UpdatePersonalChannelQuery>(std::move(query_promise))->send(channel_id);
}
void ContactsManager::on_set_personal_channel(ChannelId channel_id, Promise<Unit> &&promise) {
auto my_user_id = get_my_id();
UserFull *user_full = get_user_full_force(my_user_id, "on_set_personal_channel");
if (user_full != nullptr && user_full->personal_channel_id != channel_id) {
user_full->personal_channel_id = channel_id;
user_full->is_changed = true;
update_user_full(user_full, my_user_id, "on_set_personal_channel");
}
promise.set_value(Unit());
}
void ContactsManager::set_emoji_status(const EmojiStatus &emoji_status, Promise<Unit> &&promise) { void ContactsManager::set_emoji_status(const EmojiStatus &emoji_status, Promise<Unit> &&promise) {
if (!td_->option_manager_->get_option_boolean("is_premium")) { if (!td_->option_manager_->get_option_boolean("is_premium")) {
return promise.set_error(Status::Error(400, "The method is available only to Telegram Premium users")); return promise.set_error(Status::Error(400, "The method is available only to Telegram Premium users"));

View File

@ -462,6 +462,8 @@ class ContactsManager final : public Actor {
void set_birthdate(Birthdate &&birthdate, Promise<Unit> &&promise); void set_birthdate(Birthdate &&birthdate, Promise<Unit> &&promise);
void set_personal_channel(DialogId dialog_id, Promise<Unit> &&promise);
void set_emoji_status(const EmojiStatus &emoji_status, Promise<Unit> &&promise); void set_emoji_status(const EmojiStatus &emoji_status, Promise<Unit> &&promise);
void set_chat_description(ChatId chat_id, const string &description, Promise<Unit> &&promise); void set_chat_description(ChatId chat_id, const string &description, Promise<Unit> &&promise);
@ -1300,6 +1302,8 @@ class ContactsManager final : public Actor {
void on_set_birthdate(Birthdate birthdate, Promise<Unit> &&promise); void on_set_birthdate(Birthdate birthdate, Promise<Unit> &&promise);
void on_set_personal_channel(ChannelId channel_id, Promise<Unit> &&promise);
void on_set_emoji_status(EmojiStatus emoji_status, Promise<Unit> &&promise); void on_set_emoji_status(EmojiStatus emoji_status, Promise<Unit> &&promise);
void on_update_user_name(User *u, UserId user_id, string &&first_name, string &&last_name); void on_update_user_name(User *u, UserId user_id, string &&first_name, string &&last_name);

View File

@ -7681,6 +7681,12 @@ void Td::on_request(uint64 id, td_api::setBirthdate &request) {
contacts_manager_->set_birthdate(Birthdate(std::move(request.birthdate_)), std::move(promise)); contacts_manager_->set_birthdate(Birthdate(std::move(request.birthdate_)), std::move(promise));
} }
void Td::on_request(uint64 id, const td_api::setPersonalChat &request) {
CHECK_IS_USER();
CREATE_OK_REQUEST_PROMISE();
contacts_manager_->set_personal_channel(DialogId(request.chat_id_), std::move(promise));
}
void Td::on_request(uint64 id, const td_api::setEmojiStatus &request) { void Td::on_request(uint64 id, const td_api::setEmojiStatus &request) {
CHECK_IS_USER(); CHECK_IS_USER();
CREATE_OK_REQUEST_PROMISE(); CREATE_OK_REQUEST_PROMISE();

View File

@ -1354,6 +1354,8 @@ class Td final : public Actor {
void on_request(uint64 id, td_api::setBirthdate &request); void on_request(uint64 id, td_api::setBirthdate &request);
void on_request(uint64 id, const td_api::setPersonalChat &request);
void on_request(uint64 id, const td_api::setEmojiStatus &request); void on_request(uint64 id, const td_api::setEmojiStatus &request);
void on_request(uint64 id, const td_api::getThemedEmojiStatuses &request); void on_request(uint64 id, const td_api::getThemedEmojiStatuses &request);

View File

@ -5841,6 +5841,10 @@ class CliClient final : public Actor {
send_request( send_request(
td_api::make_object<td_api::setBirthdate>(td_api::make_object<td_api::birthdate>(day, month, year))); td_api::make_object<td_api::setBirthdate>(td_api::make_object<td_api::birthdate>(day, month, year)));
} }
} else if (op == "spec") {
ChatId chat_id;
get_args(args, chat_id);
send_request(td_api::make_object<td_api::setPersonalChat>(chat_id));
} else if (op == "sese") { } else if (op == "sese") {
send_request(td_api::make_object<td_api::setEmojiStatus>(nullptr)); send_request(td_api::make_object<td_api::setEmojiStatus>(nullptr));
} else if (op == "ses") { } else if (op == "ses") {