2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2018-12-31 20:04:05 +01: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/PrivacyManager.h"
|
|
|
|
|
|
|
|
#include "td/telegram/ContactsManager.h"
|
|
|
|
#include "td/telegram/Global.h"
|
|
|
|
#include "td/telegram/net/NetQueryCreator.h"
|
|
|
|
#include "td/telegram/net/NetQueryDispatcher.h"
|
|
|
|
#include "td/telegram/Td.h"
|
2023-05-08 12:50:51 +02:00
|
|
|
#include "td/telegram/UserId.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-01-01 13:59:53 +01:00
|
|
|
#include "td/utils/algorithm.h"
|
2023-05-08 14:23:57 +02:00
|
|
|
#include "td/utils/buffer.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/logging.h"
|
|
|
|
|
2020-04-19 11:53:07 +02:00
|
|
|
#include <algorithm>
|
|
|
|
#include <iterator>
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
namespace td {
|
|
|
|
|
2023-05-08 14:23:57 +02:00
|
|
|
class GetPrivacyQuery final : public Td::ResultHandler {
|
|
|
|
Promise<UserPrivacySettingRules> promise_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit GetPrivacyQuery(Promise<UserPrivacySettingRules> &&promise) : promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void send(UserPrivacySetting user_privacy_setting) {
|
|
|
|
send_query(G()->net_query_creator().create(
|
|
|
|
telegram_api::account_getPrivacy(user_privacy_setting.get_input_privacy_key())));
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_result(BufferSlice packet) final {
|
|
|
|
auto result_ptr = fetch_result<telegram_api::account_getPrivacy>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
|
|
|
return on_error(result_ptr.move_as_error());
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ptr = result_ptr.move_as_ok();
|
|
|
|
LOG(INFO) << "Receive result for GetPrivacyQuery: " << to_string(ptr);
|
|
|
|
promise_.set_value(UserPrivacySettingRules::get_user_privacy_setting_rules(td_, std::move(ptr)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_error(Status status) final {
|
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class SetPrivacyQuery final : public Td::ResultHandler {
|
|
|
|
Promise<UserPrivacySettingRules> promise_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit SetPrivacyQuery(Promise<UserPrivacySettingRules> &&promise) : promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void send(UserPrivacySetting user_privacy_setting, UserPrivacySettingRules &&privacy_rules) {
|
|
|
|
send_query(G()->net_query_creator().create(telegram_api::account_setPrivacy(
|
|
|
|
user_privacy_setting.get_input_privacy_key(), privacy_rules.get_input_privacy_rules(td_))));
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_result(BufferSlice packet) final {
|
|
|
|
auto result_ptr = fetch_result<telegram_api::account_setPrivacy>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
|
|
|
return on_error(result_ptr.move_as_error());
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ptr = result_ptr.move_as_ok();
|
|
|
|
LOG(INFO) << "Receive result for SetPrivacyQuery: " << to_string(ptr);
|
|
|
|
promise_.set_value(UserPrivacySettingRules::get_user_privacy_setting_rules(td_, std::move(ptr)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_error(Status status) final {
|
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
PrivacyManager::PrivacyManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrivacyManager::tear_down() {
|
|
|
|
parent_.reset();
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void PrivacyManager::get_privacy(tl_object_ptr<td_api::UserPrivacySetting> key,
|
|
|
|
Promise<tl_object_ptr<td_api::userPrivacySettingRules>> promise) {
|
2020-06-22 03:28:03 +02:00
|
|
|
auto r_user_privacy_setting = UserPrivacySetting::get_user_privacy_setting(std::move(key));
|
2018-12-31 20:04:05 +01:00
|
|
|
if (r_user_privacy_setting.is_error()) {
|
|
|
|
return promise.set_error(r_user_privacy_setting.move_as_error());
|
|
|
|
}
|
|
|
|
auto user_privacy_setting = r_user_privacy_setting.move_as_ok();
|
|
|
|
auto &info = get_info(user_privacy_setting);
|
2023-05-08 19:35:47 +02:00
|
|
|
if (info.is_synchronized_) {
|
|
|
|
return promise.set_value(info.rules_.get_user_privacy_setting_rules_object(td_));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2023-05-08 19:35:47 +02:00
|
|
|
info.get_promises_.push_back(std::move(promise));
|
|
|
|
if (info.get_promises_.size() > 1u) {
|
2018-12-31 20:04:05 +01:00
|
|
|
// query has already been sent, just wait for the result
|
|
|
|
return;
|
|
|
|
}
|
2023-05-08 14:23:57 +02:00
|
|
|
auto query_promise = PromiseCreator::lambda(
|
|
|
|
[actor_id = actor_id(this), user_privacy_setting](Result<UserPrivacySettingRules> r_privacy_rules) {
|
|
|
|
send_closure(actor_id, &PrivacyManager::on_get_user_privacy_settings, user_privacy_setting,
|
|
|
|
std::move(r_privacy_rules));
|
|
|
|
});
|
|
|
|
td_->create_handler<GetPrivacyQuery>(std::move(query_promise))->send(user_privacy_setting);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PrivacyManager::set_privacy(tl_object_ptr<td_api::UserPrivacySetting> key,
|
2018-12-28 23:48:32 +01:00
|
|
|
tl_object_ptr<td_api::userPrivacySettingRules> rules, Promise<Unit> promise) {
|
2021-09-28 16:31:00 +02:00
|
|
|
TRY_RESULT_PROMISE(promise, user_privacy_setting, UserPrivacySetting::get_user_privacy_setting(std::move(key)));
|
2023-05-08 14:23:57 +02:00
|
|
|
TRY_RESULT_PROMISE(promise, privacy_rules,
|
|
|
|
UserPrivacySettingRules::get_user_privacy_setting_rules(td_, std::move(rules)));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
auto &info = get_info(user_privacy_setting);
|
2023-05-08 19:35:47 +02:00
|
|
|
if (info.has_set_query_) {
|
2023-05-08 19:54:35 +02:00
|
|
|
info.pending_rules_ = std::move(privacy_rules);
|
|
|
|
info.set_promises_.push_back(std::move(promise));
|
|
|
|
return;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2023-05-08 19:35:47 +02:00
|
|
|
info.has_set_query_ = true;
|
2023-05-08 14:23:57 +02:00
|
|
|
|
2023-05-08 19:54:35 +02:00
|
|
|
set_privacy_impl(user_privacy_setting, std::move(privacy_rules), std::move(promise));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrivacyManager::set_privacy_impl(UserPrivacySetting user_privacy_setting, UserPrivacySettingRules &&privacy_rules,
|
|
|
|
Promise<Unit> &&promise) {
|
2023-05-08 14:23:57 +02:00
|
|
|
auto query_promise =
|
|
|
|
PromiseCreator::lambda([actor_id = actor_id(this), user_privacy_setting,
|
|
|
|
promise = std::move(promise)](Result<UserPrivacySettingRules> r_privacy_rules) mutable {
|
|
|
|
send_closure(actor_id, &PrivacyManager::on_set_user_privacy_settings, user_privacy_setting,
|
|
|
|
std::move(r_privacy_rules), std::move(promise));
|
|
|
|
});
|
|
|
|
td_->create_handler<SetPrivacyQuery>(std::move(query_promise))->send(user_privacy_setting, std::move(privacy_rules));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2023-05-08 13:54:57 +02:00
|
|
|
void PrivacyManager::on_update_privacy(tl_object_ptr<telegram_api::updatePrivacy> update) {
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(update != nullptr);
|
|
|
|
CHECK(update->key_ != nullptr);
|
|
|
|
UserPrivacySetting user_privacy_setting(*update->key_);
|
2023-05-08 14:23:57 +02:00
|
|
|
auto privacy_rules = UserPrivacySettingRules::get_user_privacy_setting_rules(td_, std::move(update->rules_));
|
2023-05-08 13:54:57 +02:00
|
|
|
do_update_privacy(user_privacy_setting, std::move(privacy_rules), true);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2023-05-08 14:23:57 +02:00
|
|
|
void PrivacyManager::on_get_user_privacy_settings(UserPrivacySetting user_privacy_setting,
|
|
|
|
Result<UserPrivacySettingRules> r_privacy_rules) {
|
|
|
|
G()->ignore_result_if_closing(r_privacy_rules);
|
2018-12-31 20:04:05 +01:00
|
|
|
auto &info = get_info(user_privacy_setting);
|
2023-05-08 19:35:47 +02:00
|
|
|
auto promises = std::move(info.get_promises_);
|
|
|
|
reset_to_empty(info.get_promises_);
|
2018-12-31 20:04:05 +01:00
|
|
|
for (auto &promise : promises) {
|
2022-04-13 16:40:12 +02:00
|
|
|
if (r_privacy_rules.is_error()) {
|
|
|
|
promise.set_error(r_privacy_rules.error().clone());
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2023-05-08 14:23:57 +02:00
|
|
|
promise.set_value(r_privacy_rules.ok().get_user_privacy_setting_rules_object(td_));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
2022-04-13 16:40:12 +02:00
|
|
|
if (r_privacy_rules.is_ok()) {
|
|
|
|
do_update_privacy(user_privacy_setting, r_privacy_rules.move_as_ok(), false);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-08 14:23:57 +02:00
|
|
|
void PrivacyManager::on_set_user_privacy_settings(UserPrivacySetting user_privacy_setting,
|
|
|
|
Result<UserPrivacySettingRules> r_privacy_rules,
|
|
|
|
Promise<Unit> &&promise) {
|
|
|
|
G()->ignore_result_if_closing(r_privacy_rules);
|
|
|
|
auto &info = get_info(user_privacy_setting);
|
2023-05-08 19:35:47 +02:00
|
|
|
CHECK(info.has_set_query_);
|
|
|
|
info.has_set_query_ = false;
|
2023-05-08 14:23:57 +02:00
|
|
|
if (r_privacy_rules.is_error()) {
|
2023-05-08 19:54:35 +02:00
|
|
|
promise.set_error(r_privacy_rules.move_as_error());
|
|
|
|
if (G()->close_flag()) {
|
|
|
|
return fail_promises(info.set_promises_, Global::request_aborted_error());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
do_update_privacy(user_privacy_setting, r_privacy_rules.move_as_ok(), true);
|
|
|
|
promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
if (!info.set_promises_.empty()) {
|
|
|
|
info.has_set_query_ = true;
|
|
|
|
auto join_promise =
|
|
|
|
PromiseCreator::lambda([promises = std::move(info.set_promises_)](Result<Unit> &&result) mutable {
|
|
|
|
if (result.is_ok()) {
|
|
|
|
set_promises(promises);
|
|
|
|
} else {
|
|
|
|
fail_promises(promises, result.move_as_error());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
reset_to_empty(info.set_promises_);
|
|
|
|
set_privacy_impl(user_privacy_setting, std::move(info.pending_rules_), std::move(join_promise));
|
2023-05-08 14:23:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void PrivacyManager::do_update_privacy(UserPrivacySetting user_privacy_setting, UserPrivacySettingRules &&privacy_rules,
|
|
|
|
bool from_update) {
|
|
|
|
auto &info = get_info(user_privacy_setting);
|
2023-05-08 19:35:47 +02:00
|
|
|
bool was_synchronized = info.is_synchronized_;
|
|
|
|
info.is_synchronized_ = true;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2023-05-08 19:35:47 +02:00
|
|
|
if (!(info.rules_ == privacy_rules)) {
|
2020-05-01 00:21:02 +02:00
|
|
|
if ((from_update || was_synchronized) && !G()->close_flag()) {
|
|
|
|
switch (user_privacy_setting.type()) {
|
|
|
|
case UserPrivacySetting::Type::UserStatus: {
|
|
|
|
send_closure_later(G()->contacts_manager(), &ContactsManager::on_update_online_status_privacy);
|
|
|
|
|
2023-05-08 19:35:47 +02:00
|
|
|
auto old_restricted = info.rules_.get_restricted_user_ids();
|
2020-05-01 00:21:02 +02:00
|
|
|
auto new_restricted = privacy_rules.get_restricted_user_ids();
|
|
|
|
if (old_restricted != new_restricted) {
|
|
|
|
// if a user was unrestricted, it is not received from the server anymore
|
|
|
|
// we need to reget their online status manually
|
2021-03-29 00:34:33 +02:00
|
|
|
std::vector<UserId> unrestricted;
|
2020-05-01 00:21:02 +02:00
|
|
|
std::set_difference(old_restricted.begin(), old_restricted.end(), new_restricted.begin(),
|
2021-03-29 00:34:33 +02:00
|
|
|
new_restricted.end(), std::back_inserter(unrestricted),
|
|
|
|
[](UserId lhs, UserId rhs) { return lhs.get() < rhs.get(); });
|
2020-05-01 00:21:02 +02:00
|
|
|
for (auto &user_id : unrestricted) {
|
2021-03-29 00:34:33 +02:00
|
|
|
send_closure_later(G()->contacts_manager(), &ContactsManager::reload_user, user_id, Promise<Unit>());
|
2020-05-01 00:21:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2020-04-19 11:53:07 +02:00
|
|
|
}
|
2020-05-01 00:21:02 +02:00
|
|
|
case UserPrivacySetting::Type::UserPhoneNumber:
|
|
|
|
send_closure_later(G()->contacts_manager(), &ContactsManager::on_update_phone_number_privacy);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2020-04-19 11:53:07 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-01 00:21:02 +02:00
|
|
|
|
2023-05-08 19:35:47 +02:00
|
|
|
info.rules_ = std::move(privacy_rules);
|
2023-05-08 14:23:57 +02:00
|
|
|
send_closure(
|
|
|
|
G()->td(), &Td::send_update,
|
|
|
|
make_tl_object<td_api::updateUserPrivacySettingRules>(user_privacy_setting.get_user_privacy_setting_object(),
|
2023-05-08 19:35:47 +02:00
|
|
|
info.rules_.get_user_privacy_setting_rules_object(td_)));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|