// // Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2018 // // 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 #include "td/telegram/UserId.h" #include "td/utils/common.h" #include "td/utils/Slice.h" #include "td/utils/Status.h" #include "td/utils/StringBuilder.h" #include "td/utils/tl_helpers.h" #include "td/telegram/secret_api.h" #include "td/telegram/td_api.h" #include "td/telegram/telegram_api.h" #include #include #include namespace td { class ContactsManager; class MessageEntity { tl_object_ptr get_text_entity_type_object() const; public: enum class Type : int32 { Mention, Hashtag, BotCommand, Url, EmailAddress, Bold, Italic, Code, Pre, PreCode, TextUrl, MentionName, Cashtag }; Type type; int32 offset; int32 length; string argument; UserId user_id; MessageEntity() = default; MessageEntity(Type type, int32 offset, int32 length, string argument = "") : type(type), offset(offset), length(length), argument(std::move(argument)), user_id() { } MessageEntity(int32 offset, int32 length, UserId user_id) : type(Type::MentionName), offset(offset), length(length), argument(), user_id(user_id) { } tl_object_ptr get_text_entity_object() const; bool operator==(const MessageEntity &other) const { return offset == other.offset && length == other.length && type == other.type && argument == other.argument && user_id == other.user_id; } bool operator<(const MessageEntity &other) const { return std::tie(offset, length, type) < std::tie(other.offset, other.length, other.type); } bool operator!=(const MessageEntity &rhs) const { return !(*this == rhs); } // TODO move to hpp template void store(StorerT &storer) const { using td::store; store(type, storer); store(offset, storer); store(length, storer); if (type == Type::PreCode || type == Type::TextUrl) { store(argument, storer); } if (type == Type::MentionName) { store(user_id, storer); } } template void parse(ParserT &parser) { using td::parse; parse(type, parser); parse(offset, parser); parse(length, parser); if (type == Type::PreCode || type == Type::TextUrl) { parse(argument, parser); } if (type == Type::MentionName) { parse(user_id, parser); } } }; StringBuilder &operator<<(StringBuilder &string_builder, const MessageEntity &message_entity); struct FormattedText { string text; vector entities; template void store(StorerT &storer) const { td::store(text, storer); td::store(entities, storer); } template void parse(ParserT &parser) { td::parse(text, parser); td::parse(entities, parser); } }; 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); } const std::unordered_set &get_valid_short_usernames(); Result> get_message_entities(const ContactsManager *contacts_manager, const vector> &input_entities); vector> get_text_entities_object(const vector &entities); td_api::object_ptr get_formatted_text_object(const FormattedText &text); vector find_entities(Slice text, bool skip_bot_commands, bool only_urls = false); vector find_mentions(Slice str); vector find_bot_commands(Slice str); vector find_hashtags(Slice str); vector find_cashtags(Slice str); bool is_email_address(Slice str); vector> find_urls(Slice str); // slice + is_email_address string get_first_url(Slice text, const vector &entities); Result> parse_markdown(string &text); Result> parse_html(string &text); vector> get_input_message_entities(const ContactsManager *contacts_manager, const vector &entities); vector> get_input_secret_message_entities( const vector &entities); vector get_message_entities(const ContactsManager *contacts_manager, vector> &&server_entities, const char *source); vector get_message_entities(vector> &&secret_entities); // like clean_input_string but also validates entities Status fix_formatted_text(string &text, vector &entities, bool allow_empty, bool skip_new_entities, bool skip_bot_commands, bool for_draft) TD_WARN_UNUSED_RESULT; } // namespace td