Don't use downcast_call in simple cases.

This commit is contained in:
levlam 2023-07-01 13:11:27 +03:00
parent 6702a001f0
commit d67d93e5af
6 changed files with 108 additions and 36 deletions

View File

@ -17,7 +17,6 @@
#include "td/telegram/net/NetQueryDispatcher.h"
#include "td/telegram/NotificationManager.h"
#include "td/telegram/Td.h"
#include "td/telegram/telegram_api.hpp"
#include "td/telegram/UpdatesManager.h"
#include "td/utils/algorithm.h"
@ -475,8 +474,25 @@ void CallActor::on_save_log_query_result(FileId file_id, Promise<Unit> promise,
// Requests
void CallActor::update_call(tl_object_ptr<telegram_api::PhoneCall> call) {
LOG(INFO) << "Receive " << to_string(call);
Status status;
downcast_call(*call, [&](auto &call) { status = this->do_update_call(call); });
auto status = [&] {
switch (call->get_id()) {
case telegram_api::phoneCallEmpty::ID:
return do_update_call(static_cast<telegram_api::phoneCallEmpty &>(*call));
case telegram_api::phoneCallWaiting::ID:
return do_update_call(static_cast<telegram_api::phoneCallWaiting &>(*call));
case telegram_api::phoneCallRequested::ID:
return do_update_call(static_cast<telegram_api::phoneCallRequested &>(*call));
case telegram_api::phoneCallAccepted::ID:
return do_update_call(static_cast<telegram_api::phoneCallAccepted &>(*call));
case telegram_api::phoneCall::ID:
return do_update_call(static_cast<telegram_api::phoneCall &>(*call));
case telegram_api::phoneCallDiscarded::ID:
return do_update_call(static_cast<telegram_api::phoneCallDiscarded &>(*call));
default:
UNREACHABLE();
return Status::OK();
}
}();
if (status.is_error()) {
LOG(INFO) << "Receive error " << status << ", while handling update " << to_string(call);
on_error(std::move(status));

View File

@ -6,8 +6,6 @@
//
#include "td/telegram/CallManager.h"
#include "td/telegram/telegram_api.hpp"
#include "td/utils/common.h"
#include "td/utils/logging.h"
#include "td/utils/misc.h"
@ -21,8 +19,25 @@ CallManager::CallManager(ActorShared<> parent) : parent_(std::move(parent)) {
}
void CallManager::update_call(Update call) {
int64 call_id = 0;
downcast_call(*call->phone_call_, [&](auto &update) { call_id = update.id_; });
auto call_id = [phone_call = call->phone_call_.get()] {
switch (phone_call->get_id()) {
case telegram_api::phoneCallEmpty::ID:
return static_cast<const telegram_api::phoneCallEmpty *>(phone_call)->id_;
case telegram_api::phoneCallWaiting::ID:
return static_cast<const telegram_api::phoneCallWaiting *>(phone_call)->id_;
case telegram_api::phoneCallRequested::ID:
return static_cast<const telegram_api::phoneCallRequested *>(phone_call)->id_;
case telegram_api::phoneCallAccepted::ID:
return static_cast<const telegram_api::phoneCallAccepted *>(phone_call)->id_;
case telegram_api::phoneCall::ID:
return static_cast<const telegram_api::phoneCall *>(phone_call)->id_;
case telegram_api::phoneCallDiscarded::ID:
return static_cast<const telegram_api::phoneCallDiscarded *>(phone_call)->id_;
default:
UNREACHABLE();
return static_cast<int64>(0);
}
}();
LOG(DEBUG) << "Receive UpdateCall for " << call_id;
auto &info = call_info_[call_id];

View File

@ -49,7 +49,6 @@
#include "td/telegram/StoryManager.h"
#include "td/telegram/Td.h"
#include "td/telegram/TdDb.h"
#include "td/telegram/telegram_api.hpp"
#include "td/telegram/UpdatesManager.h"
#include "td/telegram/Version.h"
@ -12750,7 +12749,25 @@ void ContactsManager::on_get_user_photos(UserId user_id, int32 offset, int32 lim
void ContactsManager::on_get_chat(tl_object_ptr<telegram_api::Chat> &&chat, const char *source) {
LOG(DEBUG) << "Receive from " << source << ' ' << to_string(chat);
downcast_call(*chat, [this, source](auto &c) { this->on_chat_update(c, source); });
switch (chat->get_id()) {
case telegram_api::chatEmpty::ID:
on_chat_update(static_cast<telegram_api::chatEmpty &>(*chat), source);
break;
case telegram_api::chat::ID:
on_chat_update(static_cast<telegram_api::chat &>(*chat), source);
break;
case telegram_api::chatForbidden::ID:
on_chat_update(static_cast<telegram_api::chatForbidden &>(*chat), source);
break;
case telegram_api::channel::ID:
on_chat_update(static_cast<telegram_api::channel &>(*chat), source);
break;
case telegram_api::channelForbidden::ID:
on_chat_update(static_cast<telegram_api::channelForbidden &>(*chat), source);
break;
default:
UNREACHABLE();
}
}
void ContactsManager::on_get_chats(vector<tl_object_ptr<telegram_api::Chat>> &&chats, const char *source) {

View File

@ -36,7 +36,6 @@
#include "td/telegram/Td.h"
#include "td/telegram/td_api.hpp"
#include "td/telegram/TdDb.h"
#include "td/telegram/telegram_api.hpp"
#include "td/telegram/ThemeManager.h"
#include "td/telegram/UpdatesManager.h"
#include "td/telegram/Venue.h"
@ -1856,9 +1855,17 @@ void InlineQueriesManager::on_get_inline_query_results(DialogId dialog_id, UserI
continue;
}
vector<tl_object_ptr<telegram_api::DocumentAttribute>> attributes;
downcast_call(*result->content_,
[&attributes](auto &web_document) { attributes = std::move(web_document.attributes_); });
auto attributes = [content = result->content_.get()] {
switch (content->get_id()) {
case telegram_api::webDocument::ID:
return std::move(static_cast<telegram_api::webDocument *>(content)->attributes_);
case telegram_api::webDocumentNoProxy::ID:
return std::move(static_cast<telegram_api::webDocumentNoProxy *>(content)->attributes_);
default:
UNREACHABLE();
return vector<telegram_api::object_ptr<telegram_api::DocumentAttribute>>();
}
}();
bool is_animation = result->type_ == "gif" && (content_type == "image/gif" || content_type == "video/mp4");
if (is_animation) {

View File

@ -18,7 +18,6 @@
#include "td/telegram/SequenceDispatcher.h"
#include "td/telegram/StateManager.h"
#include "td/telegram/TdDb.h"
#include "td/telegram/telegram_api.hpp"
#include "td/mtproto/DhCallback.h"
@ -181,12 +180,24 @@ void SecretChatsManager::on_update_chat(tl_object_ptr<telegram_api::updateEncryp
}
void SecretChatsManager::do_update_chat(tl_object_ptr<telegram_api::updateEncryption> update) {
int32 id = 0;
downcast_call(*update->chat_, [&](auto &x) { id = x.id_; });
send_closure(
update->chat_->get_id() == telegram_api::encryptedChatDiscarded::ID ? get_chat_actor(id) : create_chat_actor(id),
&SecretChatActor::update_chat, std::move(update->chat_));
auto actor_id = [this, chat = update->chat_.get()] {
switch (chat->get_id()) {
case telegram_api::encryptedChatEmpty::ID:
return create_chat_actor(static_cast<const telegram_api::encryptedChatEmpty *>(chat)->id_);
case telegram_api::encryptedChatWaiting::ID:
return create_chat_actor(static_cast<const telegram_api::encryptedChatWaiting *>(chat)->id_);
case telegram_api::encryptedChatRequested::ID:
return create_chat_actor(static_cast<const telegram_api::encryptedChatRequested *>(chat)->id_);
case telegram_api::encryptedChat::ID:
return create_chat_actor(static_cast<const telegram_api::encryptedChat *>(chat)->id_);
case telegram_api::encryptedChatDiscarded::ID:
return get_chat_actor(static_cast<const telegram_api::encryptedChatDiscarded *>(chat)->id_);
default:
UNREACHABLE();
return ActorId<SecretChatActor>();
}
}();
send_closure(actor_id, &SecretChatActor::update_chat, std::move(update->chat_));
}
void SecretChatsManager::on_new_message(tl_object_ptr<telegram_api::EncryptedMessage> &&message_ptr,
@ -198,14 +209,24 @@ void SecretChatsManager::on_new_message(tl_object_ptr<telegram_api::EncryptedMes
auto event = make_unique<log_event::InboundSecretMessage>();
event->promise = std::move(promise);
downcast_call(*message_ptr, [&](auto &x) {
event->chat_id = x.chat_id_;
event->date = x.date_;
event->encrypted_message = std::move(x.bytes_);
});
if (message_ptr->get_id() == telegram_api::encryptedMessage::ID) {
auto message = move_tl_object_as<telegram_api::encryptedMessage>(message_ptr);
event->file = EncryptedFile::get_encrypted_file(std::move(message->file_));
switch (message_ptr->get_id()) {
case telegram_api::encryptedMessage::ID: {
auto message = telegram_api::move_object_as<telegram_api::encryptedMessage>(message_ptr);
event->chat_id = message->chat_id_;
event->date = message->date_;
event->encrypted_message = std::move(message->bytes_);
event->file = EncryptedFile::get_encrypted_file(std::move(message->file_));
break;
}
case telegram_api::encryptedMessageService::ID: {
auto message = telegram_api::move_object_as<telegram_api::encryptedMessageService>(message_ptr);
event->chat_id = message->chat_id_;
event->date = message->date_;
event->encrypted_message = std::move(message->bytes_);
break;
}
default:
UNREACHABLE();
}
add_inbound_message(std::move(event));
}

View File

@ -15,7 +15,6 @@
#include "td/telegram/misc.h"
#include "td/telegram/net/DcId.h"
#include "td/telegram/OrderInfo.h"
#include "td/telegram/telegram_api.hpp"
#include "td/utils/algorithm.h"
#include "td/utils/base64.h"
@ -24,7 +23,6 @@
#include "td/utils/JsonBuilder.h"
#include "td/utils/logging.h"
#include "td/utils/misc.h"
#include "td/utils/overloaded.h"
#include "td/utils/SliceBuilder.h"
#include "td/utils/utf8.h"
@ -399,12 +397,10 @@ telegram_api::object_ptr<telegram_api::InputSecureFile> get_input_secure_file_ob
if (res == nullptr) {
return file_manager->get_file_view(file.file.file_id).remote_location().as_input_secure_file();
}
telegram_api::downcast_call(*res, overloaded(
[&](telegram_api::inputSecureFileUploaded &uploaded) {
uploaded.secret_ = BufferSlice(file.encrypted_secret);
uploaded.file_hash_ = BufferSlice(file.file_hash);
},
[&](telegram_api::inputSecureFile &) { UNREACHABLE(); }));
CHECK(res->get_id() == telegram_api::inputSecureFileUploaded::ID);
auto uploaded = static_cast<telegram_api::inputSecureFileUploaded *>(res.get());
uploaded->secret_ = BufferSlice(file.encrypted_secret);
uploaded->file_hash_ = BufferSlice(file.file_hash);
return res;
}