2019-09-16 11:14:06 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
|
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
|
|
*/
|
|
|
|
#include "api/api_text_entities.h"
|
|
|
|
|
|
|
|
#include "main/main_session.h"
|
2022-06-29 14:56:59 +00:00
|
|
|
#include "data/stickers/data_custom_emoji.h"
|
|
|
|
#include "data/stickers/data_stickers_set.h"
|
2019-09-16 11:14:06 +00:00
|
|
|
#include "data/data_session.h"
|
2022-07-05 19:36:25 +00:00
|
|
|
#include "data/data_document.h"
|
2019-09-16 11:14:06 +00:00
|
|
|
#include "data/data_user.h"
|
|
|
|
|
|
|
|
namespace Api {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
using namespace TextUtilities;
|
|
|
|
|
2022-06-29 14:56:59 +00:00
|
|
|
[[nodiscard]] QString CustomEmojiEntityData(
|
|
|
|
not_null<Main::Session*> session,
|
|
|
|
const MTPDmessageEntityCustomEmoji &data) {
|
|
|
|
return Data::SerializeCustomEmojiId({
|
|
|
|
.selfId = session->userId().bare,
|
|
|
|
.id = data.vdocument_id().v,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] std::optional<MTPMessageEntity> CustomEmojiEntity(
|
|
|
|
not_null<Main::Session*> session,
|
|
|
|
MTPint offset,
|
|
|
|
MTPint length,
|
|
|
|
const QString &data) {
|
|
|
|
const auto parsed = Data::ParseCustomEmojiData(data);
|
|
|
|
if (!parsed.id || parsed.selfId != session->userId().bare) {
|
|
|
|
return {};
|
|
|
|
}
|
2022-07-05 19:36:25 +00:00
|
|
|
const auto document = session->data().document(parsed.id);
|
|
|
|
if (!document->sticker()) {
|
|
|
|
return {};
|
|
|
|
}
|
2022-06-29 14:56:59 +00:00
|
|
|
return MTP_messageEntityCustomEmoji(
|
|
|
|
offset,
|
|
|
|
length,
|
2022-07-05 19:36:25 +00:00
|
|
|
MTP_long(document->id));
|
2022-06-29 14:56:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] std::optional<MTPMessageEntity> MentionNameEntity(
|
|
|
|
not_null<Main::Session*> session,
|
|
|
|
MTPint offset,
|
|
|
|
MTPint length,
|
|
|
|
const QString &data) {
|
|
|
|
const auto parsed = MentionNameDataToFields(data);
|
|
|
|
if (!parsed.userId || parsed.selfId != session->userId().bare) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
return MTP_inputMessageEntityMentionName(
|
|
|
|
offset,
|
|
|
|
length,
|
|
|
|
(parsed.userId == parsed.selfId
|
|
|
|
? MTP_inputUserSelf()
|
|
|
|
: MTP_inputUser(
|
|
|
|
MTP_long(parsed.userId),
|
|
|
|
MTP_long(parsed.accessHash))));
|
|
|
|
}
|
|
|
|
|
2019-09-16 11:14:06 +00:00
|
|
|
} // namespace
|
|
|
|
|
2020-06-08 08:03:45 +00:00
|
|
|
EntitiesInText EntitiesFromMTP(
|
|
|
|
Main::Session *session,
|
|
|
|
const QVector<MTPMessageEntity> &entities) {
|
2019-09-16 11:14:06 +00:00
|
|
|
auto result = EntitiesInText();
|
|
|
|
if (!entities.isEmpty()) {
|
|
|
|
result.reserve(entities.size());
|
2020-06-08 08:03:45 +00:00
|
|
|
for (const auto &entity : entities) {
|
2019-09-16 11:14:06 +00:00
|
|
|
switch (entity.type()) {
|
|
|
|
case mtpc_messageEntityUrl: { auto &d = entity.c_messageEntityUrl(); result.push_back({ EntityType::Url, d.voffset().v, d.vlength().v }); } break;
|
2022-01-06 23:04:26 +00:00
|
|
|
case mtpc_messageEntityTextUrl: { auto &d = entity.c_messageEntityTextUrl(); result.push_back({ EntityType::CustomUrl, d.voffset().v, d.vlength().v, qs(d.vurl()) }); } break;
|
2019-09-16 11:14:06 +00:00
|
|
|
case mtpc_messageEntityEmail: { auto &d = entity.c_messageEntityEmail(); result.push_back({ EntityType::Email, d.voffset().v, d.vlength().v }); } break;
|
|
|
|
case mtpc_messageEntityHashtag: { auto &d = entity.c_messageEntityHashtag(); result.push_back({ EntityType::Hashtag, d.voffset().v, d.vlength().v }); } break;
|
|
|
|
case mtpc_messageEntityCashtag: { auto &d = entity.c_messageEntityCashtag(); result.push_back({ EntityType::Cashtag, d.voffset().v, d.vlength().v }); } break;
|
|
|
|
case mtpc_messageEntityPhone: break; // Skipping phones.
|
|
|
|
case mtpc_messageEntityMention: { auto &d = entity.c_messageEntityMention(); result.push_back({ EntityType::Mention, d.voffset().v, d.vlength().v }); } break;
|
2022-06-29 14:56:59 +00:00
|
|
|
case mtpc_messageEntityMentionName: if (session) {
|
2020-06-08 08:03:45 +00:00
|
|
|
const auto &d = entity.c_messageEntityMentionName();
|
2021-04-01 21:04:10 +00:00
|
|
|
const auto userId = UserId(d.vuser_id());
|
2022-06-29 14:56:59 +00:00
|
|
|
const auto user = session->data().userLoaded(userId);
|
|
|
|
const auto data = MentionNameDataFromFields({
|
|
|
|
.selfId = session->userId().bare,
|
|
|
|
.userId = userId.bare,
|
|
|
|
.accessHash = user ? user->accessHash() : 0,
|
|
|
|
});
|
2020-06-08 08:03:45 +00:00
|
|
|
result.push_back({ EntityType::MentionName, d.voffset().v, d.vlength().v, data });
|
2019-09-16 11:14:06 +00:00
|
|
|
} break;
|
2022-06-29 14:56:59 +00:00
|
|
|
case mtpc_inputMessageEntityMentionName: if (session) {
|
2020-06-08 08:03:45 +00:00
|
|
|
const auto &d = entity.c_inputMessageEntityMentionName();
|
2022-06-29 14:56:59 +00:00
|
|
|
const auto data = d.vuser_id().match([&](
|
|
|
|
const MTPDinputUserSelf &) {
|
|
|
|
return MentionNameDataFromFields({
|
|
|
|
.selfId = session->userId().bare,
|
|
|
|
.userId = session->userId().bare,
|
|
|
|
.accessHash = session->user()->accessHash(),
|
|
|
|
});
|
|
|
|
}, [&](const MTPDinputUser &data) {
|
|
|
|
return MentionNameDataFromFields({
|
|
|
|
.selfId = session->userId().bare,
|
|
|
|
.userId = UserId(data.vuser_id()).bare,
|
|
|
|
.accessHash = data.vaccess_hash().v,
|
|
|
|
});
|
|
|
|
}, [&](const auto &) {
|
2019-09-16 11:14:06 +00:00
|
|
|
return QString();
|
2022-06-29 14:56:59 +00:00
|
|
|
});
|
2019-09-16 11:14:06 +00:00
|
|
|
if (!data.isEmpty()) {
|
|
|
|
result.push_back({ EntityType::MentionName, d.voffset().v, d.vlength().v, data });
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case mtpc_messageEntityBotCommand: { auto &d = entity.c_messageEntityBotCommand(); result.push_back({ EntityType::BotCommand, d.voffset().v, d.vlength().v }); } break;
|
|
|
|
case mtpc_messageEntityBold: { auto &d = entity.c_messageEntityBold(); result.push_back({ EntityType::Bold, d.voffset().v, d.vlength().v }); } break;
|
|
|
|
case mtpc_messageEntityItalic: { auto &d = entity.c_messageEntityItalic(); result.push_back({ EntityType::Italic, d.voffset().v, d.vlength().v }); } break;
|
|
|
|
case mtpc_messageEntityUnderline: { auto &d = entity.c_messageEntityUnderline(); result.push_back({ EntityType::Underline, d.voffset().v, d.vlength().v }); } break;
|
|
|
|
case mtpc_messageEntityStrike: { auto &d = entity.c_messageEntityStrike(); result.push_back({ EntityType::StrikeOut, d.voffset().v, d.vlength().v }); } break;
|
|
|
|
case mtpc_messageEntityCode: { auto &d = entity.c_messageEntityCode(); result.push_back({ EntityType::Code, d.voffset().v, d.vlength().v }); } break;
|
2022-01-06 23:04:26 +00:00
|
|
|
case mtpc_messageEntityPre: { auto &d = entity.c_messageEntityPre(); result.push_back({ EntityType::Pre, d.voffset().v, d.vlength().v, qs(d.vlanguage()) }); } break;
|
2022-06-29 14:56:59 +00:00
|
|
|
case mtpc_messageEntityBankCard: break; // Skipping cards. // #TODO entities
|
2021-12-19 14:07:57 +00:00
|
|
|
case mtpc_messageEntitySpoiler: { auto &d = entity.c_messageEntitySpoiler(); result.push_back({ EntityType::Spoiler, d.voffset().v, d.vlength().v }); } break;
|
2022-06-29 14:56:59 +00:00
|
|
|
case mtpc_messageEntityCustomEmoji: if (session) {
|
|
|
|
const auto &d = entity.c_messageEntityCustomEmoji();
|
|
|
|
result.push_back({ EntityType::CustomEmoji, d.voffset().v, d.vlength().v, CustomEmojiEntityData(session, d) });
|
|
|
|
} break;
|
2019-09-16 11:14:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
MTPVector<MTPMessageEntity> EntitiesToMTP(
|
2020-06-08 08:03:45 +00:00
|
|
|
not_null<Main::Session*> session,
|
2019-09-16 11:14:06 +00:00
|
|
|
const EntitiesInText &entities,
|
|
|
|
ConvertOption option) {
|
|
|
|
auto v = QVector<MTPMessageEntity>();
|
|
|
|
v.reserve(entities.size());
|
2020-06-08 08:03:45 +00:00
|
|
|
for (const auto &entity : entities) {
|
2019-09-16 11:14:06 +00:00
|
|
|
if (entity.length() <= 0) continue;
|
|
|
|
if (option == ConvertOption::SkipLocal
|
|
|
|
&& entity.type() != EntityType::Bold
|
2020-05-08 08:12:43 +00:00
|
|
|
//&& entity.type() != EntityType::Semibold // Not in API.
|
2019-09-16 11:14:06 +00:00
|
|
|
&& entity.type() != EntityType::Italic
|
|
|
|
&& entity.type() != EntityType::Underline
|
|
|
|
&& entity.type() != EntityType::StrikeOut
|
|
|
|
&& entity.type() != EntityType::Code // #TODO entities
|
|
|
|
&& entity.type() != EntityType::Pre
|
2021-12-19 14:07:57 +00:00
|
|
|
&& entity.type() != EntityType::Spoiler
|
2019-09-16 11:14:06 +00:00
|
|
|
&& entity.type() != EntityType::MentionName
|
2022-06-29 14:56:59 +00:00
|
|
|
&& entity.type() != EntityType::CustomUrl
|
|
|
|
&& entity.type() != EntityType::CustomEmoji) {
|
2019-09-16 11:14:06 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto offset = MTP_int(entity.offset());
|
|
|
|
auto length = MTP_int(entity.length());
|
|
|
|
switch (entity.type()) {
|
|
|
|
case EntityType::Url: v.push_back(MTP_messageEntityUrl(offset, length)); break;
|
|
|
|
case EntityType::CustomUrl: v.push_back(MTP_messageEntityTextUrl(offset, length, MTP_string(entity.data()))); break;
|
|
|
|
case EntityType::Email: v.push_back(MTP_messageEntityEmail(offset, length)); break;
|
|
|
|
case EntityType::Hashtag: v.push_back(MTP_messageEntityHashtag(offset, length)); break;
|
|
|
|
case EntityType::Cashtag: v.push_back(MTP_messageEntityCashtag(offset, length)); break;
|
|
|
|
case EntityType::Mention: v.push_back(MTP_messageEntityMention(offset, length)); break;
|
|
|
|
case EntityType::MentionName: {
|
2022-06-29 14:56:59 +00:00
|
|
|
if (const auto valid = MentionNameEntity(session, offset, length, entity.data())) {
|
|
|
|
v.push_back(*valid);
|
2019-09-16 11:14:06 +00:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case EntityType::BotCommand: v.push_back(MTP_messageEntityBotCommand(offset, length)); break;
|
|
|
|
case EntityType::Bold: v.push_back(MTP_messageEntityBold(offset, length)); break;
|
|
|
|
case EntityType::Italic: v.push_back(MTP_messageEntityItalic(offset, length)); break;
|
|
|
|
case EntityType::Underline: v.push_back(MTP_messageEntityUnderline(offset, length)); break;
|
|
|
|
case EntityType::StrikeOut: v.push_back(MTP_messageEntityStrike(offset, length)); break;
|
|
|
|
case EntityType::Code: v.push_back(MTP_messageEntityCode(offset, length)); break; // #TODO entities
|
|
|
|
case EntityType::Pre: v.push_back(MTP_messageEntityPre(offset, length, MTP_string(entity.data()))); break;
|
2021-12-19 14:07:57 +00:00
|
|
|
case EntityType::Spoiler: v.push_back(MTP_messageEntitySpoiler(offset, length)); break;
|
2022-06-29 14:56:59 +00:00
|
|
|
case EntityType::CustomEmoji: {
|
|
|
|
if (const auto valid = CustomEmojiEntity(session, offset, length, entity.data())) {
|
|
|
|
v.push_back(*valid);
|
|
|
|
}
|
|
|
|
} break;
|
2019-09-16 11:14:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return MTP_vector<MTPMessageEntity>(std::move(v));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Api
|