[Improvement] Convert tg://user?id=<userid> to mentions if possible

This commit is contained in:
Eric Kotato 2022-09-11 04:43:46 +03:00 committed by Eric Kotato
parent f08467a368
commit 72dc6a4cf3

View file

@ -13,6 +13,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "data/stickers/data_custom_emoji.h" #include "data/stickers/data_custom_emoji.h"
#include "data/stickers/data_stickers_set.h" #include "data/stickers/data_stickers_set.h"
#include "main/main_session.h" #include "main/main_session.h"
#include "base/qthelp_regex.h"
#include "base/qthelp_url.h"
namespace Api { namespace Api {
namespace { namespace {
@ -256,11 +258,41 @@ MTPVector<MTPMessageEntity> EntitiesToMTP(
v.push_back(MTP_messageEntityUrl(offset, length)); v.push_back(MTP_messageEntityUrl(offset, length));
} break; } break;
case EntityType::CustomUrl: { case EntityType::CustomUrl: {
v.push_back( auto url = entity.data();
MTP_messageEntityTextUrl( auto inputUser = [&](const QString &data) -> MTPInputUser {
offset, const auto trimmed = url.trimmed();
length, if (trimmed.isEmpty()) {
MTP_string(entity.data()))); return MTP_inputUserEmpty();
}
auto regex = QRegularExpression(
QString::fromUtf8("^(?i)tg://user\\?(.+)"),
QRegularExpression::UseUnicodePropertiesOption);
regex.optimize();
const auto match = regex.match(trimmed);
if (!match.hasMatch() || match.capturedStart() != 0) {
return MTP_inputUserEmpty();
}
const auto parsed = qthelp::url_parse_params(match.captured(1), qthelp::UrlParamNameTransform::ToLower);
const auto qstr_uid = parsed.value("id");
if (qstr_uid.isEmpty()) {
return MTP_inputUserEmpty();
}
bool success;
UserId uid = qstr_uid.toLongLong(&success);
if (success && session) {
if (uid == session->userId()) {
return MTP_inputUserSelf();
} else if (const auto user = session->data().userLoaded(uid)) {
return MTP_inputUser(MTP_long(uid.bare), MTP_long(user->accessHash()));
}
}
return MTP_inputUserEmpty();
}(url);
if (inputUser.type() != mtpc_inputUserEmpty) {
v.push_back(MTP_inputMessageEntityMentionName(offset, length, inputUser));
} else {
v.push_back(MTP_messageEntityTextUrl(offset, length, MTP_string(url)));
}
} break; } break;
case EntityType::Email: { case EntityType::Email: {
v.push_back(MTP_messageEntityEmail(offset, length)); v.push_back(MTP_messageEntityEmail(offset, length));