Explicitly pass Td to UserPrivacySettingRule.

This commit is contained in:
levlam 2023-05-08 14:15:32 +03:00
parent 586586d50c
commit c4504af340
3 changed files with 79 additions and 72 deletions

View File

@ -169,7 +169,7 @@ void PrivacyManager::get_privacy(tl_object_ptr<td_api::UserPrivacySetting> key,
auto user_privacy_setting = r_user_privacy_setting.move_as_ok();
auto &info = get_info(user_privacy_setting);
if (info.is_synchronized) {
return promise.set_value(info.rules.get_user_privacy_setting_rules_object());
return promise.set_value(info.rules.get_user_privacy_setting_rules_object(G()->td().get_actor_unsafe()));
}
info.get_promises.push_back(std::move(promise));
if (info.get_promises.size() > 1u) {
@ -185,7 +185,8 @@ void PrivacyManager::get_privacy(tl_object_ptr<td_api::UserPrivacySetting> key,
TRY_RESULT(net_query, std::move(x_net_query));
TRY_RESULT(rules, fetch_result<telegram_api::account_getPrivacy>(std::move(net_query)));
LOG(INFO) << "Receive " << to_string(rules);
return UserPrivacySettingRules::get_user_privacy_setting_rules(std::move(rules));
return UserPrivacySettingRules::get_user_privacy_setting_rules(G()->td().get_actor_unsafe(),
std::move(rules));
}());
}));
}
@ -193,26 +194,30 @@ void PrivacyManager::get_privacy(tl_object_ptr<td_api::UserPrivacySetting> key,
void PrivacyManager::set_privacy(tl_object_ptr<td_api::UserPrivacySetting> key,
tl_object_ptr<td_api::userPrivacySettingRules> rules, Promise<Unit> promise) {
TRY_RESULT_PROMISE(promise, user_privacy_setting, UserPrivacySetting::get_user_privacy_setting(std::move(key)));
TRY_RESULT_PROMISE(promise, privacy_rules, UserPrivacySettingRules::get_user_privacy_setting_rules(std::move(rules)));
TRY_RESULT_PROMISE(
promise, privacy_rules,
UserPrivacySettingRules::get_user_privacy_setting_rules(G()->td().get_actor_unsafe(), std::move(rules)));
auto &info = get_info(user_privacy_setting);
if (info.has_set_query) {
// TODO cancel previous query
return promise.set_error(Status::Error(400, "Another set_privacy query is active"));
}
auto net_query = G()->net_query_creator().create(telegram_api::account_setPrivacy(
user_privacy_setting.get_input_privacy_key(), privacy_rules.get_input_privacy_rules()));
auto net_query = G()->net_query_creator().create(
telegram_api::account_setPrivacy(user_privacy_setting.get_input_privacy_key(),
privacy_rules.get_input_privacy_rules(G()->td().get_actor_unsafe())));
info.has_set_query = true;
send_with_promise(
std::move(net_query), PromiseCreator::lambda([this, user_privacy_setting, promise = std::move(promise)](
Result<NetQueryPtr> x_net_query) mutable {
send_with_promise(std::move(net_query),
PromiseCreator::lambda([this, user_privacy_setting,
promise = std::move(promise)](Result<NetQueryPtr> x_net_query) mutable {
promise.set_result([&]() -> Result<Unit> {
get_info(user_privacy_setting).has_set_query = false;
TRY_RESULT(net_query, std::move(x_net_query));
TRY_RESULT(rules, fetch_result<telegram_api::account_setPrivacy>(std::move(net_query)));
LOG(INFO) << "Receive " << to_string(rules);
TRY_RESULT(privacy_rules, UserPrivacySettingRules::get_user_privacy_setting_rules(std::move(rules)));
TRY_RESULT(privacy_rules, UserPrivacySettingRules::get_user_privacy_setting_rules(
G()->td().get_actor_unsafe(), std::move(rules)));
do_update_privacy(user_privacy_setting, std::move(privacy_rules), true);
return Unit();
}());
@ -223,7 +228,8 @@ void PrivacyManager::update_privacy(tl_object_ptr<telegram_api::updatePrivacy> u
CHECK(update != nullptr);
CHECK(update->key_ != nullptr);
UserPrivacySetting user_privacy_setting(*update->key_);
auto r_privacy_rules = UserPrivacySettingRules::get_user_privacy_setting_rules(std::move(update->rules_));
auto r_privacy_rules =
UserPrivacySettingRules::get_user_privacy_setting_rules(G()->td().get_actor_unsafe(), std::move(update->rules_));
if (r_privacy_rules.is_error()) {
LOG(INFO) << "Skip updatePrivacy: " << r_privacy_rules.error().message();
auto &info = get_info(user_privacy_setting);
@ -242,7 +248,7 @@ void PrivacyManager::on_get_result(UserPrivacySetting user_privacy_setting,
if (r_privacy_rules.is_error()) {
promise.set_error(r_privacy_rules.error().clone());
} else {
promise.set_value(r_privacy_rules.ok().get_user_privacy_setting_rules_object());
promise.set_value(r_privacy_rules.ok().get_user_privacy_setting_rules_object(G()->td().get_actor_unsafe()));
}
}
if (r_privacy_rules.is_ok()) {
@ -286,10 +292,10 @@ void PrivacyManager::do_update_privacy(UserPrivacySetting user_privacy_setting,
}
info.rules = std::move(privacy_rules);
send_closure(
G()->td(), &Td::send_update,
make_tl_object<td_api::updateUserPrivacySettingRules>(user_privacy_setting.get_user_privacy_setting_object(),
info.rules.get_user_privacy_setting_rules_object()));
send_closure(G()->td(), &Td::send_update,
make_tl_object<td_api::updateUserPrivacySettingRules>(
user_privacy_setting.get_user_privacy_setting_object(),
info.rules.get_user_privacy_setting_rules_object(G()->td().get_actor_unsafe())));
}
}

View File

@ -10,7 +10,6 @@
#include "td/telegram/ChatId.h"
#include "td/telegram/ContactsManager.h"
#include "td/telegram/DialogId.h"
#include "td/telegram/Global.h"
#include "td/telegram/MessagesManager.h"
#include "td/telegram/Td.h"
@ -21,9 +20,8 @@
namespace td {
void UserPrivacySettingRule::set_chat_ids(const vector<int64> &dialog_ids) {
void UserPrivacySettingRule::set_chat_ids(Td *td, const vector<int64> &dialog_ids) {
chat_ids_.clear();
auto td = G()->td().get_actor_unsafe();
for (auto dialog_id_int : dialog_ids) {
DialogId dialog_id(dialog_id_int);
if (!td->messages_manager_->have_dialog_force(dialog_id, "UserPrivacySettingRule::set_chat_ids")) {
@ -50,7 +48,7 @@ void UserPrivacySettingRule::set_chat_ids(const vector<int64> &dialog_ids) {
}
}
UserPrivacySettingRule::UserPrivacySettingRule(const td_api::UserPrivacySettingRule &rule) {
UserPrivacySettingRule::UserPrivacySettingRule(Td *td, const td_api::UserPrivacySettingRule &rule) {
switch (rule.get_id()) {
case td_api::userPrivacySettingRuleAllowContacts::ID:
type_ = Type::AllowContacts;
@ -67,7 +65,7 @@ UserPrivacySettingRule::UserPrivacySettingRule(const td_api::UserPrivacySettingR
break;
case td_api::userPrivacySettingRuleAllowChatMembers::ID:
type_ = Type::AllowChatParticipants;
set_chat_ids(static_cast<const td_api::userPrivacySettingRuleAllowChatMembers &>(rule).chat_ids_);
set_chat_ids(td, static_cast<const td_api::userPrivacySettingRuleAllowChatMembers &>(rule).chat_ids_);
break;
case td_api::userPrivacySettingRuleRestrictContacts::ID:
type_ = Type::RestrictContacts;
@ -82,11 +80,12 @@ UserPrivacySettingRule::UserPrivacySettingRule(const td_api::UserPrivacySettingR
break;
case td_api::userPrivacySettingRuleRestrictChatMembers::ID:
type_ = Type::RestrictChatParticipants;
set_chat_ids(static_cast<const td_api::userPrivacySettingRuleRestrictChatMembers &>(rule).chat_ids_);
set_chat_ids(td, static_cast<const td_api::userPrivacySettingRuleRestrictChatMembers &>(rule).chat_ids_);
break;
default:
UNREACHABLE();
}
td::remove_if(user_ids_, [](UserId user_id) { return !user_id.is_valid(); });
}
UserPrivacySettingRule::UserPrivacySettingRule(const telegram_api::PrivacyRule &rule) {
@ -127,8 +126,8 @@ UserPrivacySettingRule::UserPrivacySettingRule(const telegram_api::PrivacyRule &
}
}
td_api::object_ptr<td_api::UserPrivacySettingRule> UserPrivacySettingRule::get_user_privacy_setting_rule_object()
const {
td_api::object_ptr<td_api::UserPrivacySettingRule> UserPrivacySettingRule::get_user_privacy_setting_rule_object(
Td *td) const {
switch (type_) {
case Type::AllowContacts:
return make_tl_object<td_api::userPrivacySettingRuleAllowContacts>();
@ -137,23 +136,25 @@ td_api::object_ptr<td_api::UserPrivacySettingRule> UserPrivacySettingRule::get_u
case Type::AllowAll:
return make_tl_object<td_api::userPrivacySettingRuleAllowAll>();
case Type::AllowUsers:
return make_tl_object<td_api::userPrivacySettingRuleAllowUsers>(UserId::get_input_user_ids(user_ids_));
return make_tl_object<td_api::userPrivacySettingRuleAllowUsers>(
td->contacts_manager_->get_user_ids_object(user_ids_, "userPrivacySettingRuleAllowUsers"));
case Type::AllowChatParticipants:
return make_tl_object<td_api::userPrivacySettingRuleAllowChatMembers>(chat_ids_as_dialog_ids());
return make_tl_object<td_api::userPrivacySettingRuleAllowChatMembers>(chat_ids_as_dialog_ids(td));
case Type::RestrictContacts:
return make_tl_object<td_api::userPrivacySettingRuleRestrictContacts>();
case Type::RestrictAll:
return make_tl_object<td_api::userPrivacySettingRuleRestrictAll>();
case Type::RestrictUsers:
return make_tl_object<td_api::userPrivacySettingRuleRestrictUsers>(UserId::get_input_user_ids(user_ids_));
return make_tl_object<td_api::userPrivacySettingRuleRestrictUsers>(
td->contacts_manager_->get_user_ids_object(user_ids_, "userPrivacySettingRuleRestrictUsers"));
case Type::RestrictChatParticipants:
return make_tl_object<td_api::userPrivacySettingRuleRestrictChatMembers>(chat_ids_as_dialog_ids());
return make_tl_object<td_api::userPrivacySettingRuleRestrictChatMembers>(chat_ids_as_dialog_ids(td));
default:
UNREACHABLE();
}
}
telegram_api::object_ptr<telegram_api::InputPrivacyRule> UserPrivacySettingRule::get_input_privacy_rule() const {
telegram_api::object_ptr<telegram_api::InputPrivacyRule> UserPrivacySettingRule::get_input_privacy_rule(Td *td) const {
switch (type_) {
case Type::AllowContacts:
return make_tl_object<telegram_api::inputPrivacyValueAllowContacts>();
@ -162,7 +163,7 @@ telegram_api::object_ptr<telegram_api::InputPrivacyRule> UserPrivacySettingRule:
case Type::AllowAll:
return make_tl_object<telegram_api::inputPrivacyValueAllowAll>();
case Type::AllowUsers:
return make_tl_object<telegram_api::inputPrivacyValueAllowUsers>(get_input_users());
return make_tl_object<telegram_api::inputPrivacyValueAllowUsers>(get_input_users(td));
case Type::AllowChatParticipants:
return make_tl_object<telegram_api::inputPrivacyValueAllowChatParticipants>(vector<int64>{chat_ids_});
case Type::RestrictContacts:
@ -170,7 +171,7 @@ telegram_api::object_ptr<telegram_api::InputPrivacyRule> UserPrivacySettingRule:
case Type::RestrictAll:
return make_tl_object<telegram_api::inputPrivacyValueDisallowAll>();
case Type::RestrictUsers:
return make_tl_object<telegram_api::inputPrivacyValueDisallowUsers>(get_input_users());
return make_tl_object<telegram_api::inputPrivacyValueDisallowUsers>(get_input_users(td));
case Type::RestrictChatParticipants:
return make_tl_object<telegram_api::inputPrivacyValueDisallowChatParticipants>(vector<int64>{chat_ids_});
default:
@ -179,10 +180,9 @@ telegram_api::object_ptr<telegram_api::InputPrivacyRule> UserPrivacySettingRule:
}
Result<UserPrivacySettingRule> UserPrivacySettingRule::get_user_privacy_setting_rule(
telegram_api::object_ptr<telegram_api::PrivacyRule> rule) {
Td *td, telegram_api::object_ptr<telegram_api::PrivacyRule> rule) {
CHECK(rule != nullptr);
UserPrivacySettingRule result(*rule);
auto td = G()->td().get_actor_unsafe();
for (auto user_id : result.user_ids_) {
if (!td->contacts_manager_->have_user(user_id)) {
return Status::Error(500, "Receive inaccessible user from the server");
@ -203,10 +203,10 @@ Result<UserPrivacySettingRule> UserPrivacySettingRule::get_user_privacy_setting_
return result;
}
vector<telegram_api::object_ptr<telegram_api::InputUser>> UserPrivacySettingRule::get_input_users() const {
vector<telegram_api::object_ptr<telegram_api::InputUser>> UserPrivacySettingRule::get_input_users(Td *td) const {
vector<telegram_api::object_ptr<telegram_api::InputUser>> result;
for (auto user_id : user_ids_) {
auto r_input_user = G()->td().get_actor_unsafe()->contacts_manager_->get_input_user(user_id);
auto r_input_user = td->contacts_manager_->get_input_user(user_id);
if (r_input_user.is_ok()) {
result.push_back(r_input_user.move_as_ok());
} else {
@ -216,9 +216,8 @@ vector<telegram_api::object_ptr<telegram_api::InputUser>> UserPrivacySettingRule
return result;
}
vector<int64> UserPrivacySettingRule::chat_ids_as_dialog_ids() const {
vector<int64> UserPrivacySettingRule::chat_ids_as_dialog_ids(Td *td) const {
vector<int64> result;
auto td = G()->td().get_actor_unsafe();
for (auto chat_id_int : chat_ids_) {
ChatId chat_id(chat_id_int);
DialogId dialog_id(chat_id);
@ -241,20 +240,20 @@ vector<UserId> UserPrivacySettingRule::get_restricted_user_ids() const {
}
Result<UserPrivacySettingRules> UserPrivacySettingRules::get_user_privacy_setting_rules(
telegram_api::object_ptr<telegram_api::account_privacyRules> rules) {
G()->td().get_actor_unsafe()->contacts_manager_->on_get_users(std::move(rules->users_), "on get privacy rules");
G()->td().get_actor_unsafe()->contacts_manager_->on_get_chats(std::move(rules->chats_), "on get privacy rules");
return get_user_privacy_setting_rules(std::move(rules->rules_));
Td *td, telegram_api::object_ptr<telegram_api::account_privacyRules> rules) {
td->contacts_manager_->on_get_users(std::move(rules->users_), "on get privacy rules");
td->contacts_manager_->on_get_chats(std::move(rules->chats_), "on get privacy rules");
return get_user_privacy_setting_rules(td, std::move(rules->rules_));
}
Result<UserPrivacySettingRules> UserPrivacySettingRules::get_user_privacy_setting_rules(
vector<telegram_api::object_ptr<telegram_api::PrivacyRule>> rules) {
Td *td, vector<telegram_api::object_ptr<telegram_api::PrivacyRule>> rules) {
UserPrivacySettingRules result;
for (auto &rule : rules) {
TRY_RESULT(new_rule, UserPrivacySettingRule::get_user_privacy_setting_rule(std::move(rule)));
TRY_RESULT(new_rule, UserPrivacySettingRule::get_user_privacy_setting_rule(td, std::move(rule)));
result.rules_.push_back(new_rule);
}
if (!result.rules_.empty() && result.rules_.back().get_user_privacy_setting_rule_object()->get_id() ==
if (!result.rules_.empty() && result.rules_.back().get_user_privacy_setting_rule_object(td)->get_id() ==
td_api::userPrivacySettingRuleRestrictAll::ID) {
result.rules_.pop_back();
}
@ -262,7 +261,7 @@ Result<UserPrivacySettingRules> UserPrivacySettingRules::get_user_privacy_settin
}
Result<UserPrivacySettingRules> UserPrivacySettingRules::get_user_privacy_setting_rules(
td_api::object_ptr<td_api::userPrivacySettingRules> rules) {
Td *td, td_api::object_ptr<td_api::userPrivacySettingRules> rules) {
if (rules == nullptr) {
return Status::Error(400, "UserPrivacySettingRules must be non-empty");
}
@ -271,20 +270,20 @@ Result<UserPrivacySettingRules> UserPrivacySettingRules::get_user_privacy_settin
if (rule == nullptr) {
return Status::Error(400, "UserPrivacySettingRule must be non-empty");
}
result.rules_.emplace_back(*rule);
result.rules_.emplace_back(td, *rule);
}
return result;
}
td_api::object_ptr<td_api::userPrivacySettingRules> UserPrivacySettingRules::get_user_privacy_setting_rules_object()
const {
td_api::object_ptr<td_api::userPrivacySettingRules> UserPrivacySettingRules::get_user_privacy_setting_rules_object(
Td *td) const {
return make_tl_object<td_api::userPrivacySettingRules>(
transform(rules_, [](const auto &rule) { return rule.get_user_privacy_setting_rule_object(); }));
transform(rules_, [td](const auto &rule) { return rule.get_user_privacy_setting_rule_object(td); }));
}
vector<telegram_api::object_ptr<telegram_api::InputPrivacyRule>> UserPrivacySettingRules::get_input_privacy_rules()
const {
auto result = transform(rules_, [](const auto &rule) { return rule.get_input_privacy_rule(); });
vector<telegram_api::object_ptr<telegram_api::InputPrivacyRule>> UserPrivacySettingRules::get_input_privacy_rules(
Td *td) const {
auto result = transform(rules_, [td](const auto &rule) { return rule.get_input_privacy_rule(td); });
if (!result.empty() && result.back()->get_id() == telegram_api::inputPrivacyValueDisallowAll::ID) {
result.pop_back();
}

View File

@ -15,18 +15,20 @@
namespace td {
class Td;
class UserPrivacySettingRule {
public:
UserPrivacySettingRule() = default;
explicit UserPrivacySettingRule(const td_api::UserPrivacySettingRule &rule);
UserPrivacySettingRule(Td *td, const td_api::UserPrivacySettingRule &rule);
static Result<UserPrivacySettingRule> get_user_privacy_setting_rule(
telegram_api::object_ptr<telegram_api::PrivacyRule> rule);
Td *td, telegram_api::object_ptr<telegram_api::PrivacyRule> rule);
td_api::object_ptr<td_api::UserPrivacySettingRule> get_user_privacy_setting_rule_object() const;
td_api::object_ptr<td_api::UserPrivacySettingRule> get_user_privacy_setting_rule_object(Td *td) const;
telegram_api::object_ptr<telegram_api::InputPrivacyRule> get_input_privacy_rule() const;
telegram_api::object_ptr<telegram_api::InputPrivacyRule> get_input_privacy_rule(Td *td) const;
bool operator==(const UserPrivacySettingRule &other) const {
return type_ == other.type_ && user_ids_ == other.user_ids_ && chat_ids_ == other.chat_ids_;
@ -50,11 +52,11 @@ class UserPrivacySettingRule {
vector<UserId> user_ids_;
vector<int64> chat_ids_;
vector<telegram_api::object_ptr<telegram_api::InputUser>> get_input_users() const;
vector<telegram_api::object_ptr<telegram_api::InputUser>> get_input_users(Td *td) const;
void set_chat_ids(const vector<int64> &dialog_ids);
void set_chat_ids(Td *td, const vector<int64> &dialog_ids);
vector<int64> chat_ids_as_dialog_ids() const;
vector<int64> chat_ids_as_dialog_ids(Td *td) const;
explicit UserPrivacySettingRule(const telegram_api::PrivacyRule &rule);
};
@ -64,17 +66,17 @@ class UserPrivacySettingRules {
UserPrivacySettingRules() = default;
static Result<UserPrivacySettingRules> get_user_privacy_setting_rules(
telegram_api::object_ptr<telegram_api::account_privacyRules> rules);
Td *td, telegram_api::object_ptr<telegram_api::account_privacyRules> rules);
static Result<UserPrivacySettingRules> get_user_privacy_setting_rules(
vector<telegram_api::object_ptr<telegram_api::PrivacyRule>> rules);
Td *td, vector<telegram_api::object_ptr<telegram_api::PrivacyRule>> rules);
static Result<UserPrivacySettingRules> get_user_privacy_setting_rules(
td_api::object_ptr<td_api::userPrivacySettingRules> rules);
Td *td, td_api::object_ptr<td_api::userPrivacySettingRules> rules);
td_api::object_ptr<td_api::userPrivacySettingRules> get_user_privacy_setting_rules_object() const;
td_api::object_ptr<td_api::userPrivacySettingRules> get_user_privacy_setting_rules_object(Td *td) const;
vector<telegram_api::object_ptr<telegram_api::InputPrivacyRule>> get_input_privacy_rules() const;
vector<telegram_api::object_ptr<telegram_api::InputPrivacyRule>> get_input_privacy_rules(Td *td) const;
bool operator==(const UserPrivacySettingRules &other) const {
return rules_ == other.rules_;