Actually support targetChatChosen.

This commit is contained in:
levlam 2022-06-01 18:35:34 +03:00
parent 8c59d4bd6d
commit 5c7f842f72
2 changed files with 60 additions and 10 deletions

View File

@ -147,6 +147,28 @@ static AdministratorRights get_administrator_rights(Slice rights, bool for_chann
for_channel ? ChannelType::Broadcast : ChannelType::Megagroup);
}
td_api::object_ptr<td_api::targetChatChosen> get_target_chat_chosen(Slice chat_types) {
bool allow_users = false;
bool allow_bots = false;
bool allow_groups = false;
bool allow_channels = false;
for (auto chat_type : full_split(chat_types, ' ')) {
if (chat_type == "users") {
allow_users = true;
} else if (chat_type == "bots") {
allow_bots = true;
} else if (chat_type == "groups") {
allow_groups = true;
} else if (chat_type == "channels") {
allow_channels = true;
}
}
if (!allow_users && !allow_bots && !allow_groups && !allow_channels) {
return nullptr;
}
return td_api::make_object<td_api::targetChatChosen>(allow_users, allow_bots, allow_groups, allow_channels);
}
class LinkManager::InternalLinkActiveSessions final : public InternalLink {
td_api::object_ptr<td_api::InternalLinkType> get_internal_link_type_object() const final {
return td_api::make_object<td_api::internalLinkTypeActiveSessions>();
@ -1003,10 +1025,10 @@ unique_ptr<LinkManager::InternalLink> LinkManager::parse_tg_link_query(Slice que
nullptr, td::make_unique<InternalLinkPublicDialog>(std::move(username)), url_query.get_arg("attach").str(),
url_query.get_arg("startattach"));
} else if (url_query.has_arg("startattach")) {
// resolve?domain=<bot_username>&startattach
// resolve?domain=<bot_username>&startattach=<start_parameter>
return td::make_unique<InternalLinkAttachMenuBot>(nullptr, nullptr, std::move(username),
url_query.get_arg("startattach"));
// resolve?domain=<bot_username>&startattach&choose=users+bots+groups+channels
// resolve?domain=<bot_username>&startattach=<start_parameter>&choose=users+bots+groups+channels
return td::make_unique<InternalLinkAttachMenuBot>(get_target_chat_chosen(url_query.get_arg("choose")), nullptr,
std::move(username), url_query.get_arg("startattach"));
}
if (username == "telegrampassport") {
// resolve?domain=telegrampassport&bot_id=<bot_user_id>&scope=<scope>&public_key=<public_key>&nonce=<nonce>
@ -1330,10 +1352,10 @@ unique_ptr<LinkManager::InternalLink> LinkManager::parse_t_me_link_query(Slice q
nullptr, td::make_unique<InternalLinkPublicDialog>(std::move(username)), url_query.get_arg("attach").str(),
url_query.get_arg("startattach"));
} else if (url_query.has_arg("startattach")) {
// /<bot_username>?startattach
// /<bot_username>?startattach=<start_parameter>
return td::make_unique<InternalLinkAttachMenuBot>(nullptr, nullptr, std::move(username),
url_query.get_arg("startattach"));
// /<bot_username>?startattach&choose=users+bots+groups+channels
// /<bot_username>?startattach=<start_parameter>&choose=users+bots+groups+channels
return td::make_unique<InternalLinkAttachMenuBot>(get_target_chat_chosen(url_query.get_arg("choose")), nullptr,
std::move(username), url_query.get_arg("startattach"));
}
// /<username>

View File

@ -95,6 +95,10 @@ static void parse_internal_link(const td::string &url, td::td_api::object_ptr<td
}
TEST(Link, parse_internal_link) {
auto target_chat_chosen = [](bool allow_users, bool allow_bots, bool allow_groups, bool allow_channels) {
return td::td_api::make_object<td::td_api::targetChatChosen>(allow_users, allow_bots, allow_groups, allow_channels);
};
auto active_sessions = [] {
return td::td_api::make_object<td::td_api::internalLinkTypeActiveSessions>();
};
@ -254,6 +258,18 @@ TEST(Link, parse_internal_link) {
parse_internal_link("tg:resolve?domain=telegram&startattach", attachment_menu_bot(nullptr, nullptr, "telegram", ""));
parse_internal_link("tg:resolve?domain=telegram&startattach=1",
attachment_menu_bot(nullptr, nullptr, "telegram", "1"));
parse_internal_link("tg:resolve?domain=telegram&startattach=1&choose=cats+dogs",
attachment_menu_bot(nullptr, nullptr, "telegram", "1"));
parse_internal_link("tg:resolve?domain=telegram&startattach=1&choose=users",
attachment_menu_bot(target_chat_chosen(true, false, false, false), nullptr, "telegram", "1"));
parse_internal_link("tg:resolve?domain=telegram&startattach=1&choose=bots",
attachment_menu_bot(target_chat_chosen(false, true, false, false), nullptr, "telegram", "1"));
parse_internal_link("tg:resolve?domain=telegram&startattach=1&choose=groups",
attachment_menu_bot(target_chat_chosen(false, false, true, false), nullptr, "telegram", "1"));
parse_internal_link("tg:resolve?domain=telegram&startattach=1&choose=channels",
attachment_menu_bot(target_chat_chosen(false, false, false, true), nullptr, "telegram", "1"));
parse_internal_link("tg:resolve?domain=telegram&startattach=1&choose=users+channels",
attachment_menu_bot(target_chat_chosen(true, false, false, true), nullptr, "telegram", "1"));
parse_internal_link("tg:resolve?domain=telegram&attach=&startattach",
attachment_menu_bot(nullptr, nullptr, "telegram", ""));
parse_internal_link("tg:resolve?domain=telegram&attach=&startattach=1",
@ -272,7 +288,7 @@ TEST(Link, parse_internal_link) {
parse_internal_link("tg:resolve?phone=123456&attach=&startattach=123", user_phone_number("123456"));
parse_internal_link("tg:resolve?phone=123456&attach=test",
attachment_menu_bot(nullptr, user_phone_number("123456"), "test", ""));
parse_internal_link("tg:resolve?phone=123456&attach=test&startattach",
parse_internal_link("tg:resolve?phone=123456&attach=test&startattach&choose=users",
attachment_menu_bot(nullptr, user_phone_number("123456"), "test", ""));
parse_internal_link("tg:resolve?phone=123456&attach=test&startattach=123",
attachment_menu_bot(nullptr, user_phone_number("123456"), "test", "123"));
@ -302,13 +318,25 @@ TEST(Link, parse_internal_link) {
parse_internal_link("https://telegram.dog/telegram/?single", public_chat("telegram"));
parse_internal_link("t.me/username?startattach", attachment_menu_bot(nullptr, nullptr, "username", ""));
parse_internal_link("t.me/username?startattach=1", attachment_menu_bot(nullptr, nullptr, "username", "1"));
parse_internal_link("t.me/username?startattach=1&choose=cats+dogs",
attachment_menu_bot(nullptr, nullptr, "username", "1"));
parse_internal_link("t.me/username?startattach=1&choose=users",
attachment_menu_bot(target_chat_chosen(true, false, false, false), nullptr, "username", "1"));
parse_internal_link("t.me/username?startattach=1&choose=bots",
attachment_menu_bot(target_chat_chosen(false, true, false, false), nullptr, "username", "1"));
parse_internal_link("t.me/username?startattach=1&choose=groups",
attachment_menu_bot(target_chat_chosen(false, false, true, false), nullptr, "username", "1"));
parse_internal_link("t.me/username?startattach=1&choose=channels",
attachment_menu_bot(target_chat_chosen(false, false, false, true), nullptr, "username", "1"));
parse_internal_link("t.me/username?startattach=1&choose=bots+groups",
attachment_menu_bot(target_chat_chosen(false, true, true, false), nullptr, "username", "1"));
parse_internal_link("t.me/username?attach=", public_chat("username"));
parse_internal_link("t.me/username?attach=&startattach", attachment_menu_bot(nullptr, nullptr, "username", ""));
parse_internal_link("t.me/username?attach=&startattach=1", attachment_menu_bot(nullptr, nullptr, "username", "1"));
parse_internal_link("t.me/username?attach=bot", attachment_menu_bot(nullptr, public_chat("username"), "bot", ""));
parse_internal_link("t.me/username?attach=bot&startattach",
attachment_menu_bot(nullptr, public_chat("username"), "bot", ""));
parse_internal_link("t.me/username?attach=bot&startattach=1",
parse_internal_link("t.me/username?attach=bot&startattach=1&choose=users",
attachment_menu_bot(nullptr, public_chat("username"), "bot", "1"));
parse_internal_link("tg:privatepost?domain=username/12345&single",