From 6235ec62b0c2ab6e3f2dc3cd7a61af0544e9c732 Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 25 Mar 2019 17:35:37 +0300 Subject: [PATCH] Add MESSAGE_ANNOUNCEMENT support. GitOrigin-RevId: 754a484735c1a7366796181b715d0e79f91a77c1 --- td/telegram/MessagesManager.cpp | 18 ++++++++-------- td/telegram/NotificationManager.cpp | 33 ++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 1fc577cd..364a3ee4 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -4860,21 +4860,21 @@ MessagesManager::Dialog *MessagesManager::get_service_notifications_dialog() { void MessagesManager::on_update_service_notification(tl_object_ptr &&update) { int32 ttl = 0; + bool has_date = (update->flags_ & telegram_api::updateServiceNotification::INBOX_DATE_MASK) != 0; + auto date = has_date ? update->inbox_date_ : G()->unix_time(); auto content = get_message_content( td_, - get_message_text(td_->contacts_manager_.get(), std::move(update->message_), std::move(update->entities_), - update->inbox_date_, "on_update_service_notification"), + get_message_text(td_->contacts_manager_.get(), std::move(update->message_), std::move(update->entities_), date, + "on_update_service_notification"), std::move(update->media_), td_->auth_manager_->is_bot() ? DialogId() : get_service_notifications_dialog()->dialog_id, false, UserId(), &ttl); bool is_content_secret = is_secret_message_content(ttl, content->get_type()); if ((update->flags_ & telegram_api::updateServiceNotification::POPUP_MASK) != 0) { - send_closure( - G()->td(), &Td::send_update, - make_tl_object( - update->type_, get_message_content_object(content.get(), td_, update->inbox_date_, is_content_secret))); + send_closure(G()->td(), &Td::send_update, + td_api::make_object( + update->type_, get_message_content_object(content.get(), td_, date, is_content_secret))); } - if ((update->flags_ & telegram_api::updateServiceNotification::INBOX_DATE_MASK) != 0 && - !td_->auth_manager_->is_bot()) { + if (has_date && !td_->auth_manager_->is_bot()) { Dialog *d = get_service_notifications_dialog(); CHECK(d != nullptr); auto dialog_id = d->dialog_id; @@ -4883,7 +4883,7 @@ void MessagesManager::on_update_service_notification(tl_object_ptrmessage_id = get_next_local_message_id(d); new_message->random_y = get_random_y(new_message->message_id); new_message->sender_user_id = dialog_id.get_user_id(); - new_message->date = update->inbox_date_; + new_message->date = date; new_message->ttl = ttl; new_message->is_content_secret = is_content_secret; new_message->content = std::move(content); diff --git a/td/telegram/NotificationManager.cpp b/td/telegram/NotificationManager.cpp index f0bb659f..78a22ad9 100644 --- a/td/telegram/NotificationManager.cpp +++ b/td/telegram/NotificationManager.cpp @@ -12,6 +12,7 @@ #include "td/telegram/DeviceTokenManager.h" #include "td/telegram/Global.h" #include "td/telegram/MessagesManager.h" +#include "td/telegram/misc.h" #include "td/telegram/net/ConnectionCreator.h" #include "td/telegram/net/DcId.h" #include "td/telegram/StateManager.h" @@ -2337,7 +2338,34 @@ Status NotificationManager::process_push_notification_payload(string payload) { } } if (badge < 0) { - return Status::Error("Expected badge to be positive"); + return Status::Error("Expected badge to be non-negative"); + } + if (!clean_input_string(loc_key)) { + return Status::Error(PSLICE() << "Receive invalid loc_key " << format::escaped(loc_key)); + } + if (!clean_input_string(announcement_message_text)) { + return Status::Error(PSLICE() << "Receive invalid announcement_message_text " + << format::escaped(announcement_message_text)); + } + for (auto &loc_arg : loc_args) { + if (!clean_input_string(loc_arg)) { + return Status::Error(PSLICE() << "Receive invalid loc_arg " << format::escaped(loc_arg)); + } + } + + if (loc_key == "MESSAGE_ANNOUNCEMENT") { + if (announcement_message_text.empty()) { + return Status::Error("Have empty announcement message text"); + } + + auto update = telegram_api::make_object( + telegram_api::updateServiceNotification::INBOX_DATE_MASK, false, G()->unix_time(), string(), + announcement_message_text, nullptr, vector>()); + send_closure(G()->messages_manager(), &MessagesManager::on_update_service_notification, std::move(update)); + return Status::OK(); + } + if (!announcement_message_text.empty()) { + LOG(ERROR) << "Have non-empty announcement message text with loc_key = " << loc_key; } if (loc_key == "DC_UPDATE") { @@ -2346,6 +2374,9 @@ Status NotificationManager::process_push_notification_payload(string payload) { if (!DcId::is_valid(dc_id)) { return Status::Error("Invalid dc id"); } + if (!clean_input_string(addr)) { + return Status::Error(PSLICE() << "Receive invalid addr " << format::escaped(addr)); + } send_closure(G()->connection_creator(), &ConnectionCreator::on_dc_update, DcId::internal(dc_id), std::move(addr), Promise()); return Status::OK();