Make DraftMessage fields private and improve their names.

This commit is contained in:
levlam 2023-05-31 17:46:06 +03:00
parent 0b56f92190
commit e599d16fc2
4 changed files with 53 additions and 40 deletions

View File

@ -41,15 +41,15 @@ class SaveDraftMessageQuery final : public Td::ResultHandler {
ServerMessageId reply_to_message_id; ServerMessageId reply_to_message_id;
vector<telegram_api::object_ptr<telegram_api::MessageEntity>> input_message_entities; vector<telegram_api::object_ptr<telegram_api::MessageEntity>> input_message_entities;
if (draft_message != nullptr) { if (draft_message != nullptr) {
if (draft_message->reply_to_message_id.is_valid() && draft_message->reply_to_message_id.is_server()) { if (draft_message->reply_to_message_id_.is_valid() && draft_message->reply_to_message_id_.is_server()) {
reply_to_message_id = draft_message->reply_to_message_id.get_server_message_id(); reply_to_message_id = draft_message->reply_to_message_id_.get_server_message_id();
flags |= telegram_api::messages_saveDraft::REPLY_TO_MSG_ID_MASK; flags |= telegram_api::messages_saveDraft::REPLY_TO_MSG_ID_MASK;
} }
if (draft_message->input_message_text.disable_web_page_preview) { if (draft_message->input_message_text_.disable_web_page_preview) {
flags |= telegram_api::messages_saveDraft::NO_WEBPAGE_MASK; flags |= telegram_api::messages_saveDraft::NO_WEBPAGE_MASK;
} }
input_message_entities = get_input_message_entities( input_message_entities = get_input_message_entities(
td_->contacts_manager_.get(), draft_message->input_message_text.text.entities, "SaveDraftMessageQuery"); td_->contacts_manager_.get(), draft_message->input_message_text_.text.entities, "SaveDraftMessageQuery");
if (!input_message_entities.empty()) { if (!input_message_entities.empty()) {
flags |= telegram_api::messages_saveDraft::ENTITIES_MASK; flags |= telegram_api::messages_saveDraft::ENTITIES_MASK;
} }
@ -57,7 +57,7 @@ class SaveDraftMessageQuery final : public Td::ResultHandler {
send_query(G()->net_query_creator().create( send_query(G()->net_query_creator().create(
telegram_api::messages_saveDraft( telegram_api::messages_saveDraft(
flags, false /*ignored*/, reply_to_message_id.get(), 0, std::move(input_peer), flags, false /*ignored*/, reply_to_message_id.get(), 0, std::move(input_peer),
draft_message == nullptr ? string() : draft_message->input_message_text.text.text, draft_message == nullptr ? string() : draft_message->input_message_text_.text.text,
std::move(input_message_entities)), std::move(input_message_entities)),
{{dialog_id}})); {{dialog_id}}));
} }
@ -139,32 +139,32 @@ class ClearAllDraftsQuery final : public Td::ResultHandler {
}; };
bool DraftMessage::need_update_to(const DraftMessage &other, bool from_update) const { 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) { if (reply_to_message_id_ == other.reply_to_message_id_ && input_message_text_ == other.input_message_text_) {
return date < other.date; return date_ < other.date_;
} else { } else {
return !from_update || date <= other.date; return !from_update || date_ <= other.date_;
} }
} }
void DraftMessage::add_dependencies(Dependencies &dependencies) const { void DraftMessage::add_dependencies(Dependencies &dependencies) const {
add_formatted_text_dependencies(dependencies, &input_message_text.text); add_formatted_text_dependencies(dependencies, &input_message_text_.text);
} }
td_api::object_ptr<td_api::draftMessage> DraftMessage::get_draft_message_object() const { td_api::object_ptr<td_api::draftMessage> DraftMessage::get_draft_message_object() const {
return td_api::make_object<td_api::draftMessage>(reply_to_message_id.get(), date, return td_api::make_object<td_api::draftMessage>(reply_to_message_id_.get(), date_,
get_input_message_text_object(input_message_text)); get_input_message_text_object(input_message_text_));
} }
DraftMessage::DraftMessage(ContactsManager *contacts_manager, DraftMessage::DraftMessage(ContactsManager *contacts_manager,
telegram_api::object_ptr<telegram_api::draftMessage> &&draft_message) { telegram_api::object_ptr<telegram_api::draftMessage> &&draft_message) {
CHECK(draft_message != nullptr); CHECK(draft_message != nullptr);
auto flags = draft_message->flags_; auto flags = draft_message->flags_;
date = draft_message->date_; date_ = draft_message->date_;
if ((flags & telegram_api::draftMessage::REPLY_TO_MSG_ID_MASK) != 0) { if ((flags & telegram_api::draftMessage::REPLY_TO_MSG_ID_MASK) != 0) {
reply_to_message_id = MessageId(ServerMessageId(draft_message->reply_to_msg_id_)); reply_to_message_id_ = MessageId(ServerMessageId(draft_message->reply_to_msg_id_));
if (!reply_to_message_id.is_valid()) { if (!reply_to_message_id_.is_valid()) {
LOG(ERROR) << "Receive " << reply_to_message_id << " as reply_to_message_id in the draft message"; LOG(ERROR) << "Receive " << reply_to_message_id_ << " as reply_to_message_id in the draft message";
reply_to_message_id = MessageId(); reply_to_message_id_ = MessageId();
} }
} }
@ -177,9 +177,9 @@ DraftMessage::DraftMessage(ContactsManager *contacts_manager,
} }
entities = find_entities(draft_message->message_, false, true); entities = find_entities(draft_message->message_, false, true);
} }
input_message_text.text = FormattedText{std::move(draft_message->message_), std::move(entities)}; 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_.disable_web_page_preview = draft_message->no_webpage_;
input_message_text.clear_draft = false; input_message_text_.clear_draft = false;
} }
Result<unique_ptr<DraftMessage>> DraftMessage::get_draft_message( Result<unique_ptr<DraftMessage>> DraftMessage::get_draft_message(
@ -190,12 +190,12 @@ Result<unique_ptr<DraftMessage>> DraftMessage::get_draft_message(
} }
auto result = make_unique<DraftMessage>(); auto result = make_unique<DraftMessage>();
result->reply_to_message_id = MessageId(draft_message->reply_to_message_id_); result->reply_to_message_id_ = MessageId(draft_message->reply_to_message_id_);
if (result->reply_to_message_id != MessageId() && !result->reply_to_message_id.is_valid()) { if (result->reply_to_message_id_ != MessageId() && !result->reply_to_message_id_.is_valid()) {
return Status::Error(400, "Invalid reply_to_message_id specified"); return Status::Error(400, "Invalid reply_to_message_id specified");
} }
result->reply_to_message_id = td->messages_manager_->get_reply_to_message_id(dialog_id, top_thread_message_id, result->reply_to_message_id_ = td->messages_manager_->get_reply_to_message_id(dialog_id, top_thread_message_id,
result->reply_to_message_id, true); result->reply_to_message_id_, true);
auto input_message_content = std::move(draft_message->input_message_text_); auto input_message_content = std::move(draft_message->input_message_text_);
if (input_message_content != nullptr) { if (input_message_content != nullptr) {
@ -204,14 +204,14 @@ Result<unique_ptr<DraftMessage>> DraftMessage::get_draft_message(
} }
TRY_RESULT(message_content, TRY_RESULT(message_content,
process_input_message_text(td, dialog_id, std::move(input_message_content), false, true)); process_input_message_text(td, dialog_id, std::move(input_message_content), false, true));
result->input_message_text = std::move(message_content); result->input_message_text_ = std::move(message_content);
} }
if (!result->reply_to_message_id.is_valid() && result->input_message_text.text.text.empty()) { if (!result->reply_to_message_id_.is_valid() && result->input_message_text_.text.text.empty()) {
return nullptr; return nullptr;
} }
result->date = G()->unix_time(); result->date_ = G()->unix_time();
return std::move(result); return std::move(result);
} }

