From 078417d423621c6984e034be6a8562dc33526560 Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 4 Apr 2019 21:50:26 +0300 Subject: [PATCH] Add mtpeer.access_hash parsing in push notifications. GitOrigin-RevId: bba57f1a73627383a2e363c821475cdc6a3dcaad --- td/telegram/ContactsManager.cpp | 33 +++++++++++++++++---------- td/telegram/ContactsManager.h | 1 + td/telegram/NotificationManager.cpp | 35 +++++++++++++++++++++++++++-- 3 files changed, 55 insertions(+), 14 deletions(-) diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index eeb06854..23b2278e 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -5080,10 +5080,10 @@ void ContactsManager::on_get_user(tl_object_ptr &&user_ptr, bool is_received = (flags & USER_FLAG_IS_INACCESSIBLE) == 0; User *u = add_user(user_id, "on_get_user"); - if ((have_access_hash || u->access_hash == -1) && u->access_hash != user->access_hash_) { - LOG(DEBUG) << "Access hash has changed for " << user_id << " from " << u->access_hash << " to " - << user->access_hash_; - u->access_hash = user->access_hash_; + auto access_hash = have_access_hash ? user->access_hash_ : -1; + if (have_access_hash && u->access_hash != access_hash) { + LOG(DEBUG) << "Access hash has changed for " << user_id << " from " << u->access_hash << " to " << access_hash; + u->access_hash = access_hash; u->is_changed = true; } if (is_received) { @@ -5135,13 +5135,6 @@ void ContactsManager::on_get_user(tl_object_ptr &&user_ptr, LOG_IF(ERROR, need_location_bot && !is_inline_bot) << "Receive not inline bot " << user_id << " which needs user location from " << source; - if (is_received && !u->is_received) { - u->is_received = true; - - LOG(DEBUG) << "Receive " << user_id; - u->need_send_update = true; - } - if (is_deleted) { // just in case is_verified = false; @@ -5163,7 +5156,7 @@ void ContactsManager::on_get_user(tl_object_ptr &&user_ptr, can_join_groups != u->can_join_groups || can_read_all_group_messages != u->can_read_all_group_messages || restriction_reason != u->restriction_reason || is_inline_bot != u->is_inline_bot || inline_query_placeholder != u->inline_query_placeholder || need_location_bot != u->need_location_bot) { - LOG_IF(ERROR, is_bot != u->is_bot && !is_deleted && !u->is_deleted) + LOG_IF(ERROR, is_bot != u->is_bot && !is_deleted && !u->is_deleted && u->is_received) << "User.is_bot has changed for " << user_id << "/" << u->username << " from " << source << " from " << u->is_bot << " to " << is_bot; u->is_verified = is_verified; @@ -5186,6 +5179,13 @@ void ContactsManager::on_get_user(tl_object_ptr &&user_ptr, u->is_changed = true; } + if (is_received && !u->is_received) { + u->is_received = true; + + LOG(DEBUG) << "Receive " << user_id; + u->need_send_update = true; + } + if (is_deleted != u->is_deleted) { u->is_deleted = is_deleted; @@ -6249,6 +6249,15 @@ void ContactsManager::update_user(User *u, UserId user_id, bool from_binlog, boo if (!from_database) { save_user(u, user_id, from_binlog); } + + if (!u->is_received && u->access_hash != -1 && !u->is_repaired) { + u->is_repaired = true; + auto input_user = get_input_user(user_id); + CHECK(input_user != nullptr); + vector> users; + users.push_back(std::move(input_user)); + td_->create_handler(Promise())->send(std::move(users)); + } } void ContactsManager::update_chat(Chat *c, ChatId chat_id, bool from_binlog, bool from_database) { diff --git a/td/telegram/ContactsManager.h b/td/telegram/ContactsManager.h index 1919f90d..8014d696 100644 --- a/td/telegram/ContactsManager.h +++ b/td/telegram/ContactsManager.h @@ -494,6 +494,7 @@ class ContactsManager : public Actor { bool need_location_bot = false; bool is_photo_inited = false; + bool is_repaired = false; bool is_name_changed = true; bool is_username_changed = true; diff --git a/td/telegram/NotificationManager.cpp b/td/telegram/NotificationManager.cpp index aa2fb455..67aa5c36 100644 --- a/td/telegram/NotificationManager.cpp +++ b/td/telegram/NotificationManager.cpp @@ -2849,7 +2849,7 @@ Status NotificationManager::process_push_notification_payload(string payload, Pr } announcement_message_text = field_value.second.get_string().str(); } else if (field_value.first == "google.sent_time") { - TRY_RESULT(google_sent_time, get_json_object_long_field(json_value.get_object(), "google.sent_time")); + TRY_RESULT(google_sent_time, get_json_object_long_field(data, "google.sent_time")); google_sent_time /= 1000; if (sent_date - 28 * 86400 <= google_sent_time && google_sent_time <= sent_date + 5) { sent_date = narrow_cast(google_sent_time); @@ -3062,6 +3062,37 @@ Status NotificationManager::process_push_notification_payload(string payload, Pr // chat title or sender name for PINNED_* loc_args.erase(loc_args.begin()); + if (sender_user_id.is_valid() && !td_->contacts_manager_->have_user_force(sender_user_id)) { + int64 sender_access_hash = -1; + telegram_api::object_ptr sender_photo; + TRY_RESULT(mtpeer, get_json_object_field(custom, "mtpeer", JsonValue::Type::Object)); + if (mtpeer.type() != JsonValue::Type::Null) { + TRY_RESULT(ah, get_json_object_string_field(mtpeer.get_object(), "ah")); + if (!ah.empty()) { + TRY_RESULT(sender_access_hash_safe, to_integer_safe(ah)); + sender_access_hash = sender_access_hash_safe; + } + TRY_RESULT(ph, get_json_object_field(mtpeer.get_object(), "ph", JsonValue::Type::Object)); + if (ph.type() != JsonValue::Type::Null) { + // TODO parse photo + } + } + + int32 flags = telegram_api::user::FIRST_NAME_MASK | telegram_api::user::MIN_MASK; + if (sender_access_hash != -1) { + flags |= telegram_api::user::ACCESS_HASH_MASK; + } + if (sender_photo != nullptr) { + flags |= telegram_api::user::PHOTO_MASK; + } + auto user = telegram_api::make_object( + flags, false /*ignored*/, false /*ignored*/, false /*ignored*/, false /*ignored*/, false /*ignored*/, + false /*ignored*/, false /*ignored*/, false /*ignored*/, false /*ignored*/, false /*ignored*/, + false /*ignored*/, false /*ignored*/, sender_user_id.get(), sender_access_hash, sender_name, string(), string(), + string(), std::move(sender_photo), nullptr, 0, string(), string(), string()); + td_->contacts_manager_->on_get_user(std::move(user), "process_push_notification_payload"); + } + if (loc_args.size() > 1) { return Status::Error("Receive too much arguments"); } @@ -3218,7 +3249,7 @@ void NotificationManager::process_message_push_notification(DialogId dialog_id, CHECK(logevent_id != 0); } - if (sender_user_id.is_valid() && !td_->contacts_manager_->have_user(sender_user_id)) { + if (sender_user_id.is_valid() && !td_->contacts_manager_->have_user_force(sender_user_id)) { int32 flags = telegram_api::user::FIRST_NAME_MASK | telegram_api::user::MIN_MASK; auto user = telegram_api::make_object( flags, false /*ignored*/, false /*ignored*/, false /*ignored*/, false /*ignored*/, false /*ignored*/,