2018-12-31 22:04:05 +03:00
|
|
|
//
|
2023-01-01 00:28:08 +03:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2018-12-31 22:04:05 +03: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)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
2022-10-03 01:26:32 +03:00
|
|
|
#include "td/telegram/CustomEmojiId.h"
|
2018-09-29 04:41:15 +03:00
|
|
|
#include "td/telegram/DialogId.h"
|
2021-09-19 00:47:05 +03:00
|
|
|
#include "td/telegram/secret_api.h"
|
|
|
|
#include "td/telegram/td_api.h"
|
|
|
|
#include "td/telegram/telegram_api.h"
|
2018-12-31 22:04:05 +03:00
|
|
|
#include "td/telegram/UserId.h"
|
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
2022-03-11 21:38:48 +03:00
|
|
|
#include "td/utils/FlatHashSet.h"
|
2023-01-18 20:45:46 +03:00
|
|
|
#include "td/utils/HashTableUtils.h"
|
2018-12-31 22:04:05 +03:00
|
|
|
#include "td/utils/Slice.h"
|
|
|
|
#include "td/utils/Status.h"
|
|
|
|
#include "td/utils/StringBuilder.h"
|
|
|
|
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
2018-01-30 20:06:54 +03:00
|
|
|
class ContactsManager;
|
2022-03-11 15:10:24 +03:00
|
|
|
class Dependencies;
|
2022-07-27 23:47:16 +03:00
|
|
|
class MultiPromiseActor;
|
2022-07-27 20:35:40 +03:00
|
|
|
class Td;
|
2018-01-30 20:06:54 +03:00
|
|
|
|
2018-12-31 22:04:05 +03:00
|
|
|
class MessageEntity {
|
|
|
|
public:
|
|
|
|
enum class Type : int32 {
|
|
|
|
Mention,
|
|
|
|
Hashtag,
|
|
|
|
BotCommand,
|
|
|
|
Url,
|
|
|
|
EmailAddress,
|
|
|
|
Bold,
|
|
|
|
Italic,
|
|
|
|
Code,
|
|
|
|
Pre,
|
|
|
|
PreCode,
|
|
|
|
TextUrl,
|
2018-03-08 16:28:54 +03:00
|
|
|
MentionName,
|
2018-03-13 00:17:29 +03:00
|
|
|
Cashtag,
|
2019-09-19 22:07:11 +03:00
|
|
|
PhoneNumber,
|
|
|
|
Underline,
|
|
|
|
Strikethrough,
|
2020-02-13 18:07:40 +03:00
|
|
|
BlockQuote,
|
2020-08-23 21:25:06 +03:00
|
|
|
BankCardNumber,
|
2021-07-26 07:53:36 +03:00
|
|
|
MediaTimestamp,
|
2021-12-28 20:41:37 +03:00
|
|
|
Spoiler,
|
2022-07-18 01:03:58 +03:00
|
|
|
CustomEmoji,
|
2020-08-23 21:25:06 +03:00
|
|
|
Size
|
2018-12-31 22:04:05 +03:00
|
|
|
};
|
2021-08-06 09:14:52 +03:00
|
|
|
Type type = Type::Size;
|
|
|
|
int32 offset = -1;
|
|
|
|
int32 length = -1;
|
|
|
|
int32 media_timestamp = -1;
|
2018-12-31 22:04:05 +03:00
|
|
|
string argument;
|
|
|
|
UserId user_id;
|
2022-10-03 01:26:32 +03:00
|
|
|
CustomEmojiId custom_emoji_id;
|
2018-12-31 22:04:05 +03:00
|
|
|
|
|
|
|
MessageEntity() = default;
|
|
|
|
|
|
|
|
MessageEntity(Type type, int32 offset, int32 length, string argument = "")
|
2021-08-13 13:10:54 +03:00
|
|
|
: type(type), offset(offset), length(length), argument(std::move(argument)) {
|
2018-12-31 22:04:05 +03:00
|
|
|
}
|
|
|
|
MessageEntity(int32 offset, int32 length, UserId user_id)
|
2021-08-13 13:10:54 +03:00
|
|
|
: type(Type::MentionName), offset(offset), length(length), user_id(user_id) {
|
2021-08-06 09:14:52 +03:00
|
|
|
}
|
|
|
|
MessageEntity(Type type, int32 offset, int32 length, int32 media_timestamp)
|
2021-08-13 13:10:54 +03:00
|
|
|
: type(type), offset(offset), length(length), media_timestamp(media_timestamp) {
|
2021-08-06 09:14:52 +03:00
|
|
|
CHECK(type == Type::MediaTimestamp);
|
2018-12-31 22:04:05 +03:00
|
|
|
}
|
2022-10-03 01:26:32 +03:00
|
|
|
MessageEntity(Type type, int32 offset, int32 length, CustomEmojiId custom_emoji_id)
|
|
|
|
: type(type), offset(offset), length(length), custom_emoji_id(custom_emoji_id) {
|
2022-07-18 01:03:58 +03:00
|
|
|
CHECK(type == Type::CustomEmoji);
|
|
|
|
}
|
2018-12-31 22:04:05 +03:00
|
|
|
|
|
|
|
tl_object_ptr<td_api::textEntity> get_text_entity_object() const;
|
|
|
|
|
|
|
|
bool operator==(const MessageEntity &other) const {
|
2021-08-09 19:37:47 +03:00
|
|
|
return offset == other.offset && length == other.length && type == other.type &&
|
2022-07-18 23:40:57 +03:00
|
|
|
media_timestamp == other.media_timestamp && argument == other.argument && user_id == other.user_id &&
|
2022-10-03 01:26:32 +03:00
|
|
|
custom_emoji_id == other.custom_emoji_id;
|
2018-12-31 22:04:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator<(const MessageEntity &other) const {
|
2019-09-23 22:57:02 +03:00
|
|
|
if (offset != other.offset) {
|
|
|
|
return offset < other.offset;
|
|
|
|
}
|
|
|
|
if (length != other.length) {
|
|
|
|
return length > other.length;
|
|
|
|
}
|
|
|
|
auto priority = get_type_priority(type);
|
|
|
|
auto other_priority = get_type_priority(other.type);
|
|
|
|
return priority < other_priority;
|
2018-12-31 22:04:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const MessageEntity &rhs) const {
|
|
|
|
return !(*this == rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class StorerT>
|
2018-04-02 01:45:51 +03:00
|
|
|
void store(StorerT &storer) const;
|
2018-12-31 22:04:05 +03:00
|
|
|
|
|
|
|
template <class ParserT>
|
2018-04-02 01:45:51 +03:00
|
|
|
void parse(ParserT &parser);
|
2019-09-23 22:57:02 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
tl_object_ptr<td_api::TextEntityType> get_text_entity_type_object() const;
|
|
|
|
|
|
|
|
static int get_type_priority(Type type);
|
2018-12-31 22:04:05 +03:00
|
|
|
};
|
|
|
|
|
2019-10-03 02:31:06 +03:00
|
|
|
StringBuilder &operator<<(StringBuilder &string_builder, const MessageEntity::Type &message_entity_type);
|
|
|
|
|
2018-12-31 22:04:05 +03:00
|
|
|
StringBuilder &operator<<(StringBuilder &string_builder, const MessageEntity &message_entity);
|
|
|
|
|
2018-01-30 20:06:54 +03:00
|
|
|
struct FormattedText {
|
|
|
|
string text;
|
|
|
|
vector<MessageEntity> entities;
|
|
|
|
|
|
|
|
template <class StorerT>
|
2018-04-02 01:45:51 +03:00
|
|
|
void store(StorerT &storer) const;
|
2018-01-30 20:06:54 +03:00
|
|
|
|
|
|
|
template <class ParserT>
|
2018-04-02 01:45:51 +03:00
|
|
|
void parse(ParserT &parser);
|
2018-01-30 20:06:54 +03:00
|
|
|
};
|
|
|
|
|
2023-01-18 20:45:46 +03:00
|
|
|
struct FormattedTextHash {
|
|
|
|
uint32 operator()(const FormattedText &formatted_text) const {
|
|
|
|
auto hash = Hash<string>()(formatted_text.text);
|
|
|
|
for (auto &entity : formatted_text.entities) {
|
2023-07-27 19:05:15 +03:00
|
|
|
hash = combine_hashes(hash, Hash<int32>()(static_cast<int32>(entity.type)));
|
|
|
|
hash = combine_hashes(hash, Hash<int32>()(entity.length));
|
|
|
|
hash = combine_hashes(hash, Hash<int32>()(entity.offset));
|
2023-01-18 20:45:46 +03:00
|
|
|
}
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-11 04:09:23 +03:00
|
|
|
StringBuilder &operator<<(StringBuilder &string_builder, const FormattedText &text);
|
|
|
|
|
2018-01-30 20:06:54 +03:00
|
|
|
inline bool operator==(const FormattedText &lhs, const FormattedText &rhs) {
|
|
|
|
return lhs.text == rhs.text && lhs.entities == rhs.entities;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator!=(const FormattedText &lhs, const FormattedText &rhs) {
|
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
|
2022-03-11 21:38:48 +03:00
|
|
|
const FlatHashSet<Slice, SliceHash> &get_valid_short_usernames();
|
2018-12-31 22:04:05 +03:00
|
|
|
|
2018-01-30 20:06:54 +03:00
|
|
|
Result<vector<MessageEntity>> get_message_entities(const ContactsManager *contacts_manager,
|
2020-03-10 03:51:56 +03:00
|
|
|
vector<tl_object_ptr<td_api::textEntity>> &&input_entities,
|
|
|
|
bool allow_all = false);
|
2018-01-30 20:06:54 +03:00
|
|
|
|
2021-07-22 05:39:16 +03:00
|
|
|
vector<tl_object_ptr<td_api::textEntity>> get_text_entities_object(const vector<MessageEntity> &entities,
|
2021-08-04 09:28:53 +03:00
|
|
|
bool skip_bot_commands, int32 max_media_timestamp);
|
2018-12-31 22:04:05 +03:00
|
|
|
|
2021-08-04 09:28:53 +03:00
|
|
|
td_api::object_ptr<td_api::formattedText> get_formatted_text_object(const FormattedText &text, bool skip_bot_commands,
|
|
|
|
int32 max_media_timestamp);
|
2018-01-30 20:06:54 +03:00
|
|
|
|
2022-07-27 23:36:44 +03:00
|
|
|
void remove_premium_custom_emoji_entities(const Td *td, vector<MessageEntity> &entities, bool remove_unknown);
|
|
|
|
|
2022-07-27 20:35:40 +03:00
|
|
|
void remove_unallowed_entities(const Td *td, FormattedText &text, DialogId dialog_id);
|
2022-07-22 16:47:58 +03:00
|
|
|
|
2022-07-16 16:10:06 +03:00
|
|
|
vector<MessageEntity> find_entities(Slice text, bool skip_bot_commands, bool skip_media_timestamps);
|
2018-12-31 22:04:05 +03:00
|
|
|
|
|
|
|
vector<Slice> find_mentions(Slice str);
|
|
|
|
vector<Slice> find_bot_commands(Slice str);
|
|
|
|
vector<Slice> find_hashtags(Slice str);
|
2018-03-07 20:29:33 +03:00
|
|
|
vector<Slice> find_cashtags(Slice str);
|
2020-02-13 18:07:40 +03:00
|
|
|
vector<Slice> find_bank_card_numbers(Slice str);
|
2021-06-03 18:27:40 +03:00
|
|
|
vector<Slice> find_tg_urls(Slice str);
|
2018-12-31 22:04:05 +03:00
|
|
|
bool is_email_address(Slice str);
|
2021-07-28 08:30:22 +03:00
|
|
|
vector<std::pair<Slice, bool>> find_urls(Slice str); // slice + is_email_address
|
|
|
|
vector<std::pair<Slice, int32>> find_media_timestamps(Slice str); // slice + media_timestamp
|
2018-12-31 22:04:05 +03:00
|
|
|
|
2022-07-31 00:34:30 +03:00
|
|
|
void remove_empty_entities(vector<MessageEntity> &entities);
|
|
|
|
|
2022-08-15 15:55:48 +03:00
|
|
|
string get_first_url(const FormattedText &text);
|
2018-12-31 22:04:05 +03:00
|
|
|
|
|
|
|
Result<vector<MessageEntity>> parse_markdown(string &text);
|
|
|
|
|
2019-10-03 02:31:06 +03:00
|
|
|
Result<vector<MessageEntity>> parse_markdown_v2(string &text);
|
|
|
|
|
2020-03-09 23:55:32 +03:00
|
|
|
FormattedText parse_markdown_v3(FormattedText text);
|
|
|
|
|
2020-03-12 06:22:14 +03:00
|
|
|
FormattedText get_markdown_v3(FormattedText text);
|
|
|
|
|
2023-01-09 12:59:14 +03:00
|
|
|
Result<vector<MessageEntity>> parse_html(string &str);
|
2018-12-31 22:04:05 +03:00
|
|
|
|
|
|
|
vector<tl_object_ptr<telegram_api::MessageEntity>> get_input_message_entities(const ContactsManager *contacts_manager,
|
2018-04-02 01:10:22 +03:00
|
|
|
const vector<MessageEntity> &entities,
|
|
|
|
const char *source);
|
2018-12-31 22:04:05 +03:00
|
|
|
|
2018-09-28 05:09:28 +03:00
|
|
|
vector<tl_object_ptr<telegram_api::MessageEntity>> get_input_message_entities(const ContactsManager *contacts_manager,
|
|
|
|
const FormattedText *text,
|
|
|
|
const char *source);
|
|
|
|
|
2018-12-31 22:04:05 +03:00
|
|
|
vector<tl_object_ptr<secret_api::MessageEntity>> get_input_secret_message_entities(
|
2019-09-19 22:07:11 +03:00
|
|
|
const vector<MessageEntity> &entities, int32 layer);
|
2018-12-31 22:04:05 +03:00
|
|
|
|
2018-01-26 22:53:20 +03:00
|
|
|
vector<MessageEntity> get_message_entities(const ContactsManager *contacts_manager,
|
2018-02-11 19:43:58 +03:00
|
|
|
vector<tl_object_ptr<telegram_api::MessageEntity>> &&server_entities,
|
|
|
|
const char *source);
|
2018-12-31 22:04:05 +03:00
|
|
|
|
2022-07-27 23:47:16 +03:00
|
|
|
vector<MessageEntity> get_message_entities(Td *td, vector<tl_object_ptr<secret_api::MessageEntity>> &&secret_entities,
|
|
|
|
bool is_premium, MultiPromiseActor &load_data_multipromise);
|
2018-12-31 22:04:05 +03:00
|
|
|
|
2023-01-18 15:38:36 +03:00
|
|
|
telegram_api::object_ptr<telegram_api::textWithEntities> get_input_text_with_entities(
|
|
|
|
const ContactsManager *contacts_manager, const FormattedText &text, const char *source);
|
|
|
|
|
|
|
|
FormattedText get_formatted_text(const ContactsManager *contacts_manager,
|
|
|
|
telegram_api::object_ptr<telegram_api::textWithEntities> text_with_entities,
|
2023-01-18 20:45:46 +03:00
|
|
|
bool allow_empty, bool skip_new_entities, bool skip_bot_commands,
|
2023-02-11 22:37:43 +03:00
|
|
|
bool skip_media_timestamps, bool skip_trim, const char *source);
|
2023-01-18 15:38:36 +03:00
|
|
|
|
2018-02-20 05:41:17 +03:00
|
|
|
// like clean_input_string but also validates entities
|
|
|
|
Status fix_formatted_text(string &text, vector<MessageEntity> &entities, bool allow_empty, bool skip_new_entities,
|
2023-02-11 22:37:43 +03:00
|
|
|
bool skip_bot_commands, bool skip_media_timestamps, bool skip_trim) TD_WARN_UNUSED_RESULT;
|
2018-02-20 05:41:17 +03:00
|
|
|
|
2018-09-28 23:57:34 +03:00
|
|
|
FormattedText get_message_text(const ContactsManager *contacts_manager, string message_text,
|
2019-03-26 01:24:23 +03:00
|
|
|
vector<tl_object_ptr<telegram_api::MessageEntity>> &&server_entities,
|
2021-07-26 22:48:33 +03:00
|
|
|
bool skip_new_entities, bool skip_media_timestamps, int32 send_date, bool from_album,
|
|
|
|
const char *source);
|
2018-09-28 23:57:34 +03:00
|
|
|
|
2018-12-12 17:05:32 +03:00
|
|
|
td_api::object_ptr<td_api::formattedText> extract_input_caption(
|
|
|
|
tl_object_ptr<td_api::InputMessageContent> &input_message_content);
|
|
|
|
|
2022-08-15 15:37:17 +03:00
|
|
|
Result<FormattedText> get_formatted_text(const Td *td, DialogId dialog_id,
|
2022-08-15 16:16:50 +03:00
|
|
|
td_api::object_ptr<td_api::formattedText> &&text, bool is_bot,
|
2023-02-11 22:37:43 +03:00
|
|
|
bool allow_empty, bool skip_media_timestamps, bool skip_trim);
|
2022-08-15 15:37:17 +03:00
|
|
|
|
2018-09-28 05:09:28 +03:00
|
|
|
void add_formatted_text_dependencies(Dependencies &dependencies, const FormattedText *text);
|
2018-09-28 04:21:20 +03:00
|
|
|
|
2021-08-06 09:14:52 +03:00
|
|
|
bool has_media_timestamps(const FormattedText *text, int32 min_media_timestamp, int32 max_media_timestamp);
|
2021-08-05 05:41:24 +03:00
|
|
|
|
2021-07-22 06:54:43 +03:00
|
|
|
bool has_bot_commands(const FormattedText *text);
|
|
|
|
|
2021-07-22 04:38:15 +03:00
|
|
|
bool need_always_skip_bot_commands(const ContactsManager *contacts_manager, DialogId dialog_id, bool is_bot);
|
2018-09-28 23:57:34 +03:00
|
|
|
|
2018-12-31 22:04:05 +03:00
|
|
|
} // namespace td
|