diff --git a/td/telegram/DraftMessage.cpp b/td/telegram/DraftMessage.cpp index d0032cc5d..d4f8f4f90 100644 --- a/td/telegram/DraftMessage.cpp +++ b/td/telegram/DraftMessage.cpp @@ -138,82 +138,53 @@ class ClearAllDraftsQuery final : public Td::ResultHandler { } }; -bool need_update_draft_message(const unique_ptr &old_draft_message, - const unique_ptr &new_draft_message, bool from_update) { - if (new_draft_message == nullptr) { - return old_draft_message != nullptr; - } - if (old_draft_message == nullptr) { - return true; - } - if (old_draft_message->reply_to_message_id == new_draft_message->reply_to_message_id && - old_draft_message->input_message_text == new_draft_message->input_message_text) { - return old_draft_message->date < new_draft_message->date; +bool DraftMessage::need_update_to(const DraftMessage &other, bool from_update) const { + if (reply_to_message_id == other.reply_to_message_id && input_message_text == other.input_message_text) { + return date < other.date; } else { - return !from_update || old_draft_message->date <= new_draft_message->date; + return !from_update || date <= other.date; } } -void add_draft_message_dependencies(Dependencies &dependencies, const unique_ptr &draft_message) { - if (draft_message == nullptr) { - return; - } - add_formatted_text_dependencies(dependencies, &draft_message->input_message_text.text); +void DraftMessage::add_dependencies(Dependencies &dependencies) const { + add_formatted_text_dependencies(dependencies, &input_message_text.text); } -td_api::object_ptr get_draft_message_object(const unique_ptr &draft_message) { - if (draft_message == nullptr) { - return nullptr; - } - return td_api::make_object(draft_message->reply_to_message_id.get(), draft_message->date, - get_input_message_text_object(draft_message->input_message_text)); +td_api::object_ptr DraftMessage::get_draft_message_object() const { + return td_api::make_object(reply_to_message_id.get(), date, + get_input_message_text_object(input_message_text)); } -unique_ptr get_draft_message(ContactsManager *contacts_manager, - telegram_api::object_ptr &&draft_message_ptr) { - if (draft_message_ptr == nullptr) { - return nullptr; - } - auto constructor_id = draft_message_ptr->get_id(); - switch (constructor_id) { - case telegram_api::draftMessageEmpty::ID: - return nullptr; - case telegram_api::draftMessage::ID: { - auto draft = move_tl_object_as(draft_message_ptr); - auto flags = draft->flags_; - auto result = make_unique(); - result->date = draft->date_; - if ((flags & telegram_api::draftMessage::REPLY_TO_MSG_ID_MASK) != 0) { - result->reply_to_message_id = MessageId(ServerMessageId(draft->reply_to_msg_id_)); - if (!result->reply_to_message_id.is_valid()) { - LOG(ERROR) << "Receive " << result->reply_to_message_id << " as reply_to_message_id in the draft"; - result->reply_to_message_id = MessageId(); - } - } - - auto entities = get_message_entities(contacts_manager, std::move(draft->entities_), "draftMessage"); - auto status = fix_formatted_text(draft->message_, entities, true, true, true, true, true); - if (status.is_error()) { - LOG(ERROR) << "Receive error " << status << " while parsing draft " << draft->message_; - if (!clean_input_string(draft->message_)) { - draft->message_.clear(); - } - entities = find_entities(draft->message_, false, true); - } - result->input_message_text.text = FormattedText{std::move(draft->message_), std::move(entities)}; - result->input_message_text.disable_web_page_preview = draft->no_webpage_; - result->input_message_text.clear_draft = false; - - return result; +DraftMessage::DraftMessage(ContactsManager *contacts_manager, + telegram_api::object_ptr &&draft_message) { + CHECK(draft_message != nullptr); + auto flags = draft_message->flags_; + date = draft_message->date_; + if ((flags & telegram_api::draftMessage::REPLY_TO_MSG_ID_MASK) != 0) { + reply_to_message_id = MessageId(ServerMessageId(draft_message->reply_to_msg_id_)); + if (!reply_to_message_id.is_valid()) { + LOG(ERROR) << "Receive " << reply_to_message_id << " as reply_to_message_id in the draft message"; + reply_to_message_id = MessageId(); } - default: - UNREACHABLE(); - return nullptr; } + + auto entities = get_message_entities(contacts_manager, std::move(draft_message->entities_), "draftMessage"); + auto status = fix_formatted_text(draft_message->message_, entities, true, true, true, true, true); + if (status.is_error()) { + LOG(ERROR) << "Receive error " << status << " while parsing draft " << draft_message->message_; + if (!clean_input_string(draft_message->message_)) { + draft_message->message_.clear(); + } + entities = find_entities(draft_message->message_, false, true); + } + input_message_text.text = FormattedText{std::move(draft_message->message_), std::move(entities)}; + input_message_text.disable_web_page_preview = draft_message->no_webpage_; + input_message_text.clear_draft = false; } -Result> get_draft_message(Td *td, DialogId dialog_id, MessageId top_thread_message_id, - td_api::object_ptr &&draft_message) { +Result> DraftMessage::get_draft_message( + Td *td, DialogId dialog_id, MessageId top_thread_message_id, + td_api::object_ptr &&draft_message) { if (draft_message == nullptr) { return nullptr; } @@ -244,6 +215,49 @@ Result> get_draft_message(Td *td, DialogId dialog_id, M return std::move(result); } +bool need_update_draft_message(const unique_ptr &old_draft_message, + const unique_ptr &new_draft_message, bool from_update) { + if (new_draft_message == nullptr) { + return old_draft_message != nullptr; + } + if (old_draft_message == nullptr) { + return true; + } + return old_draft_message->need_update_to(*new_draft_message, from_update); +} + +void add_draft_message_dependencies(Dependencies &dependencies, const unique_ptr &draft_message) { + if (draft_message == nullptr) { + return; + } + draft_message->add_dependencies(dependencies); +} + +td_api::object_ptr get_draft_message_object(const unique_ptr &draft_message) { + if (draft_message == nullptr) { + return nullptr; + } + return draft_message->get_draft_message_object(); +} + +unique_ptr get_draft_message(ContactsManager *contacts_manager, + telegram_api::object_ptr &&draft_message_ptr) { + if (draft_message_ptr == nullptr) { + return nullptr; + } + auto constructor_id = draft_message_ptr->get_id(); + switch (constructor_id) { + case telegram_api::draftMessageEmpty::ID: + return nullptr; + case telegram_api::draftMessage::ID: + return td::make_unique(contacts_manager, + telegram_api::move_object_as(draft_message_ptr)); + default: + UNREACHABLE(); + return nullptr; + } +} + void save_draft_message(Td *td, DialogId dialog_id, const unique_ptr &draft_message, Promise &&promise) { td->create_handler(std::move(promise))->send(dialog_id, draft_message); diff --git a/td/telegram/DraftMessage.h b/td/telegram/DraftMessage.h index 77f33fbae..f6890d21a 100644 --- a/td/telegram/DraftMessage.h +++ b/td/telegram/DraftMessage.h @@ -27,6 +27,18 @@ class DraftMessage { int32 date = 0; MessageId reply_to_message_id; InputMessageText input_message_text; + + DraftMessage() = default; + DraftMessage(ContactsManager *contacts_manager, telegram_api::object_ptr &&draft_message); + + bool need_update_to(const DraftMessage &other, bool from_update) const; + + void add_dependencies(Dependencies &dependencies) const; + + td_api::object_ptr get_draft_message_object() const; + + static Result> get_draft_message(Td *td, DialogId dialog_id, MessageId top_thread_message_id, + td_api::object_ptr &&draft_message); }; bool need_update_draft_message(const unique_ptr &old_draft_message, @@ -39,9 +51,6 @@ td_api::object_ptr get_draft_message_object(const unique_p unique_ptr get_draft_message(ContactsManager *contacts_manager, telegram_api::object_ptr &&draft_message_ptr); -Result> get_draft_message(Td *td, DialogId dialog_id, MessageId top_thread_message_id, - td_api::object_ptr &&draft_message); - void save_draft_message(Td *td, DialogId dialog_id, const unique_ptr &draft_message, Promise &&promise); diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 2d1573955..44fb108f2 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -18932,7 +18932,8 @@ Status MessagesManager::set_dialog_draft_message(DialogId dialog_id, MessageId t TRY_STATUS(can_use_top_thread_message_id(d, top_thread_message_id, MessageId())); - TRY_RESULT(new_draft_message, get_draft_message(td_, dialog_id, top_thread_message_id, std::move(draft_message))); + TRY_RESULT(new_draft_message, + DraftMessage::get_draft_message(td_, dialog_id, top_thread_message_id, std::move(draft_message))); if (top_thread_message_id != MessageId()) { CHECK(top_thread_message_id.is_valid());