View File

@ -23,14 +23,20 @@ class Dependencies;
class Td; class Td;
class DraftMessage { class DraftMessage {
public: int32 date_ = 0;
int32 date = 0; MessageId reply_to_message_id_;
MessageId reply_to_message_id; InputMessageText input_message_text_;
InputMessageText input_message_text;
friend class SaveDraftMessageQuery;
public:
DraftMessage() = default; DraftMessage() = default;
DraftMessage(ContactsManager *contacts_manager, telegram_api::object_ptr<telegram_api::draftMessage> &&draft_message); DraftMessage(ContactsManager *contacts_manager, telegram_api::object_ptr<telegram_api::draftMessage> &&draft_message);
int32 get_date() const {
return date_;
}
bool need_update_to(const DraftMessage &other, bool from_update) const; bool need_update_to(const DraftMessage &other, bool from_update) const;
void add_dependencies(Dependencies &dependencies) const; void add_dependencies(Dependencies &dependencies) const;
@ -39,6 +45,12 @@ class DraftMessage {
static Result<unique_ptr<DraftMessage>> get_draft_message(Td *td, DialogId dialog_id, MessageId top_thread_message_id, static Result<unique_ptr<DraftMessage>> get_draft_message(Td *td, DialogId dialog_id, MessageId top_thread_message_id,
td_api::object_ptr<td_api::draftMessage> &&draft_message); td_api::object_ptr<td_api::draftMessage> &&draft_message);
template <class StorerT>
void store(StorerT &storer) const;
template <class ParserT>
void parse(ParserT &parser);
}; };
bool need_update_draft_message(const unique_ptr<DraftMessage> &old_draft_message, bool need_update_draft_message(const unique_ptr<DraftMessage> &old_draft_message,

View File

@ -15,17 +15,17 @@
namespace td { namespace td {
template <class StorerT> template <class StorerT>
void store(const DraftMessage &draft_message, StorerT &storer) { void DraftMessage::store(StorerT &storer) const {
store(draft_message.date, storer); td::store(date_, storer);
store(draft_message.reply_to_message_id, storer); td::store(reply_to_message_id_, storer);
store(draft_message.input_message_text, storer); td::store(input_message_text_, storer);
} }
template <class ParserT> template <class ParserT>
void parse(DraftMessage &draft_message, ParserT &parser) { void DraftMessage::parse(ParserT &parser) {
parse(draft_message.date, parser); td::parse(date_, parser);
parse(draft_message.reply_to_message_id, parser); td::parse(reply_to_message_id_, parser);
parse(draft_message.input_message_text, parser); td::parse(input_message_text_, parser);
} }
} // namespace td } // namespace td

View File

@ -36974,8 +36974,9 @@ void MessagesManager::update_dialog_pos(Dialog *d, const char *source, bool need
} }
} }
if (d->draft_message != nullptr && can_send_message(d->dialog_id).is_ok()) { if (d->draft_message != nullptr && can_send_message(d->dialog_id).is_ok()) {
LOG(INFO) << "Draft message at " << d->draft_message->date << " found"; auto draft_message_date = d->draft_message->get_date();
int64 draft_order = get_dialog_order(MessageId(), d->draft_message->date); LOG(INFO) << "Draft message at " << draft_message_date << " found";
int64 draft_order = get_dialog_order(MessageId(), draft_message_date);
if (draft_order > new_order) { if (draft_order > new_order) {
new_order = draft_order; new_order = draft_order;
} }