1 //
2 // Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 #include "td/telegram/RestrictionReason.h"
8 
9 #include "td/telegram/ConfigShared.h"
10 #include "td/telegram/Global.h"
11 
12 #include "td/utils/algorithm.h"
13 #include "td/utils/common.h"
14 #include "td/utils/misc.h"
15 
16 #include <tuple>
17 
18 namespace td {
19 
get_restriction_reason_description(const vector<RestrictionReason> & restriction_reasons)20 string get_restriction_reason_description(const vector<RestrictionReason> &restriction_reasons) {
21   if (restriction_reasons.empty()) {
22     return string();
23   }
24 
25   auto ignored_restriction_reasons =
26       full_split(G()->shared_config().get_option_string("ignored_restriction_reasons"), ',');
27   auto platform = [] {
28     if (G()->shared_config().get_option_boolean("ignore_platform_restrictions")) {
29       return Slice();
30     }
31 
32 #if TD_ANDROID
33     return Slice("android");
34 #elif TD_WINDOWS
35     return Slice("ms");
36 #elif TD_DARWIN
37     return Slice("ios");
38 #else
39     return Slice();
40 #endif
41   }();
42 
43   if (!platform.empty()) {
44     for (auto &restriction_reason : restriction_reasons) {
45       if (restriction_reason.platform_ == platform &&
46           !td::contains(ignored_restriction_reasons, restriction_reason.reason_)) {
47         return restriction_reason.description_;
48       }
49     }
50   }
51 
52   for (auto &restriction_reason : restriction_reasons) {
53     if (restriction_reason.platform_ == "all" &&
54         !td::contains(ignored_restriction_reasons, restriction_reason.reason_)) {
55       return restriction_reason.description_;
56     }
57   }
58 
59   return string();
60 }
61 
get_restriction_reasons(Slice legacy_restriction_reason)62 vector<RestrictionReason> get_restriction_reasons(Slice legacy_restriction_reason) {
63   Slice type;
64   Slice description;
65   std::tie(type, description) = split(legacy_restriction_reason, ':');
66   auto parts = full_split(type, '-');
67   description = trim(description);
68 
69   vector<RestrictionReason> result;
70   if (parts.size() <= 1) {
71     return result;
72   }
73   for (size_t i = 1; i < parts.size(); i++) {
74     result.emplace_back(parts[i].str(), parts[0].str(), description.str());
75   }
76   return result;
77 }
78 
get_restriction_reasons(vector<telegram_api::object_ptr<telegram_api::restrictionReason>> && restriction_reasons)79 vector<RestrictionReason> get_restriction_reasons(
80     vector<telegram_api::object_ptr<telegram_api::restrictionReason>> &&restriction_reasons) {
81   return transform(std::move(restriction_reasons),
82                    [](telegram_api::object_ptr<telegram_api::restrictionReason> &&restriction_reason) {
83                      return RestrictionReason(std::move(restriction_reason->platform_),
84                                               std::move(restriction_reason->reason_),
85                                               std::move(restriction_reason->text_));
86                    });
87 }
88 
89 }  // namespace td
90