From 9243f51ff9398db09e3ca32ad08b5667e73d6bff Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 12 Apr 2024 02:10:55 +0300 Subject: [PATCH] Add SendQuickReplyShortcutMessagesLogEvent. --- td/telegram/MessagesManager.cpp | 105 +++++++++++++++++++++++++++++++- td/telegram/MessagesManager.h | 5 ++ td/telegram/TdDb.cpp | 1 + td/telegram/logevent/LogEvent.h | 1 + 4 files changed, 111 insertions(+), 1 deletion(-) diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 0c34cee90..cc576f4db 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -26876,6 +26876,44 @@ Result> MessagesManager::send_quick_reply_s return get_messages_object(-1, std::move(result), false); } +class MessagesManager::SendQuickReplyShortcutMessagesLogEvent { + public: + DialogId dialog_id; + QuickReplyShortcutId shortcut_id; + vector message_ids; + vector messages_in; + vector> messages_out; + + template + void store(StorerT &storer) const { + BEGIN_STORE_FLAGS(); + END_STORE_FLAGS(); + td::store(dialog_id, storer); + td::store(shortcut_id, storer); + td::store(message_ids, storer); + td::store(messages_in, storer); + } + + template + void parse(ParserT &parser) { + BEGIN_PARSE_FLAGS(); + END_PARSE_FLAGS(); + td::parse(dialog_id, parser); + td::parse(shortcut_id, parser); + td::parse(message_ids, parser); + td::parse(messages_out, parser); + } +}; + +uint64 MessagesManager::save_send_quick_reply_shortcut_messages_log_event(DialogId dialog_id, + QuickReplyShortcutId shortcut_id, + const vector &messages, + const vector &message_ids) { + SendQuickReplyShortcutMessagesLogEvent log_event{dialog_id, shortcut_id, message_ids, messages, Auto()}; + return binlog_add(G()->td_db()->get_binlog(), LogEvent::HandlerType::SendQuickReplyShortcutMessages, + get_log_event_storer(log_event)); +} + void MessagesManager::do_send_quick_reply_shortcut_messages(DialogId dialog_id, QuickReplyShortcutId shortcut_id, const vector &messages, const vector &message_ids, uint64 log_event_id) { @@ -26889,7 +26927,7 @@ void MessagesManager::do_send_quick_reply_shortcut_messages(DialogId dialog_id, } if (log_event_id == 0 && G()->use_message_database()) { - // log_event_id = save_send_quick_reply_shortcut_messages_log_event(dialog_id, shprtcut_id, messages, message_ids); + log_event_id = save_send_quick_reply_shortcut_messages_log_event(dialog_id, shortcut_id, messages, message_ids); } vector random_ids = @@ -37505,6 +37543,71 @@ void MessagesManager::on_binlog_events(vector &&events) { log_event.drop_author, log_event.drop_media_captions, event.id_); break; } + case LogEvent::HandlerType::SendQuickReplyShortcutMessages: { + if (!have_old_message_database) { + binlog_erase(G()->td_db()->get_binlog(), event.id_); + continue; + } + + SendQuickReplyShortcutMessagesLogEvent log_event; + log_event_parse(log_event, event.get_data()).ensure(); + + auto dialog_id = log_event.dialog_id; + auto messages = std::move(log_event.messages_out); + + Dependencies dependencies; + dependencies.add_dialog_and_dependencies(dialog_id); + for (auto &message : messages) { + add_message_dependencies(dependencies, message.get()); + } + if (!dependencies.resolve_force(td_, "SendQuickReplyShortcutMessagesLogEvent")) { + binlog_erase(G()->td_db()->get_binlog(), event.id_); + continue; + } + + Dialog *d = get_dialog(dialog_id); + CHECK(d != nullptr); + d->was_opened = true; + + auto now = G()->unix_time(); + if (can_send_message(dialog_id).is_error() || messages.empty() || + (messages[0]->send_date < now - MAX_RESEND_DELAY && + dialog_id != td_->dialog_manager_->get_my_dialog_id())) { + LOG(WARNING) << "Can't continue sending quick reply shortcut " << messages.size() << " message(s) to " + << dialog_id; + binlog_erase(G()->td_db()->get_binlog(), event.id_); + continue; + } + + LOG(INFO) << "Continue to send quick reply shortcut " << messages.size() << " message(s) to " << dialog_id + << " from binlog"; + + bool need_update = false; + bool need_update_dialog_pos = false; + vector sent_messages; + for (auto &message : messages) { + message->message_id = get_next_yet_unsent_message_id(d); + message->date = now; + message->content = dup_message_content(td_, dialog_id, message->content.get(), + MessageContentDupType::ServerCopy, MessageCopyOptions(true, false)); + CHECK(message->content != nullptr); + + restore_message_reply_to_message_id(d, message.get()); + + sent_messages.push_back(add_message_to_dialog(d, std::move(message), false, true, &need_update, + &need_update_dialog_pos, + "send quick reply shortcut message again")); + send_update_new_message(d, sent_messages.back()); + } + + if (need_update_dialog_pos) { + send_update_chat_last_message(d, "on_resent_quick_reply_shortcut_message"); + } + + do_send_quick_reply_shortcut_messages(dialog_id, log_event.shortcut_id, sent_messages, log_event.message_ids, + event.id_); + break; + } case LogEvent::HandlerType::DeleteMessage: { if (!have_old_message_database) { binlog_erase(G()->td_db()->get_binlog(), event.id_); diff --git a/td/telegram/MessagesManager.h b/td/telegram/MessagesManager.h index 088447327..a31a1f88c 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -1566,6 +1566,7 @@ class MessagesManager final : public Actor { class SendBotStartMessageLogEvent; class SendInlineQueryResultMessageLogEvent; class SendMessageLogEvent; + class SendQuickReplyShortcutMessagesLogEvent; class SendScreenshotTakenNotificationMessageLogEvent; class SetDialogFolderIdOnServerLogEvent; class ToggleDialogIsBlockedOnServerLogEvent; @@ -1773,6 +1774,10 @@ class MessagesManager final : public Actor { const vector &message_ids, bool drop_author, bool drop_media_captions, uint64 log_event_id); + uint64 save_send_quick_reply_shortcut_messages_log_event(DialogId dialog_id, QuickReplyShortcutId shortcut_id, + const vector &messages, + const vector &message_ids); + void do_send_quick_reply_shortcut_messages(DialogId dialog_id, QuickReplyShortcutId shortcut_id, const vector &messages, const vector &message_ids, uint64 log_event_id); diff --git a/td/telegram/TdDb.cpp b/td/telegram/TdDb.cpp index 167bfdebc..4727a13b6 100644 --- a/td/telegram/TdDb.cpp +++ b/td/telegram/TdDb.cpp @@ -126,6 +126,7 @@ Status init_binlog(Binlog &binlog, string path, BinlogKeyValue &binlog_p case LogEvent::HandlerType::DeleteTopicHistoryOnServer: case LogEvent::HandlerType::ToggleDialogIsTranslatableOnServer: case LogEvent::HandlerType::ToggleDialogViewAsMessagesOnServer: + case LogEvent::HandlerType::SendQuickReplyShortcutMessages: events.to_messages_manager.push_back(event.clone()); break; case LogEvent::HandlerType::DeleteStoryOnServer: diff --git a/td/telegram/logevent/LogEvent.h b/td/telegram/logevent/LogEvent.h index 7a53ad6e1..576b3cc37 100644 --- a/td/telegram/logevent/LogEvent.h +++ b/td/telegram/logevent/LogEvent.h @@ -104,6 +104,7 @@ class LogEvent { DeleteTopicHistoryOnServer = 0x125, ToggleDialogIsTranslatableOnServer = 0x126, ToggleDialogViewAsMessagesOnServer = 0x127, + SendQuickReplyShortcutMessages = 0x128, GetChannelDifference = 0x140, AddMessagePushNotification = 0x200, EditMessagePushNotification = 0x201,