Make internalLinkTypeMessage self-contained.

This commit is contained in:
levlam 2021-08-20 20:27:39 +03:00
parent 618742acc2
commit df11403739
3 changed files with 75 additions and 40 deletions

View File

@ -3070,14 +3070,14 @@ internalLinkTypeGame bot_username:string game_short_name:string = InternalLinkTy
//@description The link is a link to a language pack. Call getLanguagePackInfo with the given language pack identifier to process the link @language_pack_id Language pack identifier
internalLinkTypeLanguagePack language_pack_id:string = InternalLinkType;
//@description The link is a link to a Telegram message. Call getMessageLinkInfo to process the link
internalLinkTypeMessage = InternalLinkType;
//@description The link is a link to a Telegram message. Call getMessageLinkInfo with the given URL to process the link @url URL to be passed to getMessageLinkInfo
internalLinkTypeMessage url:string = InternalLinkType;
//@description The link contains a message draft text. A share screen needs to be shown to the user, then the chosen chat should be open and the text should be added to the input field
//@text Message draft text @contains_link True, if the first line of the text contains a link. If true, the input field needs to be focused and the text after the link should be selected
internalLinkTypeMessageDraft text:formattedText contains_link:Bool = InternalLinkType;
//@description The link contains a request of Telegram passport data. Call getPassportAuthorizationForm to process the link if the link was received outside of the app, otherwise ignore it
//@description The link contains a request of Telegram passport data. Call getPassportAuthorizationForm with the given parameters to process the link if the link was received from outside of the app, otherwise ignore it
//@bot_user_id User identifier of the service's bot @scope Telegram Passport element types requested by the service @public_key Service's public key @nonce Unique request identifier provided by the service
//@callback_url An HTTP URL to open once the request is finished or canceled with the parameter tg_passport=success or tg_passport=cancel respectively. If empty, then the link tgbot{bot_user_id}://passport/success or tgbot{bot_user_id}://passport/cancel needs to be opened instead
internalLinkTypePassportDataRequest bot_user_id:int32 scope:string public_key:string nonce:string callback_url:string = InternalLinkType;
@ -3086,7 +3086,7 @@ internalLinkTypePassportDataRequest bot_user_id:int32 scope:string public_key:st
//@hash Hash value from the link @phone_number Phone number value from the link
internalLinkTypePhoneNumberConfirmation hash:string phone_number:string = InternalLinkType;
//@description The link is a link to a proxy. Call addProxy to process the link and add the proxy
//@description The link is a link to a proxy. Call addProxy with the given parameters to process the link and add the proxy
//@server Proxy server IP address @port Proxy server port @type Type of the proxy
internalLinkTypeProxy server:string port:int32 type:ProxyType = InternalLinkType;

View File

