2019-11-23 23:37:46 +01:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
2019-11-23 23:37:46 +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/RestrictionReason.h"
|
|
|
|
|
|
|
|
#include "td/telegram/Global.h"
|
|
|
|
|
2021-01-01 13:59:53 +01:00
|
|
|
#include "td/utils/algorithm.h"
|
2019-11-23 23:37:46 +01:00
|
|
|
#include "td/utils/common.h"
|
|
|
|
#include "td/utils/misc.h"
|
|
|
|
|
|
|
|
#include <tuple>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
2024-08-13 20:01:01 +02:00
|
|
|
const RestrictionReason *get_restriction_reason(const vector<RestrictionReason> &restriction_reasons, bool sensitive) {
|
2019-11-23 23:37:46 +01:00
|
|
|
if (restriction_reasons.empty()) {
|
2024-08-13 19:27:02 +02:00
|
|
|
return nullptr;
|
2019-11-23 23:37:46 +01:00
|
|
|
}
|
2019-12-19 17:58:33 +01:00
|
|
|
|
2022-08-17 15:11:13 +02:00
|
|
|
auto ignored_restriction_reasons = full_split(G()->get_option_string("ignored_restriction_reasons"), ',');
|
2022-11-02 18:38:52 +01:00
|
|
|
auto restriction_add_platforms = full_split(G()->get_option_string("restriction_add_platforms"), ',');
|
2019-11-23 23:37:46 +01:00
|
|
|
auto platform = [] {
|
|
|
|
#if TD_ANDROID
|
|
|
|
return Slice("android");
|
|
|
|
#elif TD_WINDOWS
|
|
|
|
return Slice("ms");
|
|
|
|
#elif TD_DARWIN
|
|
|
|
return Slice("ios");
|
|
|
|
#else
|
|
|
|
return Slice();
|
|
|
|
#endif
|
|
|
|
}();
|
|
|
|
|
2022-11-02 18:38:52 +01:00
|
|
|
if (G()->get_option_boolean("ignore_platform_restrictions")) {
|
|
|
|
platform = Slice();
|
|
|
|
restriction_add_platforms.clear();
|
|
|
|
}
|
|
|
|
|
2019-11-23 23:37:46 +01:00
|
|
|
if (!platform.empty()) {
|
2022-11-02 18:38:52 +01:00
|
|
|
// first find restriction for the current platform
|
2019-11-23 23:37:46 +01:00
|
|
|
for (auto &restriction_reason : restriction_reasons) {
|
2019-12-19 17:58:33 +01:00
|
|
|
if (restriction_reason.platform_ == platform &&
|
2024-08-13 20:01:01 +02:00
|
|
|
!td::contains(ignored_restriction_reasons, restriction_reason.reason_) &&
|
|
|
|
restriction_reason.is_sensitive() == sensitive) {
|
2024-08-13 19:27:02 +02:00
|
|
|
return &restriction_reason;
|
2019-11-23 23:37:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-02 18:38:52 +01:00
|
|
|
if (!restriction_add_platforms.empty()) {
|
|
|
|
// then find restriction for added platforms
|
|
|
|
for (auto &restriction_reason : restriction_reasons) {
|
|
|
|
if (td::contains(restriction_add_platforms, restriction_reason.platform_) &&
|
2024-08-13 20:01:01 +02:00
|
|
|
!td::contains(ignored_restriction_reasons, restriction_reason.reason_) &&
|
|
|
|
restriction_reason.is_sensitive() == sensitive) {
|
2024-08-13 19:27:02 +02:00
|
|
|
return &restriction_reason;
|
2022-11-02 18:38:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// then find restriction for all platforms
|
2019-11-23 23:37:46 +01:00
|
|
|
for (auto &restriction_reason : restriction_reasons) {
|
2019-12-19 17:58:33 +01:00
|
|
|
if (restriction_reason.platform_ == "all" &&
|
2024-08-13 20:01:01 +02:00
|
|
|
!td::contains(ignored_restriction_reasons, restriction_reason.reason_) &&
|
|
|
|
restriction_reason.is_sensitive() == sensitive) {
|
2024-08-13 19:27:02 +02:00
|
|
|
return &restriction_reason;
|
2019-11-23 23:37:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-13 19:27:02 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2024-08-13 20:08:08 +02:00
|
|
|
bool get_restriction_reason_has_sensitive_content(const vector<RestrictionReason> &restriction_reasons) {
|
|
|
|
return get_restriction_reason(restriction_reasons, true) != nullptr;
|
|
|
|
}
|
|
|
|
|
2024-08-13 19:27:02 +02:00
|
|
|
string get_restriction_reason_description(const vector<RestrictionReason> &restriction_reasons) {
|
2024-08-13 20:01:01 +02:00
|
|
|
const auto *restriction_reason = get_restriction_reason(restriction_reasons, false);
|
2024-08-13 19:27:02 +02:00
|
|
|
if (restriction_reason == nullptr) {
|
|
|
|
return string();
|
|
|
|
}
|
|
|
|
return restriction_reason->description_;
|
2019-11-23 23:37:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
vector<RestrictionReason> get_restriction_reasons(Slice legacy_restriction_reason) {
|
|
|
|
Slice type;
|
|
|
|
Slice description;
|
|
|
|
std::tie(type, description) = split(legacy_restriction_reason, ':');
|
|
|
|
auto parts = full_split(type, '-');
|
|
|
|
description = trim(description);
|
|
|
|
|
|
|
|
vector<RestrictionReason> result;
|
|
|
|
if (parts.size() <= 1) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
for (size_t i = 1; i < parts.size(); i++) {
|
|
|
|
result.emplace_back(parts[i].str(), parts[0].str(), description.str());
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<RestrictionReason> get_restriction_reasons(
|
|
|
|
vector<telegram_api::object_ptr<telegram_api::restrictionReason>> &&restriction_reasons) {
|
|
|
|
return transform(std::move(restriction_reasons),
|
|
|
|
[](telegram_api::object_ptr<telegram_api::restrictionReason> &&restriction_reason) {
|
|
|
|
return RestrictionReason(std::move(restriction_reason->platform_),
|
|
|
|
std::move(restriction_reason->reason_),
|
|
|
|
std::move(restriction_reason->text_));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|