From 0f8b5f89dccdc73004f9d4fd89eba1fea98d3505 Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 25 Mar 2019 06:35:01 +0300 Subject: [PATCH] Basic push notifications parsing. GitOrigin-RevId: 1e7e955551831d1438826d1e5a860c24a6432f23 --- td/telegram/NotificationManager.cpp | 89 ++++++++++++++++++++++++++--- td/telegram/NotificationManager.h | 4 +- 2 files changed, 84 insertions(+), 9 deletions(-) diff --git a/td/telegram/NotificationManager.cpp b/td/telegram/NotificationManager.cpp index b869b5cdc..3f0d36bbb 100644 --- a/td/telegram/NotificationManager.cpp +++ b/td/telegram/NotificationManager.cpp @@ -2248,18 +2248,26 @@ void NotificationManager::process_push_notification(string payload, Promisedevice_token_manager_->get_actor_unsafe()->get_encryption_keys(); for (auto &key : encryption_keys) { - VLOG(notifications) << "Have key " << key.first << ": \"" << format::escaped(key.second) << '"'; + // VLOG(notifications) << "Have key " << key.first << ": \"" << format::escaped(key.second) << '"'; if (key.first == receiver_id) { - if (key.second.empty()) { - VLOG(notifications) << "Process unencrypted push notification"; - } else { - VLOG(notifications) << "Process encrypted push notification"; + if (!key.second.empty()) { + auto r_payload = decrypt_push(key.first, key.second.str(), std::move(payload)); + if (r_payload.is_error()) { + LOG(ERROR) << "Failed to decrypt push: " << r_payload.error(); + promise.set_error(Status::Error(400, "Failed to decrypt push payload")); + return; + } + payload = r_payload.move_as_ok(); } - promise.set_value(Unit()); - return; + receiver_id = 0; + break; } } - if (receiver_id != 0) { // TODO move this check up + if (receiver_id == 0) { + auto status = process_push_notification_payload(payload); + if (status.is_error()) { + LOG(ERROR) << "Receive error " << status << ", while parsing push payload " << payload; + } promise.set_value(Unit()); return; } @@ -2268,6 +2276,71 @@ void NotificationManager::process_push_notification(string payload, Promise loc_args; + for (auto &field_value : json_value.get_object()) { + if (field_value.first == "loc_key") { + if (field_value.second.type() != JsonValue::Type::String) { + return Status::Error("Expected loc_key as a String"); + } + loc_key = field_value.second.get_string().str(); + } else if (field_value.first == "loc_args") { + if (field_value.second.type() != JsonValue::Type::Array) { + return Status::Error("Expected loc_args as an Array"); + } + loc_args.reserve(field_value.second.get_array().size()); + for (auto &arg : field_value.second.get_array()) { + if (arg.type() != JsonValue::Type::String) { + return Status::Error("Expected loc_arg as a String"); + } + loc_args.push_back(arg.get_string().str()); + } + } else if (field_value.first == "custom") { + if (field_value.second.type() != JsonValue::Type::Object) { + return Status::Error("Expected custom as an Object"); + } + custom = std::move(field_value.second.get_object()); + } else if (field_value.first == "badge") { + if (field_value.second.type() == JsonValue::Type::String) { + TRY_RESULT(badge_value, to_integer_safe(field_value.second.get_string())); + badge = badge_value; + continue; + } + if (field_value.second.type() == JsonValue::Type::Number) { + TRY_RESULT(badge_value, to_integer_safe(field_value.second.get_number())); + badge = badge_value; + continue; + } + return Status::Error("Expected badge as a Number"); + } else if (field_value.first == "message") { + if (field_value.second.type() != JsonValue::Type::String) { + return Status::Error("Expected announcement message text as a String"); + } + announcement_message_text = field_value.second.get_string().str(); + } + } + if (badge < 0) { + return Status::Error("Expected badge to be positive"); + } + + return Status::OK(); +} + Result NotificationManager::get_push_receiver_id(string payload) { if (payload == "{}") { return static_cast(0); diff --git a/td/telegram/NotificationManager.h b/td/telegram/NotificationManager.h index a5f243117..95ee11576 100644 --- a/td/telegram/NotificationManager.h +++ b/td/telegram/NotificationManager.h @@ -102,7 +102,7 @@ class NotificationManager : public Actor { static Result get_push_receiver_id(string push); - static Result decrypt_push(int64 encryption_key_id, string encryption_key, string push); + static Result decrypt_push(int64 encryption_key_id, string encryption_key, string push); // public for tests void before_get_difference(); @@ -253,6 +253,8 @@ class NotificationManager : public Actor { static Result decrypt_push_payload(int64 encryption_key_id, string encryption_key, string payload); + Status process_push_notification_payload(string payload); + void after_get_difference_impl(); void after_get_chat_difference_impl(NotificationGroupId group_id);