From 5624f9115555efc0e18997a52ea7d4f0f7ce686f Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 19 Jan 2024 15:35:57 +0300 Subject: [PATCH] Add td_api::canSendMessageToUser. --- td/generate/scheme/td_api.tl | 4 ++++ td/telegram/ContactsManager.cpp | 21 +++++++++++++++++++++ td/telegram/ContactsManager.h | 2 ++ td/telegram/Td.cpp | 6 ++++++ td/telegram/Td.h | 2 ++ td/telegram/cli.cpp | 4 ++++ 6 files changed, 39 insertions(+) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 3ac0c8b58..cdb80c1ca 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -9525,6 +9525,10 @@ setNewChatPrivacySettings settings:newChatPrivacySettings = Ok; getNewChatPrivacySettings = NewChatPrivacySettings; +//@description Check whether the current user can message another user or try to create a chat with them @user_id Identifier of the other user +canSendMessageToUser user_id:int53 = Ok; + + //@description Returns the value of an option by its name. (Check the list of available options on https://core.telegram.org/tdlib/options.) Can be called before authorization. Can be called synchronously for options "version" and "commit_hash" //@name The name of the option getOption name:string = OptionValue; diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index ce3e3a5ac..90b8c63da 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -5491,6 +5491,27 @@ int32 ContactsManager::get_user_was_online(const User *u, UserId user_id, int32 return was_online; } +void ContactsManager::can_send_message_to_user(UserId user_id, Promise &&promise) { + if (user_id == get_my_id()) { + return promise.set_value(Unit()); + } + const auto *u = get_user(user_id); + if (!have_input_peer_user(u, user_id, AccessRights::Write)) { + return promise.set_error(Status::Error(400, "Have no write access to the chat")); + } + if (!u->contact_require_premium || td_->option_manager_->get_option_boolean("is_premium") || u->is_mutual_contact) { + return promise.set_value(Unit()); + } + auto user_full = get_user_full_force(user_id, "can_send_message_to_user"); + if (user_full != nullptr) { + if (!user_full->contact_require_premium) { + return promise.set_value(Unit()); + } + return promise.set_error(Status::Error(400, "Can't write to the user first")); + } + return promise.set_value(Unit()); +} + void ContactsManager::load_contacts(Promise &&promise) { if (td_->auth_manager_->is_bot()) { are_contacts_loaded_ = true; diff --git a/td/telegram/ContactsManager.h b/td/telegram/ContactsManager.h index 7e9164729..e67f1ccd2 100644 --- a/td/telegram/ContactsManager.h +++ b/td/telegram/ContactsManager.h @@ -185,6 +185,8 @@ class ContactsManager final : public Actor { int32 get_secret_chat_layer(SecretChatId secret_chat_id) const; FolderId get_secret_chat_initial_folder_id(SecretChatId secret_chat_id) const; + void can_send_message_to_user(UserId user_id, Promise &&promise); + void on_imported_contacts(int64 random_id, Result> result); void on_deleted_contacts(const vector &deleted_contact_user_ids); diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 0efb4bc8c..2055e5087 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -6528,6 +6528,12 @@ void Td::on_request(uint64 id, td_api::setNewChatPrivacySettings &request) { std::move(promise)); } +void Td::on_request(uint64 id, const td_api::canSendMessageToUser &request) { + CHECK_IS_USER(); + CREATE_OK_REQUEST_PROMISE(); + contacts_manager_->can_send_message_to_user(UserId(request.user_id_), std::move(promise)); +} + void Td::on_request(uint64 id, td_api::setChatTitle &request) { CLEAN_INPUT_STRING(request.title_); CREATE_OK_REQUEST_PROMISE(); diff --git a/td/telegram/Td.h b/td/telegram/Td.h index 899d47213..f3700db74 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -1059,6 +1059,8 @@ class Td final : public Actor { void on_request(uint64 id, td_api::setNewChatPrivacySettings &request); + void on_request(uint64 id, const td_api::canSendMessageToUser &request); + void on_request(uint64 id, td_api::setChatTitle &request); void on_request(uint64 id, const td_api::setChatPhoto &request); diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index 6c815911e..80167349f 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -5449,6 +5449,10 @@ class CliClient final : public Actor { get_args(args, allow_new_chats_from_unknown_users); auto settings = td_api::make_object(allow_new_chats_from_unknown_users); send_request(td_api::make_object(std::move(settings))); + } else if (op == "csmtu") { + UserId user_id; + get_args(args, user_id); + send_request(td_api::make_object(user_id)); } else if (op == "sct") { ChatId chat_id; string title;