Add td_api::setChatTheme.

This commit is contained in:
levlam 2021-08-27 19:23:22 +03:00
parent 532a0f4502
commit c20fd1dd91
6 changed files with 113 additions and 0 deletions

View File

@ -4591,6 +4591,10 @@ setChatMessageTtlSetting chat_id:int53 ttl:int32 = Ok;
//@chat_id Chat identifier @permissions New non-administrator members permissions in the chat
setChatPermissions chat_id:int53 permissions:chatPermissions = Ok;
//@description Changes the chat theme. Requires can_change_info administrator right in groups, supergroups and channels @chat_id Chat identifier
//@theme_name Name of the new chat theme; may be empty to return the default theme
setChatTheme chat_id:int53 theme_name:string = Ok;
//@description Changes the draft message in a chat @chat_id Chat identifier @message_thread_id If not 0, a message thread identifier in which the draft was changed @draft_message New draft message; may be null
setChatDraftMessage chat_id:int53 message_thread_id:int53 draft_message:draftMessage = Ok;

View File

@ -1364,6 +1364,45 @@ class EditDialogTitleQuery final : public Td::ResultHandler {
}
};
class SetChatThemeQuery final : public Td::ResultHandler {
Promise<Unit> promise_;
DialogId dialog_id_;
public:
explicit SetChatThemeQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
}
void send(DialogId dialog_id, const string &theme_name) {
dialog_id_ = dialog_id;
auto input_peer = td->messages_manager_->get_input_peer(dialog_id, AccessRights::Write);
CHECK(input_peer != nullptr);
send_query(G()->net_query_creator().create(telegram_api::messages_setChatTheme(std::move(input_peer), theme_name)));
}
void on_result(uint64 id, BufferSlice packet) final {
auto result_ptr = fetch_result<telegram_api::messages_setChatTheme>(packet);
if (result_ptr.is_error()) {
return on_error(id, result_ptr.move_as_error());
}
auto ptr = result_ptr.move_as_ok();
LOG(INFO) << "Receive result for SetChatThemeQuery: " << to_string(ptr);
td->updates_manager_->on_get_updates(std::move(ptr), std::move(promise_));
}
void on_error(uint64 id, Status status) final {
if (status.message() == "CHAT_NOT_MODIFIED") {
if (!td->auth_manager_->is_bot()) {
promise_.set_value(Unit());
return;
}
} else {
td->messages_manager_->on_get_dialog_error(dialog_id_, status, "SetChatThemeQuery");
}
promise_.set_error(std::move(status));
}
};
class SetHistoryTtlQuery final : public Td::ResultHandler {
Promise<Unit> promise_;
DialogId dialog_id_;
@ -31309,6 +31348,60 @@ void MessagesManager::set_dialog_permissions(DialogId dialog_id,
td_->create_handler<EditChatDefaultBannedRightsQuery>(std::move(promise))->send(dialog_id, new_permissions);
}
void MessagesManager::set_dialog_theme(DialogId dialog_id, const string &theme_name, Promise<Unit> &&promise) {
LOG(INFO) << "Receive setChatTheme request to change theme of " << dialog_id << " to " << theme_name;
auto d = get_dialog_force(dialog_id, "set_dialog_theme");
if (d == nullptr) {
return promise.set_error(Status::Error(3, "Chat not found"));
}
if (!have_input_peer(dialog_id, AccessRights::Write)) {
return promise.set_error(Status::Error(3, "Can't access the chat"));
}
switch (dialog_id.get_type()) {
case DialogType::User:
break;
case DialogType::Chat: {
auto chat_id = dialog_id.get_chat_id();
auto status = td_->contacts_manager_->get_chat_permissions(chat_id);
if (!status.can_change_info_and_settings()) {
return promise.set_error(Status::Error(3, "Not enough rights to change chat theme"));
}
break;
}
case DialogType::Channel: {
if (is_broadcast_channel(dialog_id)) {
return promise.set_error(Status::Error(3, "Can't change channel chat permissions"));
}
auto status = td_->contacts_manager_->get_channel_permissions(dialog_id.get_channel_id());
if (!status.can_change_info_and_settings()) {
return promise.set_error(Status::Error(3, "Not enough rights to change chat theme"));
}
break;
}
case DialogType::SecretChat: {
auto user_id = td_->contacts_manager_->get_secret_chat_user_id(dialog_id.get_secret_chat_id());
if (!user_id.is_valid()) {
return promise.set_error(Status::Error(3, "Can't access the user"));
}
dialog_id = DialogId(user_id);
break;
}
case DialogType::None:
default:
UNREACHABLE();
}
// TODO this can be wrong if there was previous change theme requests
if (d->theme_name == theme_name) {
return promise.set_value(Unit());
}
// TODO invoke after
td_->create_handler<SetChatThemeQuery>(std::move(promise))->send(dialog_id, theme_name);
}
void MessagesManager::set_dialog_description(DialogId dialog_id, const string &description, Promise<Unit> &&promise) {
LOG(INFO) << "Receive setChatDescription request to set description of " << dialog_id << " to \"" << description
<< '"';

View File

@ -490,6 +490,8 @@ class MessagesManager final : public Actor {
void set_dialog_permissions(DialogId dialog_id, const td_api::object_ptr<td_api::chatPermissions> &permissions,
Promise<Unit> &&promise);
void set_dialog_theme(DialogId dialog_id, const string &theme_name, Promise<Unit> &&promise);
void pin_dialog_message(DialogId dialog_id, MessageId message_id, bool disable_notification, bool only_for_self,
bool is_unpin, Promise<Unit> &&promise);

View File

@ -6296,6 +6296,13 @@ void Td::on_request(uint64 id, const td_api::setChatPermissions &request) {
messages_manager_->set_dialog_permissions(DialogId(request.chat_id_), request.permissions_, std::move(promise));
}
void Td::on_request(uint64 id, td_api::setChatTheme &request) {
CHECK_IS_USER();
CLEAN_INPUT_STRING(request.theme_name_);
CREATE_OK_REQUEST_PROMISE();
messages_manager_->set_dialog_theme(DialogId(request.chat_id_), request.theme_name_, std::move(promise));
}
void Td::on_request(uint64 id, td_api::setChatDraftMessage &request) {
CHECK_IS_USER();
answer_ok_query(

View File

@ -799,6 +799,8 @@ class Td final : public NetQueryCallback {
void on_request(uint64 id, const td_api::setChatPermissions &request);
void on_request(uint64 id, td_api::setChatTheme &request);
void on_request(uint64 id, td_api::setChatDraftMessage &request);
void on_request(uint64 id, const td_api::toggleChatIsPinned &request);

View File

@ -3778,6 +3778,11 @@ class CliClient final : public Actor {
} else {
LOG(ERROR) << "Wrong permissions size, expected 8";
}
} else if (op == "sctn") {
string chat_id;
string theme_name;
get_args(args, chat_id, theme_name);
send_request(td_api::make_object<td_api::setChatTheme>(as_chat_id(chat_id), theme_name));
} else if (op == "sccd") {
string chat_id;
string client_data;