2023-05-08 12:50:51 +02:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
2023-05-08 12:50:51 +02:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#include "td/telegram/UserPrivacySettingRule.h"
|
|
|
|
|
|
|
|
#include "td/telegram/ChannelId.h"
|
|
|
|
#include "td/telegram/ChatId.h"
|
|
|
|
#include "td/telegram/ContactsManager.h"
|
2023-07-03 19:03:26 +02:00
|
|
|
#include "td/telegram/Dependencies.h"
|
2024-01-04 13:26:42 +01:00
|
|
|
#include "td/telegram/DialogManager.h"
|
2023-05-08 12:50:51 +02:00
|
|
|
#include "td/telegram/Td.h"
|
|
|
|
|
|
|
|
#include "td/utils/algorithm.h"
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
2023-07-03 18:20:41 +02:00
|
|
|
void UserPrivacySettingRule::set_dialog_ids(Td *td, const vector<int64> &chat_ids) {
|
|
|
|
dialog_ids_.clear();
|
|
|
|
for (auto chat_id : chat_ids) {
|
|
|
|
DialogId dialog_id(chat_id);
|
2024-01-04 13:26:42 +01:00
|
|
|
if (!td->dialog_manager_->have_dialog_force(dialog_id, "UserPrivacySettingRule::set_dialog_ids")) {
|
2023-07-03 18:20:41 +02:00
|
|
|
LOG(INFO) << "Ignore not found " << dialog_id;
|
2023-05-08 12:50:51 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (dialog_id.get_type()) {
|
|
|
|
case DialogType::Chat:
|
2023-07-03 18:20:41 +02:00
|
|
|
dialog_ids_.push_back(dialog_id);
|
2023-05-08 12:50:51 +02:00
|
|
|
break;
|
|
|
|
case DialogType::Channel: {
|
|
|
|
auto channel_id = dialog_id.get_channel_id();
|
|
|
|
if (!td->contacts_manager_->is_megagroup_channel(channel_id)) {
|
2023-07-03 18:20:41 +02:00
|
|
|
LOG(INFO) << "Ignore broadcast " << channel_id;
|
2023-05-08 12:50:51 +02:00
|
|
|
break;
|
|
|
|
}
|
2023-07-03 18:20:41 +02:00
|
|
|
dialog_ids_.push_back(dialog_id);
|
2023-05-08 12:50:51 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2023-07-03 18:20:41 +02:00
|
|
|
LOG(INFO) << "Ignore " << dialog_id;
|
2023-05-08 12:50:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-08 13:15:32 +02:00
|
|
|
UserPrivacySettingRule::UserPrivacySettingRule(Td *td, const td_api::UserPrivacySettingRule &rule) {
|
2023-05-08 12:50:51 +02:00
|
|
|
switch (rule.get_id()) {
|
|
|
|
case td_api::userPrivacySettingRuleAllowContacts::ID:
|
|
|
|
type_ = Type::AllowContacts;
|
|
|
|
break;
|
|
|
|
case td_api::userPrivacySettingRuleAllowAll::ID:
|
|
|
|
type_ = Type::AllowAll;
|
|
|
|
break;
|
|
|
|
case td_api::userPrivacySettingRuleAllowUsers::ID:
|
|
|
|
type_ = Type::AllowUsers;
|
|
|
|
user_ids_ = UserId::get_user_ids(static_cast<const td_api::userPrivacySettingRuleAllowUsers &>(rule).user_ids_);
|
|
|
|
break;
|
|
|
|
case td_api::userPrivacySettingRuleAllowChatMembers::ID:
|
|
|
|
type_ = Type::AllowChatParticipants;
|
2023-07-03 18:20:41 +02:00
|
|
|
set_dialog_ids(td, static_cast<const td_api::userPrivacySettingRuleAllowChatMembers &>(rule).chat_ids_);
|
2023-05-08 12:50:51 +02:00
|
|
|
break;
|
|
|
|
case td_api::userPrivacySettingRuleRestrictContacts::ID:
|
|
|
|
type_ = Type::RestrictContacts;
|
|
|
|
break;
|
|
|
|
case td_api::userPrivacySettingRuleRestrictAll::ID:
|
|
|
|
type_ = Type::RestrictAll;
|
|
|
|
break;
|
|
|
|
case td_api::userPrivacySettingRuleRestrictUsers::ID:
|
|
|
|
type_ = Type::RestrictUsers;
|
|
|
|
user_ids_ =
|
|
|
|
UserId::get_user_ids(static_cast<const td_api::userPrivacySettingRuleRestrictUsers &>(rule).user_ids_);
|
|
|
|
break;
|
|
|
|
case td_api::userPrivacySettingRuleRestrictChatMembers::ID:
|
|
|
|
type_ = Type::RestrictChatParticipants;
|
2023-07-03 18:20:41 +02:00
|
|
|
set_dialog_ids(td, static_cast<const td_api::userPrivacySettingRuleRestrictChatMembers &>(rule).chat_ids_);
|
2023-05-08 12:50:51 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-03 18:20:41 +02:00
|
|
|
UserPrivacySettingRule::UserPrivacySettingRule(Td *td,
|
|
|
|
const telegram_api::object_ptr<telegram_api::PrivacyRule> &rule) {
|
|
|
|
CHECK(rule != nullptr);
|
|
|
|
switch (rule->get_id()) {
|
2023-05-08 12:50:51 +02:00
|
|
|
case telegram_api::privacyValueAllowContacts::ID:
|
|
|
|
type_ = Type::AllowContacts;
|
|
|
|
break;
|
|
|
|
case telegram_api::privacyValueAllowCloseFriends::ID:
|
|
|
|
type_ = Type::AllowCloseFriends;
|
|
|
|
break;
|
|
|
|
case telegram_api::privacyValueAllowAll::ID:
|
|
|
|
type_ = Type::AllowAll;
|
|
|
|
break;
|
|
|
|
case telegram_api::privacyValueAllowUsers::ID:
|
|
|
|
type_ = Type::AllowUsers;
|
2023-07-03 18:20:41 +02:00
|
|
|
user_ids_ = UserId::get_user_ids(static_cast<const telegram_api::privacyValueAllowUsers &>(*rule).users_);
|
2023-05-08 12:50:51 +02:00
|
|
|
break;
|
|
|
|
case telegram_api::privacyValueAllowChatParticipants::ID:
|
|
|
|
type_ = Type::AllowChatParticipants;
|
2023-07-03 18:20:41 +02:00
|
|
|
set_dialog_ids_from_server(td,
|
|
|
|
static_cast<const telegram_api::privacyValueAllowChatParticipants &>(*rule).chats_);
|
2023-05-08 12:50:51 +02:00
|
|
|
break;
|
|
|
|
case telegram_api::privacyValueDisallowContacts::ID:
|
|
|
|
type_ = Type::RestrictContacts;
|
|
|
|
break;
|
|
|
|
case telegram_api::privacyValueDisallowAll::ID:
|
|
|
|
type_ = Type::RestrictAll;
|
|
|
|
break;
|
|
|
|
case telegram_api::privacyValueDisallowUsers::ID:
|
|
|
|
type_ = Type::RestrictUsers;
|
2023-07-03 18:20:41 +02:00
|
|
|
user_ids_ = UserId::get_user_ids(static_cast<const telegram_api::privacyValueDisallowUsers &>(*rule).users_);
|
2023-05-08 12:50:51 +02:00
|
|
|
break;
|
|
|
|
case telegram_api::privacyValueDisallowChatParticipants::ID:
|
|
|
|
type_ = Type::RestrictChatParticipants;
|
2023-07-03 18:20:41 +02:00
|
|
|
set_dialog_ids_from_server(td,
|
|
|
|
static_cast<const telegram_api::privacyValueDisallowChatParticipants &>(*rule).chats_);
|
2023-05-08 12:50:51 +02:00
|
|
|
break;
|
2024-03-07 13:24:28 +01:00
|
|
|
case telegram_api::privacyValueAllowPremium::ID:
|
|
|
|
type_ = Type::AllowContacts;
|
|
|
|
break;
|
2023-05-08 12:50:51 +02:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
2023-07-03 18:20:41 +02:00
|
|
|
td::remove_if(user_ids_, [td](UserId user_id) {
|
|
|
|
if (!td->contacts_manager_->have_user(user_id)) {
|
|
|
|
LOG(ERROR) << "Receive unknown " << user_id;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void UserPrivacySettingRule::set_dialog_ids_from_server(Td *td, const vector<int64> &server_chat_ids) {
|
|
|
|
dialog_ids_.clear();
|
|
|
|
for (auto server_chat_id : server_chat_ids) {
|
|
|
|
ChatId chat_id(server_chat_id);
|
|
|
|
DialogId dialog_id(chat_id);
|
|
|
|
if (!td->contacts_manager_->have_chat(chat_id)) {
|
|
|
|
ChannelId channel_id(server_chat_id);
|
|
|
|
dialog_id = DialogId(channel_id);
|
|
|
|
if (!td->contacts_manager_->have_channel(channel_id)) {
|
|
|
|
LOG(ERROR) << "Receive unknown group " << server_chat_id << " from the server";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2024-01-04 13:38:01 +01:00
|
|
|
td->dialog_manager_->force_create_dialog(dialog_id, "set_dialog_ids_from_server");
|
2023-07-03 18:20:41 +02:00
|
|
|
dialog_ids_.push_back(dialog_id);
|
|
|
|
}
|
2023-05-08 12:50:51 +02:00
|
|
|
}
|
|
|
|
|
2023-05-08 13:15:32 +02:00
|
|
|
td_api::object_ptr<td_api::UserPrivacySettingRule> UserPrivacySettingRule::get_user_privacy_setting_rule_object(
|
|
|
|
Td *td) const {
|
2023-05-08 12:50:51 +02:00
|
|
|
switch (type_) {
|
|
|
|
case Type::AllowContacts:
|
|
|
|
return make_tl_object<td_api::userPrivacySettingRuleAllowContacts>();
|
|
|
|
case Type::AllowCloseFriends:
|
2023-07-18 15:00:06 +02:00
|
|
|
LOG(ERROR) << "Have AllowCloseFriends rule";
|
|
|
|
return make_tl_object<td_api::userPrivacySettingRuleAllowUsers>();
|
2023-05-08 12:50:51 +02:00
|
|
|
case Type::AllowAll:
|
|
|
|
return make_tl_object<td_api::userPrivacySettingRuleAllowAll>();
|
|
|
|
case Type::AllowUsers:
|
2023-05-08 13:15:32 +02:00
|
|
|
return make_tl_object<td_api::userPrivacySettingRuleAllowUsers>(
|
|
|
|
td->contacts_manager_->get_user_ids_object(user_ids_, "userPrivacySettingRuleAllowUsers"));
|
2023-05-08 12:50:51 +02:00
|
|
|
case Type::AllowChatParticipants:
|
2023-07-03 18:20:41 +02:00
|
|
|
return make_tl_object<td_api::userPrivacySettingRuleAllowChatMembers>(
|
2024-01-04 14:13:20 +01:00
|
|
|
td->dialog_manager_->get_chat_ids_object(dialog_ids_, "UserPrivacySettingRule"));
|
2023-05-08 12:50:51 +02:00
|
|
|
case Type::RestrictContacts:
|
|
|
|
return make_tl_object<td_api::userPrivacySettingRuleRestrictContacts>();
|
|
|
|
case Type::RestrictAll:
|
|
|
|
return make_tl_object<td_api::userPrivacySettingRuleRestrictAll>();
|
|
|
|
case Type::RestrictUsers:
|
2023-05-08 13:15:32 +02:00
|
|
|
return make_tl_object<td_api::userPrivacySettingRuleRestrictUsers>(
|
|
|
|
td->contacts_manager_->get_user_ids_object(user_ids_, "userPrivacySettingRuleRestrictUsers"));
|
2023-05-08 12:50:51 +02:00
|
|
|
case Type::RestrictChatParticipants:
|
2023-07-03 18:20:41 +02:00
|
|
|
return make_tl_object<td_api::userPrivacySettingRuleRestrictChatMembers>(
|
2024-01-04 14:13:20 +01:00
|
|
|
td->dialog_manager_->get_chat_ids_object(dialog_ids_, "UserPrivacySettingRule"));
|
2023-05-08 12:50:51 +02:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
2023-07-03 18:20:41 +02:00
|
|
|
return nullptr;
|
2023-05-08 12:50:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-08 13:15:32 +02:00
|
|
|
telegram_api::object_ptr<telegram_api::InputPrivacyRule> UserPrivacySettingRule::get_input_privacy_rule(Td *td) const {
|
2023-05-08 12:50:51 +02:00
|
|
|
switch (type_) {
|
|
|
|
case Type::AllowContacts:
|
|
|
|
return make_tl_object<telegram_api::inputPrivacyValueAllowContacts>();
|
|
|
|
case Type::AllowCloseFriends:
|
|
|
|
return make_tl_object<telegram_api::inputPrivacyValueAllowCloseFriends>();
|
|
|
|
case Type::AllowAll:
|
|
|
|
return make_tl_object<telegram_api::inputPrivacyValueAllowAll>();
|
|
|
|
case Type::AllowUsers:
|
2023-05-08 13:15:32 +02:00
|
|
|
return make_tl_object<telegram_api::inputPrivacyValueAllowUsers>(get_input_users(td));
|
2023-05-08 12:50:51 +02:00
|
|
|
case Type::AllowChatParticipants:
|
2023-07-03 18:20:41 +02:00
|
|
|
return make_tl_object<telegram_api::inputPrivacyValueAllowChatParticipants>(get_input_chat_ids(td));
|
2023-05-08 12:50:51 +02:00
|
|
|
case Type::RestrictContacts:
|
|
|
|
return make_tl_object<telegram_api::inputPrivacyValueDisallowContacts>();
|
|
|
|
case Type::RestrictAll:
|
|
|
|
return make_tl_object<telegram_api::inputPrivacyValueDisallowAll>();
|
|
|
|
case Type::RestrictUsers:
|
2023-05-08 13:15:32 +02:00
|
|
|
return make_tl_object<telegram_api::inputPrivacyValueDisallowUsers>(get_input_users(td));
|
2023-05-08 12:50:51 +02:00
|
|
|
case Type::RestrictChatParticipants:
|
2023-07-03 18:20:41 +02:00
|
|
|
return make_tl_object<telegram_api::inputPrivacyValueDisallowChatParticipants>(get_input_chat_ids(td));
|
2023-05-08 12:50:51 +02:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-08 13:15:32 +02:00
|
|
|
vector<telegram_api::object_ptr<telegram_api::InputUser>> UserPrivacySettingRule::get_input_users(Td *td) const {
|
2023-05-08 12:50:51 +02:00
|
|
|
vector<telegram_api::object_ptr<telegram_api::InputUser>> result;
|
|
|
|
for (auto user_id : user_ids_) {
|
2023-05-08 13:15:32 +02:00
|
|
|
auto r_input_user = td->contacts_manager_->get_input_user(user_id);
|
2023-05-08 12:50:51 +02:00
|
|
|
if (r_input_user.is_ok()) {
|
|
|
|
result.push_back(r_input_user.move_as_ok());
|
|
|
|
} else {
|
2023-07-03 18:20:41 +02:00
|
|
|
LOG(INFO) << "Have no access to " << user_id;
|
2023-05-08 12:50:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-07-03 18:20:41 +02:00
|
|
|
vector<int64> UserPrivacySettingRule::get_input_chat_ids(Td *td) const {
|
2023-05-08 12:50:51 +02:00
|
|
|
vector<int64> result;
|
2023-07-03 18:20:41 +02:00
|
|
|
for (auto dialog_id : dialog_ids_) {
|
|
|
|
switch (dialog_id.get_type()) {
|
|
|
|
case DialogType::Chat:
|
|
|
|
result.push_back(dialog_id.get_chat_id().get());
|
|
|
|
break;
|
|
|
|
case DialogType::Channel:
|
|
|
|
result.push_back(dialog_id.get_channel_id().get());
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
2023-05-08 12:50:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<UserId> UserPrivacySettingRule::get_restricted_user_ids() const {
|
|
|
|
if (type_ == Type::RestrictUsers) {
|
|
|
|
return user_ids_;
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-07-03 19:03:26 +02:00
|
|
|
void UserPrivacySettingRule::add_dependencies(Dependencies &dependencies) const {
|
|
|
|
for (auto user_id : user_ids_) {
|
|
|
|
dependencies.add(user_id);
|
|
|
|
}
|
|
|
|
for (auto dialog_id : dialog_ids_) {
|
|
|
|
dependencies.add_dialog_and_dependencies(dialog_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-08 13:54:57 +02:00
|
|
|
UserPrivacySettingRules UserPrivacySettingRules::get_user_privacy_setting_rules(
|
2023-05-08 13:15:32 +02:00
|
|
|
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_));
|
2023-05-08 12:50:51 +02:00
|
|
|
}
|
|
|
|
|
2023-05-08 13:54:57 +02:00
|
|
|
UserPrivacySettingRules UserPrivacySettingRules::get_user_privacy_setting_rules(
|
2023-05-08 13:15:32 +02:00
|
|
|
Td *td, vector<telegram_api::object_ptr<telegram_api::PrivacyRule>> rules) {
|
2023-05-08 12:50:51 +02:00
|
|
|
UserPrivacySettingRules result;
|
|
|
|
for (auto &rule : rules) {
|
2023-07-03 18:20:41 +02:00
|
|
|
result.rules_.push_back(UserPrivacySettingRule(td, std::move(rule)));
|
2023-05-08 12:50:51 +02:00
|
|
|
}
|
2023-07-18 15:00:06 +02:00
|
|
|
if (!result.rules_.empty() && result.rules_.back().type_ == UserPrivacySettingRule::Type::RestrictAll) {
|
2023-05-08 12:50:51 +02:00
|
|
|
result.rules_.pop_back();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Result<UserPrivacySettingRules> UserPrivacySettingRules::get_user_privacy_setting_rules(
|
2023-05-08 13:15:32 +02:00
|
|
|
Td *td, td_api::object_ptr<td_api::userPrivacySettingRules> rules) {
|
2023-05-08 12:50:51 +02:00
|
|
|
if (rules == nullptr) {
|
|
|
|
return Status::Error(400, "UserPrivacySettingRules must be non-empty");
|
|
|
|
}
|
|
|
|
UserPrivacySettingRules result;
|
|
|
|
for (auto &rule : rules->rules_) {
|
|
|
|
if (rule == nullptr) {
|
|
|
|
return Status::Error(400, "UserPrivacySettingRule must be non-empty");
|
|
|
|
}
|
2023-05-08 13:15:32 +02:00
|
|
|
result.rules_.emplace_back(td, *rule);
|
2023-05-08 12:50:51 +02:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-07-18 15:00:06 +02:00
|
|
|
Result<UserPrivacySettingRules> UserPrivacySettingRules::get_user_privacy_setting_rules(
|
|
|
|
Td *td, td_api::object_ptr<td_api::StoryPrivacySettings> settings) {
|
|
|
|
if (settings == nullptr) {
|
|
|
|
return Status::Error(400, "StoryPrivacySettings must be non-empty");
|
|
|
|
}
|
|
|
|
UserPrivacySettingRules result;
|
|
|
|
switch (settings->get_id()) {
|
2023-08-02 12:50:44 +02:00
|
|
|
case td_api::storyPrivacySettingsEveryone::ID: {
|
|
|
|
auto user_ids = std::move(static_cast<td_api::storyPrivacySettingsEveryone &>(*settings).except_user_ids_);
|
|
|
|
if (!user_ids.empty()) {
|
|
|
|
result.rules_.emplace_back(td, td_api::userPrivacySettingRuleRestrictUsers(std::move(user_ids)));
|
|
|
|
}
|
2023-07-18 15:00:06 +02:00
|
|
|
result.rules_.emplace_back(td, td_api::userPrivacySettingRuleAllowAll());
|
|
|
|
break;
|
2023-08-02 12:50:44 +02:00
|
|
|
}
|
2023-07-18 15:00:06 +02:00
|
|
|
case td_api::storyPrivacySettingsContacts::ID: {
|
|
|
|
auto user_ids = std::move(static_cast<td_api::storyPrivacySettingsContacts &>(*settings).except_user_ids_);
|
|
|
|
if (!user_ids.empty()) {
|
|
|
|
result.rules_.emplace_back(td, td_api::userPrivacySettingRuleRestrictUsers(std::move(user_ids)));
|
|
|
|
}
|
|
|
|
result.rules_.emplace_back(td, td_api::userPrivacySettingRuleAllowContacts());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case td_api::storyPrivacySettingsCloseFriends::ID: {
|
|
|
|
UserPrivacySettingRule rule;
|
|
|
|
rule.type_ = UserPrivacySettingRule::Type::AllowCloseFriends;
|
|
|
|
result.rules_.push_back(std::move(rule));
|
|
|
|
break;
|
|
|
|
}
|
2023-09-13 14:19:16 +02:00
|
|
|
case td_api::storyPrivacySettingsSelectedUsers::ID: {
|
|
|
|
auto user_ids = std::move(static_cast<td_api::storyPrivacySettingsSelectedUsers &>(*settings).user_ids_);
|
2023-07-18 15:00:06 +02:00
|
|
|
result.rules_.emplace_back(td, td_api::userPrivacySettingRuleAllowUsers(std::move(user_ids)));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-05-08 13:15:32 +02:00
|
|
|
td_api::object_ptr<td_api::userPrivacySettingRules> UserPrivacySettingRules::get_user_privacy_setting_rules_object(
|
|
|
|
Td *td) const {
|
2023-05-08 12:50:51 +02:00
|
|
|
return make_tl_object<td_api::userPrivacySettingRules>(
|
2023-05-08 13:15:32 +02:00
|
|
|
transform(rules_, [td](const auto &rule) { return rule.get_user_privacy_setting_rule_object(td); }));
|
2023-05-08 12:50:51 +02:00
|
|
|
}
|
|
|
|
|
2023-07-18 15:00:06 +02:00
|
|
|
td_api::object_ptr<td_api::StoryPrivacySettings> UserPrivacySettingRules::get_story_privacy_settings_object(
|
|
|
|
Td *td) const {
|
2023-07-18 15:19:11 +02:00
|
|
|
if (rules_.empty()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2023-07-18 15:00:06 +02:00
|
|
|
if (rules_.size() == 1u && rules_[0].type_ == UserPrivacySettingRule::Type::AllowAll) {
|
|
|
|
return td_api::make_object<td_api::storyPrivacySettingsEveryone>();
|
|
|
|
}
|
2023-08-02 12:50:44 +02:00
|
|
|
if (rules_.size() == 2u && rules_[0].type_ == UserPrivacySettingRule::Type::RestrictUsers &&
|
|
|
|
rules_[1].type_ == UserPrivacySettingRule::Type::AllowAll) {
|
|
|
|
return td_api::make_object<td_api::storyPrivacySettingsEveryone>(
|
|
|
|
td->contacts_manager_->get_user_ids_object(rules_[0].user_ids_, "storyPrivacySettingsEveryone"));
|
|
|
|
}
|
2023-07-18 15:00:06 +02:00
|
|
|
if (rules_.size() == 1u && rules_[0].type_ == UserPrivacySettingRule::Type::AllowContacts) {
|
|
|
|
return td_api::make_object<td_api::storyPrivacySettingsContacts>();
|
|
|
|
}
|
|
|
|
if (rules_.size() == 2u && rules_[0].type_ == UserPrivacySettingRule::Type::RestrictUsers &&
|
|
|
|
rules_[1].type_ == UserPrivacySettingRule::Type::AllowContacts) {
|
|
|
|
return td_api::make_object<td_api::storyPrivacySettingsContacts>(
|
|
|
|
td->contacts_manager_->get_user_ids_object(rules_[0].user_ids_, "storyPrivacySettingsContacts"));
|
|
|
|
}
|
|
|
|
if (rules_.size() == 1u && rules_[0].type_ == UserPrivacySettingRule::Type::AllowCloseFriends) {
|
|
|
|
return td_api::make_object<td_api::storyPrivacySettingsCloseFriends>();
|
|
|
|
}
|
|
|
|
if (rules_.size() == 1u && rules_[0].type_ == UserPrivacySettingRule::Type::AllowUsers) {
|
2023-09-13 14:19:16 +02:00
|
|
|
return td_api::make_object<td_api::storyPrivacySettingsSelectedUsers>(
|
|
|
|
td->contacts_manager_->get_user_ids_object(rules_[0].user_ids_, "storyPrivacySettingsSelectedUsers"));
|
2023-07-18 15:00:06 +02:00
|
|
|
}
|
2023-09-13 14:19:16 +02:00
|
|
|
return td_api::make_object<td_api::storyPrivacySettingsSelectedUsers>();
|
2023-07-18 15:00:06 +02:00
|
|
|
}
|
|
|
|
|
2023-05-08 13:15:32 +02:00
|
|
|
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); });
|
2023-05-08 12:50:51 +02:00
|
|
|
if (!result.empty() && result.back()->get_id() == telegram_api::inputPrivacyValueDisallowAll::ID) {
|
|
|
|
result.pop_back();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<UserId> UserPrivacySettingRules::get_restricted_user_ids() const {
|
|
|
|
vector<UserId> result;
|
|
|
|
for (auto &rule : rules_) {
|
|
|
|
combine(result, rule.get_restricted_user_ids());
|
|
|
|
}
|
|
|
|
std::sort(result.begin(), result.end(), [](UserId lhs, UserId rhs) { return lhs.get() < rhs.get(); });
|
|
|
|
result.erase(std::unique(result.begin(), result.end()), result.end());
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-07-03 19:03:26 +02:00
|
|
|
void UserPrivacySettingRules::add_dependencies(Dependencies &dependencies) const {
|
|
|
|
for (auto &rule : rules_) {
|
|
|
|
rule.add_dependencies(dependencies);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-08 12:50:51 +02:00
|
|
|
} // namespace td
|