2018-09-29 01:45:43 +02:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2018-09-29 01:45:43 +02:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#include "td/telegram/DraftMessage.h"
|
|
|
|
|
2023-07-01 15:22:01 +02:00
|
|
|
#include "td/telegram/AccessRights.h"
|
2023-05-31 15:34:12 +02:00
|
|
|
#include "td/telegram/Dependencies.h"
|
2021-09-29 13:06:43 +02:00
|
|
|
#include "td/telegram/Global.h"
|
2018-09-29 01:45:43 +02:00
|
|
|
#include "td/telegram/MessageEntity.h"
|
2023-05-31 14:57:30 +02:00
|
|
|
#include "td/telegram/MessagesManager.h"
|
2018-09-29 01:45:43 +02:00
|
|
|
#include "td/telegram/misc.h"
|
2019-11-26 17:33:18 +01:00
|
|
|
#include "td/telegram/ServerMessageId.h"
|
2023-05-31 14:57:30 +02:00
|
|
|
#include "td/telegram/Td.h"
|
2023-07-01 13:53:04 +02:00
|
|
|
#include "td/telegram/telegram_api.h"
|
2023-05-31 15:17:20 +02:00
|
|
|
#include "td/telegram/UpdatesManager.h"
|
2023-10-16 19:12:33 +02:00
|
|
|
#include "td/telegram/WebPagesManager.h"
|
2018-09-29 01:45:43 +02:00
|
|
|
|
2023-05-31 14:57:30 +02:00
|
|
|
#include "td/utils/buffer.h"
|
2018-09-29 01:45:43 +02:00
|
|
|
#include "td/utils/logging.h"
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
2023-05-31 14:57:30 +02:00
|
|
|
class SaveDraftMessageQuery final : public Td::ResultHandler {
|
|
|
|
Promise<Unit> promise_;
|
|
|
|
DialogId dialog_id_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit SaveDraftMessageQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void send(DialogId dialog_id, const unique_ptr<DraftMessage> &draft_message) {
|
|
|
|
dialog_id_ = dialog_id;
|
|
|
|
|
|
|
|
auto input_peer = td_->messages_manager_->get_input_peer(dialog_id, AccessRights::Write);
|
|
|
|
if (input_peer == nullptr) {
|
|
|
|
LOG(INFO) << "Can't update draft message because have no write access to " << dialog_id;
|
|
|
|
return on_error(Status::Error(400, "Can't save draft message"));
|
|
|
|
}
|
|
|
|
|
|
|
|
int32 flags = 0;
|
|
|
|
ServerMessageId reply_to_message_id;
|
|
|
|
vector<telegram_api::object_ptr<telegram_api::MessageEntity>> input_message_entities;
|
2023-10-16 19:12:33 +02:00
|
|
|
telegram_api::object_ptr<telegram_api::InputMedia> media;
|
2023-05-31 14:57:30 +02:00
|
|
|
if (draft_message != nullptr) {
|
2023-09-28 15:13:36 +02:00
|
|
|
/*
|
2023-05-31 16:46:06 +02:00
|
|
|
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();
|
2023-05-31 14:57:30 +02:00
|
|
|
flags |= telegram_api::messages_saveDraft::REPLY_TO_MSG_ID_MASK;
|
|
|
|
}
|
2023-09-28 15:13:36 +02:00
|
|
|
*/
|
2023-05-31 16:46:06 +02:00
|
|
|
if (draft_message->input_message_text_.disable_web_page_preview) {
|
2023-05-31 14:57:30 +02:00
|
|
|
flags |= telegram_api::messages_saveDraft::NO_WEBPAGE_MASK;
|
|
|
|
}
|
|
|
|
input_message_entities = get_input_message_entities(
|
2023-05-31 16:46:06 +02:00
|
|
|
td_->contacts_manager_.get(), draft_message->input_message_text_.text.entities, "SaveDraftMessageQuery");
|
2023-05-31 14:57:30 +02:00
|
|
|
if (!input_message_entities.empty()) {
|
|
|
|
flags |= telegram_api::messages_saveDraft::ENTITIES_MASK;
|
|
|
|
}
|
2023-10-16 19:12:33 +02:00
|
|
|
media = draft_message->input_message_text_.get_input_media_web_page();
|
|
|
|
if (media != nullptr) {
|
|
|
|
flags |= telegram_api::messages_saveDraft::MEDIA_MASK;
|
|
|
|
}
|
2023-05-31 14:57:30 +02:00
|
|
|
}
|
|
|
|
send_query(G()->net_query_creator().create(
|
|
|
|
telegram_api::messages_saveDraft(
|
2023-09-28 15:13:36 +02:00
|
|
|
flags, false /*ignored*/, false /*ignored*/, nullptr, std::move(input_peer),
|
2023-05-31 16:46:06 +02:00
|
|
|
draft_message == nullptr ? string() : draft_message->input_message_text_.text.text,
|
2023-10-16 19:12:33 +02:00
|
|
|
std::move(input_message_entities), std::move(media)),
|
2023-05-31 14:57:30 +02:00
|
|
|
{{dialog_id}}));
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_result(BufferSlice packet) final {
|
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_saveDraft>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
|
|
|
return on_error(result_ptr.move_as_error());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool result = result_ptr.ok();
|
|
|
|
if (!result) {
|
|
|
|
return on_error(Status::Error(400, "Save draft failed"));
|
|
|
|
}
|
|
|
|
|
|
|
|
promise_.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_error(Status status) final {
|
2023-07-22 21:37:27 +02:00
|
|
|
if (status.message() == "TOPIC_CLOSED") {
|
|
|
|
// when the draft is a reply to a message in a closed topic, server will not allow to save it
|
|
|
|
// with the error "TOPIC_CLOSED", but the draft will be kept locally
|
|
|
|
return promise_.set_value(Unit());
|
|
|
|
}
|
2023-05-31 14:57:30 +02:00
|
|
|
if (!td_->messages_manager_->on_get_dialog_error(dialog_id_, status, "SaveDraftMessageQuery")) {
|
|
|
|
LOG(ERROR) << "Receive error for SaveDraftMessageQuery: " << status;
|
|
|
|
}
|
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-05-31 15:17:20 +02:00
|
|
|
class GetAllDraftsQuery final : public Td::ResultHandler {
|
|
|
|
public:
|
|
|
|
void send() {
|
|
|
|
send_query(G()->net_query_creator().create(telegram_api::messages_getAllDrafts()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_result(BufferSlice packet) final {
|
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_getAllDrafts>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
|
|
|
return on_error(result_ptr.move_as_error());
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ptr = result_ptr.move_as_ok();
|
|
|
|
LOG(INFO) << "Receive result for GetAllDraftsQuery: " << to_string(ptr);
|
|
|
|
td_->updates_manager_->on_get_updates(std::move(ptr), Promise<Unit>());
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_error(Status status) final {
|
|
|
|
if (!G()->is_expected_error(status)) {
|
|
|
|
LOG(ERROR) << "Receive error for GetAllDraftsQuery: " << status;
|
|
|
|
}
|
|
|
|
status.ignore();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-05-31 15:23:23 +02:00
|
|
|
class ClearAllDraftsQuery final : public Td::ResultHandler {
|
|
|
|
Promise<Unit> promise_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit ClearAllDraftsQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void send() {
|
|
|
|
send_query(G()->net_query_creator().create(telegram_api::messages_clearAllDrafts()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_result(BufferSlice packet) final {
|
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_clearAllDrafts>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
|
|
|
return on_error(result_ptr.move_as_error());
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(INFO) << "Receive result for ClearAllDraftsQuery: " << result_ptr.ok();
|
|
|
|
promise_.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_error(Status status) final {
|
|
|
|
if (!G()->is_expected_error(status)) {
|
|
|
|
LOG(ERROR) << "Receive error for ClearAllDraftsQuery: " << status;
|
|
|
|
}
|
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-05-31 16:35:56 +02:00
|
|
|
bool DraftMessage::need_update_to(const DraftMessage &other, bool from_update) const {
|
2023-05-31 16:46:06 +02:00
|
|
|
if (reply_to_message_id_ == other.reply_to_message_id_ && input_message_text_ == other.input_message_text_) {
|
|
|
|
return date_ < other.date_;
|
2023-05-31 14:29:37 +02:00
|
|
|
} else {
|
2023-05-31 16:46:06 +02:00
|
|
|
return !from_update || date_ <= other.date_;
|
2023-05-31 15:34:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-31 16:35:56 +02:00
|
|
|
void DraftMessage::add_dependencies(Dependencies &dependencies) const {
|
2023-10-17 11:31:01 +02:00
|
|
|
input_message_text_.add_dependencies(dependencies);
|
2023-05-31 14:29:37 +02:00
|
|
|
}
|
|
|
|
|
2023-05-31 16:35:56 +02:00
|
|
|
td_api::object_ptr<td_api::draftMessage> DraftMessage::get_draft_message_object() const {
|
2023-05-31 16:46:06 +02:00
|
|
|
return td_api::make_object<td_api::draftMessage>(reply_to_message_id_.get(), date_,
|
2023-10-16 19:56:28 +02:00
|
|
|
input_message_text_.get_input_message_text_object());
|
2018-09-29 01:45:43 +02:00
|
|
|
}
|
|
|
|
|
2023-10-16 19:12:33 +02:00
|
|
|
DraftMessage::DraftMessage(Td *td, telegram_api::object_ptr<telegram_api::draftMessage> &&draft_message) {
|
2023-05-31 16:35:56 +02:00
|
|
|
CHECK(draft_message != nullptr);
|
2023-05-31 16:46:06 +02:00
|
|
|
date_ = draft_message->date_;
|
2023-09-28 15:13:36 +02:00
|
|
|
/*
|
|
|
|
auto flags = draft_message->flags_;
|
2023-05-31 16:35:56 +02:00
|
|
|
if ((flags & telegram_api::draftMessage::REPLY_TO_MSG_ID_MASK) != 0) {
|
2023-05-31 16:46:06 +02:00
|
|
|
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();
|
2023-05-31 16:35:56 +02:00
|
|
|
}
|
2018-09-29 01:45:43 +02:00
|
|
|
}
|
2023-09-28 15:13:36 +02:00
|
|
|
*/
|
2018-09-29 01:45:43 +02:00
|
|
|
|
2023-10-16 19:12:33 +02:00
|
|
|
auto entities =
|
|
|
|
get_message_entities(td->contacts_manager_.get(), std::move(draft_message->entities_), "draftMessage");
|
2023-05-31 16:35:56 +02:00
|
|
|
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();
|
2018-09-29 01:45:43 +02:00
|
|
|
}
|
2023-05-31 16:35:56 +02:00
|
|
|
entities = find_entities(draft_message->message_, false, true);
|
2018-09-29 01:45:43 +02:00
|
|
|
}
|
2023-10-16 19:12:33 +02:00
|
|
|
string web_page_url;
|
|
|
|
bool force_small_media = false;
|
|
|
|
bool force_large_media = false;
|
|
|
|
if (draft_message->media_ != nullptr) {
|
|
|
|
if (draft_message->media_->get_id() != telegram_api::inputMediaWebPage::ID) {
|
|
|
|
LOG(ERROR) << "Receive draft message with " << to_string(draft_message->media_);
|
|
|
|
} else {
|
|
|
|
auto media = telegram_api::move_object_as<telegram_api::inputMediaWebPage>(draft_message->media_);
|
|
|
|
web_page_url = std::move(media->url_);
|
2023-10-16 22:09:17 +02:00
|
|
|
if (web_page_url.empty()) {
|
|
|
|
LOG(ERROR) << "Have no URL in a draft with manual link preview";
|
|
|
|
}
|
2023-10-16 19:12:33 +02:00
|
|
|
force_small_media = media->force_small_media_;
|
|
|
|
force_large_media = media->force_large_media_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
input_message_text_ =
|
|
|
|
InputMessageText(FormattedText{std::move(draft_message->message_), std::move(entities)}, std::move(web_page_url),
|
|
|
|
draft_message->no_webpage_, force_small_media, force_large_media, false);
|
2018-09-29 01:45:43 +02:00
|
|
|
}
|
|
|
|
|
2023-05-31 16:35:56 +02:00
|
|
|
Result<unique_ptr<DraftMessage>> DraftMessage::get_draft_message(
|
|
|
|
Td *td, DialogId dialog_id, MessageId top_thread_message_id,
|
|
|
|
td_api::object_ptr<td_api::draftMessage> &&draft_message) {
|
2021-09-29 13:06:43 +02:00
|
|
|
if (draft_message == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto result = make_unique<DraftMessage>();
|
2023-05-31 16:46:06 +02:00
|
|
|
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()) {
|
2021-09-29 13:06:43 +02:00
|
|
|
return Status::Error(400, "Invalid reply_to_message_id specified");
|
|
|
|
}
|
2023-06-08 16:27:24 +02:00
|
|
|
result->reply_to_message_id_ =
|
|
|
|
td->messages_manager_
|
|
|
|
->get_message_input_reply_to(
|
|
|
|
dialog_id, top_thread_message_id,
|
2023-10-06 10:58:05 +02:00
|
|
|
td_api::make_object<td_api::inputMessageReplyToMessage>(result->reply_to_message_id_.get()), true)
|
2023-06-08 16:27:24 +02:00
|
|
|
.message_id_;
|
2021-09-29 13:06:43 +02:00
|
|
|
|
|
|
|
auto input_message_content = std::move(draft_message->input_message_text_);
|
|
|
|
if (input_message_content != nullptr) {
|
|
|
|
if (input_message_content->get_id() != td_api::inputMessageText::ID) {
|
|
|
|
return Status::Error(400, "Input message content type must be InputMessageText");
|
|
|
|
}
|
2023-10-16 19:12:33 +02:00
|
|
|
TRY_RESULT(input_message_text,
|
2022-07-22 17:11:55 +02:00
|
|
|
process_input_message_text(td, dialog_id, std::move(input_message_content), false, true));
|
2023-10-16 19:12:33 +02:00
|
|
|
result->input_message_text_ = std::move(input_message_text);
|
2021-09-29 13:06:43 +02:00
|
|
|
}
|
|
|
|
|
2023-10-16 19:12:33 +02:00
|
|
|
if (!result->reply_to_message_id_.is_valid() && result->input_message_text_.is_empty()) {
|
2023-05-31 16:19:31 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2023-05-31 16:46:06 +02:00
|
|
|
result->date_ = G()->unix_time();
|
2021-09-30 23:12:28 +02:00
|
|
|
return std::move(result);
|
2021-09-29 13:06:43 +02:00
|
|
|
}
|
|
|
|
|
2023-05-31 16:35:56 +02:00
|
|
|
bool need_update_draft_message(const unique_ptr<DraftMessage> &old_draft_message,
|
|
|
|
const unique_ptr<DraftMessage> &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<DraftMessage> &draft_message) {
|
|
|
|
if (draft_message == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
draft_message->add_dependencies(dependencies);
|
|
|
|
}
|
|
|
|
|
|
|
|
td_api::object_ptr<td_api::draftMessage> get_draft_message_object(const unique_ptr<DraftMessage> &draft_message) {
|
|
|
|
if (draft_message == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return draft_message->get_draft_message_object();
|
|
|
|
}
|
|
|
|
|
2023-10-16 19:12:33 +02:00
|
|
|
unique_ptr<DraftMessage> get_draft_message(Td *td,
|
2023-05-31 16:35:56 +02:00
|
|
|
telegram_api::object_ptr<telegram_api::DraftMessage> &&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:
|
2023-10-16 19:12:33 +02:00
|
|
|
return td::make_unique<DraftMessage>(td,
|
2023-05-31 16:35:56 +02:00
|
|
|
telegram_api::move_object_as<telegram_api::draftMessage>(draft_message_ptr));
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-31 14:57:30 +02:00
|
|
|
void save_draft_message(Td *td, DialogId dialog_id, const unique_ptr<DraftMessage> &draft_message,
|
|
|
|
Promise<Unit> &&promise) {
|
|
|
|
td->create_handler<SaveDraftMessageQuery>(std::move(promise))->send(dialog_id, draft_message);
|
|
|
|
}
|
|
|
|
|
2023-05-31 15:17:20 +02:00
|
|
|
void load_all_draft_messages(Td *td) {
|
|
|
|
td->create_handler<GetAllDraftsQuery>()->send();
|
|
|
|
}
|
|
|
|
|
2023-05-31 15:23:23 +02:00
|
|
|
void clear_all_draft_messages(Td *td, Promise<Unit> &&promise) {
|
|
|
|
td->create_handler<ClearAllDraftsQuery>(std::move(promise))->send();
|
|
|
|
}
|
|
|
|
|
2018-09-29 01:45:43 +02:00
|
|
|
} // namespace td
|