2018-09-28 22:57:34 +02:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
2018-09-28 22:57:34 +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/InputMessageText.h"
|
|
|
|
|
2024-04-02 12:06:22 +02:00
|
|
|
#include "td/telegram/ChatManager.h"
|
2023-10-30 17:04:14 +01:00
|
|
|
#include "td/telegram/Dependencies.h"
|
2018-09-28 22:57:34 +02:00
|
|
|
#include "td/telegram/MessageEntity.h"
|
2023-10-16 19:12:33 +02:00
|
|
|
#include "td/telegram/misc.h"
|
2023-10-16 23:08:50 +02:00
|
|
|
#include "td/telegram/Td.h"
|
2018-09-28 22:57:34 +02:00
|
|
|
|
2019-02-12 21:48:16 +01:00
|
|
|
#include "td/utils/common.h"
|
2018-09-29 01:45:43 +02:00
|
|
|
|
2018-09-28 22:57:34 +02:00
|
|
|
namespace td {
|
|
|
|
|
|
|
|
bool operator==(const InputMessageText &lhs, const InputMessageText &rhs) {
|
|
|
|
return lhs.text == rhs.text && lhs.disable_web_page_preview == rhs.disable_web_page_preview &&
|
|
|
|
lhs.clear_draft == rhs.clear_draft;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const InputMessageText &lhs, const InputMessageText &rhs) {
|
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
|
2022-07-22 17:11:55 +02:00
|
|
|
Result<InputMessageText> process_input_message_text(const Td *td, DialogId dialog_id,
|
2018-09-28 22:57:34 +02:00
|
|
|
tl_object_ptr<td_api::InputMessageContent> &&input_message_content,
|
|
|
|
bool is_bot, bool for_draft) {
|
|
|
|
CHECK(input_message_content != nullptr);
|
|
|
|
CHECK(input_message_content->get_id() == td_api::inputMessageText::ID);
|
|
|
|
auto input_message_text = static_cast<td_api::inputMessageText *>(input_message_content.get());
|
2023-10-16 19:12:33 +02:00
|
|
|
string web_page_url;
|
|
|
|
bool disable_web_page_preview = false;
|
|
|
|
bool force_small_media = false;
|
|
|
|
bool force_large_media = false;
|
2023-10-21 23:50:19 +02:00
|
|
|
bool show_above_text = false;
|
2023-10-16 19:12:33 +02:00
|
|
|
if (input_message_text->link_preview_options_ != nullptr) {
|
|
|
|
auto options = std::move(input_message_text->link_preview_options_);
|
|
|
|
web_page_url = std::move(options->url_);
|
|
|
|
disable_web_page_preview = options->is_disabled_;
|
|
|
|
force_small_media = options->force_small_media_;
|
|
|
|
force_large_media = options->force_large_media_;
|
2023-10-21 23:50:19 +02:00
|
|
|
show_above_text = options->show_above_text_;
|
2023-10-16 19:12:33 +02:00
|
|
|
|
|
|
|
if (!clean_input_string(web_page_url)) {
|
|
|
|
return Status::Error(400, "Link preview URL must be encoded in UTF-8");
|
|
|
|
}
|
2023-10-16 23:08:50 +02:00
|
|
|
|
|
|
|
if (disable_web_page_preview ||
|
|
|
|
(dialog_id.get_type() == DialogType::Channel &&
|
2024-04-02 12:06:22 +02:00
|
|
|
!td->chat_manager_->get_channel_permissions(dialog_id.get_channel_id()).can_add_web_page_previews())) {
|
2023-10-16 23:08:50 +02:00
|
|
|
web_page_url.clear();
|
|
|
|
}
|
2023-10-16 22:06:48 +02:00
|
|
|
if (web_page_url.empty()) {
|
|
|
|
force_small_media = false;
|
|
|
|
force_large_media = false;
|
|
|
|
}
|
2023-10-16 19:12:33 +02:00
|
|
|
}
|
2023-10-17 01:07:40 +02:00
|
|
|
TRY_RESULT(text, get_formatted_text(td, dialog_id, std::move(input_message_text->text_), is_bot,
|
|
|
|
for_draft || !web_page_url.empty(), for_draft, for_draft));
|
2023-10-16 23:37:44 +02:00
|
|
|
if (!disable_web_page_preview && web_page_url.empty() && dialog_id.get_type() == DialogType::SecretChat) {
|
2023-11-01 20:13:29 +01:00
|
|
|
web_page_url = get_first_url(text).str();
|
2023-10-16 23:37:44 +02:00
|
|
|
}
|
2023-10-21 23:50:19 +02:00
|
|
|
return InputMessageText{
|
|
|
|
std::move(text), std::move(web_page_url), disable_web_page_preview, force_small_media,
|
|
|
|
force_large_media, show_above_text, input_message_text->clear_draft_};
|
2023-10-16 19:12:33 +02:00
|
|
|
}
|
|
|
|
|
2023-10-17 11:31:01 +02:00
|
|
|
void InputMessageText::add_dependencies(Dependencies &dependencies) const {
|
|
|
|
add_formatted_text_dependencies(dependencies, &text);
|
|
|
|
}
|
|
|
|
|
2023-10-16 19:12:33 +02:00
|
|
|
telegram_api::object_ptr<telegram_api::InputMedia> InputMessageText::get_input_media_web_page() const {
|
2023-10-16 22:06:48 +02:00
|
|
|
if (web_page_url.empty()) {
|
2023-10-16 19:12:33 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
int32 flags = 0;
|
|
|
|
if (force_small_media) {
|
|
|
|
flags |= telegram_api::inputMediaWebPage::FORCE_SMALL_MEDIA_MASK;
|
|
|
|
}
|
|
|
|
if (force_large_media) {
|
|
|
|
flags |= telegram_api::inputMediaWebPage::FORCE_LARGE_MEDIA_MASK;
|
|
|
|
}
|
|
|
|
if (!text.text.empty()) {
|
|
|
|
flags |= telegram_api::inputMediaWebPage::OPTIONAL_MASK;
|
|
|
|
}
|
|
|
|
return telegram_api::make_object<telegram_api::inputMediaWebPage>(flags, false /*ignored*/, false /*ignored*/,
|
|
|
|
false /*ignored*/, web_page_url);
|
2018-09-28 22:57:34 +02:00
|
|
|
}
|
|
|
|
|
2021-08-04 08:28:53 +02:00
|
|
|
// used only for draft
|
2024-07-21 11:33:02 +02:00
|
|
|
td_api::object_ptr<td_api::inputMessageText> InputMessageText::get_input_message_text_object(
|
|
|
|
const UserManager *user_manager) const {
|
2023-10-16 19:12:33 +02:00
|
|
|
td_api::object_ptr<td_api::linkPreviewOptions> options;
|
2023-10-21 23:50:19 +02:00
|
|
|
if (!web_page_url.empty() || disable_web_page_preview || force_small_media || force_large_media || show_above_text) {
|
2023-10-16 22:06:48 +02:00
|
|
|
options = td_api::make_object<td_api::linkPreviewOptions>(disable_web_page_preview, web_page_url, force_small_media,
|
2023-10-21 23:50:19 +02:00
|
|
|
force_large_media, show_above_text);
|
2023-10-16 19:12:33 +02:00
|
|
|
}
|
2024-07-21 11:33:02 +02:00
|
|
|
return td_api::make_object<td_api::inputMessageText>(get_formatted_text_object(user_manager, text, false, -1),
|
|
|
|
std::move(options), clear_draft);
|
2018-09-28 22:57:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|