tdesktop/Telegram/SourceFiles/api/api_global_privacy.cpp

191 lines
5 KiB
C++
Raw Normal View History

2020-07-03 16:53:24 +00:00
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "api/api_global_privacy.h"
#include "apiwrap.h"
#include "main/main_session.h"
#include "main/main_app_config.h"
namespace Api {
GlobalPrivacy::GlobalPrivacy(not_null<ApiWrap*> api)
: _session(&api->session())
, _api(&api->instance()) {
}
void GlobalPrivacy::reload(Fn<void()> callback) {
if (callback) {
_callbacks.push_back(std::move(callback));
}
2020-07-03 16:53:24 +00:00
if (_requestId) {
return;
}
_requestId = _api.request(MTPaccount_GetGlobalPrivacySettings(
)).done([=](const MTPGlobalPrivacySettings &result) {
_requestId = 0;
apply(result);
for (const auto &callback : base::take(_callbacks)) {
callback();
}
}).fail([=] {
2020-07-03 16:53:24 +00:00
_requestId = 0;
for (const auto &callback : base::take(_callbacks)) {
callback();
}
2020-07-03 16:53:24 +00:00
}).send();
2024-03-29 11:30:50 +00:00
_session->appConfig().value(
2020-07-03 16:53:24 +00:00
) | rpl::start_with_next([=] {
2024-03-29 11:30:50 +00:00
_showArchiveAndMute = _session->appConfig().get<bool>(
2020-07-03 16:53:24 +00:00
u"autoarchive_setting_available"_q,
false);
}, _session->lifetime());
2020-07-03 16:53:24 +00:00
}
bool GlobalPrivacy::archiveAndMuteCurrent() const {
return _archiveAndMute.current();
}
rpl::producer<bool> GlobalPrivacy::archiveAndMute() const {
return _archiveAndMute.value();
}
2023-07-13 15:33:57 +00:00
UnarchiveOnNewMessage GlobalPrivacy::unarchiveOnNewMessageCurrent() const {
return _unarchiveOnNewMessage.current();
}
auto GlobalPrivacy::unarchiveOnNewMessage() const
-> rpl::producer<UnarchiveOnNewMessage> {
return _unarchiveOnNewMessage.value();
}
2020-07-03 16:53:24 +00:00
rpl::producer<bool> GlobalPrivacy::showArchiveAndMute() const {
using namespace rpl::mappers;
return rpl::combine(
archiveAndMute(),
_showArchiveAndMute.value(),
_1 || _2);
}
rpl::producer<> GlobalPrivacy::suggestArchiveAndMute() const {
2024-03-29 11:30:50 +00:00
return _session->appConfig().suggestionRequested(
2020-07-03 16:53:24 +00:00
u"AUTOARCHIVE_POPULAR"_q);
}
void GlobalPrivacy::dismissArchiveAndMuteSuggestion() {
2024-03-29 11:30:50 +00:00
_session->appConfig().dismissSuggestion(
2020-07-03 16:53:24 +00:00
u"AUTOARCHIVE_POPULAR"_q);
}
2024-01-09 09:13:30 +00:00
void GlobalPrivacy::updateHideReadTime(bool hide) {
update(
archiveAndMuteCurrent(),
unarchiveOnNewMessageCurrent(),
hide,
newRequirePremiumCurrent());
}
bool GlobalPrivacy::hideReadTimeCurrent() const {
return _hideReadTime.current();
}
rpl::producer<bool> GlobalPrivacy::hideReadTime() const {
return _hideReadTime.value();
}
void GlobalPrivacy::updateNewRequirePremium(bool value) {
update(
archiveAndMuteCurrent(),
unarchiveOnNewMessageCurrent(),
hideReadTimeCurrent(),
value);
}
bool GlobalPrivacy::newRequirePremiumCurrent() const {
return _newRequirePremium.current();
}
rpl::producer<bool> GlobalPrivacy::newRequirePremium() const {
return _newRequirePremium.value();
}
2023-08-11 14:20:38 +00:00
void GlobalPrivacy::updateArchiveAndMute(bool value) {
2024-01-09 09:13:30 +00:00
update(
value,
unarchiveOnNewMessageCurrent(),
hideReadTimeCurrent(),
newRequirePremiumCurrent());
2023-08-11 14:20:38 +00:00
}
void GlobalPrivacy::updateUnarchiveOnNewMessage(
UnarchiveOnNewMessage value) {
2024-01-09 09:13:30 +00:00
update(
archiveAndMuteCurrent(),
value,
hideReadTimeCurrent(),
newRequirePremiumCurrent());
2023-08-11 14:20:38 +00:00
}
void GlobalPrivacy::update(
bool archiveAndMute,
2024-01-09 09:13:30 +00:00
UnarchiveOnNewMessage unarchiveOnNewMessage,
bool hideReadTime,
bool newRequirePremium) {
2020-07-03 16:53:24 +00:00
using Flag = MTPDglobalPrivacySettings::Flag;
_api.request(_requestId).cancel();
const auto newRequirePremiumAllowed = _session->premium()
|| _session->appConfig().newRequirePremiumFree();
2023-07-13 15:33:57 +00:00
const auto flags = Flag()
| (archiveAndMute
? Flag::f_archive_and_mute_new_noncontact_peers
: Flag())
| (unarchiveOnNewMessage == UnarchiveOnNewMessage::None
2023-07-13 15:33:57 +00:00
? Flag::f_keep_archived_unmuted
: Flag())
| (unarchiveOnNewMessage != UnarchiveOnNewMessage::AnyUnmuted
2023-07-13 15:33:57 +00:00
? Flag::f_keep_archived_folders
2024-01-09 09:13:30 +00:00
: Flag())
| (hideReadTime ? Flag::f_hide_read_marks : Flag())
| ((newRequirePremium && newRequirePremiumAllowed)
2024-01-09 09:13:30 +00:00
? Flag::f_new_noncontact_peers_require_premium
2023-07-13 15:33:57 +00:00
: Flag());
2020-07-03 16:53:24 +00:00
_requestId = _api.request(MTPaccount_SetGlobalPrivacySettings(
2023-07-13 15:33:57 +00:00
MTP_globalPrivacySettings(MTP_flags(flags))
2020-07-03 16:53:24 +00:00
)).done([=](const MTPGlobalPrivacySettings &result) {
_requestId = 0;
apply(result);
2024-01-09 09:13:30 +00:00
}).fail([=](const MTP::Error &error) {
2020-07-03 16:53:24 +00:00
_requestId = 0;
2024-01-09 09:13:30 +00:00
if (error.type() == u"PREMIUM_ACCOUNT_REQUIRED"_q) {
update(archiveAndMute, unarchiveOnNewMessage, hideReadTime, {});
}
2020-07-03 16:53:24 +00:00
}).send();
_archiveAndMute = archiveAndMute;
2023-08-11 14:20:38 +00:00
_unarchiveOnNewMessage = unarchiveOnNewMessage;
2024-01-09 09:13:30 +00:00
_hideReadTime = hideReadTime;
_newRequirePremium = newRequirePremium;
2020-07-03 16:53:24 +00:00
}
void GlobalPrivacy::apply(const MTPGlobalPrivacySettings &data) {
data.match([&](const MTPDglobalPrivacySettings &data) {
2023-07-13 15:33:57 +00:00
_archiveAndMute = data.is_archive_and_mute_new_noncontact_peers();
_unarchiveOnNewMessage = data.is_keep_archived_unmuted()
? UnarchiveOnNewMessage::None
2023-07-13 15:33:57 +00:00
: data.is_keep_archived_folders()
? UnarchiveOnNewMessage::NotInFoldersUnmuted
: UnarchiveOnNewMessage::AnyUnmuted;
2024-01-09 09:13:30 +00:00
_hideReadTime = data.is_hide_read_marks();
_newRequirePremium = data.is_new_noncontact_peers_require_premium();
2020-07-03 16:53:24 +00:00
});
}
} // namespace Api