Merge remote-tracking branch 'td/master'

This commit is contained in:
Andrea Cavalli 2021-10-01 15:06:14 +02:00
commit 5c2b4fe983
6 changed files with 77 additions and 90 deletions

View File

@ -36,10 +36,18 @@ UserId Contact::get_user_id() const {
return user_id_;
}
string Contact::get_phone_number() const {
const string &Contact::get_phone_number() const {
return phone_number_;
}
const string &Contact::get_first_name() const {
return first_name_;
}
const string &Contact::get_last_name() const {
return last_name_;
}
tl_object_ptr<td_api::contact> Contact::get_contact_object() const {
return make_tl_object<td_api::contact>(phone_number_, first_name_, last_name_, vcard_, user_id_.get());
}
@ -82,10 +90,7 @@ StringBuilder &operator<<(StringBuilder &string_builder, const Contact &contact)
<< ", vCard size = " << contact.vcard_.size() << contact.user_id_ << "]";
}
Result<Contact> process_input_message_contact(tl_object_ptr<td_api::InputMessageContent> &&input_message_content) {
CHECK(input_message_content != nullptr);
CHECK(input_message_content->get_id() == td_api::inputMessageContact::ID);
auto contact = std::move(static_cast<td_api::inputMessageContact *>(input_message_content.get())->contact_);
Result<Contact> get_contact(td_api::object_ptr<td_api::contact> &&contact) {
if (contact == nullptr) {
return Status::Error(400, "Contact must be non-empty");
}
@ -103,8 +108,14 @@ Result<Contact> process_input_message_contact(tl_object_ptr<td_api::InputMessage
return Status::Error(400, "vCard must be encoded in UTF-8");
}
return Contact(contact->phone_number_, contact->first_name_, contact->last_name_, contact->vcard_,
UserId(contact->user_id_));
return Contact(std::move(contact->phone_number_), std::move(contact->first_name_), std::move(contact->last_name_),
std::move(contact->vcard_), UserId(contact->user_id_));
}
Result<Contact> process_input_message_contact(tl_object_ptr<td_api::InputMessageContent> &&input_message_content) {
CHECK(input_message_content != nullptr);
CHECK(input_message_content->get_id() == td_api::inputMessageContact::ID);
return get_contact(std::move(static_cast<td_api::inputMessageContact *>(input_message_content.get())->contact_));
}
} // namespace td

View File

@ -46,7 +46,11 @@ class Contact {
UserId get_user_id() const;
string get_phone_number() const;
const string &get_phone_number() const;
const string &get_first_name() const;
const string &get_last_name() const;
tl_object_ptr<td_api::contact> get_contact_object() const;
@ -139,6 +143,8 @@ struct ContactHash {
}
};
Result<Contact> get_contact(td_api::object_ptr<td_api::contact> &&contact) TD_WARN_UNUSED_RESULT;
Result<Contact> process_input_message_contact(tl_object_ptr<td_api::InputMessageContent> &&input_message_content)
TD_WARN_UNUSED_RESULT;

View File

