Add and use statis UserId::get_user_ids/get_input_user_ids methods.

This commit is contained in:
levlam 2021-03-29 00:50:15 +03:00
parent 4df03c8491
commit 4f644ce389
6 changed files with 56 additions and 52 deletions

View File

@ -165,7 +165,7 @@ void AuthManager::check_bot_token(uint64 query_id, string bot_token) {
telegram_api::auth_importBotAuthorization(0, api_id_, api_hash_, bot_token_)));
}
void AuthManager::request_qr_code_authentication(uint64 query_id, vector<int32> other_user_ids) {
void AuthManager::request_qr_code_authentication(uint64 query_id, vector<UserId> other_user_ids) {
if (state_ != State::WaitPhoneNumber) {
if ((state_ == State::WaitCode || state_ == State::WaitPassword || state_ == State::WaitRegistration) &&
net_query_id_ == 0) {
@ -181,8 +181,7 @@ void AuthManager::request_qr_code_authentication(uint64 query_id, vector<int32>
"Cannot request QR code authentication after bot token was entered. You need to log out first"));
}
for (auto &other_user_id : other_user_ids) {
UserId user_id(other_user_id);
if (!user_id.is_valid()) {
if (!other_user_id.is_valid()) {
return on_query_error(query_id, Status::Error(400, "Invalid user_id among other user_ids"));
}
}
@ -200,8 +199,8 @@ void AuthManager::request_qr_code_authentication(uint64 query_id, vector<int32>
void AuthManager::send_export_login_token_query() {
poll_export_login_code_timeout_.cancel_timeout();
start_net_query(NetQueryType::RequestQrCode,
G()->net_query_creator().create_unauth(
telegram_api::auth_exportLoginToken(api_id_, api_hash_, vector<int32>(other_user_ids_))));
G()->net_query_creator().create_unauth(telegram_api::auth_exportLoginToken(
api_id_, api_hash_, UserId::get_input_user_ids(other_user_ids_))));
}
void AuthManager::set_login_token_expires_at(double login_token_expires_at) {

View File

@ -9,10 +9,10 @@
#include "td/telegram/net/NetActor.h"
#include "td/telegram/net/NetQuery.h"
#include "td/telegram/SendCodeHelper.h"
#include "td/telegram/TermsOfService.h"
#include "td/telegram/td_api.h"
#include "td/telegram/telegram_api.h"
#include "td/telegram/TermsOfService.h"
#include "td/telegram/UserId.h"
#include "td/actor/actor.h"
#include "td/actor/Timeout.h"
@ -38,7 +38,7 @@ class AuthManager : public NetActor {
void resend_authentication_code(uint64 query_id);
void check_code(uint64 query_id, string code);
void register_user(uint64 query_id, string first_name, string last_name);
void request_qr_code_authentication(uint64 query_id, vector<int32> other_user_ids);
void request_qr_code_authentication(uint64 query_id, vector<UserId> other_user_ids);
void check_bot_token(uint64 query_id, string bot_token);
void check_password(uint64 query_id, string password);
void request_password_recovery(uint64 query_id);
@ -113,7 +113,7 @@ class AuthManager : public NetActor {
SendCodeHelper send_code_helper_;
// WaitQrCodeConfirmation
vector<int32> other_user_ids_;
vector<UserId> other_user_ids_;
string login_token_;
double login_token_expires_at_ = 0;
@ -130,7 +130,7 @@ class AuthManager : public NetActor {
return state;
}
static DbState wait_qr_code_confirmation(int32 api_id, string api_hash, vector<int32> other_user_ids,
static DbState wait_qr_code_confirmation(int32 api_id, string api_hash, vector<UserId> other_user_ids,
string login_token, double login_token_expires_at) {
DbState state(State::WaitQrCodeConfirmation, api_id, api_hash);
state.other_user_ids_ = std::move(other_user_ids);
@ -179,7 +179,7 @@ class AuthManager : public NetActor {
string code_;
// State::WaitQrCodeConfirmation
vector<int32> other_user_ids_;
vector<UserId> other_user_ids_;
string login_token_;
double login_token_expires_at_ = 0.0;
int32 imported_dc_id_ = -1;

View File

@ -120,7 +120,7 @@ StringBuilder &operator<<(StringBuilder &string_builder, const DeviceTokenManage
}
void DeviceTokenManager::register_device(tl_object_ptr<td_api::DeviceToken> device_token_ptr,
vector<int32> other_user_ids,
vector<UserId> other_user_ids,
Promise<td_api::object_ptr<td_api::pushReceiverId>> promise) {
CHECK(device_token_ptr != nullptr);
TokenType token_type;
@ -227,8 +227,7 @@ void DeviceTokenManager::register_device(tl_object_ptr<td_api::DeviceToken> devi
return promise.set_error(Status::Error(400, "Device token must be encoded in UTF-8"));
}
for (auto &other_user_id : other_user_ids) {
UserId user_id(other_user_id);
if (!user_id.is_valid()) {
if (!other_user_id.is_valid()) {
return promise.set_error(Status::Error(400, "Invalid user_id among other user_ids"));
}
}
@ -373,12 +372,12 @@ void DeviceTokenManager::loop() {
auto other_user_ids = info.other_user_ids;
if (info.state == TokenInfo::State::Unregister) {
net_query = G()->net_query_creator().create(
telegram_api::account_unregisterDevice(token_type, info.token, std::move(other_user_ids)));
telegram_api::account_unregisterDevice(token_type, info.token, UserId::get_input_user_ids(other_user_ids)));
} else {
int32 flags = telegram_api::account_registerDevice::NO_MUTED_MASK;
net_query = G()->net_query_creator().create(
telegram_api::account_registerDevice(flags, false /*ignored*/, token_type, info.token, info.is_app_sandbox,
BufferSlice(info.encryption_key), std::move(other_user_ids)));
net_query = G()->net_query_creator().create(telegram_api::account_registerDevice(
flags, false /*ignored*/, token_type, info.token, info.is_app_sandbox, BufferSlice(info.encryption_key),
UserId::get_input_user_ids(other_user_ids)));
}
info.net_query_id = net_query->id();
G()->net_query_dispatcher().dispatch_with_callback(std::move(net_query), actor_shared(this, token_type));

View File

@ -10,8 +10,8 @@
#include "td/actor/PromiseFuture.h"
#include "td/telegram/net/NetQuery.h"
#include "td/telegram/td_api.h"
#include "td/telegram/UserId.h"
#include "td/utils/common.h"
#include "td/utils/Slice.h"
@ -26,7 +26,7 @@ class DeviceTokenManager : public NetQueryCallback {
public:
explicit DeviceTokenManager(ActorShared<> parent) : parent_(std::move(parent)) {
}
void register_device(tl_object_ptr<td_api::DeviceToken> device_token_ptr, vector<int32> other_user_ids,
void register_device(tl_object_ptr<td_api::DeviceToken> device_token_ptr, vector<UserId> other_user_ids,
Promise<td_api::object_ptr<td_api::pushReceiverId>> promise);
void reregister_device();
@ -55,7 +55,7 @@ class DeviceTokenManager : public NetQueryCallback {
State state = State::Sync;
string token;
uint64 net_query_id = 0;
vector<int32> other_user_ids;
vector<UserId> other_user_ids;
bool is_app_sandbox = false;
bool encrypt = false;
string encryption_key;

View File

@ -1840,11 +1840,11 @@ class CreateNewGroupChatRequest : public RequestActor<> {
}
public:
CreateNewGroupChatRequest(ActorShared<Td> td, uint64 request_id, vector<int32> user_ids, string title)
: RequestActor(std::move(td), request_id), title_(std::move(title)), random_id_(0) {
for (auto user_id : user_ids) {
user_ids_.emplace_back(user_id);
}
CreateNewGroupChatRequest(ActorShared<Td> td, uint64 request_id, vector<UserId> user_ids, string title)
: RequestActor(std::move(td), request_id)
, user_ids_(std::move(user_ids))
, title_(std::move(title))
, random_id_(0) {
}
};
@ -2058,16 +2058,14 @@ class GetChatEventLogRequest : public RequestOnceActor {
public:
GetChatEventLogRequest(ActorShared<Td> td, uint64 request_id, int64 dialog_id, string &&query, int64 from_event_id,
int32 limit, tl_object_ptr<td_api::chatEventLogFilters> &&filters, vector<int32> user_ids)
int32 limit, tl_object_ptr<td_api::chatEventLogFilters> &&filters, vector<UserId> user_ids)
: RequestOnceActor(std::move(td), request_id)
, dialog_id_(dialog_id)
, query_(std::move(query))
, from_event_id_(from_event_id)
, limit_(limit)
, filters_(std::move(filters)) {
for (auto user_id : user_ids) {
user_ids_.emplace_back(user_id);
}
, filters_(std::move(filters))
, user_ids_(std::move(user_ids)) {
}
};
@ -2152,11 +2150,8 @@ class RemoveContactsRequest : public RequestActor<> {
}
public:
RemoveContactsRequest(ActorShared<Td> td, uint64 request_id, vector<int32> &&user_ids)
: RequestActor(std::move(td), request_id) {
for (auto user_id : user_ids) {
user_ids_.emplace_back(user_id);
}
RemoveContactsRequest(ActorShared<Td> td, uint64 request_id, vector<UserId> &&user_ids)
: RequestActor(std::move(td), request_id), user_ids_(std::move(user_ids)) {
set_tries(3); // load_contacts + delete_contacts
}
};
@ -4761,7 +4756,7 @@ void Td::on_request(uint64 id, td_api::registerUser &request) {
void Td::on_request(uint64 id, td_api::requestQrCodeAuthentication &request) {
send_closure(auth_manager_actor_, &AuthManager::request_qr_code_authentication, id,
std::move(request.other_user_ids_));
UserId::get_user_ids(request.other_user_ids_));
}
void Td::on_request(uint64 id, td_api::checkAuthenticationPassword &request) {
@ -4950,7 +4945,7 @@ void Td::on_request(uint64 id, td_api::registerDevice &request) {
}
CREATE_REQUEST_PROMISE();
send_closure(device_token_manager_, &DeviceTokenManager::register_device, std::move(request.device_token_),
std::move(request.other_user_ids_), std::move(promise));
UserId::get_user_ids(request.other_user_ids_), std::move(promise));
}
void Td::on_request(uint64 id, td_api::getUserPrivacySettingRules &request) {
@ -5881,7 +5876,7 @@ void Td::on_request(uint64 id, td_api::createSecretChat &request) {
void Td::on_request(uint64 id, td_api::createNewBasicGroupChat &request) {
CHECK_IS_USER();
CLEAN_INPUT_STRING(request.title_);
CREATE_REQUEST(CreateNewGroupChatRequest, request.user_ids_, std::move(request.title_));
CREATE_REQUEST(CreateNewGroupChatRequest, UserId::get_user_ids(request.user_ids_), std::move(request.title_));
}
void Td::on_request(uint64 id, td_api::createNewSupergroupChat &request) {
@ -6023,12 +6018,8 @@ void Td::on_request(uint64 id, const td_api::revokeGroupCallInviteLink &request)
void Td::on_request(uint64 id, const td_api::inviteGroupCallParticipants &request) {
CHECK_IS_USER();
CREATE_OK_REQUEST_PROMISE();
vector<UserId> user_ids;
for (auto &user_id : request.user_ids_) {
user_ids.emplace_back(user_id);
}
group_call_manager_->invite_group_call_participants(GroupCallId(request.group_call_id_), std::move(user_ids),
std::move(promise));
group_call_manager_->invite_group_call_participants(GroupCallId(request.group_call_id_),
UserId::get_user_ids(request.user_ids_), std::move(promise));
}
void Td::on_request(uint64 id, const td_api::getGroupCallInviteLink &request) {
@ -6337,11 +6328,8 @@ void Td::on_request(uint64 id, const td_api::addChatMember &request) {
void Td::on_request(uint64 id, const td_api::addChatMembers &request) {
CHECK_IS_USER();
CREATE_OK_REQUEST_PROMISE();
vector<UserId> user_ids;
for (auto &user_id : request.user_ids_) {
user_ids.emplace_back(user_id);
}
contacts_manager_->add_dialog_participants(DialogId(request.chat_id_), user_ids, std::move(promise));
contacts_manager_->add_dialog_participants(DialogId(request.chat_id_), UserId::get_user_ids(request.user_ids_),
std::move(promise));
}
void Td::on_request(uint64 id, td_api::setChatMemberStatus &request) {
@ -6488,7 +6476,7 @@ void Td::on_request(uint64 id, td_api::getChatEventLog &request) {
CHECK_IS_USER();
CLEAN_INPUT_STRING(request.query_);
CREATE_REQUEST(GetChatEventLogRequest, request.chat_id_, std::move(request.query_), request.from_event_id_,
request.limit_, std::move(request.filters_), std::move(request.user_ids_));
request.limit_, std::move(request.filters_), UserId::get_user_ids(request.user_ids_));
}
void Td::on_request(uint64 id, const td_api::clearAllDraftMessages &request) {
@ -6728,7 +6716,7 @@ void Td::on_request(uint64 id, td_api::searchContacts &request) {
void Td::on_request(uint64 id, td_api::removeContacts &request) {
CHECK_IS_USER();
CREATE_REQUEST(RemoveContactsRequest, std::move(request.user_ids_));
CREATE_REQUEST(RemoveContactsRequest, UserId::get_user_ids(request.user_ids_));
}
void Td::on_request(uint64 id, const td_api::getImportedContactCount &request) {

View File

@ -25,6 +25,24 @@ class UserId {
template <class T, typename = std::enable_if_t<std::is_convertible<T, int32>::value>>
UserId(T user_id) = delete;
static vector<UserId> get_user_ids(const vector<int32> &input_user_ids) {
vector<UserId> user_ids;
user_ids.reserve(input_user_ids.size());
for (auto &input_user_id : input_user_ids) {
user_ids.emplace_back(input_user_id);
}
return user_ids;
}
static vector<int32> get_input_user_ids(const vector<UserId> &user_ids) {
vector<int32> input_user_ids;
input_user_ids.reserve(user_ids.size());
for (auto &user_id : user_ids) {
input_user_ids.emplace_back(user_id.get());
}
return input_user_ids;
}
bool is_valid() const {
return id > 0;
}