Move toggle_dialog_has_protected_content to DialogManager.

This commit is contained in:
levlam 2024-01-04 20:25:41 +03:00
parent 17012dff1c
commit e5f0b33508
5 changed files with 84 additions and 84 deletions

View File

@ -121,6 +121,44 @@ class EditChatDefaultBannedRightsQuery final : public Td::ResultHandler {
}
};
class ToggleNoForwardsQuery final : public Td::ResultHandler {
Promise<Unit> promise_;
DialogId dialog_id_;
public:
explicit ToggleNoForwardsQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
}
void send(DialogId dialog_id, bool has_protected_content) {
dialog_id_ = dialog_id;
auto input_peer = td_->dialog_manager_->get_input_peer(dialog_id, AccessRights::Read);
CHECK(input_peer != nullptr);
send_query(G()->net_query_creator().create(
telegram_api::messages_toggleNoForwards(std::move(input_peer), has_protected_content)));
}
void on_result(BufferSlice packet) final {
auto result_ptr = fetch_result<telegram_api::messages_toggleNoForwards>(packet);
if (result_ptr.is_error()) {
return on_error(result_ptr.move_as_error());
}
auto ptr = result_ptr.move_as_ok();
LOG(INFO) << "Receive result for ToggleNoForwardsQuery: " << to_string(ptr);
td_->updates_manager_->on_get_updates(std::move(ptr), std::move(promise_));
}
void on_error(Status status) final {
if (status.message() == "CHAT_NOT_MODIFIED") {
promise_.set_value(Unit());
return;
} else {
td_->dialog_manager_->on_get_dialog_error(dialog_id_, status, "ToggleNoForwardsQuery");
}
promise_.set_error(std::move(status));
}
};
DialogManager::DialogManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) {
}
@ -917,6 +955,48 @@ void DialogManager::set_dialog_emoji_status(DialogId dialog_id, const EmojiStatu
promise.set_error(Status::Error(400, "Can't change emoji status in the chat"));
}
void DialogManager::toggle_dialog_has_protected_content(DialogId dialog_id, bool has_protected_content,
Promise<Unit> &&promise) {
if (!have_dialog_force(dialog_id, "toggle_dialog_has_protected_content")) {
return promise.set_error(Status::Error(400, "Chat not found"));
}
if (!have_input_peer(dialog_id, AccessRights::Read)) {
return promise.set_error(Status::Error(400, "Can't access the chat"));
}
switch (dialog_id.get_type()) {
case DialogType::User:
case DialogType::SecretChat:
return promise.set_error(Status::Error(400, "Can't restrict saving content in the chat"));
case DialogType::Chat: {
auto chat_id = dialog_id.get_chat_id();
auto status = td_->contacts_manager_->get_chat_status(chat_id);
if (!status.is_creator()) {
return promise.set_error(Status::Error(400, "Only owner can restrict saving content"));
}
break;
}
case DialogType::Channel: {
auto status = td_->contacts_manager_->get_channel_status(dialog_id.get_channel_id());
if (!status.is_creator()) {
return promise.set_error(Status::Error(400, "Only owner can restrict saving content"));
}
break;
}
case DialogType::None:
default:
UNREACHABLE();
}
// TODO this can be wrong if there were previous toggle_dialog_has_protected_content requests
if (get_dialog_has_protected_content(dialog_id) == has_protected_content) {
return promise.set_value(Unit());
}
// TODO invoke after
td_->create_handler<ToggleNoForwardsQuery>(std::move(promise))->send(dialog_id, has_protected_content);
}
void DialogManager::set_dialog_description(DialogId dialog_id, const string &description, Promise<Unit> &&promise) {
if (!have_dialog_force(dialog_id, "set_dialog_description")) {
return promise.set_error(Status::Error(400, "Chat not found"));

View File

@ -131,6 +131,8 @@ class DialogManager final : public Actor {
void set_dialog_emoji_status(DialogId dialog_id, const EmojiStatus &emoji_status, Promise<Unit> &&promise);
void toggle_dialog_has_protected_content(DialogId dialog_id, bool has_protected_content, Promise<Unit> &&promise);
void set_dialog_description(DialogId dialog_id, const string &description, Promise<Unit> &&promise);
Status can_pin_messages(DialogId dialog_id) const;

View File

@ -1227,44 +1227,6 @@ class SetHistoryTtlQuery final : public Td::ResultHandler {
}
};
class ToggleNoForwardsQuery final : public Td::ResultHandler {
Promise<Unit> promise_;
DialogId dialog_id_;
public:
explicit ToggleNoForwardsQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
}
void send(DialogId dialog_id, bool has_protected_content) {
dialog_id_ = dialog_id;
auto input_peer = td_->dialog_manager_->get_input_peer(dialog_id, AccessRights::Read);
CHECK(input_peer != nullptr);
send_query(G()->net_query_creator().create(
telegram_api::messages_toggleNoForwards(std::move(input_peer), has_protected_content)));
}
void on_result(BufferSlice packet) final {
auto result_ptr = fetch_result<telegram_api::messages_toggleNoForwards>(packet);
if (result_ptr.is_error()) {
return on_error(result_ptr.move_as_error());
}
auto ptr = result_ptr.move_as_ok();
LOG(INFO) << "Receive result for ToggleNoForwardsQuery: " << to_string(ptr);
td_->updates_manager_->on_get_updates(std::move(ptr), std::move(promise_));
}
void on_error(Status status) final {
if (status.message() == "CHAT_NOT_MODIFIED") {
promise_.set_value(Unit());
return;
} else {
td_->dialog_manager_->on_get_dialog_error(dialog_id_, status, "ToggleNoForwardsQuery");
}
promise_.set_error(std::move(status));
}
};
class ToggleDialogPinQuery final : public Td::ResultHandler {
Promise<Unit> promise_;
DialogId dialog_id_;
@ -33173,48 +33135,6 @@ void MessagesManager::set_dialog_message_ttl(DialogId dialog_id, int32 ttl, Prom
}
}
void MessagesManager::toggle_dialog_has_protected_content(DialogId dialog_id, bool has_protected_content,
Promise<Unit> &&promise) {
if (!have_dialog_force(dialog_id, "toggle_dialog_has_protected_content")) {
return promise.set_error(Status::Error(400, "Chat not found"));
}
if (!td_->dialog_manager_->have_input_peer(dialog_id, AccessRights::Read)) {
return promise.set_error(Status::Error(400, "Can't access the chat"));
}
switch (dialog_id.get_type()) {
case DialogType::User:
case DialogType::SecretChat:
return promise.set_error(Status::Error(400, "Can't restrict saving content in the chat"));
case DialogType::Chat: {
auto chat_id = dialog_id.get_chat_id();
auto status = td_->contacts_manager_->get_chat_status(chat_id);
if (!status.is_creator()) {
return promise.set_error(Status::Error(400, "Only owner can restrict saving content"));
}
break;
}
case DialogType::Channel: {
auto status = td_->contacts_manager_->get_channel_status(dialog_id.get_channel_id());
if (!status.is_creator()) {
return promise.set_error(Status::Error(400, "Only owner can restrict saving content"));
}
break;
}
case DialogType::None:
default:
UNREACHABLE();
}
// TODO this can be wrong if there were previous toggle_dialog_has_protected_content requests
if (td_->dialog_manager_->get_dialog_has_protected_content(dialog_id) == has_protected_content) {
return promise.set_value(Unit());
}
// TODO invoke after
td_->create_handler<ToggleNoForwardsQuery>(std::move(promise))->send(dialog_id, has_protected_content);
}
void MessagesManager::set_dialog_theme(DialogId dialog_id, const string &theme_name, Promise<Unit> &&promise) {
auto d = get_dialog_force(dialog_id, "set_dialog_theme");
if (d == nullptr) {

View File

@ -531,8 +531,6 @@ class MessagesManager final : public Actor {
td_api::object_ptr<td_api::ChatAvailableReactions> &&available_reactions_ptr,
Promise<Unit> &&promise);
void toggle_dialog_has_protected_content(DialogId dialog_id, bool has_protected_content, 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,

View File

@ -6610,8 +6610,8 @@ void Td::on_request(uint64 id, td_api::setChatDraftMessage &request) {
void Td::on_request(uint64 id, const td_api::toggleChatHasProtectedContent &request) {
CHECK_IS_USER();
CREATE_OK_REQUEST_PROMISE();
messages_manager_->toggle_dialog_has_protected_content(DialogId(request.chat_id_), request.has_protected_content_,
std::move(promise));
dialog_manager_->toggle_dialog_has_protected_content(DialogId(request.chat_id_), request.has_protected_content_,
std::move(promise));
}
void Td::on_request(uint64 id, const td_api::toggleChatIsPinned &request) {