Allow internal links in bio of non-premium users.

This commit is contained in:
levlam 2022-07-16 16:10:06 +03:00
parent bedec2c9ba
commit 02396be7eb
5 changed files with 30 additions and 13 deletions

View File

@ -16692,7 +16692,19 @@ tl_object_ptr<td_api::userFullInfo> ContactsManager::get_user_full_info_object(U
} else {
FormattedText bio;
bio.text = user_full->about;
bio.entities = find_entities(bio.text, true, true, !is_user_premium(user_id));
bio.entities = find_entities(bio.text, true, true);
if (!is_user_premium(user_id)) {
td::remove_if(bio.entities, [&](const MessageEntity &entity) {
if (entity.type == MessageEntity::Type::EmailAddress) {
return true;
}
if (entity.type == MessageEntity::Type::Url &&
!LinkManager::is_internal_link(utf8_utf16_substr(Slice(bio.text), entity.offset, entity.length))) {
return true;
}
return false;
});
}
bio_object = get_formatted_text_object(bio, true, 0);
}
return make_tl_object<td_api::userFullInfo>(

View File

@ -924,6 +924,11 @@ LinkManager::LinkInfo LinkManager::get_link_info(Slice link) {
return result;
}
bool LinkManager::is_internal_link(Slice link) {
auto info = get_link_info(link);
return info.is_internal_;
}
unique_ptr<LinkManager::InternalLink> LinkManager::parse_internal_link(Slice link, bool is_trusted) {
auto info = get_link_info(link);
if (!info.is_internal_) {

View File

@ -52,6 +52,9 @@ class LinkManager final : public Actor {
// same as check_link, but returns an empty string instead of an error
static string get_checked_link(Slice link, bool http_only = false, bool https_only = false);
// returns whether a link is an internal link, supported or not
static bool is_internal_link(Slice link);
// checks whether the link is a supported tg or t.me link and parses it
static unique_ptr<InternalLink> parse_internal_link(Slice link, bool is_trusted = false);

View File

@ -1636,7 +1636,7 @@ static void fix_entity_offsets(Slice text, vector<MessageEntity> &entities) {
}
}
vector<MessageEntity> find_entities(Slice text, bool skip_bot_commands, bool skip_media_timestamps, bool skip_urls) {
vector<MessageEntity> find_entities(Slice text, bool skip_bot_commands, bool skip_media_timestamps) {
vector<MessageEntity> entities;
auto add_entities = [&entities, &text](MessageEntity::Type type, vector<Slice> (*find_entities_f)(Slice)) mutable {
@ -1655,15 +1655,13 @@ vector<MessageEntity> find_entities(Slice text, bool skip_bot_commands, bool ski
add_entities(MessageEntity::Type::Cashtag, find_cashtags);
// TODO find_phone_numbers
add_entities(MessageEntity::Type::BankCardNumber, find_bank_card_numbers);
if (!skip_urls) {
add_entities(MessageEntity::Type::Url, find_tg_urls);
auto urls = find_urls(text);
for (auto &url : urls) {
auto type = url.second ? MessageEntity::Type::EmailAddress : MessageEntity::Type::Url;
auto offset = narrow_cast<int32>(url.first.begin() - text.begin());
auto length = narrow_cast<int32>(url.first.size());
entities.emplace_back(type, offset, length);
}
add_entities(MessageEntity::Type::Url, find_tg_urls);
auto urls = find_urls(text);
for (auto &url : urls) {
auto type = url.second ? MessageEntity::Type::EmailAddress : MessageEntity::Type::Url;
auto offset = narrow_cast<int32>(url.first.begin() - text.begin());
auto length = narrow_cast<int32>(url.first.size());
entities.emplace_back(type, offset, length);
}
if (!skip_media_timestamps) {
auto media_timestamps = find_media_timestamps(text);

View File

@ -143,8 +143,7 @@ vector<tl_object_ptr<td_api::textEntity>> get_text_entities_object(const vector<
td_api::object_ptr<td_api::formattedText> get_formatted_text_object(const FormattedText &text, bool skip_bot_commands,
int32 max_media_timestamp);
vector<MessageEntity> find_entities(Slice text, bool skip_bot_commands, bool skip_media_timestamps,
bool skip_urls = false);
vector<MessageEntity> find_entities(Slice text, bool skip_bot_commands, bool skip_media_timestamps);
vector<Slice> find_mentions(Slice str);
vector<Slice> find_bot_commands(Slice str);