Better get_first_url.

GitOrigin-RevId: d153fcd3291758f29a99421f42c1193a51cc248b
This commit is contained in:
levlam 2018-07-07 18:01:20 +03:00
parent 05aab44742
commit 1ab40c7278

View File

@ -1165,6 +1165,10 @@ static vector<MessageEntity> merge_entities(vector<MessageEntity> old_entities,
return result; return result;
} }
static bool is_plain_domain(Slice url) {
return url.find('/') >= url.size() && url.find('?') >= url.size() && url.find('#') >= url.size();
}
string get_first_url(Slice text, const vector<MessageEntity> &entities) { string get_first_url(Slice text, const vector<MessageEntity> &entities) {
for (auto &entity : entities) { for (auto &entity : entities) {
switch (entity.type) { switch (entity.type) {
@ -1174,8 +1178,13 @@ string get_first_url(Slice text, const vector<MessageEntity> &entities) {
break; break;
case MessageEntity::Type::BotCommand: case MessageEntity::Type::BotCommand:
break; break;
case MessageEntity::Type::Url: case MessageEntity::Type::Url: {
return utf8_utf16_substr(text, entity.offset, entity.length).str(); Slice url = utf8_utf16_substr(text, entity.offset, entity.length);
if (begins_with(url, "tg:") || is_plain_domain(url)) {
continue;
}
return url.str();
}
case MessageEntity::Type::EmailAddress: case MessageEntity::Type::EmailAddress:
break; break;
case MessageEntity::Type::Bold: case MessageEntity::Type::Bold:
@ -1189,6 +1198,9 @@ string get_first_url(Slice text, const vector<MessageEntity> &entities) {
case MessageEntity::Type::PreCode: case MessageEntity::Type::PreCode:
break; break;
case MessageEntity::Type::TextUrl: case MessageEntity::Type::TextUrl:
if (begins_with(entity.argument, "tg:")) {
continue;
}
return entity.argument; return entity.argument;
case MessageEntity::Type::MentionName: case MessageEntity::Type::MentionName:
break; break;