From f4ec6e6cfcd681bac760a9ecdf3a40eafccb2004 Mon Sep 17 00:00:00 2001 From: levlam Date: Sat, 26 Dec 2020 00:43:38 +0300 Subject: [PATCH] tg_cli: add get_search_query. --- td/telegram/cli.cpp | 135 +++++++++++++++++++++----------------------- 1 file changed, 63 insertions(+), 72 deletions(-) diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index eaf032779..514372d59 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -59,6 +59,7 @@ #include #include #include +#include #ifdef USE_READLINE /* Standard readline include files. */ @@ -657,6 +658,18 @@ class CliClient final : public Actor { return transform(transform(full_split(ids_string, get_delimiter(ids_string)), trim), to_integer); } + static std::pair get_search_query(string args) { + string limit; + string query; + std::tie(limit, query) = split(trim(args)); + auto r_limit = to_integer_safe(limit); + if (r_limit.is_error() || r_limit.ok() <= 0) { + query = limit + ' ' + query; + r_limit = 10; + } + return {r_limit.ok(), std::move(query)}; + } + void on_result(uint64 generation, uint64 id, td_api::object_ptr result) { auto result_str = to_string(result); if (result != nullptr) { @@ -1843,7 +1856,7 @@ class CliClient final : public Actor { string thread_message_id; string from_message_id; string offset; - string limit; + int32 limit; std::tie(chat_id, args) = split(args); if (op == "gmth") { @@ -1857,20 +1870,16 @@ class CliClient final : public Actor { if (offset.empty()) { offset = "0"; } - std::tie(limit, args) = split(args); - if (limit.empty()) { - limit = "10"; - } + std::tie(limit, args) = get_search_query(args); if (!args.empty()) { LOG(ERROR) << "Wrong parameters to function getChatHistory specified"; } else if (op == "gmth") { send_request(td_api::make_object( as_chat_id(chat_id), as_message_id(thread_message_id), as_message_id(from_message_id), - to_integer(offset), to_integer(limit))); + to_integer(offset), limit)); } else { send_request(td_api::make_object(as_chat_id(chat_id), as_message_id(from_message_id), - to_integer(offset), to_integer(limit), - op == "ghl")); + to_integer(offset), limit, op == "ghl")); } } else if (op == "gcsm") { string chat_id = args; @@ -1929,17 +1938,14 @@ class CliClient final : public Actor { as_search_messages_filter(filter), 1, 2147483647)); } else if (op == "SCM") { string chat_id; - string limit; + int32 limit; string query; std::tie(chat_id, args) = split(args); - std::tie(limit, query) = split(args); - if (limit.empty()) { - limit = "10"; - } + std::tie(limit, query) = get_search_query(args); - send_request(td_api::make_object(as_chat_id(chat_id), query, nullptr, 0, 0, - to_integer(limit), nullptr, 0)); + send_request(td_api::make_object(as_chat_id(chat_id), query, nullptr, 0, 0, limit, + nullptr, 0)); } else if (op == "SMME") { string chat_id; string limit; @@ -2019,7 +2025,7 @@ class CliClient final : public Actor { } else if (op == "SearchAudio") { string chat_id; string offset_message_id; - string limit; + int32 limit; string query; std::tie(chat_id, args) = split(args); @@ -2027,17 +2033,14 @@ class CliClient final : public Actor { if (offset_message_id.empty()) { offset_message_id = "0"; } - std::tie(limit, query) = split(args); - if (limit.empty()) { - limit = "10"; - } + std::tie(limit, query) = get_search_query(args); send_request(td_api::make_object( - as_chat_id(chat_id), query, nullptr, as_message_id(offset_message_id), 0, to_integer(limit), + as_chat_id(chat_id), query, nullptr, as_message_id(offset_message_id), 0, limit, td_api::make_object(), 0)); } else if (op == "SearchDocument") { string chat_id; string offset_message_id; - string limit; + int32 limit; string query; std::tie(chat_id, args) = split(args); @@ -2045,17 +2048,14 @@ class CliClient final : public Actor { if (offset_message_id.empty()) { offset_message_id = "0"; } - std::tie(limit, query) = split(args); - if (limit.empty()) { - limit = "10"; - } + std::tie(limit, query) = get_search_query(args); send_request(td_api::make_object( - as_chat_id(chat_id), query, nullptr, to_integer(offset_message_id), 0, to_integer(limit), + as_chat_id(chat_id), query, nullptr, to_integer(offset_message_id), 0, limit, td_api::make_object(), 0)); } else if (op == "SearchPhoto") { string chat_id; string offset_message_id; - string limit; + int32 limit; string query; std::tie(chat_id, args) = split(args); @@ -2063,17 +2063,14 @@ class CliClient final : public Actor { if (offset_message_id.empty()) { offset_message_id = "2000000000000000000"; } - std::tie(limit, query) = split(args); - if (limit.empty()) { - limit = "10"; - } + std::tie(limit, query) = get_search_query(args); send_request(td_api::make_object( - as_chat_id(chat_id), query, nullptr, as_message_id(offset_message_id), 0, to_integer(limit), + as_chat_id(chat_id), query, nullptr, as_message_id(offset_message_id), 0, limit, td_api::make_object(), 0)); } else if (op == "SearchChatPhoto") { string chat_id; string offset_message_id; - string limit; + int32 limit; string query; std::tie(chat_id, args) = split(args); @@ -2081,12 +2078,9 @@ class CliClient final : public Actor { if (offset_message_id.empty()) { offset_message_id = "2000000000000000000"; } - std::tie(limit, query) = split(args); - if (limit.empty()) { - limit = "10"; - } + std::tie(limit, query) = get_search_query(args); send_request(td_api::make_object( - as_chat_id(chat_id), query, nullptr, as_message_id(offset_message_id), 0, to_integer(limit), + as_chat_id(chat_id), query, nullptr, as_message_id(offset_message_id), 0, limit, td_api::make_object(), 0)); } else if (op == "gcmc") { string chat_id; @@ -2101,22 +2095,19 @@ class CliClient final : public Actor { } else if (op == "gup" || op == "gupp") { string user_id; string offset; - string limit; + int32 limit; std::tie(user_id, args) = split(args); std::tie(offset, args) = split(args); if (offset.empty()) { offset = "0"; } - std::tie(limit, args) = split(args); - if (limit.empty()) { - limit = "10"; - } + std::tie(limit, args) = get_search_query(args); if (!args.empty()) { LOG(ERROR) << "Wrong parameters to function getUserProfilePhotos specified"; } else { - send_request(td_api::make_object(as_user_id(user_id), to_integer(offset), - to_integer(limit))); + send_request( + td_api::make_object(as_user_id(user_id), to_integer(offset), limit)); } } else if (op == "dcrm") { string chat_id; @@ -2341,15 +2332,15 @@ class CliClient final : public Actor { } else if (op == "gsu") { send_request(td_api::make_object()); } else if (op == "gs") { - string limit; + int32 limit; string emoji; - std::tie(limit, emoji) = split(args); - send_request(td_api::make_object(emoji, to_integer(limit))); + std::tie(limit, emoji) = get_search_query(args); + send_request(td_api::make_object(emoji, limit)); } else if (op == "sst") { - string limit; + int32 limit; string emoji; - std::tie(limit, emoji) = split(args); - send_request(td_api::make_object(emoji, to_integer(limit))); + std::tie(limit, emoji) = get_search_query(args); + send_request(td_api::make_object(emoji, limit)); } else if (op == "gss") { send_request(td_api::make_object(to_integer(args))); } else if (op == "giss") { @@ -2544,14 +2535,14 @@ class CliClient final : public Actor { send_request(td_api::make_object(as_secret_chat_id(args))); } else if (op == "scm") { string chat_id; - string limit; - string query; string filter; + int32 limit; + string query; std::tie(chat_id, args) = split(args); - std::tie(limit, args) = split(args); - std::tie(query, filter) = split(args); - send_request(td_api::make_object(as_chat_id(chat_id), query, to_integer(limit), + std::tie(filter, args) = split(args); + std::tie(limit, query) = get_search_query(args); + send_request(td_api::make_object(as_chat_id(chat_id), query, limit, get_chat_members_filter(filter))); } else if (op == "gcm") { string chat_id; @@ -3062,18 +3053,18 @@ class CliClient final : public Actor { } } else if (op == "ssm") { string chat_id; - string offset; - string limit; string filter; + string offset; + int32 limit; string query; std::tie(chat_id, args) = split(args); + std::tie(filter, args) = split(args); std::tie(offset, args) = split(args); - std::tie(limit, args) = split(args); - std::tie(filter, query) = split(args); + std::tie(limit, query) = get_search_query(args); - send_request(td_api::make_object( - as_chat_id(chat_id), query, offset, to_integer(limit), as_search_messages_filter(filter))); + send_request(td_api::make_object(as_chat_id(chat_id), query, offset, limit, + as_search_messages_filter(filter))); } else if (op == "ssd") { schedule_date_ = args; } else if (op == "smti") { @@ -4052,15 +4043,15 @@ class CliClient final : public Actor { } else if (op == "spcs") { send_request(td_api::make_object(args)); } else if (op == "sc") { - string limit; + int32 limit; string query; - std::tie(limit, query) = split(args); - send_request(td_api::make_object(query, to_integer(limit))); + std::tie(limit, query) = get_search_query(args); + send_request(td_api::make_object(query, limit)); } else if (op == "scos") { - string limit; + int32 limit; string query; - std::tie(limit, query) = split(args); - send_request(td_api::make_object(query, to_integer(limit))); + std::tie(limit, query) = get_search_query(args); + send_request(td_api::make_object(query, limit)); } else if (op == "scn") { string latitude; string longitude; @@ -4074,10 +4065,10 @@ class CliClient final : public Actor { std::tie(latitude, longitude) = split(args); send_request(td_api::make_object(as_location(latitude, longitude))); } else if (op == "sco") { - string limit; + int32 limit; string query; - std::tie(limit, query) = split(args); - send_request(td_api::make_object(query, to_integer(limit))); + std::tie(limit, query) = get_search_query(args); + send_request(td_api::make_object(query, limit)); } else if (op == "arfc") { send_request(td_api::make_object(as_chat_id(args))); } else if (op == "rrfc") {