Make canSendMessageToUser response strongly typed.

This commit is contained in:
levlam 2024-01-19 22:14:03 +03:00
parent 0a6207700d
commit 94141a18dd
4 changed files with 26 additions and 12 deletions

View File

@ -5403,6 +5403,18 @@ readDatePrivacySettings show_read_date:Bool = ReadDatePrivacySettings;
newChatPrivacySettings allow_new_chats_from_unknown_users:Bool = NewChatPrivacySettings;
//@class CanSendMessageToUserResult @description Describes result of canSendMessageToUser
//@description The user can be messaged
canSendMessageToUserResultOk = CanSendMessageToUserResult;
//@description The user can't be messaged, because they are deleted or unknown
canSendMessageToUserResultUserIsDeleted = CanSendMessageToUserResult;
//@description The user can't be messaged, because they restrict new chats with non-contacts
canSendMessageToUserResultUserRestrictsNewChats = CanSendMessageToUserResult;
//@description Contains information about the period of inactivity after which the current user's account will automatically be deleted @days Number of days of inactivity before the account will be flagged for deletion; 30-366 days
accountTtl days:int32 = AccountTtl;
@ -9526,7 +9538,7 @@ 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;
canSendMessageToUser user_id:int53 = CanSendMessageToUserResult;
//@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"

View File

@ -5528,38 +5528,39 @@ 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, bool force, Promise<Unit> &&promise) {
void ContactsManager::can_send_message_to_user(
UserId user_id, bool force, Promise<td_api::object_ptr<td_api::CanSendMessageToUserResult>> &&promise) {
TRY_STATUS_PROMISE(promise, G()->close_status());
if (user_id == get_my_id()) {
return promise.set_value(Unit());
return promise.set_value(td_api::make_object<td_api::canSendMessageToUserResultOk>());
}
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"));
return promise.set_value(td_api::make_object<td_api::canSendMessageToUserResultUserIsDeleted>());
}
if (!u->contact_require_premium || td_->option_manager_->get_option_boolean("is_premium") || u->is_mutual_contact) {
return promise.set_value(Unit());
return promise.set_value(td_api::make_object<td_api::canSendMessageToUserResultOk>());
}
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_value(td_api::make_object<td_api::canSendMessageToUserResultOk>());
}
return promise.set_error(Status::Error(400, "Can't write to the user first"));
return promise.set_value(td_api::make_object<td_api::canSendMessageToUserResultUserRestrictsNewChats>());
}
auto it = user_full_contact_require_premium_.find(user_id);
if (it != user_full_contact_require_premium_.end()) {
if (!it->second) {
return promise.set_value(Unit());
return promise.set_value(td_api::make_object<td_api::canSendMessageToUserResultOk>());
}
return promise.set_error(Status::Error(400, "Can't write to the user first"));
return promise.set_value(td_api::make_object<td_api::canSendMessageToUserResultUserRestrictsNewChats>());
}
if (force) {
LOG(ERROR) << "Can't check " << user_id << " message privacy settings";
return promise.set_value(Unit());
return promise.set_value(td_api::make_object<td_api::canSendMessageToUserResultOk>());
}
auto query_promise = PromiseCreator::lambda(

View File

@ -185,7 +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, bool force, Promise<Unit> &&promise);
void can_send_message_to_user(UserId user_id, bool force,
Promise<td_api::object_ptr<td_api::CanSendMessageToUserResult>> &&promise);
void on_imported_contacts(int64 random_id, Result<tl_object_ptr<telegram_api::contacts_importedContacts>> result);

View File

@ -6530,7 +6530,7 @@ void Td::on_request(uint64 id, td_api::setNewChatPrivacySettings &request) {
void Td::on_request(uint64 id, const td_api::canSendMessageToUser &request) {
CHECK_IS_USER();
CREATE_OK_REQUEST_PROMISE();
CREATE_REQUEST_PROMISE();
contacts_manager_->can_send_message_to_user(UserId(request.user_id_), false, std::move(promise));
}