@ -208,8 +208,14 @@ class LinkManager::InternalLinkLanguage final : public InternalLink {
};
class LinkManager::InternalLinkMessage final : public InternalLink {
string url_;
td_api::object_ptr<td_api::InternalLinkType> get_internal_link_type_object() const final {
return td_api::make_object<td_api::internalLinkTypeMessage>();
return td_api::make_object<td_api::internalLinkTypeMessage>(url_);
}
public:
explicit InternalLinkMessage(string url) : url_(std::move(url)) {
}
};
@ -713,6 +719,13 @@ struct CopyArg {
StringBuilder &operator<<(StringBuilder &string_builder, const CopyArg &copy_arg) {
auto arg = copy_arg.url_query_->get_arg(copy_arg.name_);
if (arg.empty()) {
for (const auto &query_arg : copy_arg.url_query_->args_) {
if (query_arg.first == copy_arg.name_) {
char c = *copy_arg.is_first_ ? '?' : '&';
*copy_arg.is_first_ = false;
return string_builder << c << copy_arg.name_;
}
}
return string_builder;
}
char c = *copy_arg.is_first_ ? '?' : '&';
@ -742,8 +755,10 @@ unique_ptr<LinkManager::InternalLink> LinkManager::parse_tg_link_query(Slice que
if (path.size() == 1 && path[0] == "resolve") {
if (is_valid_username(get_arg("domain"))) {
if (has_arg("post")) {
// resolve?domain=<username>&post=12345&single
return td::make_unique<InternalLinkMessage>();
// resolve?domain=<username>&post=12345&single&thread=<thread_id>&comment=<message_id>&t=<media_timestamp>
return td::make_unique<InternalLinkMessage>(PSTRING() << "tg:resolve" << copy_arg("domain") << copy_arg("post")
<< copy_arg("single") << copy_arg("thread")
<< copy_arg("comment") << copy_arg("t"));
}
auto username = get_arg("domain");
for (auto &arg : url_query.args_) {
@ -851,9 +866,11 @@ unique_ptr<LinkManager::InternalLink> LinkManager::parse_tg_link_query(Slice que
}
}
} else if (path.size() == 1 && path[0] == "privatepost") {
// privatepost?channel=123456789&msg_id=12345
// privatepost?channel=123456789&msg_id=12345&single&thread=<thread_id>&comment=<message_id>&t=<media_timestamp>
if (has_arg("channel") && has_arg("msg_id")) {
return td::make_unique<InternalLinkMessage>();
return td::make_unique<InternalLinkMessage>(
PSTRING() << "tg:privatepost" << copy_arg("channel") << copy_arg("msg_id") << copy_arg("single")
<< copy_arg("thread") << copy_arg("comment") << copy_arg("t"));
}
} else if (path.size() == 1 && path[0] == "bg") {
// bg?color=<color>
@ -905,8 +922,12 @@ unique_ptr<LinkManager::InternalLink> LinkManager::parse_t_me_link_query(Slice q
if (path[0] == "c") {
if (path.size() >= 3 && to_integer<int64>(path[1]) > 0 && to_integer<int64>(path[2]) > 0) {
// /c/123456789/12345
return td::make_unique<InternalLinkMessage>();
// /c/123456789/12345?single&thread=<thread_id>&comment=<message_id>&t=<media_timestamp>
is_first_arg = false;
return td::make_unique<InternalLinkMessage>(PSTRING()
<< "tg:privatepost?channel=" << to_integer<int64>(path[1])
<< "&msg_id=" << to_integer<int64>(path[2]) << copy_arg("single")
<< copy_arg("thread") << copy_arg("comment") << copy_arg("t"));
}
} else if (path[0] == "login") {
if (path.size() >= 2 && !path[1].empty()) {
@ -982,8 +1003,11 @@ unique_ptr<LinkManager::InternalLink> LinkManager::parse_t_me_link_query(Slice q
}
} else if (is_valid_username(path[0])) {
if (path.size() >= 2 && to_integer<int64>(path[1]) > 0) {
// /<username>/12345?single&thread=<thread_id>&comment=<message_id>
return td::make_unique<InternalLinkMessage>();
// /<username>/12345?single&thread=<thread_id>&comment=<message_id>&t=<media_timestamp>
is_first_arg = false;
return td::make_unique<InternalLinkMessage>(
PSTRING() << "tg:resolve?domain=" << url_encode(path[0]) << "&post=" << to_integer<int64>(path[1])
<< copy_arg("single") << copy_arg("thread") << copy_arg("comment") << copy_arg("t"));
}
auto username = path[0];
for (auto &arg : url_query.args_) {
@ -1207,8 +1231,8 @@ Result<MessageLinkInfo> LinkManager::get_message_link_info(Slice url) {
bool is_single = false;
bool for_comment = false;
if (link_info.is_tg_) {
// resolve?domain=username&post=12345&single
// privatepost?channel=123456789&msg_id=12345
// resolve?domain=username&post=12345&single&t=123&comment=12&thread=21
// privatepost?channel=123456789&msg_id=12345&single&t=123&comment=12&thread=21
bool is_resolve = false;
if (begins_with(url, "resolve")) {

View File

@ -125,8 +125,8 @@ TEST(Link, parse_internal_link) {
auto language_pack = [](td::string language_pack_name) {
return td::td_api::make_object<td::td_api::internalLinkTypeLanguagePack>(language_pack_name);
};
auto message = [] {
return td::td_api::make_object<td::td_api::internalLinkTypeMessage>();
auto message = [](td::string url) {
return td::td_api::make_object<td::td_api::internalLinkTypeMessage>(url);
};
auto message_draft = [](td::string text, bool contains_url) {
auto formatted_text = td::td_api::make_object<td::td_api::formattedText>();
@ -174,28 +174,32 @@ TEST(Link, parse_internal_link) {
return td::td_api::make_object<td::td_api::internalLinkTypeVoiceChat>(chat_username, invite_hash);
};
parse_internal_link("t.me/levlam/1", message());
parse_internal_link("telegram.me/levlam/1", message());
parse_internal_link("telegram.dog/levlam/1", message());
parse_internal_link("www.t.me/levlam/1", message());
parse_internal_link("www%2etelegram.me/levlam/1", message());
parse_internal_link("www%2Etelegram.dog/levlam/1", message());
parse_internal_link("t.me/levlam/1", message("tg:resolve?domain=levlam&post=1"));
parse_internal_link("telegram.me/levlam/1", message("tg:resolve?domain=levlam&post=1"));
parse_internal_link("telegram.dog/levlam/1", message("tg:resolve?domain=levlam&post=1"));
parse_internal_link("www.t.me/levlam/1", message("tg:resolve?domain=levlam&post=1"));
parse_internal_link("www%2etelegram.me/levlam/1", message("tg:resolve?domain=levlam&post=1"));
parse_internal_link("www%2Etelegram.dog/levlam/1", message("tg:resolve?domain=levlam&post=1"));
parse_internal_link("www%252Etelegram.dog/levlam/1", nullptr);
parse_internal_link("www.t.me/s/s/s/s/s/joinchat/1", chat_invite("1"));
parse_internal_link("www.t.me/s/%73/%73/s/%73/joinchat/1", chat_invite("1"));
parse_internal_link("http://t.me/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/joinchat/1", chat_invite("1"));
parse_internal_link("http://t.me/levlam/1", message());
parse_internal_link("https://t.me/levlam/1", message());
parse_internal_link("hTtp://www.t.me:443/levlam/1", message());
parse_internal_link("httPs://t.me:80/levlam/1", message());
parse_internal_link("http://t.me/levlam/1", message("tg:resolve?domain=levlam&post=1"));
parse_internal_link("https://t.me/levlam/1", message("tg:resolve?domain=levlam&post=1"));
parse_internal_link("hTtp://www.t.me:443/levlam/1", message("tg:resolve?domain=levlam&post=1"));
parse_internal_link("httPs://t.me:80/levlam/1", message("tg:resolve?domain=levlam&post=1"));
parse_internal_link("https://t.me:200/levlam/1", nullptr);
parse_internal_link("http:t.me/levlam/1", nullptr);
parse_internal_link("t.dog/levlam/1", nullptr);
parse_internal_link("t.m/levlam/1", nullptr);
parse_internal_link("t.men/levlam/1", nullptr);
parse_internal_link("tg:resolve?domain=username&post=12345&single", message());
parse_internal_link("TG://resolve?domain=username&post=12345&single&voicechat=aasd", message());
parse_internal_link("tg:resolve?domain=username&post=12345&single",
message("tg:resolve?domain=username&post=12345&single"));
parse_internal_link("tg:resolve?domain=user%31name&post=%312345&single&comment=456&t=789&single&thread=123%20%31",
message("tg:resolve?domain=user1name&post=12345&single&thread=123%201&comment=456&t=789"));
parse_internal_link("TG://resolve?domain=username&post=12345&single&voicechat=aasd",
message("tg:resolve?domain=username&post=12345&single"));
parse_internal_link("TG://test@resolve?domain=username&post=12345&single", nullptr);
parse_internal_link("tg:resolve:80?domain=username&post=12345&single", nullptr);
parse_internal_link("tg:http://resolve?domain=username&post=12345&single", nullptr);
@ -203,14 +207,17 @@ TEST(Link, parse_internal_link) {
parse_internal_link("tg:resolve?domain=&post=12345&single", unknown_deep_link());
parse_internal_link("tg:resolve?domain=telegram&post=&single", public_chat("telegram"));
parse_internal_link("t.me/username/12345?single", message());
parse_internal_link("t.me/username/12345?asdasd", message());
parse_internal_link("t.me/username/12345", message());
parse_internal_link("t.me/username/12345/", message());
parse_internal_link("t.me/username/12345#asdasd", message());
parse_internal_link("t.me/username/12345//?voicechat=&single", message());
parse_internal_link("t.me/username/12345/asdasd//asd/asd/asd/?single", message());
parse_internal_link("t.me/username/1asdasdas/asdasd//asd/asd/asd/?single", message());
parse_internal_link("t.me/username/12345?single", message("tg:resolve?domain=username&post=12345&single"));
parse_internal_link("t.me/username/12345?asdasd", message("tg:resolve?domain=username&post=12345"));
parse_internal_link("t.me/username/12345", message("tg:resolve?domain=username&post=12345"));
parse_internal_link("t.me/username/12345/", message("tg:resolve?domain=username&post=12345"));
parse_internal_link("t.me/username/12345#asdasd", message("tg:resolve?domain=username&post=12345"));
parse_internal_link("t.me/username/12345//?voicechat=&single",
message("tg:resolve?domain=username&post=12345&single"));
parse_internal_link("t.me/username/12345/asdasd//asd/asd/asd/?single",
message("tg:resolve?domain=username&post=12345&single"));
parse_internal_link("t.me/username/1asdasdas/asdasd//asd/asd/asd/?single",
message("tg:resolve?domain=username&post=1&single"));
parse_internal_link("t.me/username/asd", public_chat("username"));
parse_internal_link("t.me/username/0", public_chat("username"));
parse_internal_link("t.me/username/-12345", public_chat("username"));
@ -219,14 +226,18 @@ TEST(Link, parse_internal_link) {
parse_internal_link("tg:privatepost?domain=username/12345&single", unknown_deep_link());
parse_internal_link("tg:privatepost?channel=username/12345&single", unknown_deep_link());
parse_internal_link("tg:privatepost?channel=username&msg_id=12345", message());
parse_internal_link("tg:privatepost?channel=username&msg_id=12345",
message("tg:privatepost?channel=username&msg_id=12345"));
parse_internal_link("t.me/c/12345?single", nullptr);
parse_internal_link("t.me/c/1/c?single", nullptr);
parse_internal_link("t.me/c/c/1?single", nullptr);
parse_internal_link("t.me/c//1?single", nullptr);
parse_internal_link("t.me/c/12345/123?single", message());
parse_internal_link("t.me/c/12345/123/asd/asd////?single", message());
parse_internal_link("t.me/c/12345/123", message("tg:privatepost?channel=12345&msg_id=123"));
parse_internal_link("t.me/c/12345/123?single", message("tg:privatepost?channel=12345&msg_id=123&single"));
parse_internal_link("t.me/c/12345/123/asd/asd////?single", message("tg:privatepost?channel=12345&msg_id=123&single"));
parse_internal_link("t.me/c/%312345/%3123?comment=456&t=789&single&thread=123%20%31",
message("tg:privatepost?channel=12345&msg_id=123&single&thread=123%201&comment=456&t=789"));
parse_internal_link("tg:bg?color=111111#asdasd", background("111111"));
parse_internal_link("tg:bg?color=11111%31", background("111111"));