@ -445,15 +445,16 @@ class AddContactQuery final : public Td::ResultHandler {
explicit AddContactQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
}
void send(UserId user_id, tl_object_ptr<telegram_api::InputUser> &&input_user, const string &first_name,
const string &last_name, const string &phone_number, bool share_phone_number) {
void send(UserId user_id, tl_object_ptr<telegram_api::InputUser> &&input_user, const Contact &contact,
bool share_phone_number) {
user_id_ = user_id;
int32 flags = 0;
if (share_phone_number) {
flags |= telegram_api::contacts_addContact::ADD_PHONE_PRIVACY_EXCEPTION_MASK;
}
send_query(G()->net_query_creator().create(telegram_api::contacts_addContact(
flags, false /*ignored*/, std::move(input_user), first_name, last_name, phone_number)));
send_query(G()->net_query_creator().create(
telegram_api::contacts_addContact(flags, false /*ignored*/, std::move(input_user), contact.get_first_name(),
contact.get_last_name(), contact.get_phone_number())));
}
void on_result(uint64 id, BufferSlice packet) final {
@ -5325,12 +5326,7 @@ void ContactsManager::reload_contacts(bool force) {
}
}
void ContactsManager::add_contact(td_api::object_ptr<td_api::contact> &&contact, bool share_phone_number,
Promise<Unit> &&promise) {
if (contact == nullptr) {
return promise.set_error(Status::Error(400, "Added contact must be non-empty"));
}
void ContactsManager::add_contact(Contact contact, bool share_phone_number, Promise<Unit> &&promise) {
if (G()->close_flag()) {
return promise.set_error(Status::Error(500, "Request aborted"));
}
@ -5343,21 +5339,20 @@ void ContactsManager::add_contact(td_api::object_ptr<td_api::contact> &&contact,
return;
}
LOG(INFO) << "Add " << oneline(to_string(contact)) << " with share_phone_number = " << share_phone_number;
LOG(INFO) << "Add " << contact << " with share_phone_number = " << share_phone_number;
UserId user_id{contact->user_id_};
auto user_id = contact.get_user_id();
auto input_user = get_input_user(user_id);
if (input_user == nullptr) {
return promise.set_error(Status::Error(400, "User not found"));
}
td_->create_handler<AddContactQuery>(std::move(promise))
->send(user_id, std::move(input_user), contact->first_name_, contact->last_name_, contact->phone_number_,
share_phone_number);
->send(user_id, std::move(input_user), contact, share_phone_number);
}
std::pair<vector<UserId>, vector<int32>> ContactsManager::import_contacts(
const vector<tl_object_ptr<td_api::contact>> &contacts, int64 &random_id, Promise<Unit> &&promise) {
std::pair<vector<UserId>, vector<int32>> ContactsManager::import_contacts(const vector<Contact> &contacts,
int64 &random_id, Promise<Unit> &&promise) {
if (!are_contacts_loaded_) {
load_contacts(std::move(promise));
return {};
@ -5374,25 +5369,13 @@ std::pair<vector<UserId>, vector<int32>> ContactsManager::import_contacts(
promise.set_value(Unit());
return result;
}
for (auto &contact : contacts) {
if (contact == nullptr) {
promise.set_error(Status::Error(400, "Imported contacts must be non-empty"));
return {};
}
}
do {
random_id = Random::secure_int64();
} while (random_id == 0 || imported_contacts_.find(random_id) != imported_contacts_.end());
imported_contacts_[random_id]; // reserve place for result
td_->create_handler<ImportContactsQuery>(std::move(promise))
->send(transform(contacts,
[](const tl_object_ptr<td_api::contact> &contact) {
return Contact(contact->phone_number_, contact->first_name_, contact->last_name_, string(),
UserId());
}),
random_id);
td_->create_handler<ImportContactsQuery>(std::move(promise))->send(contacts, random_id);
return {};
}
@ -5529,8 +5512,9 @@ void ContactsManager::on_load_imported_contacts_finished() {
}
}
std::pair<vector<UserId>, vector<int32>> ContactsManager::change_imported_contacts(
vector<tl_object_ptr<td_api::contact>> &&contacts, int64 &random_id, Promise<Unit> &&promise) {
std::pair<vector<UserId>, vector<int32>> ContactsManager::change_imported_contacts(vector<Contact> &contacts,
int64 &random_id,
Promise<Unit> &&promise) {
if (!are_contacts_loaded_) {
load_contacts(std::move(promise));
return {};
@ -5571,26 +5555,14 @@ std::pair<vector<UserId>, vector<int32>> ContactsManager::change_imported_contac
return {};
}
for (auto &contact : contacts) {
if (contact == nullptr) {
promise.set_error(Status::Error(400, "Contacts must be non-empty"));
return {};
}
}
auto new_contacts = transform(std::move(contacts), [](tl_object_ptr<td_api::contact> &&contact) {
return Contact(std::move(contact->phone_number_), std::move(contact->first_name_), std::move(contact->last_name_),
string(), UserId());
});
vector<size_t> new_contacts_unique_id(new_contacts.size());
vector<size_t> new_contacts_unique_id(contacts.size());
vector<Contact> unique_new_contacts;
unique_new_contacts.reserve(new_contacts.size());
unique_new_contacts.reserve(contacts.size());
std::unordered_map<Contact, size_t, ContactHash, ContactEqual> different_new_contacts;
std::unordered_set<string> different_new_phone_numbers;
size_t unique_size = 0;
for (size_t i = 0; i < new_contacts.size(); i++) {
auto it_success = different_new_contacts.emplace(std::move(new_contacts[i]), unique_size);
for (size_t i = 0; i < contacts.size(); i++) {
auto it_success = different_new_contacts.emplace(std::move(contacts[i]), unique_size);
new_contacts_unique_id[i] = it_success.first->second;
if (it_success.second) {
unique_new_contacts.push_back(it_success.first->first);
@ -5624,14 +5596,14 @@ std::pair<vector<UserId>, vector<int32>> ContactsManager::change_imported_contac
}
if (to_add.first.empty() && to_delete.empty()) {
for (size_t i = 0; i < new_contacts.size(); i++) {
for (size_t i = 0; i < contacts.size(); i++) {
auto unique_id = new_contacts_unique_id[i];
new_contacts[i].set_user_id(unique_new_contacts[unique_id].get_user_id());
contacts[i].set_user_id(unique_new_contacts[unique_id].get_user_id());
}
promise.set_value(Unit());
return {transform(new_contacts, [&](const Contact &contact) { return contact.get_user_id(); }),
vector<int32>(new_contacts.size())};
return {transform(contacts, [&](const Contact &contact) { return contact.get_user_id(); }),
vector<int32>(contacts.size())};
}
are_imported_contacts_changing_ = true;

View File

@ -290,10 +290,10 @@ class ContactsManager final : public Actor {
void disconnect_website(int64 authorizations_id, Promise<Unit> &&promise) const;
void disconnect_all_websites(Promise<Unit> &&promise) const;
void add_contact(td_api::object_ptr<td_api::contact> &&contact, bool share_phone_number, Promise<Unit> &&promise);
void add_contact(Contact contact, bool share_phone_number, Promise<Unit> &&promise);
std::pair<vector<UserId>, vector<int32>> import_contacts(const vector<tl_object_ptr<td_api::contact>> &contacts,
int64 &random_id, Promise<Unit> &&promise);
std::pair<vector<UserId>, vector<int32>> import_contacts(const vector<Contact> &contacts, int64 &random_id,
Promise<Unit> &&promise);
std::pair<int32, vector<UserId>> search_contacts(const string &query, int32 limit, Promise<Unit> &&promise);
@ -304,8 +304,8 @@ class ContactsManager final : public Actor {
int32 get_imported_contact_count(Promise<Unit> &&promise);
std::pair<vector<UserId>, vector<int32>> change_imported_contacts(vector<tl_object_ptr<td_api::contact>> &&contacts,
int64 &random_id, Promise<Unit> &&promise);
std::pair<vector<UserId>, vector<int32>> change_imported_contacts(vector<Contact> &contacts, int64 &random_id,
Promise<Unit> &&promise);
void clear_imported_contacts(Promise<Unit> &&promise);

View File

@ -89,7 +89,7 @@ Result<unique_ptr<DraftMessage>> get_draft_message(ContactsManager *contacts_man
result->input_message_text = std::move(message_content);
}
return result;
return std::move(result);
}
} // namespace td

View File

@ -1926,7 +1926,7 @@ class GetBlockedMessageSendersRequest final : public RequestActor<> {
};
class ImportContactsRequest final : public RequestActor<> {
vector<tl_object_ptr<td_api::contact>> contacts_;
vector<Contact> contacts_;
int64 random_id_;
std::pair<vector<UserId>, vector<int32>> imported_contacts_;
@ -1947,7 +1947,7 @@ class ImportContactsRequest final : public RequestActor<> {
}
public:
ImportContactsRequest(ActorShared<Td> td, uint64 request_id, vector<tl_object_ptr<td_api::contact>> &&contacts)
ImportContactsRequest(ActorShared<Td> td, uint64 request_id, vector<Contact> &&contacts)
: RequestActor(std::move(td), request_id), contacts_(std::move(contacts)), random_id_(0) {
set_tries(3); // load_contacts + import_contacts
}
@ -2004,15 +2004,14 @@ class GetImportedContactCountRequest final : public RequestActor<> {
};
class ChangeImportedContactsRequest final : public RequestActor<> {
vector<tl_object_ptr<td_api::contact>> contacts_;
vector<Contact> contacts_;
size_t contacts_size_;
int64 random_id_;
std::pair<vector<UserId>, vector<int32>> imported_contacts_;
void do_run(Promise<Unit> &&promise) final {
imported_contacts_ =
td->contacts_manager_->change_imported_contacts(std::move(contacts_), random_id_, std::move(promise));
imported_contacts_ = td->contacts_manager_->change_imported_contacts(contacts_, random_id_, std::move(promise));
}
void do_send_result() final {
@ -2027,8 +2026,7 @@ class ChangeImportedContactsRequest final : public RequestActor<> {
}
public:
ChangeImportedContactsRequest(ActorShared<Td> td, uint64 request_id,
vector<tl_object_ptr<td_api::contact>> &&contacts)
ChangeImportedContactsRequest(ActorShared<Td> td, uint64 request_id, vector<Contact> &&contacts)
: RequestActor(std::move(td), request_id)
, contacts_(std::move(contacts))
, contacts_size_(contacts_.size())
@ -6686,27 +6684,26 @@ void Td::on_request(uint64 id, const td_api::getBlockedMessageSenders &request)
void Td::on_request(uint64 id, td_api::addContact &request) {
CHECK_IS_USER();
if (request.contact_ == nullptr) {
return send_error_raw(id, 400, "Contact must be non-empty");
auto r_contact = get_contact(std::move(request.contact_));
if (r_contact.is_error()) {
return send_closure(actor_id(this), &Td::send_error, id, r_contact.move_as_error());
}
CLEAN_INPUT_STRING(request.contact_->phone_number_);
CLEAN_INPUT_STRING(request.contact_->first_name_);
CLEAN_INPUT_STRING(request.contact_->last_name_);
CREATE_OK_REQUEST_PROMISE();
contacts_manager_->add_contact(std::move(request.contact_), request.share_phone_number_, std::move(promise));
contacts_manager_->add_contact(r_contact.move_as_ok(), request.share_phone_number_, std::move(promise));
}
void Td::on_request(uint64 id, td_api::importContacts &request) {
CHECK_IS_USER();
vector<Contact> contacts;
contacts.reserve(request.contacts_.size());
for (auto &contact : request.contacts_) {
if (contact == nullptr) {
return send_error_raw(id, 400, "Contact must be non-empty");
auto r_contact = get_contact(std::move(contact));
if (r_contact.is_error()) {
return send_closure(actor_id(this), &Td::send_error, id, r_contact.move_as_error());
}
CLEAN_INPUT_STRING(contact->phone_number_);
CLEAN_INPUT_STRING(contact->first_name_);
CLEAN_INPUT_STRING(contact->last_name_);
contacts.push_back(r_contact.move_as_ok());
}
CREATE_REQUEST(ImportContactsRequest, std::move(request.contacts_));
CREATE_REQUEST(ImportContactsRequest, std::move(contacts));
}
void Td::on_request(uint64 id, const td_api::getContacts &request) {
@ -6732,15 +6729,16 @@ void Td::on_request(uint64 id, const td_api::getImportedContactCount &request) {
void Td::on_request(uint64 id, td_api::changeImportedContacts &request) {
CHECK_IS_USER();
vector<Contact> contacts;
contacts.reserve(request.contacts_.size());
for (auto &contact : request.contacts_) {
if (contact == nullptr) {
return send_error_raw(id, 400, "Contact must be non-empty");
auto r_contact = get_contact(std::move(contact));
if (r_contact.is_error()) {
return send_closure(actor_id(this), &Td::send_error, id, r_contact.move_as_error());
}
CLEAN_INPUT_STRING(contact->phone_number_);
CLEAN_INPUT_STRING(contact->first_name_);
CLEAN_INPUT_STRING(contact->last_name_);
contacts.push_back(r_contact.move_as_ok());
}
CREATE_REQUEST(ChangeImportedContactsRequest, std::move(request.contacts_));
CREATE_REQUEST(ChangeImportedContactsRequest, std::move(contacts));
}
void Td::on_request(uint64 id, const td_api::clearImportedContacts &request) {