Reget poll messages from TDLib.

This commit is contained in:
levlam 2022-11-22 13:34:16 +03:00
parent ce6ddc74d7
commit 53aa4d2f03
2 changed files with 39 additions and 25 deletions

View File

@ -813,7 +813,7 @@ class Client::JsonChat final : public Jsonable {
} }
if (pinned_message_id_ != 0) { if (pinned_message_id_ != 0) {
CHECK(pinned_message_id_ != -1); CHECK(pinned_message_id_ != -1);
const MessageInfo *pinned_message = client_->get_message(chat_id_, pinned_message_id_); const MessageInfo *pinned_message = client_->get_message(chat_id_, pinned_message_id_, true);
if (pinned_message != nullptr) { if (pinned_message != nullptr) {
object("pinned_message", JsonMessage(pinned_message, false, "pin in JsonChat", client_)); object("pinned_message", JsonMessage(pinned_message, false, "pin in JsonChat", client_));
} else { } else {
@ -1824,7 +1824,7 @@ void Client::JsonMessage::store(JsonValueScope *scope) const {
object("forward_date", message_->initial_send_date); object("forward_date", message_->initial_send_date);
} }
if (message_->reply_to_message_id > 0 && need_reply_ && !message_->is_reply_to_message_deleted) { if (message_->reply_to_message_id > 0 && need_reply_ && !message_->is_reply_to_message_deleted) {
const MessageInfo *reply_to_message = client_->get_message(message_->chat_id, message_->reply_to_message_id); const MessageInfo *reply_to_message = client_->get_message(message_->chat_id, message_->reply_to_message_id, true);
if (reply_to_message != nullptr) { if (reply_to_message != nullptr) {
object("reply_to_message", JsonMessage(reply_to_message, false, "reply in " + source_, client_)); object("reply_to_message", JsonMessage(reply_to_message, false, "reply in " + source_, client_));
} else { } else {
@ -2034,7 +2034,7 @@ void Client::JsonMessage::store(JsonValueScope *scope) const {
auto content = static_cast<const td_api::messagePinMessage *>(message_->content.get()); auto content = static_cast<const td_api::messagePinMessage *>(message_->content.get());
auto message_id = content->message_id_; auto message_id = content->message_id_;
if (message_id > 0) { if (message_id > 0) {
const MessageInfo *pinned_message = client_->get_message(message_->chat_id, message_id); const MessageInfo *pinned_message = client_->get_message(message_->chat_id, message_id, true);
if (pinned_message != nullptr) { if (pinned_message != nullptr) {
object("pinned_message", JsonMessage(pinned_message, false, "pin in " + source_, client_)); object("pinned_message", JsonMessage(pinned_message, false, "pin in " + source_, client_));
} else { } else {
@ -2898,7 +2898,7 @@ class Client::TdOnDeleteFailedToSendMessageCallback final : public TdQueryCallba
} }
CHECK(result->get_id() == td_api::ok::ID); CHECK(result->get_id() == td_api::ok::ID);
if (client_->get_message(chat_id_, message_id_) != nullptr) { if (client_->get_message(chat_id_, message_id_, true) != nullptr) {
LOG(ERROR) << "Have cache for message " << message_id_ << " in the chat " << chat_id_; LOG(ERROR) << "Have cache for message " << message_id_ << " in the chat " << chat_id_;
client_->delete_message(chat_id_, message_id_, false); client_->delete_message(chat_id_, message_id_, false);
} }
@ -2926,7 +2926,7 @@ class Client::TdOnEditMessageCallback final : public TdQueryCallback {
int64 chat_id = message->chat_id_; int64 chat_id = message->chat_id_;
int64 message_id = message->id_; int64 message_id = message->id_;
auto message_info = client_->get_message(chat_id, message_id); auto message_info = client_->get_message(chat_id, message_id, true);
if (message_info == nullptr) { if (message_info == nullptr) {
return fail_query_with_error(std::move(query_), 400, "message not found"); return fail_query_with_error(std::move(query_), 400, "message not found");
} }
@ -2969,7 +2969,7 @@ class Client::TdOnStopPollCallback final : public TdQueryCallback {
} }
CHECK(result->get_id() == td_api::ok::ID); CHECK(result->get_id() == td_api::ok::ID);
auto message_info = client_->get_message(chat_id_, message_id_); auto message_info = client_->get_message(chat_id_, message_id_, true);
if (message_info == nullptr) { if (message_info == nullptr) {
return fail_query_with_error(std::move(query_), 400, "message not found"); return fail_query_with_error(std::move(query_), 400, "message not found");
} }
@ -3227,7 +3227,7 @@ class Client::TdOnCheckMessageThreadCallback final : public TdQueryCallback {
CHECK(full_message_id.chat_id == chat_id_); CHECK(full_message_id.chat_id == chat_id_);
CHECK(full_message_id.message_id == message_thread_id_); CHECK(full_message_id.message_id == message_thread_id_);
const MessageInfo *message_info = client_->get_message(chat_id_, message_thread_id_); const MessageInfo *message_info = client_->get_message(chat_id_, message_thread_id_, true);
CHECK(message_info != nullptr); CHECK(message_info != nullptr);
if (message_info->message_thread_id != message_thread_id_) { if (message_info->message_thread_id != message_thread_id_) {
return fail_query_with_error(std::move(query_), 400, "MESSAGE_THREAD_INVALID", "Message thread not found"); return fail_query_with_error(std::move(query_), 400, "MESSAGE_THREAD_INVALID", "Message thread not found");
@ -4115,7 +4115,7 @@ void Client::on_get_reply_message(int64 chat_id, object_ptr<td_api::message> rep
add_message(std::move(reply_to_message)); add_message(std::move(reply_to_message));
} }
process_new_message_queue(chat_id); process_new_message_queue(chat_id, 1);
} }
void Client::on_get_edited_message(object_ptr<td_api::message> edited_message) { void Client::on_get_edited_message(object_ptr<td_api::message> edited_message) {
@ -4189,7 +4189,7 @@ void Client::on_get_sticker_set(int64 set_id, int64 new_callback_query_user_id,
process_new_callback_query_queue(new_callback_query_user_id, 2); process_new_callback_query_queue(new_callback_query_user_id, 2);
} }
if (new_message_chat_id != 0) { if (new_message_chat_id != 0) {
process_new_message_queue(new_message_chat_id); process_new_message_queue(new_message_chat_id, 2);
} }
} }
@ -4448,7 +4448,7 @@ void Client::check_message_thread(int64 chat_id, int64 message_thread_id, int64
} }
if (reply_to_message_id != 0) { if (reply_to_message_id != 0) {
const MessageInfo *message_info = get_message(chat_id, reply_to_message_id); const MessageInfo *message_info = get_message(chat_id, reply_to_message_id, true);
CHECK(message_info != nullptr); CHECK(message_info != nullptr);
if (message_info->message_thread_id != message_thread_id) { if (message_info->message_thread_id != message_thread_id) {
return fail_query_with_error(std::move(query), 400, "MESSAGE_THREAD_INVALID", return fail_query_with_error(std::move(query), 400, "MESSAGE_THREAD_INVALID",
@ -7123,7 +7123,7 @@ void Client::on_message_send_succeeded(object_ptr<td_api::message> &&message, in
int64 new_message_id = full_message_id.message_id; int64 new_message_id = full_message_id.message_id;
CHECK(new_message_id > 0); CHECK(new_message_id > 0);
auto message_info = get_message(chat_id, new_message_id); auto message_info = get_message(chat_id, new_message_id, true);
CHECK(message_info != nullptr); CHECK(message_info != nullptr);
message_info->is_content_changed = false; message_info->is_content_changed = false;
@ -9931,7 +9931,7 @@ void Client::add_new_message(object_ptr<td_api::message> &&message, bool is_edit
auto chat_id = message->chat_id_; auto chat_id = message->chat_id_;
CHECK(chat_id != 0); CHECK(chat_id != 0);
new_message_queues_[chat_id].queue_.emplace(std::move(message), is_edited); new_message_queues_[chat_id].queue_.emplace(std::move(message), is_edited);
process_new_message_queue(chat_id); process_new_message_queue(chat_id, 0);
} }
void Client::add_update_poll(object_ptr<td_api::updatePoll> &&update) { void Client::add_update_poll(object_ptr<td_api::updatePoll> &&update) {
@ -9972,9 +9972,13 @@ void Client::process_new_callback_query_queue(int64 user_id, int state) {
auto &queue = new_callback_query_queues_[user_id]; auto &queue = new_callback_query_queues_[user_id];
if (queue.has_active_request_) { if (queue.has_active_request_) {
CHECK(state == 0); CHECK(state == 0);
CHECK(!queue.queue_.empty());
LOG(INFO) << "Have an active request in callback query queue of size " << queue.queue_.size() << " for user "
<< user_id;
return; return;
} }
if (logging_out_ || closing_) { if (logging_out_ || closing_) {
LOG(INFO) << "Ignore callback query while closing for user " << user_id;
new_callback_query_queues_.erase(user_id); new_callback_query_queues_.erase(user_id);
return; return;
} }
@ -9982,8 +9986,10 @@ void Client::process_new_callback_query_queue(int64 user_id, int state) {
auto &query = queue.queue_.front(); auto &query = queue.queue_.front();
int64 chat_id = query->chat_id_; int64 chat_id = query->chat_id_;
int64 message_id = query->message_id_; int64 message_id = query->message_id_;
auto message_info = get_message(chat_id, message_id); auto message_info = get_message(chat_id, message_id, state > 0);
// callback message can be already deleted in the bot outbox // callback message can be already deleted in the bot outbox
LOG(INFO) << "Process callback query from user " << user_id << " in message " << message_id << " in chat "
<< chat_id << " with state " << state;
if (state == 0) { if (state == 0) {
if (message_info == nullptr) { if (message_info == nullptr) {
// get the message from the server // get the message from the server
@ -9996,7 +10002,7 @@ void Client::process_new_callback_query_queue(int64 user_id, int state) {
if (state == 1) { if (state == 1) {
auto reply_to_message_id = auto reply_to_message_id =
message_info == nullptr || message_info->is_reply_to_message_deleted ? 0 : message_info->reply_to_message_id; message_info == nullptr || message_info->is_reply_to_message_deleted ? 0 : message_info->reply_to_message_id;
if (reply_to_message_id > 0 && get_message(chat_id, reply_to_message_id) == nullptr) { if (reply_to_message_id > 0 && get_message(chat_id, reply_to_message_id, false) == nullptr) {
queue.has_active_request_ = true; queue.has_active_request_ = true;
return send_request(make_object<td_api::getRepliedMessage>(chat_id, message_id), return send_request(make_object<td_api::getRepliedMessage>(chat_id, message_id),
td::make_unique<TdOnGetCallbackQueryMessageCallback>(this, user_id, state)); td::make_unique<TdOnGetCallbackQueryMessageCallback>(this, user_id, state));
@ -10013,7 +10019,7 @@ void Client::process_new_callback_query_queue(int64 user_id, int state) {
auto reply_to_message_id = auto reply_to_message_id =
message_info == nullptr || message_info->is_reply_to_message_deleted ? 0 : message_info->reply_to_message_id; message_info == nullptr || message_info->is_reply_to_message_deleted ? 0 : message_info->reply_to_message_id;
if (reply_to_message_id > 0) { if (reply_to_message_id > 0) {
auto reply_to_message_info = get_message(chat_id, reply_to_message_id); auto reply_to_message_info = get_message(chat_id, reply_to_message_id, true);
auto reply_sticker_set_id = auto reply_sticker_set_id =
reply_to_message_info == nullptr ? 0 : get_sticker_set_id(reply_to_message_info->content); reply_to_message_info == nullptr ? 0 : get_sticker_set_id(reply_to_message_info->content);
if (!have_sticker_set_name(reply_sticker_set_id)) { if (!have_sticker_set_name(reply_sticker_set_id)) {
@ -10189,7 +10195,7 @@ bool Client::need_skip_update_message(int64 chat_id, const object_ptr<td_api::me
if (pinned_message_id <= 0) { if (pinned_message_id <= 0) {
return true; return true;
} }
const MessageInfo *pinned_message = get_message(chat_id, pinned_message_id); const MessageInfo *pinned_message = get_message(chat_id, pinned_message_id, true);
if (pinned_message == nullptr) { if (pinned_message == nullptr) {
LOG(WARNING) << "Pinned unknown, inaccessible or deleted message " << pinned_message_id << " in " << chat_id; LOG(WARNING) << "Pinned unknown, inaccessible or deleted message " << pinned_message_id << " in " << chat_id;
return true; return true;
@ -10232,7 +10238,7 @@ bool Client::need_skip_update_message(int64 chat_id, const object_ptr<td_api::me
} }
if (is_edited) { if (is_edited) {
const MessageInfo *old_message = get_message(chat_id, message->id_); const MessageInfo *old_message = get_message(chat_id, message->id_, true);
if (old_message != nullptr && !old_message->is_content_changed) { if (old_message != nullptr && !old_message->is_content_changed) {
return true; return true;
} }
@ -10426,7 +10432,7 @@ td::string Client::get_sticker_set_name(int64 sticker_set_id) const {
return sticker_set_names_.get(sticker_set_id); return sticker_set_names_.get(sticker_set_id);
} }
void Client::process_new_message_queue(int64 chat_id) { void Client::process_new_message_queue(int64 chat_id, int state) {
auto &queue = new_message_queues_[chat_id]; auto &queue = new_message_queues_[chat_id];
if (queue.has_active_request_) { if (queue.has_active_request_) {
return; return;
@ -10440,7 +10446,7 @@ void Client::process_new_message_queue(int64 chat_id) {
CHECK(chat_id == message_ref->chat_id_); CHECK(chat_id == message_ref->chat_id_);
int64 message_id = message_ref->id_; int64 message_id = message_ref->id_;
int64 reply_to_message_id = get_reply_to_message_id(message_ref); int64 reply_to_message_id = get_reply_to_message_id(message_ref);
if (reply_to_message_id > 0 && get_message(chat_id, reply_to_message_id) == nullptr) { if (reply_to_message_id > 0 && get_message(chat_id, reply_to_message_id, state > 0) == nullptr) {
queue.has_active_request_ = true; queue.has_active_request_ = true;
return send_request(make_object<td_api::getRepliedMessage>(chat_id, message_id), return send_request(make_object<td_api::getRepliedMessage>(chat_id, message_id),
td::make_unique<TdOnGetReplyMessageCallback>(this, chat_id)); td::make_unique<TdOnGetReplyMessageCallback>(this, chat_id));
@ -10452,7 +10458,7 @@ void Client::process_new_message_queue(int64 chat_id) {
td::make_unique<TdOnGetStickerSetCallback>(this, message_sticker_set_id, 0, chat_id)); td::make_unique<TdOnGetStickerSetCallback>(this, message_sticker_set_id, 0, chat_id));
} }
if (reply_to_message_id > 0) { if (reply_to_message_id > 0) {
auto reply_to_message_info = get_message(chat_id, reply_to_message_id); auto reply_to_message_info = get_message(chat_id, reply_to_message_id, true);
CHECK(reply_to_message_info != nullptr); CHECK(reply_to_message_info != nullptr);
auto reply_sticker_set_id = get_sticker_set_id(reply_to_message_info->content); auto reply_sticker_set_id = get_sticker_set_id(reply_to_message_info->content);
if (!have_sticker_set_name(reply_sticker_set_id)) { if (!have_sticker_set_name(reply_sticker_set_id)) {
@ -10516,7 +10522,7 @@ void Client::process_new_message_queue(int64 chat_id) {
auto left_time = message_date + 86400 - now; auto left_time = message_date + 86400 - now;
add_message(std::move(message)); add_message(std::move(message));
auto message_info = get_message(chat_id, message_id); auto message_info = get_message(chat_id, message_id, true);
CHECK(message_info != nullptr); CHECK(message_info != nullptr);
message_info->is_content_changed = false; message_info->is_content_changed = false;
@ -10707,6 +10713,8 @@ Client::FullMessageId Client::add_message(object_ptr<td_api::message> &&message,
send_request(make_object<td_api::getStickerSet>(sticker_set_id), send_request(make_object<td_api::getStickerSet>(sticker_set_id),
td::make_unique<TdOnGetStickerSetCallback>(this, sticker_set_id, 0, 0)); td::make_unique<TdOnGetStickerSetCallback>(this, sticker_set_id, 0, 0));
} }
} else if (message->content_->get_id() == td_api::messagePoll::ID) {
message_info->content = std::move(message->content_);
} }
set_message_reply_markup(message_info.get(), std::move(message->reply_markup_)); set_message_reply_markup(message_info.get(), std::move(message->reply_markup_));
@ -10736,14 +10744,18 @@ void Client::on_update_message_edited(int64 chat_id, int64 message_id, int32 edi
set_message_reply_markup(message_info, std::move(reply_markup)); set_message_reply_markup(message_info, std::move(reply_markup));
} }
const Client::MessageInfo *Client::get_message(int64 chat_id, int64 message_id) const { const Client::MessageInfo *Client::get_message(int64 chat_id, int64 message_id, bool force_cache) const {
auto message_info = messages_.get_pointer({chat_id, message_id}); auto message_info = messages_.get_pointer({chat_id, message_id});
if (message_info == nullptr) { if (message_info == nullptr) {
LOG(DEBUG) << "Not found message " << message_id << " from chat " << chat_id; LOG(DEBUG) << "Not found message " << message_id << " from chat " << chat_id;
return nullptr; return nullptr;
} }
LOG(DEBUG) << "Found message " << message_id << " from chat " << chat_id; if (!force_cache && message_info->content->get_id() == td_api::messagePoll::ID) {
LOG(DEBUG) << "Ignore found message " << message_id << " from chat " << chat_id;
return nullptr;
}
LOG(DEBUG) << "Found message " << message_id << " from chat " << chat_id;
return message_info; return message_info;
} }

View File

@ -816,7 +816,8 @@ class Client final : public WebhookActor::Callback {
td::unique_ptr<MessageInfo> delete_message(int64 chat_id, int64 message_id, bool only_from_cache); td::unique_ptr<MessageInfo> delete_message(int64 chat_id, int64 message_id, bool only_from_cache);
void add_new_message(object_ptr<td_api::message> &&message, bool is_edited); void add_new_message(object_ptr<td_api::message> &&message, bool is_edited);
void process_new_message_queue(int64 chat_id);
void process_new_message_queue(int64 chat_id, int state);
struct FullMessageId { struct FullMessageId {
int64 chat_id; int64 chat_id;
@ -840,7 +841,7 @@ class Client final : public WebhookActor::Callback {
}; };
FullMessageId add_message(object_ptr<td_api::message> &&message, bool force_update_content = false); FullMessageId add_message(object_ptr<td_api::message> &&message, bool force_update_content = false);
const MessageInfo *get_message(int64 chat_id, int64 message_id) const; const MessageInfo *get_message(int64 chat_id, int64 message_id, bool force_cache) const;
MessageInfo *get_message_editable(int64 chat_id, int64 message_id); MessageInfo *get_message_editable(int64 chat_id, int64 message_id);
void update_message_content(int64 chat_id, int64 message_id, object_ptr<td_api::MessageContent> &&content); void update_message_content(int64 chat_id, int64 message_id, object_ptr<td_api::MessageContent> &&content);
@ -870,6 +871,7 @@ class Client final : public WebhookActor::Callback {
const td::string &inline_message_id); const td::string &inline_message_id);
void add_new_callback_query(object_ptr<td_api::updateNewCallbackQuery> &&query); void add_new_callback_query(object_ptr<td_api::updateNewCallbackQuery> &&query);
void process_new_callback_query_queue(int64 user_id, int state); void process_new_callback_query_queue(int64 user_id, int state);
void add_new_inline_callback_query(object_ptr<td_api::updateNewInlineCallbackQuery> &&query); void add_new_inline_callback_query(object_ptr<td_api::updateNewInlineCallbackQuery> &&query);