From 85e8116a4b640e8f3317b79353328638a91ea13a Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 25 May 2023 18:38:15 +0300 Subject: [PATCH] tg_cli: add class PrivacyRules. --- td/telegram/cli.cpp | 87 ++++++++++++++++++++++++++------------------- 1 file changed, 50 insertions(+), 37 deletions(-) diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index 5cdb337e5..74dc5f90a 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -1062,6 +1062,44 @@ class CliClient final : public Actor { } } + struct PrivacyRules { + string rules_str; + + operator td_api::object_ptr() const { + vector> rules; + for (size_t i = 0; i < rules_str.size(); i++) { + auto arg = vector{to_integer(Slice(rules_str).substr(i + 1))}; + if (rules_str[i] == 'a') { + rules.push_back(td_api::make_object()); + } else if (rules_str[i] == 'A') { + rules.push_back(td_api::make_object()); + } else if (rules_str[i] == 'c') { + rules.push_back(td_api::make_object()); + } else if (rules_str[i] == 'C') { + rules.push_back(td_api::make_object()); + } else if (rules_str[i] == 'f') { + rules.push_back(td_api::make_object()); + } else if (rules_str[i] == 'u') { + rules.push_back(td_api::make_object(std::move(arg))); + } else if (rules_str[i] == 'U') { + rules.push_back(td_api::make_object(std::move(arg))); + } else if (rules_str[i] == 'm') { + rules.push_back(td_api::make_object(std::move(arg))); + } else if (rules_str[i] == 'M') { + rules.push_back(td_api::make_object(std::move(arg))); + } else if (!is_digit(rules_str[i]) && rules_str[i] != '-') { + LOG(ERROR) << "Invalid character " << rules_str[i] << " in privacy rules " << rules_str; + break; + } + } + return td_api::make_object(std::move(rules)); + } + }; + + void get_args(string &args, PrivacyRules &arg) const { + arg.rules_str = trim(args); + } + template void get_args(string &args, FirstType &first_arg, SecondType &second_arg, Types &...other_args) const { string arg; @@ -1475,25 +1513,6 @@ class CliClient final : public Actor { return nullptr; } - td_api::object_ptr as_user_privacy_setting_rules(Slice allow, Slice ids) const { - vector> rules; - if (allow == "c" || allow == "contacts") { - rules.push_back(td_api::make_object()); - } else if (allow == "f" || allow == "friends") { - rules.push_back(td_api::make_object()); - } else if (allow == "users") { - rules.push_back(td_api::make_object(as_user_ids(ids))); - } else if (allow == "chats") { - rules.push_back(td_api::make_object(as_chat_ids(ids))); - } else if (as_bool(allow.str())) { - rules.push_back(td_api::make_object()); - rules.push_back(td_api::make_object()); - } else { - rules.push_back(td_api::make_object()); - } - return td_api::make_object(std::move(rules)); - } - static td_api::object_ptr as_search_messages_filter(Slice filter) { filter = trim(filter); string lowered_filter = to_lower(filter); @@ -2390,11 +2409,9 @@ class CliClient final : public Actor { send_request(td_api::make_object(as_user_privacy_setting(args))); } else if (op == "spr") { string setting; - string allow; - string ids; - get_args(args, setting, allow, ids); - send_request(td_api::make_object(as_user_privacy_setting(setting), - as_user_privacy_setting_rules(allow, ids))); + PrivacyRules rules; + get_args(args, setting, rules); + send_request(td_api::make_object(as_user_privacy_setting(setting), rules)); } else if (op == "cp" || op == "ChangePhone") { send_request(td_api::make_object(args, nullptr)); } else if (op == "ccpc" || op == "CheckChangePhoneCode") { @@ -3914,33 +3931,29 @@ class CliClient final : public Actor { } else if (op == "ssp") { string photo; string caption; - string allow; - string ids; + PrivacyRules rules; string sticker_file_ids; - get_args(args, photo, caption, allow, ids, sticker_file_ids); + get_args(args, photo, caption, rules, sticker_file_ids); send_request( td_api::make_object(td_api::make_object( as_input_file(photo), to_integers(sticker_file_ids)), - as_caption(caption), as_user_privacy_setting_rules(allow, ids), true)); + as_caption(caption), rules, true)); } else if (op == "ssv") { string video; string caption; - string allow; - string ids; + PrivacyRules rules; int32 duration; string sticker_file_ids; - get_args(args, video, caption, allow, ids, duration, sticker_file_ids); + get_args(args, video, caption, rules, duration, sticker_file_ids); send_request(td_api::make_object( td_api::make_object(as_input_file(video), to_integers(sticker_file_ids), duration), - as_caption(caption), as_user_privacy_setting_rules(allow, ids), false)); + as_caption(caption), rules, false)); } else if (op == "sspr") { StoryId story_id; - string allow; - string ids; - get_args(args, story_id, allow, ids); - send_request( - td_api::make_object(story_id, as_user_privacy_setting_rules(allow, ids))); + PrivacyRules rules; + get_args(args, story_id, rules); + send_request(td_api::make_object(story_id, rules)); } else if (op == "gups") { UserId user_id; StoryId from_story_id;