2023-07-14 13:54:35 +02:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
2023-07-14 13:54:35 +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/GlobalPrivacySettings.h"
|
|
|
|
|
2023-07-14 14:31:27 +02:00
|
|
|
#include "td/telegram/ConfigManager.h"
|
2023-07-14 13:54:35 +02:00
|
|
|
#include "td/telegram/Global.h"
|
|
|
|
#include "td/telegram/net/NetQueryCreator.h"
|
2023-07-14 14:31:27 +02:00
|
|
|
#include "td/telegram/SuggestedAction.h"
|
2023-07-14 13:54:35 +02:00
|
|
|
#include "td/telegram/Td.h"
|
2024-01-22 11:11:04 +01:00
|
|
|
#include "td/telegram/telegram_api.h"
|
2023-07-14 13:54:35 +02:00
|
|
|
|
2023-07-18 17:20:43 +02:00
|
|
|
#include "td/actor/actor.h"
|
|
|
|
|
2023-07-14 13:54:35 +02:00
|
|
|
#include "td/utils/buffer.h"
|
2024-01-25 18:06:07 +01:00
|
|
|
#include "td/utils/logging.h"
|
2023-07-14 13:54:35 +02:00
|
|
|
#include "td/utils/Status.h"
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
class GetGlobalPrivacySettingsQuery final : public Td::ResultHandler {
|
2024-01-09 13:14:45 +01:00
|
|
|
Promise<GlobalPrivacySettings> promise_;
|
2023-07-14 13:54:35 +02:00
|
|
|
|
|
|
|
public:
|
2024-01-09 13:14:45 +01:00
|
|
|
explicit GetGlobalPrivacySettingsQuery(Promise<GlobalPrivacySettings> &&promise) : promise_(std::move(promise)) {
|
2023-07-14 13:54:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void send() {
|
2023-07-14 14:25:32 +02:00
|
|
|
send_query(G()->net_query_creator().create(telegram_api::account_getGlobalPrivacySettings(), {{"me"}}));
|
2023-07-14 13:54:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void on_result(BufferSlice packet) final {
|
|
|
|
auto result_ptr = fetch_result<telegram_api::account_getGlobalPrivacySettings>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
|
|
|
return on_error(result_ptr.move_as_error());
|
|
|
|
}
|
|
|
|
|
2024-01-24 16:59:53 +01:00
|
|
|
LOG(INFO) << "Receive result for GetGlobalPrivacySettingsQuery: " << to_string(result_ptr.ok());
|
2024-01-09 13:14:45 +01:00
|
|
|
promise_.set_value(GlobalPrivacySettings(result_ptr.move_as_ok()));
|
2023-07-14 13:54:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void on_error(Status status) final {
|
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-07-14 14:25:32 +02:00
|
|
|
class SetGlobalPrivacySettingsQuery final : public Td::ResultHandler {
|
|
|
|
Promise<Unit> promise_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit SetGlobalPrivacySettingsQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void send(GlobalPrivacySettings settings) {
|
|
|
|
send_query(G()->net_query_creator().create(
|
|
|
|
telegram_api::account_setGlobalPrivacySettings(settings.get_input_global_privacy_settings()), {{"me"}}));
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_result(BufferSlice packet) final {
|
|
|
|
auto result_ptr = fetch_result<telegram_api::account_setGlobalPrivacySettings>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
|
|
|
return on_error(result_ptr.move_as_error());
|
|
|
|
}
|
|
|
|
|
|
|
|
promise_.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_error(Status status) final {
|
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-07-14 13:54:35 +02:00
|
|
|
GlobalPrivacySettings::GlobalPrivacySettings(telegram_api::object_ptr<telegram_api::globalPrivacySettings> &&settings)
|
|
|
|
: archive_and_mute_new_noncontact_peers_(settings->archive_and_mute_new_noncontact_peers_)
|
|
|
|
, keep_archived_unmuted_(settings->keep_archived_unmuted_)
|
2024-01-09 14:00:59 +01:00
|
|
|
, keep_archived_folders_(settings->keep_archived_folders_)
|
|
|
|
, hide_read_marks_(settings->hide_read_marks_)
|
|
|
|
, new_noncontact_peers_require_premium_(settings->new_noncontact_peers_require_premium_) {
|
2023-07-14 13:54:35 +02:00
|
|
|
}
|
|
|
|
|
2024-01-09 13:54:25 +01:00
|
|
|
GlobalPrivacySettings::GlobalPrivacySettings(td_api::object_ptr<td_api::archiveChatListSettings> &&settings)
|
|
|
|
: set_type_(SetType::Archive) {
|
2023-07-14 14:25:32 +02:00
|
|
|
if (settings != nullptr) {
|
|
|
|
archive_and_mute_new_noncontact_peers_ = settings->archive_and_mute_new_chats_from_unknown_users_;
|
|
|
|
keep_archived_unmuted_ = settings->keep_unmuted_chats_archived_;
|
2023-07-14 17:51:47 +02:00
|
|
|
keep_archived_folders_ = settings->keep_chats_from_folders_archived_;
|
2023-07-14 14:25:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-09 15:56:47 +01:00
|
|
|
GlobalPrivacySettings::GlobalPrivacySettings(td_api::object_ptr<td_api::readDatePrivacySettings> &&settings)
|
|
|
|
: set_type_(SetType::ReadDate) {
|
2024-01-24 16:59:53 +01:00
|
|
|
hide_read_marks_ = settings == nullptr || !settings->show_read_date_;
|
2024-01-09 15:56:47 +01:00
|
|
|
}
|
|
|
|
|
2024-01-19 12:25:24 +01:00
|
|
|
GlobalPrivacySettings::GlobalPrivacySettings(td_api::object_ptr<td_api::newChatPrivacySettings> &&settings)
|
|
|
|
: set_type_(SetType::NewChat) {
|
2024-01-24 16:59:53 +01:00
|
|
|
new_noncontact_peers_require_premium_ = settings == nullptr || !settings->allow_new_chats_from_unknown_users_;
|
2024-01-19 12:25:24 +01:00
|
|
|
}
|
|
|
|
|
2024-01-09 13:54:25 +01:00
|
|
|
void GlobalPrivacySettings::apply_changes(const GlobalPrivacySettings &set_settings) {
|
|
|
|
CHECK(set_type_ == SetType::None);
|
|
|
|
switch (set_settings.set_type_) {
|
|
|
|
case SetType::Archive:
|
|
|
|
archive_and_mute_new_noncontact_peers_ = set_settings.archive_and_mute_new_noncontact_peers_;
|
|
|
|
keep_archived_unmuted_ = set_settings.keep_archived_unmuted_;
|
|
|
|
keep_archived_folders_ = set_settings.keep_archived_folders_;
|
|
|
|
break;
|
2024-01-09 15:56:47 +01:00
|
|
|
case SetType::ReadDate:
|
|
|
|
hide_read_marks_ = set_settings.hide_read_marks_;
|
|
|
|
break;
|
2024-01-19 12:25:24 +01:00
|
|
|
case SetType::NewChat:
|
|
|
|
new_noncontact_peers_require_premium_ = set_settings.new_noncontact_peers_require_premium_;
|
|
|
|
break;
|
2024-01-09 13:54:25 +01:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-14 14:25:32 +02:00
|
|
|
telegram_api::object_ptr<telegram_api::globalPrivacySettings> GlobalPrivacySettings::get_input_global_privacy_settings()
|
|
|
|
const {
|
2024-01-09 13:54:25 +01:00
|
|
|
CHECK(set_type_ == SetType::None);
|
2023-07-14 14:25:32 +02:00
|
|
|
int32 flags = 0;
|
|
|
|
if (archive_and_mute_new_noncontact_peers_) {
|
|
|
|
flags |= telegram_api::globalPrivacySettings::ARCHIVE_AND_MUTE_NEW_NONCONTACT_PEERS_MASK;
|
|
|
|
}
|
|
|
|
if (keep_archived_unmuted_) {
|
|
|
|
flags |= telegram_api::globalPrivacySettings::KEEP_ARCHIVED_UNMUTED_MASK;
|
|
|
|
}
|
|
|
|
if (keep_archived_folders_) {
|
|
|
|
flags |= telegram_api::globalPrivacySettings::KEEP_ARCHIVED_FOLDERS_MASK;
|
|
|
|
}
|
2024-01-09 14:00:59 +01:00
|
|
|
if (hide_read_marks_) {
|
|
|
|
flags |= telegram_api::globalPrivacySettings::HIDE_READ_MARKS_MASK;
|
|
|
|
}
|
|
|
|
if (new_noncontact_peers_require_premium_) {
|
|
|
|
flags |= telegram_api::globalPrivacySettings::NEW_NONCONTACT_PEERS_REQUIRE_PREMIUM_MASK;
|
|
|
|
}
|
2024-01-09 10:09:10 +01:00
|
|
|
return telegram_api::make_object<telegram_api::globalPrivacySettings>(
|
|
|
|
flags, false /*ignored*/, false /*ignored*/, false /*ignored*/, false /*ignored*/, false /*ignored*/);
|
2023-07-14 14:25:32 +02:00
|
|
|
}
|
|
|
|
|
2023-07-14 13:54:35 +02:00
|
|
|
td_api::object_ptr<td_api::archiveChatListSettings> GlobalPrivacySettings::get_archive_chat_list_settings_object()
|
|
|
|
const {
|
2024-01-09 13:54:25 +01:00
|
|
|
CHECK(set_type_ == SetType::None);
|
2023-07-14 13:54:35 +02:00
|
|
|
return td_api::make_object<td_api::archiveChatListSettings>(archive_and_mute_new_noncontact_peers_,
|
|
|
|
keep_archived_unmuted_, keep_archived_folders_);
|
|
|
|
}
|
|
|
|
|
2024-01-09 15:11:22 +01:00
|
|
|
td_api::object_ptr<td_api::readDatePrivacySettings> GlobalPrivacySettings::get_read_date_privacy_settings_object()
|
|
|
|
const {
|
|
|
|
CHECK(set_type_ == SetType::None);
|
|
|
|
return td_api::make_object<td_api::readDatePrivacySettings>(!hide_read_marks_);
|
|
|
|
}
|
|
|
|
|
2024-01-19 12:25:24 +01:00
|
|
|
td_api::object_ptr<td_api::newChatPrivacySettings> GlobalPrivacySettings::get_new_chat_privacy_settings_object() const {
|
|
|
|
CHECK(set_type_ == SetType::None);
|
|
|
|
return td_api::make_object<td_api::newChatPrivacySettings>(!new_noncontact_peers_require_premium_);
|
|
|
|
}
|
|
|
|
|
2024-01-09 13:14:45 +01:00
|
|
|
void GlobalPrivacySettings::get_global_privacy_settings(Td *td, Promise<GlobalPrivacySettings> &&promise) {
|
2023-07-14 13:54:35 +02:00
|
|
|
td->create_handler<GetGlobalPrivacySettingsQuery>(std::move(promise))->send();
|
|
|
|
}
|
|
|
|
|
2023-07-14 14:25:32 +02:00
|
|
|
void GlobalPrivacySettings::set_global_privacy_settings(Td *td, GlobalPrivacySettings settings,
|
|
|
|
Promise<Unit> &&promise) {
|
2024-01-09 13:54:25 +01:00
|
|
|
CHECK(settings.set_type_ != SetType::None);
|
2023-07-14 14:31:27 +02:00
|
|
|
if (settings.archive_and_mute_new_noncontact_peers_) {
|
|
|
|
send_closure(td->config_manager_, &ConfigManager::hide_suggested_action,
|
|
|
|
SuggestedAction{SuggestedAction::Type::EnableArchiveAndMuteNewChats});
|
|
|
|
}
|
|
|
|
|
2024-01-09 13:54:25 +01:00
|
|
|
get_global_privacy_settings(
|
|
|
|
td, PromiseCreator::lambda([td, set_settings = std::move(settings),
|
|
|
|
promise = std::move(promise)](Result<GlobalPrivacySettings> result) mutable {
|
|
|
|
G()->ignore_result_if_closing(result);
|
|
|
|
if (result.is_error()) {
|
|
|
|
return promise.set_error(result.move_as_error());
|
|
|
|
}
|
|
|
|
auto settings = result.move_as_ok();
|
|
|
|
settings.apply_changes(set_settings);
|
|
|
|
td->create_handler<SetGlobalPrivacySettingsQuery>(std::move(promise))->send(std::move(settings));
|
|
|
|
}));
|
2023-07-14 14:25:32 +02:00
|
|
|
}
|
|
|
|
|
2023-07-14 13:54:35 +02:00
|
|
|
} // namespace td
|