Improve return value of UpdatesManager::get_new_messages.

This commit is contained in:
levlam 2022-12-02 12:11:07 +03:00
parent fff8e81261
commit f8a5f42886
4 changed files with 39 additions and 25 deletions

View File

@ -3658,7 +3658,7 @@ class SendMultiMediaQuery final : public Td::ResultHandler {
is_result_wrong = true;
}
for (auto &sent_message : sent_messages) {
if (MessagesManager::get_message_dialog_id(*sent_message) != dialog_id_) {
if (MessagesManager::get_message_dialog_id(sent_message.first) != dialog_id_) {
is_result_wrong = true;
}
}
@ -4155,7 +4155,7 @@ class ForwardMessagesQuery final : public Td::ResultHandler {
is_result_wrong = true;
}
for (auto &sent_message : sent_messages) {
if (MessagesManager::get_message_dialog_id(*sent_message) != to_dialog_id_) {
if (MessagesManager::get_message_dialog_id(sent_message.first) != to_dialog_id_) {
is_result_wrong = true;
}
}
@ -13254,19 +13254,19 @@ MessageId MessagesManager::get_message_id(const tl_object_ptr<telegram_api::Mess
}
}
DialogId MessagesManager::get_message_dialog_id(const tl_object_ptr<telegram_api::Message> &message_ptr) {
DialogId MessagesManager::get_message_dialog_id(const telegram_api::Message *message_ptr) {
CHECK(message_ptr != nullptr);
switch (message_ptr->get_id()) {
case telegram_api::messageEmpty::ID: {
auto message = static_cast<const telegram_api::messageEmpty *>(message_ptr.get());
auto message = static_cast<const telegram_api::messageEmpty *>(message_ptr);
return message->peer_id_ == nullptr ? DialogId() : DialogId(message->peer_id_);
}
case telegram_api::message::ID: {
auto message = static_cast<const telegram_api::message *>(message_ptr.get());
auto message = static_cast<const telegram_api::message *>(message_ptr);
return DialogId(message->peer_id_);
}
case telegram_api::messageService::ID: {
auto message = static_cast<const telegram_api::messageService *>(message_ptr.get());
auto message = static_cast<const telegram_api::messageService *>(message_ptr);
return DialogId(message->peer_id_);
}
default:
@ -13275,6 +13275,10 @@ DialogId MessagesManager::get_message_dialog_id(const tl_object_ptr<telegram_api
}
}
DialogId MessagesManager::get_message_dialog_id(const tl_object_ptr<telegram_api::Message> &message_ptr) {
return get_message_dialog_id(message_ptr.get());
}
FullMessageId MessagesManager::get_full_message_id(const tl_object_ptr<telegram_api::Message> &message_ptr,
bool is_scheduled) {
return {get_message_dialog_id(message_ptr), get_message_id(message_ptr, is_scheduled)};
@ -31970,8 +31974,8 @@ void MessagesManager::check_send_message_result(int64 random_id, DialogId dialog
};
if (sent_messages.size() != 1u || sent_messages_random_ids.size() != 1u ||
*sent_messages_random_ids.begin() != random_id || get_message_dialog_id(*sent_messages[0]) != dialog_id ||
is_invalid_poll_message(sent_messages[0]->get())) {
*sent_messages_random_ids.begin() != random_id || get_message_dialog_id(sent_messages[0].first) != dialog_id ||
is_invalid_poll_message(sent_messages[0].first)) {
LOG(ERROR) << "Receive wrong result for sending message with random_id " << random_id << " from " << source
<< " to " << dialog_id << ": " << oneline(to_string(*updates_ptr));
Dialog *d = get_dialog(dialog_id);
@ -33398,19 +33402,23 @@ void MessagesManager::on_create_new_dialog_success(int64 random_id, tl_object_pt
return on_create_new_dialog_fail(random_id, Status::Error(500, "Unsupported server response"), std::move(promise));
}
auto message = *sent_messages.begin();
auto *message = sent_messages.begin()->first;
// int64 message_random_id = *sent_messages_random_ids.begin();
// TODO check that message_random_id equals random_id after messages_createChat will be updated
auto dialog_id = get_message_dialog_id(*message);
if (sent_messages.begin()->second) {
return on_create_new_dialog_fail(random_id, Status::Error(500, "Scheduled message received"), std::move(promise));
}
auto dialog_id = get_message_dialog_id(message);
if (dialog_id.get_type() != expected_type) {
return on_create_new_dialog_fail(random_id, Status::Error(500, "Chat of wrong type has been created"),
std::move(promise));
}
if ((*message)->get_id() != telegram_api::messageService::ID) {
if (message->get_id() != telegram_api::messageService::ID) {
return on_create_new_dialog_fail(random_id, Status::Error(500, "Invalid message received"), std::move(promise));
}
auto action_id = static_cast<const telegram_api::messageService *>((*message).get())->action_->get_id();
auto action_id = static_cast<const telegram_api::messageService *>(message)->action_->get_id();
if (action_id != telegram_api::messageActionChatCreate::ID &&
action_id != telegram_api::messageActionChannelCreate::ID) {
return on_create_new_dialog_fail(random_id, Status::Error(500, "Invalid service message received"),

View File

@ -167,6 +167,8 @@ class MessagesManager final : public Actor {
static MessageId get_message_id(const tl_object_ptr<telegram_api::Message> &message_ptr, bool is_scheduled);
static DialogId get_message_dialog_id(const telegram_api::Message *message_ptr);
static DialogId get_message_dialog_id(const tl_object_ptr<telegram_api::Message> &message_ptr);
static FullMessageId get_full_message_id(const tl_object_ptr<telegram_api::Message> &message_ptr, bool is_scheduled);

View File

@ -1151,10 +1151,10 @@ FlatHashSet<int64> UpdatesManager::get_sent_messages_random_ids(const telegram_a
if (update->get_id() == telegram_api::updateMessageID::ID) {
int64 random_id = static_cast<const telegram_api::updateMessageID *>(update.get())->random_id_;
if (random_id != 0) {
bool found_message = false;
for (auto *message : new_messages) {
// TODO
}
bool found_message = true;
// for (auto *message : new_messages) {
// TODO
// }
if (found_message && !random_ids.insert(random_id).second) {
LOG(ERROR) << "Receive twice updateMessageID for " << random_id;
}
@ -1216,24 +1216,26 @@ bool UpdatesManager::is_additional_service_message(const telegram_api::Message *
return action_id == telegram_api::messageActionSetMessagesTTL::ID;
}
vector<const tl_object_ptr<telegram_api::Message> *> UpdatesManager::get_new_messages(
vector<std::pair<const telegram_api::Message *, bool>> UpdatesManager::get_new_messages(
const telegram_api::Updates *updates_ptr) {
vector<const tl_object_ptr<telegram_api::Message> *> messages;
vector<std::pair<const telegram_api::Message *, bool>> messages;
auto updates = get_updates(updates_ptr);
if (updates != nullptr) {
for (auto &update : *updates) {
const tl_object_ptr<telegram_api::Message> *message = nullptr;
const telegram_api::Message *message = nullptr;
bool is_scheduled = false;
auto constructor_id = update->get_id();
if (constructor_id == telegram_api::updateNewMessage::ID) {
message = &static_cast<const telegram_api::updateNewMessage *>(update.get())->message_;
message = static_cast<const telegram_api::updateNewMessage *>(update.get())->message_.get();
} else if (constructor_id == telegram_api::updateNewChannelMessage::ID) {
message = &static_cast<const telegram_api::updateNewChannelMessage *>(update.get())->message_;
message = static_cast<const telegram_api::updateNewChannelMessage *>(update.get())->message_.get();
} else if (constructor_id == telegram_api::updateNewScheduledMessage::ID) {
message = &static_cast<const telegram_api::updateNewScheduledMessage *>(update.get())->message_;
message = static_cast<const telegram_api::updateNewScheduledMessage *>(update.get())->message_.get();
is_scheduled = true;
}
if (is_additional_service_message((*message).get())) {
messages.push_back(message);
if (is_additional_service_message(message)) {
messages.emplace_back(message, is_scheduled);
}
}
}

View File

@ -31,6 +31,7 @@
#include <functional>
#include <map>
#include <utility>
namespace td {
@ -107,7 +108,8 @@ class UpdatesManager final : public Actor {
static const telegram_api::Message *get_message_by_random_id(const telegram_api::Updates *updates_ptr,
DialogId dialog_id, int64 random_id);
static vector<const tl_object_ptr<telegram_api::Message> *> get_new_messages(
// [Message, is_scheduled]
static vector<std::pair<const telegram_api::Message *, bool>> get_new_messages(
const telegram_api::Updates *updates_ptr);
static vector<InputGroupCallId> get_update_new_group_call_ids(const telegram_api::Updates *updates_ptr);